definitions.mk revision fa886cdb2adb0f1631b2b3caa617eede0930e546
1#
2# Copyright (C) 2008 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17##
18## Common build system definitions.  Mostly standard
19## commands for building various types of targets, which
20## are used by others to construct the final targets.
21##
22
23# These are variables we use to collect overall lists
24# of things being processed.
25
26# Full paths to all of the documentation
27ALL_DOCS:=
28
29# The short names of all of the targets in the system.
30# For each element of ALL_MODULES, two other variables
31# are defined:
32#   $(ALL_MODULES.$(target)).BUILT
33#   $(ALL_MODULES.$(target)).INSTALLED
34# The BUILT variable contains LOCAL_BUILT_MODULE for that
35# target, and the INSTALLED variable contains the LOCAL_INSTALLED_MODULE.
36# Some targets may have multiple files listed in the BUILT and INSTALLED
37# sub-variables.
38ALL_MODULES:=
39
40# Full paths to targets that should be added to the "make droid"
41# set of installed targets.
42ALL_DEFAULT_INSTALLED_MODULES:=
43
44# The list of tags that have been defined by
45# LOCAL_MODULE_TAGS.  Each word in this variable maps
46# to a corresponding ALL_MODULE_TAGS.<tagname> variable
47# that contains all of the INSTALLED_MODULEs with that tag.
48ALL_MODULE_TAGS:=
49
50# Similar to ALL_MODULE_TAGS, but contains the short names
51# of all targets for a particular tag.  The top-level variable
52# won't have the list of tags;  ust ALL_MODULE_TAGS to get
53# the list of all known tags.  (This means that this variable
54# will always be empty; it's just here as a placeholder for
55# its sub-variables.)
56ALL_MODULE_NAME_TAGS:=
57
58# Full path to all files that are made by some tool
59ALL_GENERATED_SOURCES:=
60
61# Full path to all asm, C, C++, lex and yacc generated C files.
62# These all have an order-only dependency on the copied headers
63ALL_C_CPP_ETC_OBJECTS:=
64
65# The list of dynamic binaries that haven't been stripped/compressed/etc.
66ALL_ORIGINAL_DYNAMIC_BINARIES:=
67
68# These files go into the SDK
69ALL_SDK_FILES:=
70
71# Files for dalvik.  This is often build without building the rest of the OS.
72INTERNAL_DALVIK_MODULES:=
73
74# All findbugs xml files
75ALL_FINDBUGS_FILES:=
76
77# GPL module license files
78ALL_GPL_MODULE_LICENSE_FILES:=
79
80# Target and host installed module's dependencies on shared libraries.
81# They are list of "<module_name>:<installed_file>:lib1,lib2...".
82TARGET_DEPENDENCIES_ON_SHARED_LIBRARIES :=
83$(TARGET_2ND_ARCH_VAR_PREFIX)TARGET_DEPENDENCIES_ON_SHARED_LIBRARIES :=
84HOST_DEPENDENCIES_ON_SHARED_LIBRARIES :=
85$(HOST_2ND_ARCH_VAR_PREFIX)HOST_DEPENDENCIES_ON_SHARED_LIBRARIES :=
86HOST_CROSS_DEPENDENCIES_ON_SHARED_LIBRARIES :=
87$(HOST_CROSS_2ND_ARCH_VAR_PREFIX)HOST_CROSS_DEPENDENCIES_ON_SHARED_LIBRARIES :=
88
89# Generated class file names for Android resource.
90# They are escaped and quoted so can be passed safely to a bash command.
91ANDROID_RESOURCE_GENERATED_CLASSES := 'R.class' 'R$$*.class' 'Manifest.class' 'Manifest$$*.class'
92
93# Display names for various build targets
94TARGET_DISPLAY := target
95HOST_DISPLAY := host
96HOST_CROSS_DISPLAY := host cross
97
98###########################################################
99## Debugging; prints a variable list to stdout
100###########################################################
101
102# $(1): variable name list, not variable values
103define print-vars
104$(foreach var,$(1), \
105  $(info $(var):) \
106  $(foreach word,$($(var)), \
107    $(info $(space)$(space)$(word)) \
108   ) \
109 )
110endef
111
112###########################################################
113## Evaluates to true if the string contains the word true,
114## and empty otherwise
115## $(1): a var to test
116###########################################################
117
118define true-or-empty
119$(filter true, $(1))
120endef
121
122
123###########################################################
124## Retrieve the directory of the current makefile
125## Must be called before including any other makefile!!
126###########################################################
127
128# Figure out where we are.
129define my-dir
130$(strip \
131  $(eval LOCAL_MODULE_MAKEFILE := $$(lastword $$(MAKEFILE_LIST))) \
132  $(if $(filter $(BUILD_SYSTEM)/% $(OUT_DIR)/%,$(LOCAL_MODULE_MAKEFILE)), \
133    $(error my-dir must be called before including any other makefile.) \
134   , \
135    $(patsubst %/,%,$(dir $(LOCAL_MODULE_MAKEFILE))) \
136   ) \
137 )
138endef
139
140
141###########################################################
142## Retrieve a list of all makefiles immediately below some directory
143###########################################################
144
145define all-makefiles-under
146$(wildcard $(1)/*/Android.mk)
147endef
148
149###########################################################
150## Look under a directory for makefiles that don't have parent
151## makefiles.
152###########################################################
153
154# $(1): directory to search under
155# Ignores $(1)/Android.mk
156define first-makefiles-under
157$(shell build/tools/findleaves.py $(FIND_LEAVES_EXCLUDES) \
158        --mindepth=2 $(addprefix --dir=,$(1)) Android.mk)
159endef
160
161###########################################################
162## Retrieve a list of all makefiles immediately below your directory
163## Must be called before including any other makefile!!
164###########################################################
165
166define all-subdir-makefiles
167$(call all-makefiles-under,$(call my-dir))
168endef
169
170###########################################################
171## Look in the named list of directories for makefiles,
172## relative to the current directory.
173## Must be called before including any other makefile!!
174###########################################################
175
176# $(1): List of directories to look for under this directory
177define all-named-subdir-makefiles
178$(wildcard $(addsuffix /Android.mk, $(addprefix $(call my-dir)/,$(1))))
179endef
180
181###########################################################
182## Find all of the directories under the named directories with
183## the specified name.
184## Meant to be used like:
185##    INC_DIRS := $(call all-named-dirs-under,inc,.)
186###########################################################
187
188define all-named-dirs-under
189$(call find-subdir-files,$(2) -type d -name "$(1)")
190endef
191
192###########################################################
193## Find all the directories under the current directory that
194## haves name that match $(1)
195###########################################################
196
197define all-subdir-named-dirs
198$(call all-named-dirs-under,$(1),.)
199endef
200
201###########################################################
202## Find all of the files under the named directories with
203## the specified name.
204## Meant to be used like:
205##    SRC_FILES := $(call all-named-files-under,*.h,src tests)
206###########################################################
207
208define all-named-files-under
209$(call find-files-in-subdirs,$(LOCAL_PATH),"$(1)",$(2))
210endef
211
212###########################################################
213## Find all of the files under the current directory with
214## the specified name.
215###########################################################
216
217define all-subdir-named-files
218$(call all-named-files-under,$(1),.)
219endef
220
221###########################################################
222## Find all of the java files under the named directories.
223## Meant to be used like:
224##    SRC_FILES := $(call all-java-files-under,src tests)
225###########################################################
226
227define all-java-files-under
228$(call all-named-files-under,*.java,$(1))
229endef
230
231###########################################################
232## Find all of the java files from here.  Meant to be used like:
233##    SRC_FILES := $(call all-subdir-java-files)
234###########################################################
235
236define all-subdir-java-files
237$(call all-java-files-under,.)
238endef
239
240###########################################################
241## Find all of the c files under the named directories.
242## Meant to be used like:
243##    SRC_FILES := $(call all-c-files-under,src tests)
244###########################################################
245
246define all-c-files-under
247$(call all-named-files-under,*.c,$(1))
248endef
249
250###########################################################
251## Find all of the c files from here.  Meant to be used like:
252##    SRC_FILES := $(call all-subdir-c-files)
253###########################################################
254
255define all-subdir-c-files
256$(call all-c-files-under,.)
257endef
258
259###########################################################
260## Find all of the cpp files under the named directories.
261## LOCAL_CPP_EXTENSION is respected if set.
262## Meant to be used like:
263##    SRC_FILES := $(call all-cpp-files-under,src tests)
264###########################################################
265
266define all-cpp-files-under
267$(sort $(patsubst ./%,%, \
268  $(shell cd $(LOCAL_PATH) ; \
269          find -L $(1) -name "*$(or $(LOCAL_CPP_EXTENSION),.cpp)" -and -not -name ".*") \
270 ))
271endef
272
273###########################################################
274## Find all of the cpp files from here.  Meant to be used like:
275##    SRC_FILES := $(call all-subdir-cpp-files)
276###########################################################
277
278define all-subdir-cpp-files
279$(call all-cpp-files-under,.)
280endef
281
282###########################################################
283## Find all files named "I*.aidl" under the named directories,
284## which must be relative to $(LOCAL_PATH).  The returned list
285## is relative to $(LOCAL_PATH).
286###########################################################
287
288define all-Iaidl-files-under
289$(call all-named-files-under,I*.aidl,$(1))
290endef
291
292###########################################################
293## Find all of the "I*.aidl" files under $(LOCAL_PATH).
294###########################################################
295
296define all-subdir-Iaidl-files
297$(call all-Iaidl-files-under,.)
298endef
299
300###########################################################
301## Find all files named "*.vts" under the named directories,
302## which must be relative to $(LOCAL_PATH).  The returned list
303## is relative to $(LOCAL_PATH).
304###########################################################
305
306define all-vts-files-under
307$(call all-named-files-under,*.vts,$(1))
308endef
309
310###########################################################
311## Find all of the "*.vts" files under $(LOCAL_PATH).
312###########################################################
313
314define all-subdir-vts-files
315$(call all-vts-files-under,.)
316endef
317
318###########################################################
319## Find all of the logtags files under the named directories.
320## Meant to be used like:
321##    SRC_FILES := $(call all-logtags-files-under,src)
322###########################################################
323
324define all-logtags-files-under
325$(call all-named-files-under,*.logtags,$(1))
326endef
327
328###########################################################
329## Find all of the .proto files under the named directories.
330## Meant to be used like:
331##    SRC_FILES := $(call all-proto-files-under,src)
332###########################################################
333
334define all-proto-files-under
335$(call all-named-files-under,*.proto,$(1))
336endef
337
338###########################################################
339## Find all of the RenderScript files under the named directories.
340##  Meant to be used like:
341##    SRC_FILES := $(call all-renderscript-files-under,src)
342###########################################################
343
344define all-renderscript-files-under
345$(call find-subdir-files,$(1) \( -name "*.rs" -or -name "*.fs" \) -and -not -name ".*")
346endef
347
348###########################################################
349## Find all of the S files under the named directories.
350## Meant to be used like:
351##    SRC_FILES := $(call all-c-files-under,src tests)
352###########################################################
353
354define all-S-files-under
355$(call all-named-files-under,*.S,$(1))
356endef
357
358###########################################################
359## Find all of the html files under the named directories.
360## Meant to be used like:
361##    SRC_FILES := $(call all-html-files-under,src tests)
362###########################################################
363
364define all-html-files-under
365$(call all-named-files-under,*.html,$(1))
366endef
367
368###########################################################
369## Find all of the html files from here.  Meant to be used like:
370##    SRC_FILES := $(call all-subdir-html-files)
371###########################################################
372
373define all-subdir-html-files
374$(call all-html-files-under,.)
375endef
376
377###########################################################
378## Find all of the files matching pattern
379##    SRC_FILES := $(call find-subdir-files, <pattern>)
380###########################################################
381
382define find-subdir-files
383$(sort $(patsubst ./%,%,$(shell cd $(LOCAL_PATH) ; find -L $(1))))
384endef
385
386###########################################################
387# find the files in the subdirectory $1 of LOCAL_DIR
388# matching pattern $2, filtering out files $3
389# e.g.
390#     SRC_FILES += $(call find-subdir-subdir-files, \
391#                         css, *.cpp, DontWantThis.cpp)
392###########################################################
393
394define find-subdir-subdir-files
395$(sort $(filter-out $(patsubst %,$(1)/%,$(3)),$(patsubst ./%,%,$(shell cd \
396            $(LOCAL_PATH) ; find -L $(1) -maxdepth 1 -name $(2)))))
397endef
398
399###########################################################
400## Find all of the files matching pattern
401##    SRC_FILES := $(call all-subdir-java-files)
402###########################################################
403
404define find-subdir-assets
405$(sort $(if $(1),$(patsubst ./%,%, \
406	$(shell if [ -d $(1) ] ; then cd $(1) ; find -L ./ -not -name '.*' -and -type f ; fi)), \
407	$(warning Empty argument supplied to find-subdir-assets) \
408))
409endef
410
411###########################################################
412## Find various file types in a list of directories relative to $(LOCAL_PATH)
413###########################################################
414
415define find-other-java-files
416$(call all-java-files-under,$(1))
417endef
418
419define find-other-html-files
420$(call all-html-files-under,$(1))
421endef
422
423###########################################################
424# Use utility find to find given files in the given subdirs.
425# This function uses $(1), instead of LOCAL_PATH as the base.
426# $(1): the base dir, relative to the root of the source tree.
427# $(2): the file name pattern to be passed to find as "-name".
428# $(3): a list of subdirs of the base dir.
429# Returns: a list of paths relative to the base dir.
430###########################################################
431
432define find-files-in-subdirs
433$(sort $(patsubst ./%,%, \
434  $(shell cd $(1) ; \
435          find -L $(3) -name $(2) -and -not -name ".*") \
436 ))
437endef
438
439###########################################################
440## Scan through each directory of $(1) looking for files
441## that match $(2) using $(wildcard).  Useful for seeing if
442## a given directory or one of its parents contains
443## a particular file.  Returns the first match found,
444## starting furthest from the root.
445###########################################################
446
447define find-parent-file
448$(strip \
449  $(eval _fpf := $(sort $(wildcard $(foreach f, $(2), $(strip $(1))/$(f))))) \
450  $(if $(_fpf),$(_fpf), \
451       $(if $(filter-out ./ .,$(1)), \
452             $(call find-parent-file,$(patsubst %/,%,$(dir $(1))),$(2)) \
453        ) \
454   ) \
455)
456endef
457
458###########################################################
459## Function we can evaluate to introduce a dynamic dependency
460###########################################################
461
462define add-dependency
463$(1): $(2)
464endef
465
466###########################################################
467## Reverse order of a list
468###########################################################
469
470define reverse-list
471$(if $(1),$(call reverse-list,$(wordlist 2,$(words $(1)),$(1)))) $(firstword $(1))
472endef
473
474###########################################################
475## The intermediates directory.  Where object files go for
476## a given target.  We could technically get away without
477## the "_intermediates" suffix on the directory, but it's
478## nice to be able to grep for that string to find out if
479## anyone's abusing the system.
480###########################################################
481
482# $(1): target class, like "APPS"
483# $(2): target name, like "NotePad"
484# $(3): if non-empty, this is a HOST target.
485# $(4): if non-empty, force the intermediates to be COMMON
486# $(5): if non-empty, force the intermediates to be for the 2nd arch
487# $(6): if non-empty, force the intermediates to be for the host cross os
488define intermediates-dir-for
489$(strip \
490    $(eval _idfClass := $(strip $(1))) \
491    $(if $(_idfClass),, \
492        $(error $(LOCAL_PATH): Class not defined in call to intermediates-dir-for)) \
493    $(eval _idfName := $(strip $(2))) \
494    $(if $(_idfName),, \
495        $(error $(LOCAL_PATH): Name not defined in call to intermediates-dir-for)) \
496    $(eval _idfPrefix := $(if $(strip $(3)),$(if $(strip $(6)),HOST_CROSS,HOST),TARGET)) \
497    $(eval _idf2ndArchPrefix := $(if $(strip $(5)),$(TARGET_2ND_ARCH_VAR_PREFIX))) \
498    $(if $(filter $(_idfPrefix)-$(_idfClass),$(COMMON_MODULE_CLASSES))$(4), \
499        $(eval _idfIntBase := $($(_idfPrefix)_OUT_COMMON_INTERMEDIATES)) \
500      ,$(if $(filter $(_idfClass),$(PER_ARCH_MODULE_CLASSES)),\
501          $(eval _idfIntBase := $($(_idf2ndArchPrefix)$(_idfPrefix)_OUT_INTERMEDIATES)) \
502       ,$(eval _idfIntBase := $($(_idfPrefix)_OUT_INTERMEDIATES)) \
503       ) \
504     ) \
505    $(_idfIntBase)/$(_idfClass)/$(_idfName)_intermediates \
506)
507endef
508
509# Uses LOCAL_MODULE_CLASS, LOCAL_MODULE, and LOCAL_IS_HOST_MODULE
510# to determine the intermediates directory.
511#
512# $(1): if non-empty, force the intermediates to be COMMON
513# $(2): if non-empty, force the intermediates to be for the 2nd arch
514# $(3): if non-empty, force the intermediates to be for the host cross os
515define local-intermediates-dir
516$(strip \
517    $(if $(strip $(LOCAL_MODULE_CLASS)),, \
518        $(error $(LOCAL_PATH): LOCAL_MODULE_CLASS not defined before call to local-intermediates-dir)) \
519    $(if $(strip $(LOCAL_MODULE)),, \
520        $(error $(LOCAL_PATH): LOCAL_MODULE not defined before call to local-intermediates-dir)) \
521    $(call intermediates-dir-for,$(LOCAL_MODULE_CLASS),$(LOCAL_MODULE),$(LOCAL_IS_HOST_MODULE),$(1),$(2),$(3)) \
522)
523endef
524
525###########################################################
526## The generated sources directory.  Placing generated
527## source files directly in the intermediates directory
528## causes problems for multiarch builds, where there are
529## two intermediates directories for a single target. Put
530## them in a separate directory, and they will be copied to
531## each intermediates directory automatically.
532###########################################################
533
534# $(1): target class, like "APPS"
535# $(2): target name, like "NotePad"
536# $(3): if non-empty, this is a HOST target.
537# $(4): if non-empty, force the generated sources to be COMMON
538define generated-sources-dir-for
539$(strip \
540    $(eval _idfClass := $(strip $(1))) \
541    $(if $(_idfClass),, \
542        $(error $(LOCAL_PATH): Class not defined in call to generated-sources-dir-for)) \
543    $(eval _idfName := $(strip $(2))) \
544    $(if $(_idfName),, \
545        $(error $(LOCAL_PATH): Name not defined in call to generated-sources-dir-for)) \
546    $(eval _idfPrefix := $(if $(strip $(3)),HOST,TARGET)) \
547    $(if $(filter $(_idfPrefix)-$(_idfClass),$(COMMON_MODULE_CLASSES))$(4), \
548        $(eval _idfIntBase := $($(_idfPrefix)_OUT_COMMON_GEN)) \
549      , \
550        $(eval _idfIntBase := $($(_idfPrefix)_OUT_GEN)) \
551     ) \
552    $(_idfIntBase)/$(_idfClass)/$(_idfName)_intermediates \
553)
554endef
555
556# Uses LOCAL_MODULE_CLASS, LOCAL_MODULE, and LOCAL_IS_HOST_MODULE
557# to determine the generated sources directory.
558#
559# $(1): if non-empty, force the intermediates to be COMMON
560define local-generated-sources-dir
561$(strip \
562    $(if $(strip $(LOCAL_MODULE_CLASS)),, \
563        $(error $(LOCAL_PATH): LOCAL_MODULE_CLASS not defined before call to local-generated-sources-dir)) \
564    $(if $(strip $(LOCAL_MODULE)),, \
565        $(error $(LOCAL_PATH): LOCAL_MODULE not defined before call to local-generated-sources-dir)) \
566    $(call generated-sources-dir-for,$(LOCAL_MODULE_CLASS),$(LOCAL_MODULE),$(LOCAL_IS_HOST_MODULE),$(1)) \
567)
568endef
569
570###########################################################
571## Convert "path/to/libXXX.so" to "-lXXX".
572## Any "path/to/libXXX.a" elements pass through unchanged.
573###########################################################
574
575define normalize-libraries
576$(foreach so,$(filter %.so,$(1)),-l$(patsubst lib%.so,%,$(notdir $(so))))\
577$(filter-out %.so,$(1))
578endef
579
580# TODO: change users to call the common version.
581define normalize-host-libraries
582$(call normalize-libraries,$(1))
583endef
584
585define normalize-target-libraries
586$(call normalize-libraries,$(1))
587endef
588
589###########################################################
590## Convert a list of short module names (e.g., "framework", "Browser")
591## into the list of files that are built for those modules.
592## NOTE: this won't return reliable results until after all
593## sub-makefiles have been included.
594## $(1): target list
595###########################################################
596
597define module-built-files
598$(foreach module,$(1),$(ALL_MODULES.$(module).BUILT))
599endef
600
601###########################################################
602## Convert a list of short modules names (e.g., "framework", "Browser")
603## into the list of files that are installed for those modules.
604## NOTE: this won't return reliable results until after all
605## sub-makefiles have been included.
606## $(1): target list
607###########################################################
608
609define module-installed-files
610$(foreach module,$(1),$(ALL_MODULES.$(module).INSTALLED))
611endef
612
613###########################################################
614## Convert a list of short modules names (e.g., "framework", "Browser")
615## into the list of files that should be used when linking
616## against that module as a public API.
617## TODO: Allow this for more than JAVA_LIBRARIES modules
618## NOTE: this won't return reliable results until after all
619## sub-makefiles have been included.
620## $(1): target list
621###########################################################
622
623define module-stubs-files
624$(foreach module,$(1),$(ALL_MODULES.$(module).STUBS))
625endef
626
627###########################################################
628## Evaluates to the timestamp file for a doc module, which
629## is the dependency that should be used.
630## $(1): doc module
631###########################################################
632
633define doc-timestamp-for
634$(OUT_DOCS)/$(strip $(1))-timestamp
635endef
636
637
638###########################################################
639## Convert "core ext framework" to "out/.../javalib.jar ..."
640## $(1): library list
641## $(2): Non-empty if IS_HOST_MODULE
642###########################################################
643
644# $(1): library name
645# $(2): Non-empty if IS_HOST_MODULE
646define _java-lib-dir
647$(call intermediates-dir-for, \
648	JAVA_LIBRARIES,$(1),$(2),COMMON)
649endef
650
651# $(1): library name
652# $(2): Non-empty if IS_HOST_MODULE
653define _java-lib-full-classes.jar
654$(call _java-lib-dir,$(1),$(2))/$(if $(2),javalib,classes)$(COMMON_JAVA_PACKAGE_SUFFIX)
655endef
656
657# Get the jar files (you can pass to "javac -classpath") of static or shared
658# Java libraries that you want to link against.
659# $(1): library name list
660# $(2): Non-empty if IS_HOST_MODULE
661define java-lib-files
662$(foreach lib,$(1),$(call _java-lib-full-classes.jar,$(lib),$(2)))
663endef
664
665# Get the dependency files (you can put on the right side of "|" of a build rule)
666# of the Java libraries.
667# $(1): library name list
668# $(2): Non-empty if IS_HOST_MODULE
669# Historically for target Java libraries we used a different file (javalib.jar)
670# as the dependency.
671# Now we can use classes.jar as dependency, so java-lib-deps is the same
672# as java-lib-files.
673define java-lib-deps
674$(call java-lib-files,$(1),$(2))
675endef
676
677# Get the jar files (you can pass to "javac -classpath") of host dalvik Java libraries.
678# You can also use them as dependency files.
679# A host dalvik Java library is different from a host Java library in that
680# the java lib file is classes.jar, not javalib.jar.
681# $(1): library name list
682define host-dex-java-lib-files
683$(foreach lib,$(1),$(call _java-lib-dir,$(lib),true)/classes.jar)
684endef
685
686###########################################################
687## Convert "core ext framework" to "out/.../classes.jack ..."
688## $(1): library list
689## $(2): Non-empty if IS_HOST_MODULE
690###########################################################
691
692# $(1): library name
693# $(2): Non-empty if IS_HOST_MODULE
694define _jack-lib-full-classes
695$(call _java-lib-dir,$(1),$(2))/classes.jack
696endef
697
698# $(1): library name list
699# $(2): Non-empty if IS_HOST_MODULE
700define jack-lib-files
701$(foreach lib,$(1),$(call _jack-lib-full-classes,$(lib),$(2)))
702endef
703
704###########################################################
705## Run rot13 on a string
706## $(1): the string.  Must be one line.
707###########################################################
708define rot13
709$(shell echo $(1) | tr 'a-zA-Z' 'n-za-mN-ZA-M')
710endef
711
712
713###########################################################
714## Returns true if $(1) and $(2) are equal.  Returns
715## the empty string if they are not equal.
716###########################################################
717define streq
718$(strip $(if $(strip $(1)),\
719  $(if $(strip $(2)),\
720    $(if $(filter-out __,_$(subst $(strip $(1)),,$(strip $(2)))$(subst $(strip $(2)),,$(strip $(1)))_),,true), \
721    ),\
722  $(if $(strip $(2)),\
723    ,\
724    true)\
725 ))
726endef
727
728###########################################################
729## Convert "a b c" into "a:b:c"
730###########################################################
731define normalize-path-list
732$(subst $(space),:,$(strip $(1)))
733endef
734
735###########################################################
736## Read the word out of a colon-separated list of words.
737## This has the same behavior as the built-in function
738## $(word n,str).
739##
740## The individual words may not contain spaces.
741##
742## $(1): 1 based index
743## $(2): value of the form a:b:c...
744###########################################################
745
746define word-colon
747$(word $(1),$(subst :,$(space),$(2)))
748endef
749
750###########################################################
751## Convert "a=b c= d e = f" into "a=b c=d e=f"
752##
753## $(1): list to collapse
754## $(2): if set, separator word; usually "=", ":", or ":="
755##       Defaults to "=" if not set.
756###########################################################
757
758define collapse-pairs
759$(eval _cpSEP := $(strip $(if $(2),$(2),=)))\
760$(subst $(space)$(_cpSEP)$(space),$(_cpSEP),$(strip \
761    $(subst $(_cpSEP), $(_cpSEP) ,$(1))))
762endef
763
764###########################################################
765## Given a list of pairs, if multiple pairs have the same
766## first components, keep only the first pair.
767##
768## $(1): list of pairs
769## $(2): the separator word, such as ":", "=", etc.
770define uniq-pairs-by-first-component
771$(eval _upbfc_fc_set :=)\
772$(strip $(foreach w,$(1), $(eval _first := $(word 1,$(subst $(2),$(space),$(w))))\
773    $(if $(filter $(_upbfc_fc_set),$(_first)),,$(w)\
774        $(eval _upbfc_fc_set += $(_first)))))\
775$(eval _upbfc_fc_set :=)\
776$(eval _first:=)
777endef
778
779###########################################################
780## MODULE_TAG set operations
781###########################################################
782
783# Given a list of tags, return the targets that specify
784# any of those tags.
785# $(1): tag list
786define modules-for-tag-list
787$(sort $(foreach tag,$(1),$(foreach m,$(ALL_MODULE_NAME_TAGS.$(tag)),$(ALL_MODULES.$(m).INSTALLED))))
788endef
789
790# Same as modules-for-tag-list, but operates on
791# ALL_MODULE_NAME_TAGS.
792# $(1): tag list
793define module-names-for-tag-list
794$(sort $(foreach tag,$(1),$(ALL_MODULE_NAME_TAGS.$(tag))))
795endef
796
797# Given an accept and reject list, find the matching
798# set of targets.  If a target has multiple tags and
799# any of them are rejected, the target is rejected.
800# Reject overrides accept.
801# $(1): list of tags to accept
802# $(2): list of tags to reject
803#TODO(dbort): do $(if $(strip $(1)),$(1),$(ALL_MODULE_TAGS))
804#TODO(jbq): as of 20100106 nobody uses the second parameter
805define get-tagged-modules
806$(filter-out \
807	$(call modules-for-tag-list,$(2)), \
808	    $(call modules-for-tag-list,$(1)))
809endef
810
811###########################################################
812## Append a leaf to a base path.  Properly deals with
813## base paths ending in /.
814##
815## $(1): base path
816## $(2): leaf path
817###########################################################
818
819define append-path
820$(subst //,/,$(1)/$(2))
821endef
822
823
824###########################################################
825## Color-coded warnings and errors in build rules
826##
827## $(1): message to print
828###########################################################
829define echo-warning
830echo -e "\e[1;35mwarning:\e[0m \e[1m" $(1) "\e[0m\n"
831endef
832
833define echo-error
834echo -e "\e[1;31merror:\e[0m \e[1m" $(1) "\e[0m\n"
835endef
836
837
838###########################################################
839## Package filtering
840###########################################################
841
842# Given a list of installed modules (short or long names)
843# return a list of the packages (yes, .apk packages, not
844# modules in general) that are overridden by this list and,
845# therefore, should not be installed.
846# $(1): mixed list of installed modules
847# TODO: This is fragile; find a reliable way to get this information.
848define _get-package-overrides
849 $(eval ### Discard any words containing slashes, unless they end in .apk, \
850        ### in which case trim off the directory component and the suffix. \
851        ### If there are no slashes, keep the entire word.)
852 $(eval _gpo_names := $(subst /,@@@ @@@,$(1)))
853 $(eval _gpo_names := \
854     $(filter %.apk,$(_gpo_names)) \
855     $(filter-out %@@@ @@@%,$(_gpo_names)))
856 $(eval _gpo_names := $(patsubst %.apk,%,$(_gpo_names)))
857 $(eval _gpo_names := $(patsubst @@@%,%,$(_gpo_names)))
858
859 $(eval ### Remove any remaining words that contain dots.)
860 $(eval _gpo_names := $(subst .,@@@ @@@,$(_gpo_names)))
861 $(eval _gpo_names := $(filter-out %@@@ @@@%,$(_gpo_names)))
862
863 $(eval ### Now we have a list of any words that could possibly refer to \
864        ### packages, although there may be words that do not.  Only \
865        ### real packages will be present under PACKAGES.*, though.)
866 $(foreach _gpo_name,$(_gpo_names),$(PACKAGES.$(_gpo_name).OVERRIDES))
867endef
868
869define get-package-overrides
870$(sort $(strip $(call _get-package-overrides,$(1))))
871endef
872
873###########################################################
874## Output the command lines, or not
875###########################################################
876
877ifeq ($(strip $(SHOW_COMMANDS)),)
878define pretty
879@echo $1
880endef
881else
882define pretty
883endef
884endif
885
886###########################################################
887## Commands for munging the dependency files the compiler generates
888###########################################################
889# $(1): the input .d file
890# $(2): the output .P file
891define transform-d-to-p-args
892$(hide) cp $(1) $(2); \
893	sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
894		-e '/^$$/ d' -e 's/$$/ :/' < $(1) >> $(2); \
895	rm -f $(1)
896endef
897
898define transform-d-to-p
899$(call transform-d-to-p-args,$(@:%.o=%.d),$(@:%.o=%.P))
900endef
901
902###########################################################
903## Commands for including the dependency files the compiler generates
904###########################################################
905# $(1): the .P file
906# $(2): the main build target
907define include-depfile
908$(eval $(2) : .KATI_DEPFILE := $1)
909endef
910
911# $(1): object files
912define include-depfiles-for-objs
913$(foreach obj, $(1), $(call include-depfile, $(obj:%.o=%.P), $(obj)))
914endef
915
916###########################################################
917## Track source files compiled to objects
918###########################################################
919# $(1): list of sources
920# $(2): list of matching objects
921define track-src-file-obj
922$(eval $(call _track-src-file-obj,$(1)))
923endef
924define _track-src-file-obj
925i := w
926$(foreach s,$(1),
927my_tracked_src_files += $(s)
928my_src_file_obj_$(s) := $$(word $$(words $$(i)),$$(2))
929i += w)
930endef
931
932# $(1): list of sources
933# $(2): list of matching generated sources
934define track-src-file-gen
935$(eval $(call _track-src-file-gen,$(2)))
936endef
937define _track-src-file-gen
938i := w
939$(foreach s,$(1),
940my_tracked_gen_files += $(s)
941my_src_file_gen_$(s) := $$(word $$(words $$(i)),$$(1))
942i += w)
943endef
944
945# $(1): list of generated sources
946# $(2): list of matching objects
947define track-gen-file-obj
948$(call track-src-file-obj,$(foreach f,$(1),\
949  $(or $(my_src_file_gen_$(f)),$(f))),$(2))
950endef
951
952###########################################################
953## Commands for running lex
954###########################################################
955
956define transform-l-to-c-or-cpp
957@echo "Lex: $(PRIVATE_MODULE) <= $<"
958@mkdir -p $(dir $@)
959$(hide) $(LEX) -o$@ $<
960endef
961
962###########################################################
963## Commands for running yacc
964##
965###########################################################
966
967define transform-y-to-c-or-cpp
968@echo "Yacc: $(PRIVATE_MODULE) <= $<"
969@mkdir -p $(dir $@)
970$(YACC) $(PRIVATE_YACCFLAGS) \
971  --defines=$(basename $@).h \
972  -o $@ $<
973endef
974
975###########################################################
976## Commands to compile RenderScript to Java
977###########################################################
978
979## Merge multiple .d files generated by llvm-rs-cc. This is necessary
980## because ninja can handle only a single depfile per build target.
981## .d files generated by llvm-rs-cc define .stamp, .bc, and optionally
982## .java as build targets. However, there's no way to let ninja know
983## dependencies to .bc files and .java files, so we give up build
984## targets for them. As we write the .stamp file as the target by
985## ourselves, the awk script removes the first lines before the colon
986## and append a backslash to the last line to concatenate contents of
987## multiple files.
988# $(1): .d files to be merged
989# $(2): merged .d file
990define _merge-renderscript-d
991$(hide) echo '$@: $(backslash)' > $2
992$(foreach d,$1, \
993  $(hide) awk 'start { sub(/( \\)?$$/, " \\"); print } /:/ { start=1 }' < $d >> $2$(newline))
994$(hide) echo >> $2
995endef
996
997define transform-renderscripts-to-java-and-bc
998@echo "RenderScript: $(PRIVATE_MODULE) <= $(PRIVATE_RS_SOURCE_FILES)"
999$(hide) rm -rf $(PRIVATE_RS_OUTPUT_DIR)
1000$(hide) mkdir -p $(PRIVATE_RS_OUTPUT_DIR)/res/raw
1001$(hide) mkdir -p $(PRIVATE_RS_OUTPUT_DIR)/src
1002$(hide) $(PRIVATE_RS_CC) \
1003  -o $(PRIVATE_RS_OUTPUT_DIR)/res/raw \
1004  -p $(PRIVATE_RS_OUTPUT_DIR)/src \
1005  -d $(PRIVATE_RS_OUTPUT_DIR) \
1006  -a $@ -MD \
1007  $(addprefix -target-api , $(PRIVATE_RS_TARGET_API)) \
1008  $(PRIVATE_RS_FLAGS) \
1009  $(foreach inc,$(PRIVATE_RS_INCLUDES),$(addprefix -I , $(inc))) \
1010  $(PRIVATE_RS_SOURCE_FILES)
1011$(call _merge-renderscript-d,$(PRIVATE_DEP_FILES),$@.d)
1012$(call transform-d-to-p-args,$@.d,$@.P)
1013$(hide) mkdir -p $(dir $@)
1014$(hide) touch $@
1015endef
1016
1017define transform-bc-to-so
1018@echo "Renderscript compatibility: $(notdir $@) <= $(notdir $<)"
1019$(hide) mkdir -p $(dir $@)
1020$(hide) $(BCC_COMPAT) -O3 -o $(dir $@)/$(notdir $(<:.bc=.o)) -fPIC -shared \
1021	-rt-path $(RS_PREBUILT_CLCORE) -mtriple $(RS_COMPAT_TRIPLE) $<
1022$(hide) $(PRIVATE_CXX) -shared -Wl,-soname,$(notdir $@) -nostdlib \
1023	-Wl,-rpath,\$$ORIGIN/../lib \
1024	$(dir $@)/$(notdir $(<:.bc=.o)) \
1025	$(RS_PREBUILT_COMPILER_RT) \
1026	-o $@ $(TARGET_GLOBAL_LDFLAGS) -Wl,--hash-style=sysv -L prebuilts/gcc/ \
1027	$(RS_PREBUILT_LIBPATH) -L $(TARGET_OUT_INTERMEDIATE_LIBRARIES) \
1028	-lRSSupport -lm -lc
1029endef
1030
1031###########################################################
1032## Commands to compile RenderScript to C++
1033###########################################################
1034
1035define transform-renderscripts-to-cpp-and-bc
1036@echo "RenderScript: $(PRIVATE_MODULE) <= $(PRIVATE_RS_SOURCE_FILES)"
1037$(hide) rm -rf $(PRIVATE_RS_OUTPUT_DIR)
1038$(hide) mkdir -p $(PRIVATE_RS_OUTPUT_DIR)/
1039$(hide) $(PRIVATE_RS_CC) \
1040  -o $(PRIVATE_RS_OUTPUT_DIR)/ \
1041  -d $(PRIVATE_RS_OUTPUT_DIR) \
1042  -a $@ -MD \
1043  -reflect-c++ \
1044  $(addprefix -target-api , $(PRIVATE_RS_TARGET_API)) \
1045  $(PRIVATE_RS_FLAGS) \
1046  $(addprefix -I , $(PRIVATE_RS_INCLUDES)) \
1047  $(PRIVATE_RS_SOURCE_FILES)
1048$(call _merge-renderscript-d,$(PRIVATE_DEP_FILES),$@.d)
1049$(call transform-d-to-p-args,$@.d,$@.P)
1050$(hide) mkdir -p $(dir $@)
1051$(hide) touch $@
1052endef
1053
1054
1055###########################################################
1056## Commands for running aidl
1057###########################################################
1058
1059define transform-aidl-to-java
1060@mkdir -p $(dir $@)
1061@echo "Aidl: $(PRIVATE_MODULE) <= $<"
1062$(hide) $(AIDL) -d$(patsubst %.java,%.P,$@) $(PRIVATE_AIDL_FLAGS) $< $@
1063endef
1064#$(AIDL) $(PRIVATE_AIDL_FLAGS) $< - | indent -nut -br -npcs -l1000 > $@
1065
1066define transform-aidl-to-cpp
1067@mkdir -p $(dir $@)
1068@mkdir -p $(PRIVATE_HEADER_OUTPUT_DIR)
1069@echo "Generating C++ from AIDL: $(PRIVATE_MODULE) <= $<"
1070$(hide) $(AIDL_CPP) -d$(basename $@).aidl.P $(PRIVATE_AIDL_FLAGS) \
1071    $< $(PRIVATE_HEADER_OUTPUT_DIR) $@
1072endef
1073
1074## Given a .aidl file path, generate the rule to compile it a .java file
1075# $(1): a .aidl source file
1076# $(2): a directory to place the generated .java files in
1077# $(3): name of a variable to add the path to the generated source file to
1078#
1079# You must call this with $(eval).
1080define define-aidl-java-rule
1081define-aidl-java-rule-src := $(patsubst %.aidl,%.java,$(subst ../,dotdot/,$(addprefix $(2)/,$(1))))
1082$$(define-aidl-java-rule-src) : $(LOCAL_PATH)/$(1) $(AIDL)
1083	$$(transform-aidl-to-java)
1084$(3) += $$(define-aidl-java-rule-src)
1085endef
1086
1087## Given a .aidl file path generate the rule to compile it a .cpp file.
1088# $(1): a .aidl source file
1089# $(2): a directory to place the generated .cpp files in
1090# $(3): name of a variable to add the path to the generated source file to
1091#
1092# You must call this with $(eval).
1093define define-aidl-cpp-rule
1094define-aidl-cpp-rule-src := $(patsubst %.aidl,%$(LOCAL_CPP_EXTENSION),$(subst ../,dotdot/,$(addprefix $(2)/,$(1))))
1095$$(define-aidl-cpp-rule-src) : $(LOCAL_PATH)/$(1) $(AIDL_CPP)
1096	$$(transform-aidl-to-cpp)
1097$(3) += $$(define-aidl-cpp-rule-src)
1098endef
1099
1100###########################################################
1101## Commands for running vts
1102###########################################################
1103
1104define transform-vts-to-cpp
1105@mkdir -p $(dir $@)
1106@mkdir -p $(PRIVATE_HEADER_OUTPUT_DIR)
1107@echo "Generating C++ from VTS: $(PRIVATE_MODULE) <= $<"
1108$(hide) $(VTSC) -d$(basename $@).vts.P $(PRIVATE_VTS_FLAGS) \
1109    $< $(PRIVATE_HEADER_OUTPUT_DIR) $@
1110endef
1111
1112## Given a .vts file path generate the rule to compile it a .cpp file.
1113# $(1): a .vts source file
1114# $(2): a directory to place the generated .cpp files in
1115# $(3): name of a variable to add the path to the generated source file to
1116#
1117# You must call this with $(eval).
1118define define-vts-cpp-rule
1119define-vts-cpp-rule-src := $(patsubst %.vts,%$(LOCAL_CPP_EXTENSION),$(subst ../,dotdot/,$(addprefix $(2)/,$(1))))
1120$$(define-vts-cpp-rule-src) : $(LOCAL_PATH)/$(1) $(VTSC)
1121	$$(transform-vts-to-cpp)
1122$(3) += $$(define-vts-cpp-rule-src)
1123endef
1124
1125###########################################################
1126## Commands for running java-event-log-tags.py
1127###########################################################
1128
1129define transform-logtags-to-java
1130@mkdir -p $(dir $@)
1131@echo "logtags: $@ <= $<"
1132$(hide) $(JAVATAGS) -o $@ $< $(PRIVATE_MERGED_TAG)
1133endef
1134
1135
1136###########################################################
1137## Commands for running protoc to compile .proto into .java
1138###########################################################
1139
1140define transform-proto-to-java
1141@mkdir -p $(dir $@)
1142@echo "Protoc: $@ <= $(PRIVATE_PROTO_SRC_FILES)"
1143@rm -rf $(PRIVATE_PROTO_JAVA_OUTPUT_DIR)
1144@mkdir -p $(PRIVATE_PROTO_JAVA_OUTPUT_DIR)
1145$(hide) for f in $(PRIVATE_PROTO_SRC_FILES); do \
1146        $(PROTOC) \
1147        $(addprefix --proto_path=, $(PRIVATE_PROTO_INCLUDES)) \
1148        $(PRIVATE_PROTO_JAVA_OUTPUT_OPTION)="$(PRIVATE_PROTO_JAVA_OUTPUT_PARAMS):$(PRIVATE_PROTO_JAVA_OUTPUT_DIR)" \
1149        $(PRIVATE_PROTOC_FLAGS) \
1150        $$f || exit 33; \
1151        done
1152$(hide) touch $@
1153endef
1154
1155######################################################################
1156## Commands for running protoc to compile .proto into .pb.cc (or.pb.c) and .pb.h
1157######################################################################
1158define transform-proto-to-cc
1159@echo "Protoc: $@ <= $<"
1160@mkdir -p $(dir $@)
1161$(hide) $(PROTOC) \
1162	$(addprefix --proto_path=, $(PRIVATE_PROTO_INCLUDES)) \
1163	$(PRIVATE_PROTOC_FLAGS) \
1164	$<
1165@# aprotoc outputs only .cc. Rename it to .cpp if necessary.
1166$(if $(PRIVATE_RENAME_CPP_EXT),\
1167  $(hide) mv $(basename $@).cc $@)
1168endef
1169
1170
1171######################################################################
1172## Commands for generating DBus adaptors from .dbus-xml files.
1173######################################################################
1174define generate-dbus-adaptors
1175@echo "Generating DBus adaptors for $(PRIVATE_MODULE)"
1176@mkdir -p $(dir $@)
1177$(hide) $(DBUS_GENERATOR) \
1178	--service-config=$(PRIVATE_DBUS_SERVICE_CONFIG) \
1179	--adaptor=$@ \
1180	$<
1181endef
1182
1183######################################################################
1184## Commands for generating DBus proxies from .dbus-xml files.
1185######################################################################
1186define generate-dbus-proxies
1187@echo "Generating DBus proxies for $(PRIVATE_MODULE)"
1188@mkdir -p $(dir $@)
1189$(hide) $(DBUS_GENERATOR) \
1190	--service-config=$(PRIVATE_DBUS_SERVICE_CONFIG) \
1191	--proxy=$@ \
1192	$(filter %.dbus-xml,$^)
1193endef
1194
1195
1196###########################################################
1197## Commands for running gcc to compile a C++ file
1198###########################################################
1199
1200define transform-cpp-to-o-compiler-args
1201	$(addprefix -I , $(PRIVATE_C_INCLUDES)) \
1202	$$(cat $(PRIVATE_IMPORT_INCLUDES)) \
1203	$(addprefix -isystem ,\
1204	    $(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1205	        $(filter-out $(PRIVATE_C_INCLUDES), \
1206	            $(PRIVATE_TARGET_PROJECT_INCLUDES) \
1207	            $(PRIVATE_TARGET_C_INCLUDES)))) \
1208	-c \
1209	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1210	    $(PRIVATE_TARGET_GLOBAL_CFLAGS) \
1211	    $(PRIVATE_TARGET_GLOBAL_CPPFLAGS) \
1212	    $(PRIVATE_ARM_CFLAGS) \
1213	 ) \
1214	$(PRIVATE_RTTI_FLAG) \
1215	$(PRIVATE_CFLAGS) \
1216	$(PRIVATE_CPPFLAGS) \
1217	$(PRIVATE_DEBUG_CFLAGS) \
1218	$(PRIVATE_CFLAGS_NO_OVERRIDE) \
1219	$(PRIVATE_CPPFLAGS_NO_OVERRIDE)
1220endef
1221
1222define clang-tidy-cpp
1223$(hide) $(PATH_TO_CLANG_TIDY) $(PRIVATE_TIDY_FLAGS) \
1224  -checks=$(PRIVATE_TIDY_CHECKS) \
1225  $< -- $(transform-cpp-to-o-compiler-args)
1226endef
1227
1228ifneq (,$(filter 1 true,$(WITH_TIDY_ONLY)))
1229define transform-cpp-to-o
1230$(if $(PRIVATE_TIDY_CHECKS),
1231  @echo "target tidy $(PRIVATE_ARM_MODE) C++: $<"
1232  $(clang-tidy-cpp))
1233endef
1234else
1235define transform-cpp-to-o
1236@echo "target $(PRIVATE_ARM_MODE) C++: $(PRIVATE_MODULE) <= $<"
1237@mkdir -p $(dir $@)
1238$(if $(PRIVATE_TIDY_CHECKS),$(clang-tidy-cpp))
1239$(hide) $(RELATIVE_PWD) $(PRIVATE_CXX) \
1240  $(transform-cpp-to-o-compiler-args) \
1241  -MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1242$(hide) $(transform-d-to-p)
1243endef
1244endif
1245
1246
1247###########################################################
1248## Commands for running gcc to compile a C file
1249###########################################################
1250
1251# $(1): extra flags
1252define transform-c-or-s-to-o-compiler-args
1253	$(addprefix -I , $(PRIVATE_C_INCLUDES)) \
1254	$$(cat $(PRIVATE_IMPORT_INCLUDES)) \
1255	$(addprefix -isystem ,\
1256	    $(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1257	        $(filter-out $(PRIVATE_C_INCLUDES), \
1258	            $(PRIVATE_TARGET_PROJECT_INCLUDES) \
1259	            $(PRIVATE_TARGET_C_INCLUDES)))) \
1260	-c \
1261	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1262	    $(PRIVATE_TARGET_GLOBAL_CFLAGS) \
1263	    $(PRIVATE_TARGET_GLOBAL_CONLYFLAGS) \
1264	    $(PRIVATE_ARM_CFLAGS) \
1265	 ) \
1266	 $(1)
1267endef
1268
1269define transform-c-to-o-compiler-args
1270$(call transform-c-or-s-to-o-compiler-args, \
1271  $(PRIVATE_CFLAGS) \
1272  $(PRIVATE_CONLYFLAGS) \
1273  $(PRIVATE_DEBUG_CFLAGS) \
1274  $(PRIVATE_CFLAGS_NO_OVERRIDE))
1275endef
1276
1277define clang-tidy-c
1278$(hide) $(PATH_TO_CLANG_TIDY) $(PRIVATE_TIDY_FLAGS) \
1279  -checks=$(PRIVATE_TIDY_CHECKS) \
1280  $< -- $(transform-c-to-o-compiler-args)
1281endef
1282
1283ifneq (,$(filter 1 true,$(WITH_TIDY_ONLY)))
1284define transform-c-to-o
1285$(if $(PRIVATE_TIDY_CHECKS),
1286  @echo "target tidy $(PRIVATE_ARM_MODE) C: $<"
1287  $(clang-tidy-c))
1288endef
1289else
1290define transform-c-to-o
1291@echo "target $(PRIVATE_ARM_MODE) C: $(PRIVATE_MODULE) <= $<"
1292@mkdir -p $(dir $@)
1293$(if $(PRIVATE_TIDY_CHECKS),$(clang-tidy-c))
1294$(hide) $(RELATIVE_PWD) $(PRIVATE_CC) \
1295  $(transform-c-to-o-compiler-args) \
1296  -MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1297$(hide) $(transform-d-to-p)
1298endef
1299endif
1300
1301define transform-s-to-o-no-deps
1302@echo "target asm: $(PRIVATE_MODULE) <= $<"
1303@mkdir -p $(dir $@)
1304$(RELATIVE_PWD) $(PRIVATE_CC) \
1305  $(call transform-c-or-s-to-o-compiler-args, $(PRIVATE_ASFLAGS)) \
1306  -MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1307endef
1308
1309define transform-s-to-o
1310$(transform-s-to-o-no-deps)
1311$(transform-d-to-p)
1312endef
1313
1314# YASM compilation
1315define transform-asm-to-o
1316@mkdir -p $(dir $@)
1317$(hide) $(YASM) \
1318    $(addprefix -I , $(PRIVATE_C_INCLUDES)) \
1319    $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_GLOBAL_YASM_FLAGS) \
1320    $(PRIVATE_ASFLAGS) \
1321    -o $@ $<
1322endef
1323
1324###########################################################
1325## Commands for running gcc to compile an Objective-C file
1326## This should never happen for target builds but this
1327## will error at build time.
1328###########################################################
1329
1330define transform-m-to-o-no-deps
1331@echo "target ObjC: $(PRIVATE_MODULE) <= $<"
1332$(call transform-c-or-s-to-o-no-deps, $(PRIVATE_CFLAGS) $(PRIVATE_DEBUG_CFLAGS))
1333endef
1334
1335define transform-m-to-o
1336$(transform-m-to-o-no-deps)
1337$(transform-d-to-p)
1338endef
1339
1340###########################################################
1341## Commands for running gcc to compile a host C++ file
1342###########################################################
1343
1344define transform-host-cpp-to-o-compiler-args
1345	$(addprefix -I , $(PRIVATE_C_INCLUDES)) \
1346	$$(cat $(PRIVATE_IMPORT_INCLUDES)) \
1347	$(addprefix -isystem ,\
1348	    $(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1349	        $(filter-out $(PRIVATE_C_INCLUDES), \
1350	            $($(PRIVATE_PREFIX)PROJECT_INCLUDES) \
1351	            $(PRIVATE_HOST_C_INCLUDES)))) \
1352	-c \
1353	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1354	    $(PRIVATE_HOST_GLOBAL_CFLAGS) \
1355	    $(PRIVATE_HOST_GLOBAL_CPPFLAGS) \
1356	 ) \
1357	$(PRIVATE_CFLAGS) \
1358	$(PRIVATE_CPPFLAGS) \
1359	$(PRIVATE_DEBUG_CFLAGS) \
1360	$(PRIVATE_CFLAGS_NO_OVERRIDE) \
1361	$(PRIVATE_CPPFLAGS_NO_OVERRIDE)
1362endef
1363
1364define clang-tidy-host-cpp
1365$(hide) $(PATH_TO_CLANG_TIDY) $(PRIVATE_TIDY_FLAGS) \
1366  -checks=$(PRIVATE_TIDY_CHECKS) \
1367  $< -- $(transform-host-cpp-to-o-compiler-args)
1368endef
1369
1370ifneq (,$(filter 1 true,$(WITH_TIDY_ONLY)))
1371define transform-host-cpp-to-o
1372$(if $(PRIVATE_TIDY_CHECKS),
1373  @echo "tidy $($(PRIVATE_PREFIX)DISPLAY) C++: $<"
1374  $(clang-tidy-host-cpp))
1375endef
1376else
1377define transform-host-cpp-to-o
1378@echo "$($(PRIVATE_PREFIX)DISPLAY) C++: $(PRIVATE_MODULE) <= $<"
1379@mkdir -p $(dir $@)
1380$(if $(PRIVATE_TIDY_CHECKS),$(clang-tidy-host-cpp))
1381$(hide) $(RELATIVE_PWD) $(PRIVATE_CXX) \
1382  $(transform-host-cpp-to-o-compiler-args) \
1383  -MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1384$(hide) $(transform-d-to-p)
1385endef
1386endif
1387
1388
1389###########################################################
1390## Commands for running gcc to compile a host C file
1391###########################################################
1392
1393define transform-host-c-or-s-to-o-common-args
1394	$(addprefix -I , $(PRIVATE_C_INCLUDES)) \
1395	$$(cat $(PRIVATE_IMPORT_INCLUDES)) \
1396	$(addprefix -isystem ,\
1397	    $(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1398	        $(filter-out $(PRIVATE_C_INCLUDES), \
1399	            $($(PRIVATE_PREFIX)PROJECT_INCLUDES) \
1400	            $(PRIVATE_HOST_C_INCLUDES)))) \
1401	-c \
1402	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1403	    $(PRIVATE_HOST_GLOBAL_CFLAGS) \
1404	    $(PRIVATE_HOST_GLOBAL_CONLYFLAGS) \
1405	 )
1406endef
1407
1408# $(1): extra flags
1409define transform-host-c-or-s-to-o-no-deps
1410@mkdir -p $(dir $@)
1411$(hide) $(RELATIVE_PWD) $(PRIVATE_CC) \
1412  $(transform-host-c-or-s-to-o-common-args) \
1413  $(1) \
1414  -MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1415endef
1416
1417define transform-host-c-to-o-compiler-args
1418  $(transform-host-c-or-s-to-o-common-args) \
1419  $(PRIVATE_CFLAGS) $(PRIVATE_CONLYFLAGS) \
1420  $(PRIVATE_DEBUG_CFLAGS) $(PRIVATE_CFLAGS_NO_OVERRIDE)
1421endef
1422
1423define clang-tidy-host-c
1424$(hide) $(PATH_TO_CLANG_TIDY) $(PRIVATE_TIDY_FLAGS) \
1425  -checks=$(PRIVATE_TIDY_CHECKS) \
1426  $< -- $(transform-host-c-to-o-compiler-args)
1427endef
1428
1429ifneq (,$(filter 1 true,$(WITH_TIDY_ONLY)))
1430define transform-host-c-to-o
1431$(if $(PRIVATE_TIDY_CHECKS),
1432  @echo "tidy $($(PRIVATE_PREFIX)DISPLAY) C: $<"
1433  $(clang-tidy-host-c))
1434endef
1435else
1436define transform-host-c-to-o
1437@echo "$($(PRIVATE_PREFIX)DISPLAY) C: $(PRIVATE_MODULE) <= $<"
1438@mkdir -p $(dir $@)
1439$(if $(PRIVATE_TIDY_CHECKS), $(clang-tidy-host-c))
1440$(hide) $(RELATIVE_PWD) $(PRIVATE_CC) \
1441  $(transform-host-c-to-o-compiler-args) \
1442  -MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1443$(hide) $(transform-d-to-p)
1444endef
1445endif
1446
1447define transform-host-s-to-o-no-deps
1448@echo "$($(PRIVATE_PREFIX)DISPLAY) asm: $(PRIVATE_MODULE) <= $<"
1449$(call transform-host-c-or-s-to-o-no-deps, $(PRIVATE_ASFLAGS))
1450endef
1451
1452define transform-host-s-to-o
1453$(transform-host-s-to-o-no-deps)
1454$(transform-d-to-p)
1455endef
1456
1457###########################################################
1458## Commands for running gcc to compile a host Objective-C file
1459###########################################################
1460
1461define transform-host-m-to-o-no-deps
1462@echo "$($(PRIVATE_PREFIX)DISPLAY) ObjC: $(PRIVATE_MODULE) <= $<"
1463$(call transform-host-c-or-s-to-o-no-deps, $(PRIVATE_CFLAGS) $(PRIVATE_DEBUG_CFLAGS) $(PRIVATE_CFLAGS_NO_OVERRIDE))
1464endef
1465
1466define transform-host-m-to-o
1467$(transform-host-m-to-o-no-deps)
1468$(transform-d-to-p)
1469endef
1470
1471###########################################################
1472## Commands for running gcc to compile a host Objective-C++ file
1473###########################################################
1474
1475define transform-host-mm-to-o
1476$(transform-host-cpp-to-o)
1477endef
1478
1479
1480###########################################################
1481## Rules to compile a single C/C++ source with ../ in the path
1482###########################################################
1483# Replace "../" in object paths with $(DOTDOT_REPLACEMENT).
1484DOTDOT_REPLACEMENT := dotdot/
1485
1486## Rule to compile a C++ source file with ../ in the path.
1487## Must be called with $(eval).
1488# $(1): the C++ source file in LOCAL_SRC_FILES.
1489# $(2): the additional dependencies.
1490# $(3): the variable name to collect the output object file.
1491define compile-dotdot-cpp-file
1492o := $(intermediates)/$(patsubst %$(LOCAL_CPP_EXTENSION),%.o,$(subst ../,$(DOTDOT_REPLACEMENT),$(1)))
1493$$(o) : $(TOPDIR)$(LOCAL_PATH)/$(1) $(2)
1494	$$(transform-$$(PRIVATE_HOST)cpp-to-o)
1495$$(call include-depfiles-for-objs, $$(o))
1496$(3) += $$(o)
1497endef
1498
1499## Rule to compile a C source file with ../ in the path.
1500## Must be called with $(eval).
1501# $(1): the C source file in LOCAL_SRC_FILES.
1502# $(2): the additional dependencies.
1503# $(3): the variable name to collect the output object file.
1504define compile-dotdot-c-file
1505o := $(intermediates)/$(patsubst %.c,%.o,$(subst ../,$(DOTDOT_REPLACEMENT),$(1)))
1506$$(o) : $(TOPDIR)$(LOCAL_PATH)/$(1) $(2)
1507	$$(transform-$$(PRIVATE_HOST)c-to-o)
1508$$(call include-depfiles-for-objs, $$(o))
1509$(3) += $$(o)
1510endef
1511
1512## Rule to compile a .S source file with ../ in the path.
1513## Must be called with $(eval).
1514# $(1): the .S source file in LOCAL_SRC_FILES.
1515# $(2): the additional dependencies.
1516# $(3): the variable name to collect the output object file.
1517define compile-dotdot-s-file
1518o := $(intermediates)/$(patsubst %.S,%.o,$(subst ../,$(DOTDOT_REPLACEMENT),$(1)))
1519$$(o) : $(TOPDIR)$(LOCAL_PATH)/$(1) $(2)
1520	$$(transform-$$(PRIVATE_HOST)s-to-o)
1521$$(call include-depfiles-for-objs, $$(o))
1522$(3) += $$(o)
1523endef
1524
1525## Rule to compile a .s source file with ../ in the path.
1526## Must be called with $(eval).
1527# $(1): the .s source file in LOCAL_SRC_FILES.
1528# $(2): the additional dependencies.
1529# $(3): the variable name to collect the output object file.
1530define compile-dotdot-s-file-no-deps
1531o := $(intermediates)/$(patsubst %.s,%.o,$(subst ../,$(DOTDOT_REPLACEMENT),$(1)))
1532$$(o) : $(TOPDIR)$(LOCAL_PATH)/$(1) $(2)
1533	$$(transform-$$(PRIVATE_HOST)s-to-o-no-deps)
1534$(3) += $$(o)
1535endef
1536
1537###########################################################
1538## Commands for running ar
1539###########################################################
1540
1541define _concat-if-arg2-not-empty
1542$(if $(2),$(hide) $(1) $(2))
1543endef
1544
1545# Split long argument list into smaller groups and call the command repeatedly
1546# Call the command at least once even if there are no arguments, as otherwise
1547# the output file won't be created.
1548#
1549# $(1): the command without arguments
1550# $(2): the arguments
1551define split-long-arguments
1552$(hide) $(1) $(wordlist 1,500,$(2))
1553$(call _concat-if-arg2-not-empty,$(1),$(wordlist 501,1000,$(2)))
1554$(call _concat-if-arg2-not-empty,$(1),$(wordlist 1001,1500,$(2)))
1555$(call _concat-if-arg2-not-empty,$(1),$(wordlist 1501,2000,$(2)))
1556$(call _concat-if-arg2-not-empty,$(1),$(wordlist 2001,2500,$(2)))
1557$(call _concat-if-arg2-not-empty,$(1),$(wordlist 2501,3000,$(2)))
1558$(call _concat-if-arg2-not-empty,$(1),$(wordlist 3001,99999,$(2)))
1559endef
1560
1561# $(1): the full path of the source static library.
1562define _extract-and-include-single-target-whole-static-lib
1563$(hide) ldir=$(PRIVATE_INTERMEDIATES_DIR)/WHOLE/$(basename $(notdir $(1)))_objs;\
1564    rm -rf $$ldir; \
1565    mkdir -p $$ldir; \
1566    cp $(1) $$ldir; \
1567    lib_to_include=$$ldir/$(notdir $(1)); \
1568    filelist=; \
1569    subdir=0; \
1570    for f in `$($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_AR) t $(1)`; do \
1571        if [ -e $$ldir/$$f ]; then \
1572            mkdir $$ldir/$$subdir; \
1573            ext=$$subdir/; \
1574            subdir=$$((subdir+1)); \
1575            $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_AR) m $$lib_to_include $$f; \
1576        else \
1577            ext=; \
1578        fi; \
1579        $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_AR) p $$lib_to_include $$f > $$ldir/$$ext$$f; \
1580        filelist="$$filelist $$ldir/$$ext$$f"; \
1581    done ; \
1582    $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_AR) $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_GLOBAL_ARFLAGS) \
1583        $(PRIVATE_ARFLAGS) $@ $$filelist
1584
1585endef
1586
1587# $(1): the full path of the source static library.
1588define extract-and-include-whole-static-libs-first
1589$(if $(strip $(1)),
1590$(hide) cp $(1) $@)
1591endef
1592
1593define extract-and-include-target-whole-static-libs
1594$(call extract-and-include-whole-static-libs-first, $(firstword $(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)))
1595$(foreach lib,$(wordlist 2,999,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)), \
1596    $(call _extract-and-include-single-target-whole-static-lib, $(lib)))
1597endef
1598
1599# Explicitly delete the archive first so that ar doesn't
1600# try to add to an existing archive.
1601define transform-o-to-static-lib
1602@echo "target StaticLib: $(PRIVATE_MODULE) ($@)"
1603@mkdir -p $(dir $@)
1604@rm -f $@
1605$(extract-and-include-target-whole-static-libs)
1606$(call split-long-arguments,$($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_AR) \
1607    $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_GLOBAL_ARFLAGS) \
1608    $(PRIVATE_ARFLAGS) $@,$(PRIVATE_ALL_OBJECTS))
1609endef
1610
1611###########################################################
1612## Commands for running host ar
1613###########################################################
1614
1615# $(1): the full path of the source static library.
1616define _extract-and-include-single-host-whole-static-lib
1617$(hide) ldir=$(PRIVATE_INTERMEDIATES_DIR)/WHOLE/$(basename $(notdir $(1)))_objs;\
1618    rm -rf $$ldir; \
1619    mkdir -p $$ldir; \
1620    cp $(1) $$ldir; \
1621    lib_to_include=$$ldir/$(notdir $(1)); \
1622    filelist=; \
1623    subdir=0; \
1624    for f in `$($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)AR) t $(1) | \grep '\.o$$'`; do \
1625        if [ -e $$ldir/$$f ]; then \
1626           mkdir $$ldir/$$subdir; \
1627           ext=$$subdir/; \
1628           subdir=$$((subdir+1)); \
1629           $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)AR) m $$lib_to_include $$f; \
1630        else \
1631           ext=; \
1632        fi; \
1633        $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)AR) p $$lib_to_include $$f > $$ldir/$$ext$$f; \
1634        filelist="$$filelist $$ldir/$$ext$$f"; \
1635    done ; \
1636    $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)AR) $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)GLOBAL_ARFLAGS) \
1637        $(PRIVATE_ARFLAGS) $@ $$filelist
1638
1639endef
1640
1641define extract-and-include-host-whole-static-libs
1642$(call extract-and-include-whole-static-libs-first, $(firstword $(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)))
1643$(foreach lib,$(wordlist 2,999,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)), \
1644    $(call _extract-and-include-single-host-whole-static-lib, $(lib)))
1645endef
1646
1647ifeq ($(HOST_OS),darwin)
1648# On Darwin the host ar fails if there is nothing to add to .a at all.
1649# We work around by adding a dummy.o and then deleting it.
1650define create-dummy.o-if-no-objs
1651$(if $(PRIVATE_ALL_OBJECTS),,$(hide) touch $(dir $@)dummy.o)
1652endef
1653
1654define get-dummy.o-if-no-objs
1655$(if $(PRIVATE_ALL_OBJECTS),,$(dir $@)dummy.o)
1656endef
1657
1658define delete-dummy.o-if-no-objs
1659$(if $(PRIVATE_ALL_OBJECTS),,$(hide) $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)AR) d $@ $(dir $@)dummy.o \
1660  && rm -f $(dir $@)dummy.o)
1661endef
1662endif  # HOST_OS is darwin
1663
1664# Explicitly delete the archive first so that ar doesn't
1665# try to add to an existing archive.
1666define transform-host-o-to-static-lib
1667@echo "$($(PRIVATE_PREFIX)DISPLAY) StaticLib: $(PRIVATE_MODULE) ($@)"
1668@mkdir -p $(dir $@)
1669@rm -f $@
1670$(extract-and-include-host-whole-static-libs)
1671$(create-dummy.o-if-no-objs)
1672$(call split-long-arguments,$($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)AR) \
1673    $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)GLOBAL_ARFLAGS) \
1674    $(PRIVATE_ARFLAGS) $@,$(PRIVATE_ALL_OBJECTS) $(get-dummy.o-if-no-objs))
1675$(delete-dummy.o-if-no-objs)
1676endef
1677
1678
1679###########################################################
1680## Commands for running gcc to link a shared library or package
1681###########################################################
1682
1683# ld just seems to be so finicky with command order that we allow
1684# it to be overriden en-masse see combo/linux-arm.make for an example.
1685ifneq ($(HOST_CUSTOM_LD_COMMAND),true)
1686define transform-host-o-to-shared-lib-inner
1687$(hide) $(PRIVATE_CXX) \
1688	-Wl,-rpath-link=$($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)OUT_INTERMEDIATE_LIBRARIES) \
1689	-Wl,-rpath,\$$ORIGIN/../$(notdir $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)OUT_SHARED_LIBRARIES)) \
1690	-Wl,-rpath,\$$ORIGIN/$(notdir $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)OUT_SHARED_LIBRARIES)) \
1691	-shared -Wl,-soname,$(notdir $@) \
1692	$($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)GLOBAL_LD_DIRS) \
1693	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1694	   $(PRIVATE_HOST_GLOBAL_LDFLAGS) \
1695	) \
1696	$(PRIVATE_LDFLAGS) \
1697	$(PRIVATE_ALL_OBJECTS) \
1698	-Wl,--whole-archive \
1699	$(call normalize-host-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) \
1700	-Wl,--no-whole-archive \
1701	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--start-group) \
1702	$(call normalize-host-libraries,$(PRIVATE_ALL_STATIC_LIBRARIES)) \
1703	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--end-group) \
1704	$(if $(filter true,$(NATIVE_COVERAGE)),-lgcov) \
1705	$(if $(filter true,$(NATIVE_COVERAGE)),$(PRIVATE_HOST_LIBPROFILE_RT)) \
1706	$(call normalize-host-libraries,$(PRIVATE_ALL_SHARED_LIBRARIES)) \
1707	-o $@ \
1708	$(PRIVATE_LDLIBS)
1709endef
1710endif
1711
1712define transform-host-o-to-shared-lib
1713@echo "$($(PRIVATE_PREFIX)DISPLAY) SharedLib: $(PRIVATE_MODULE) ($@)"
1714@mkdir -p $(dir $@)
1715$(transform-host-o-to-shared-lib-inner)
1716endef
1717
1718define transform-host-o-to-package
1719@echo "$($(PRIVATE_PREFIX)DISPLAY) Package: $(PRIVATE_MODULE) ($@)"
1720@mkdir -p $(dir $@)
1721$(transform-host-o-to-shared-lib-inner)
1722endef
1723
1724
1725###########################################################
1726## Commands for running gcc to link a shared library or package
1727###########################################################
1728
1729define transform-o-to-shared-lib-inner
1730$(hide) $(PRIVATE_CXX) \
1731	-nostdlib -Wl,-soname,$(notdir $@) \
1732	-Wl,--gc-sections \
1733	$(if $(filter true,$(PRIVATE_CLANG)),-shared,-Wl$(comma)-shared) \
1734	$(PRIVATE_TARGET_GLOBAL_LD_DIRS) \
1735	$(PRIVATE_TARGET_CRTBEGIN_SO_O) \
1736	$(PRIVATE_ALL_OBJECTS) \
1737	-Wl,--whole-archive \
1738	$(call normalize-target-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) \
1739	-Wl,--no-whole-archive \
1740	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--start-group) \
1741	$(call normalize-target-libraries,$(PRIVATE_ALL_STATIC_LIBRARIES)) \
1742	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--end-group) \
1743	$(if $(filter true,$(NATIVE_COVERAGE)),$(PRIVATE_TARGET_COVERAGE_LIB)) \
1744	$(PRIVATE_TARGET_LIBATOMIC) \
1745	$(PRIVATE_TARGET_LIBGCC) \
1746	$(PRIVATE_TARGET_GLOBAL_LDFLAGS) \
1747	$(PRIVATE_LDFLAGS) \
1748	$(call normalize-target-libraries,$(PRIVATE_ALL_SHARED_LIBRARIES)) \
1749	-o $@ \
1750	$(PRIVATE_TARGET_CRTEND_SO_O) \
1751	$(PRIVATE_LDLIBS)
1752endef
1753
1754define transform-o-to-shared-lib
1755@echo "target SharedLib: $(PRIVATE_MODULE) ($@)"
1756@mkdir -p $(dir $@)
1757$(transform-o-to-shared-lib-inner)
1758endef
1759
1760###########################################################
1761## Commands for filtering a target executable or library
1762###########################################################
1763
1764ifneq ($(TARGET_BUILD_VARIANT),user)
1765  TARGET_STRIP_EXTRA = && $(PRIVATE_OBJCOPY) --add-gnu-debuglink=$< $@
1766  TARGET_STRIP_KEEP_SYMBOLS_EXTRA = --add-gnu-debuglink=$<
1767endif
1768
1769define transform-to-stripped
1770@echo "target Strip: $(PRIVATE_MODULE) ($@)"
1771@mkdir -p $(dir $@)
1772$(hide) $(PRIVATE_STRIP) --strip-all $< -o $@ \
1773  $(if $(PRIVATE_NO_DEBUGLINK),,$(TARGET_STRIP_EXTRA))
1774endef
1775
1776define transform-to-stripped-keep-mini-debug-info
1777@echo "target Strip (mini debug info): $(PRIVATE_MODULE) ($@)"
1778@mkdir -p $(dir $@)
1779$(hide) $(PRIVATE_NM) -D $< --format=posix --defined-only | awk '{ print $$1 }' | sort >$@.dynsyms
1780$(hide) $(PRIVATE_NM) $< --format=posix --defined-only | awk '{ if ($$2 == "T" || $$2 == "t" || $$2 == "D") print $$1 }' | sort >$@.funcsyms
1781$(hide) comm -13 $@.dynsyms $@.funcsyms >$@.keep_symbols
1782$(hide) $(PRIVATE_OBJCOPY) --only-keep-debug $< $@.debug
1783$(hide) $(PRIVATE_OBJCOPY) --rename-section .debug_frame=saved_debug_frame $@.debug $@.mini_debuginfo
1784$(hide) $(PRIVATE_OBJCOPY) -S --remove-section .gdb_index --remove-section .comment --keep-symbols=$@.keep_symbols $@.mini_debuginfo
1785$(hide) $(PRIVATE_OBJCOPY) --rename-section saved_debug_frame=.debug_frame $@.mini_debuginfo
1786$(hide) $(PRIVATE_STRIP) --strip-all -R .comment $< -o $@
1787$(hide) rm -f $@.mini_debuginfo.xz
1788$(hide) xz $@.mini_debuginfo
1789$(hide) $(PRIVATE_OBJCOPY) --add-section .gnu_debugdata=$@.mini_debuginfo.xz $@
1790$(hide) rm -f $@.dynsyms $@.funcsyms $@.keep_symbols $@.debug $@.mini_debuginfo.xz
1791endef
1792
1793define transform-to-stripped-keep-symbols
1794@echo "target Strip (keep symbols): $(PRIVATE_MODULE) ($@)"
1795@mkdir -p $(dir $@)
1796$(hide) $(PRIVATE_OBJCOPY) \
1797    `$(PRIVATE_READELF) -S $< | awk '/.debug_/ {print "-R " $$2}' | xargs` \
1798    $(TARGET_STRIP_KEEP_SYMBOLS_EXTRA) $< $@
1799endef
1800
1801###########################################################
1802## Commands for packing a target executable or library
1803###########################################################
1804
1805define pack-elf-relocations
1806@echo "target Pack Relocations: $(PRIVATE_MODULE) ($@)"
1807$(copy-file-to-target)
1808$(hide) $(RELOCATION_PACKER) $@
1809endef
1810
1811###########################################################
1812## Commands for running gcc to link an executable
1813###########################################################
1814
1815define transform-o-to-executable-inner
1816$(hide) $(PRIVATE_CXX) -pie \
1817	-nostdlib -Bdynamic \
1818	-Wl,-dynamic-linker,$(PRIVATE_LINKER) \
1819	-Wl,--gc-sections \
1820	-Wl,-z,nocopyreloc \
1821	$(PRIVATE_TARGET_GLOBAL_LD_DIRS) \
1822	-Wl,-rpath-link=$(PRIVATE_TARGET_OUT_INTERMEDIATE_LIBRARIES) \
1823	$(PRIVATE_TARGET_CRTBEGIN_DYNAMIC_O) \
1824	$(PRIVATE_ALL_OBJECTS) \
1825	-Wl,--whole-archive \
1826	$(call normalize-target-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) \
1827	-Wl,--no-whole-archive \
1828	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--start-group) \
1829	$(call normalize-target-libraries,$(PRIVATE_ALL_STATIC_LIBRARIES)) \
1830	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--end-group) \
1831	$(if $(filter true,$(NATIVE_COVERAGE)),$(PRIVATE_TARGET_COVERAGE_LIB)) \
1832	$(PRIVATE_TARGET_LIBATOMIC) \
1833	$(PRIVATE_TARGET_LIBGCC) \
1834	$(PRIVATE_TARGET_GLOBAL_LDFLAGS) \
1835	$(PRIVATE_LDFLAGS) \
1836	$(call normalize-target-libraries,$(PRIVATE_ALL_SHARED_LIBRARIES)) \
1837	-o $@ \
1838	$(PRIVATE_TARGET_CRTEND_O) \
1839	$(PRIVATE_LDLIBS)
1840endef
1841
1842define transform-o-to-executable
1843@echo "target Executable: $(PRIVATE_MODULE) ($@)"
1844@mkdir -p $(dir $@)
1845$(transform-o-to-executable-inner)
1846endef
1847
1848
1849###########################################################
1850## Commands for linking a static executable. In practice,
1851## we only use this on arm, so the other platforms don't
1852## have transform-o-to-static-executable defined.
1853## Clang driver needs -static to create static executable.
1854## However, bionic/linker uses -shared to overwrite.
1855## Linker for x86 targets does not allow coexistance of -static and -shared,
1856## so we add -static only if -shared is not used.
1857###########################################################
1858
1859define transform-o-to-static-executable-inner
1860$(hide) $(PRIVATE_CXX) \
1861	-nostdlib -Bstatic \
1862	$(if $(filter $(PRIVATE_LDFLAGS),-shared),,-static) \
1863	-Wl,--gc-sections \
1864	-o $@ \
1865	$(PRIVATE_TARGET_GLOBAL_LD_DIRS) \
1866	$(PRIVATE_TARGET_CRTBEGIN_STATIC_O) \
1867	$(PRIVATE_TARGET_GLOBAL_LDFLAGS) \
1868	$(PRIVATE_LDFLAGS) \
1869	$(PRIVATE_ALL_OBJECTS) \
1870	-Wl,--whole-archive \
1871	$(call normalize-target-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) \
1872	-Wl,--no-whole-archive \
1873	$(call normalize-target-libraries,$(filter-out %libcompiler_rt.a,$(filter-out %libc_nomalloc.a,$(filter-out %libc.a,$(PRIVATE_ALL_STATIC_LIBRARIES))))) \
1874	-Wl,--start-group \
1875	$(call normalize-target-libraries,$(filter %libc.a,$(PRIVATE_ALL_STATIC_LIBRARIES))) \
1876	$(call normalize-target-libraries,$(filter %libc_nomalloc.a,$(PRIVATE_ALL_STATIC_LIBRARIES))) \
1877	$(if $(filter true,$(NATIVE_COVERAGE)),$(PRIVATE_TARGET_COVERAGE_LIB)) \
1878	$(PRIVATE_TARGET_LIBATOMIC) \
1879	$(call normalize-target-libraries,$(filter %libcompiler_rt.a,$(PRIVATE_ALL_STATIC_LIBRARIES))) \
1880	$(PRIVATE_TARGET_LIBGCC) \
1881	-Wl,--end-group \
1882	$(PRIVATE_TARGET_CRTEND_O)
1883endef
1884
1885define transform-o-to-static-executable
1886@echo "target StaticExecutable: $(PRIVATE_MODULE) ($@)"
1887@mkdir -p $(dir $@)
1888$(transform-o-to-static-executable-inner)
1889endef
1890
1891
1892###########################################################
1893## Commands for running gcc to link a host executable
1894###########################################################
1895ifdef BUILD_HOST_static
1896HOST_FPIE_FLAGS :=
1897else
1898HOST_FPIE_FLAGS := -pie
1899# Force the correct entry point to workaround a bug in binutils that manifests with -pie
1900ifeq ($(HOST_CROSS_OS),windows)
1901HOST_CROSS_FPIE_FLAGS += -Wl,-e_mainCRTStartup
1902endif
1903endif
1904
1905ifneq ($(HOST_CUSTOM_LD_COMMAND),true)
1906define transform-host-o-to-executable-inner
1907$(hide) $(PRIVATE_CXX) \
1908	$(PRIVATE_ALL_OBJECTS) \
1909	-Wl,--whole-archive \
1910	$(call normalize-host-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) \
1911	-Wl,--no-whole-archive \
1912	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--start-group) \
1913	$(call normalize-host-libraries,$(PRIVATE_ALL_STATIC_LIBRARIES)) \
1914	$(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--end-group) \
1915	$(if $(filter true,$(NATIVE_COVERAGE)),-lgcov) \
1916	$(if $(filter true,$(NATIVE_COVERAGE)),$(PRIVATE_HOST_LIBPROFILE_RT)) \
1917	$(call normalize-host-libraries,$(PRIVATE_ALL_SHARED_LIBRARIES)) \
1918	-Wl,-rpath-link=$($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)OUT_INTERMEDIATE_LIBRARIES) \
1919	$(foreach path,$(PRIVATE_RPATHS), \
1920	  -Wl,-rpath,\$$ORIGIN/$(path)) \
1921	$($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)GLOBAL_LD_DIRS) \
1922	$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1923		$(PRIVATE_HOST_GLOBAL_LDFLAGS) \
1924	) \
1925	$(PRIVATE_LDFLAGS) \
1926	-o $@ \
1927	$(PRIVATE_LDLIBS)
1928endef
1929endif
1930
1931define transform-host-o-to-executable
1932@echo "$($(PRIVATE_PREFIX)DISPLAY) Executable: $(PRIVATE_MODULE) ($@)"
1933@mkdir -p $(dir $@)
1934$(transform-host-o-to-executable-inner)
1935endef
1936
1937
1938###########################################################
1939## Commands for running javac to make .class files
1940###########################################################
1941
1942# Add BUILD_NUMBER to apps default version name if it's unbundled build.
1943ifdef TARGET_BUILD_APPS
1944TARGET_BUILD_WITH_APPS_VERSION_NAME := true
1945endif
1946
1947ifdef TARGET_BUILD_WITH_APPS_VERSION_NAME
1948APPS_DEFAULT_VERSION_NAME := $(PLATFORM_VERSION)-$(BUILD_NUMBER_FROM_FILE)
1949else
1950APPS_DEFAULT_VERSION_NAME := $(PLATFORM_VERSION)
1951endif
1952
1953# TODO: Right now we generate the asset resources twice, first as part
1954# of generating the Java classes, then at the end when packaging the final
1955# assets.  This should be changed to do one of two things: (1) Don't generate
1956# any resource files the first time, only create classes during that stage;
1957# or (2) Don't use the -c flag with the second stage, instead taking the
1958# resource files from the first stage as additional input.  My original intent
1959# was to use approach (2), but this requires a little more work in the tool.
1960# Maybe we should just use approach (1).
1961
1962# This rule creates the R.java and Manifest.java files, both of which
1963# are PRODUCT-neutral.  Don't pass PRIVATE_PRODUCT_AAPT_CONFIG to this invocation.
1964define create-resource-java-files
1965@mkdir -p $(PRIVATE_SOURCE_INTERMEDIATES_DIR)
1966@mkdir -p $(dir $(PRIVATE_RESOURCE_PUBLICS_OUTPUT))
1967$(hide) $(AAPT) package $(PRIVATE_AAPT_FLAGS) -m \
1968    $(eval # PRIVATE_PRODUCT_AAPT_CONFIG is intentionally missing-- see comment.) \
1969    $(addprefix -J , $(PRIVATE_SOURCE_INTERMEDIATES_DIR)) \
1970    $(addprefix -M , $(PRIVATE_ANDROID_MANIFEST)) \
1971    $(addprefix -P , $(PRIVATE_RESOURCE_PUBLICS_OUTPUT)) \
1972    $(addprefix -S , $(PRIVATE_RESOURCE_DIR)) \
1973    $(addprefix -A , $(PRIVATE_ASSET_DIR)) \
1974    $(addprefix -I , $(PRIVATE_AAPT_INCLUDES)) \
1975    $(addprefix -G , $(PRIVATE_PROGUARD_OPTIONS_FILE)) \
1976    $(addprefix --min-sdk-version , $(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
1977    $(addprefix --target-sdk-version , $(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
1978    $(if $(filter --version-code,$(PRIVATE_AAPT_FLAGS)),,--version-code $(PLATFORM_SDK_VERSION)) \
1979    $(if $(filter --version-name,$(PRIVATE_AAPT_FLAGS)),,--version-name $(APPS_DEFAULT_VERSION_NAME)) \
1980    $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \
1981    $(addprefix --rename-instrumentation-target-package , $(PRIVATE_MANIFEST_INSTRUMENTATION_FOR)) \
1982    --skip-symbols-without-default-localization
1983endef
1984
1985# Search for generated R.java/Manifest.java, copy the found R.java as $@.
1986# Also copy them to a central 'R' directory to make it easier to add the files to an IDE.
1987define find-generated-R.java
1988$(hide) for GENERATED_MANIFEST_FILE in `find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) \
1989  -name Manifest.java 2> /dev/null`; do \
1990    dir=`awk '/package/{gsub(/\./,"/",$$2);gsub(/;/,"",$$2);print $$2;exit}' $$GENERATED_MANIFEST_FILE`; \
1991    mkdir -p $(TARGET_COMMON_OUT_ROOT)/R/$$dir; \
1992    $(ACP) -fp $$GENERATED_MANIFEST_FILE $(TARGET_COMMON_OUT_ROOT)/R/$$dir; \
1993  done;
1994$(hide) for GENERATED_R_FILE in `find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) \
1995  -name R.java 2> /dev/null`; do \
1996    dir=`awk '/package/{gsub(/\./,"/",$$2);gsub(/;/,"",$$2);print $$2;exit}' $$GENERATED_R_FILE`; \
1997    mkdir -p $(TARGET_COMMON_OUT_ROOT)/R/$$dir; \
1998    $(ACP) -fp $$GENERATED_R_FILE $(TARGET_COMMON_OUT_ROOT)/R/$$dir \
1999      || exit 31; \
2000    $(ACP) -fp $$GENERATED_R_FILE $@ || exit 32; \
2001  done;
2002@# Ensure that the target file is always created, i.e. also in case we did not
2003@# enter the GENERATED_R_FILE-loop above. This avoids unnecessary rebuilding.
2004$(hide) touch $@
2005endef
2006
2007###########################################################
2008# AAPT2 compilation and link
2009###########################################################
2010define aapt2-compile-one-resource-file
2011@mkdir -p $(dir $@)
2012$(hide) $(AAPT2) compile -o $(dir $@) $(PRIVATE_AAPT2_CFLAGS) --legacy $<
2013endef
2014
2015define aapt2-compile-resource-dirs
2016@mkdir -p $(dir $@)
2017$(hide) $(AAPT2) compile -o $@ $(addprefix --dir ,$(PRIVATE_SOURCE_RES_DIRS)) \
2018  $(PRIVATE_AAPT2_CFLAGS) --legacy
2019endef
2020
2021# Set up rule to compile one resource file with aapt2.
2022# Must be called with $(eval).
2023# $(1): the source file
2024# $(2): the output file
2025define aapt2-compile-one-resource-file-rule
2026$(2) : $(1) $(AAPT2)
2027	@echo "AAPT2 compile $$@ <- $$<"
2028	$$(call aapt2-compile-one-resource-file)
2029endef
2030
2031# Convert input resource file path to output file path.
2032# values-[config]/<file>.xml -> values-[config]_<file>.arsc.flat;
2033# For other resource file, just replace the last "/" with "_" and
2034# add .flat extension.
2035#
2036# $(1): the input resource file path
2037# $(2): the base dir of the output file path
2038# Returns: the compiled output file path
2039define aapt2-compiled-resource-out-file
2040$(eval _p_w := $(strip $(subst /,$(space),$(dir $(1)))))$(2)/$(subst $(space),/,$(_p_w))_$(if $(filter values%,$(lastword $(_p_w))),$(patsubst %.xml,%.arsc,$(notdir $(1))),$(notdir $(1))).flat
2041endef
2042
2043define aapt2-link
2044@mkdir -p $(dir $@)
2045$(call dump-words-to-file,$(PRIVATE_RES_FLAT),$(dir $@)aapt2-flat-list)
2046$(call dump-words-to-file,$(PRIVATE_OVERLAY_FLAT),$(dir $@)aapt2-flat-overlay-list)
2047$(hide) $(AAPT2) link -o $@ \
2048  $(PRIVATE_AAPT_FLAGS) \
2049  $(addprefix --manifest ,$(PRIVATE_ANDROID_MANIFEST)) \
2050  $(addprefix -I ,$(PRIVATE_AAPT_INCLUDES)) \
2051  $(addprefix -I ,$(PRIVATE_SHARED_ANDROID_LIBRARIES)) \
2052  $(addprefix --java ,$(PRIVATE_SOURCE_INTERMEDIATES_DIR)) \
2053  $(addprefix --proguard ,$(PRIVATE_PROGUARD_OPTIONS_FILE)) \
2054  $(addprefix --min-sdk-version ,$(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
2055  $(addprefix --target-sdk-version ,$(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
2056  $(if $(filter --product,$(PRIVATE_AAPT_FLAGS)),,$(addprefix --product ,$(PRIVATE_TARGET_AAPT_CHARACTERISTICS))) \
2057  $(addprefix -c ,$(PRIVATE_PRODUCT_AAPT_CONFIG)) \
2058  $(addprefix --preferred-density ,$(PRIVATE_PRODUCT_AAPT_PREF_CONFIG)) \
2059  $(if $(filter --version-code,$(PRIVATE_AAPT_FLAGS)),,--version-code $(PLATFORM_SDK_VERSION)) \
2060  $(if $(filter --version-name,$(PRIVATE_AAPT_FLAGS)),,--version-name $(APPS_DEFAULT_VERSION_NAME)) \
2061  $(addprefix --rename-manifest-package ,$(PRIVATE_MANIFEST_PACKAGE_NAME)) \
2062  $(addprefix --rename-instrumentation-target-package ,$(PRIVATE_MANIFEST_INSTRUMENTATION_FOR)) \
2063  -R \@$(dir $@)aapt2-flat-overlay-list \
2064  \@$(dir $@)aapt2-flat-list
2065endef
2066
2067###########################################################
2068xlint_unchecked := -Xlint:unchecked
2069
2070# emit-line, <word list>, <output file>
2071define emit-line
2072   $(if $(1),echo -n '$(strip $(1)) ' >> $(2))
2073endef
2074
2075# dump-words-to-file, <word list>, <output file>
2076define dump-words-to-file
2077        @rm -f $(2)
2078        @touch $(2)
2079        @$(call emit-line,$(wordlist 1,500,$(1)),$(2))
2080        @$(call emit-line,$(wordlist 501,1000,$(1)),$(2))
2081        @$(call emit-line,$(wordlist 1001,1500,$(1)),$(2))
2082        @$(call emit-line,$(wordlist 1501,2000,$(1)),$(2))
2083        @$(call emit-line,$(wordlist 2001,2500,$(1)),$(2))
2084        @$(call emit-line,$(wordlist 2501,3000,$(1)),$(2))
2085        @$(call emit-line,$(wordlist 3001,3500,$(1)),$(2))
2086        @$(call emit-line,$(wordlist 3501,4000,$(1)),$(2))
2087        @$(call emit-line,$(wordlist 4001,4500,$(1)),$(2))
2088        @$(call emit-line,$(wordlist 4501,5000,$(1)),$(2))
2089        @$(call emit-line,$(wordlist 5001,5500,$(1)),$(2))
2090        @$(call emit-line,$(wordlist 5501,6000,$(1)),$(2))
2091        @$(call emit-line,$(wordlist 6001,6500,$(1)),$(2))
2092        @$(call emit-line,$(wordlist 6501,7000,$(1)),$(2))
2093        @$(call emit-line,$(wordlist 7001,7500,$(1)),$(2))
2094        @$(call emit-line,$(wordlist 7501,8000,$(1)),$(2))
2095        @$(call emit-line,$(wordlist 8001,8500,$(1)),$(2))
2096        @$(call emit-line,$(wordlist 8501,9000,$(1)),$(2))
2097        @$(call emit-line,$(wordlist 9001,9500,$(1)),$(2))
2098        @$(call emit-line,$(wordlist 9501,10000,$(1)),$(2))
2099        @$(call emit-line,$(wordlist 10001,10500,$(1)),$(2))
2100        @$(call emit-line,$(wordlist 10501,11000,$(1)),$(2))
2101        @$(call emit-line,$(wordlist 11001,11500,$(1)),$(2))
2102        @$(call emit-line,$(wordlist 11501,12000,$(1)),$(2))
2103        @$(call emit-line,$(wordlist 12001,12500,$(1)),$(2))
2104        @$(call emit-line,$(wordlist 12501,13000,$(1)),$(2))
2105        @$(call emit-line,$(wordlist 13001,13500,$(1)),$(2))
2106        @$(if $(wordlist 13501,13502,$(1)),$(error Too many words ($(words $(1)))))
2107endef
2108
2109# For a list of jar files, unzip them to a specified directory,
2110# but make sure that no META-INF files come along for the ride,
2111# unless PRIVATE_DONT_DELETE_JAR_META_INF is set.
2112#
2113# $(1): files to unzip
2114# $(2): destination directory
2115define unzip-jar-files
2116  $(hide) for f in $(1); \
2117  do \
2118    if [ ! -f $$f ]; then \
2119      echo Missing file $$f; \
2120      exit 1; \
2121    fi; \
2122    unzip -qo $$f -d $(2); \
2123  done
2124  $(if $(PRIVATE_DONT_DELETE_JAR_META_INF),,$(hide) rm -rf $(2)/META-INF)
2125endef
2126
2127# Call jack
2128#
2129define call-jack
2130 JACK_VERSION=$(PRIVATE_JACK_VERSION) $(JACK) $(DEFAULT_JACK_EXTRA_ARGS)
2131endef
2132
2133# Common definition to invoke javac on the host and target.
2134#
2135# Some historical notes:
2136# - below we write the list of java files to java-source-list to avoid argument
2137#   list length problems with Cygwin
2138# - we filter out duplicate java file names because eclipse's compiler
2139#   doesn't like them.
2140#
2141# $(1): javac
2142# $(2): bootclasspath
2143define compile-java
2144$(hide) rm -f $@
2145$(hide) rm -rf $(PRIVATE_CLASS_INTERMEDIATES_DIR)
2146$(hide) mkdir -p $(dir $@)
2147$(hide) mkdir -p $(PRIVATE_CLASS_INTERMEDIATES_DIR)
2148$(call unzip-jar-files,$(PRIVATE_STATIC_JAVA_LIBRARIES),$(PRIVATE_CLASS_INTERMEDIATES_DIR))
2149$(call dump-words-to-file,$(PRIVATE_JAVA_SOURCES),$(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list)
2150$(hide) if [ -d "$(PRIVATE_SOURCE_INTERMEDIATES_DIR)" ]; then \
2151          find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name '*.java' -and -not -name '.*' >> $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list; \
2152fi
2153$(hide) tr ' ' '\n' < $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list \
2154    | $(NORMALIZE_PATH) | sort -u > $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq
2155$(hide) if [ -s $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq ] ; then \
2156    $(1) -encoding UTF-8 \
2157    $(if $(findstring true,$(PRIVATE_WARNINGS_ENABLE)),$(xlint_unchecked),) \
2158    $(2) \
2159    $(addprefix -classpath ,$(strip \
2160        $(call normalize-path-list,$(PRIVATE_ALL_JAVA_LIBRARIES)))) \
2161    $(if $(findstring true,$(PRIVATE_WARNINGS_ENABLE)),$(xlint_unchecked),) \
2162    -extdirs "" -d $(PRIVATE_CLASS_INTERMEDIATES_DIR) \
2163    $(PRIVATE_JAVACFLAGS) \
2164    \@$(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq \
2165    || ( rm -rf $(PRIVATE_CLASS_INTERMEDIATES_DIR) ; exit 41 ) \
2166fi
2167$(if $(PRIVATE_JAVA_LAYERS_FILE), $(hide) build/tools/java-layers.py \
2168    $(PRIVATE_JAVA_LAYERS_FILE) \@$(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq,)
2169$(hide) rm -f $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list
2170$(hide) rm -f $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq
2171$(if $(PRIVATE_JAR_EXCLUDE_FILES), $(hide) find $(PRIVATE_CLASS_INTERMEDIATES_DIR) \
2172    -name $(word 1, $(PRIVATE_JAR_EXCLUDE_FILES)) \
2173    $(addprefix -o -name , $(wordlist 2, 999, $(PRIVATE_JAR_EXCLUDE_FILES))) \
2174    | xargs rm -rf)
2175$(if $(PRIVATE_JAR_PACKAGES), \
2176    $(hide) find $(PRIVATE_CLASS_INTERMEDIATES_DIR) -mindepth 1 -type f \
2177        $(foreach pkg, $(PRIVATE_JAR_PACKAGES), \
2178            -not -path $(PRIVATE_CLASS_INTERMEDIATES_DIR)/$(subst .,/,$(pkg))/\*) -delete ; \
2179        find $(PRIVATE_CLASS_INTERMEDIATES_DIR) -empty -delete)
2180$(if $(PRIVATE_JAR_EXCLUDE_PACKAGES), $(hide) rm -rf \
2181    $(foreach pkg, $(PRIVATE_JAR_EXCLUDE_PACKAGES), \
2182        $(PRIVATE_CLASS_INTERMEDIATES_DIR)/$(subst .,/,$(pkg))))
2183$(if $(PRIVATE_JAR_MANIFEST), \
2184    $(hide) sed -e "s/%BUILD_NUMBER%/$(BUILD_NUMBER_FROM_FILE)/" \
2185            $(PRIVATE_JAR_MANIFEST) > $(dir $@)/manifest.mf && \
2186        jar -cfm $@ $(dir $@)/manifest.mf \
2187            -C $(PRIVATE_CLASS_INTERMEDIATES_DIR) ., \
2188    $(hide) jar -cf $@ -C $(PRIVATE_CLASS_INTERMEDIATES_DIR) .)
2189$(if $(PRIVATE_EXTRA_JAR_ARGS),$(call add-java-resources-to,$@))
2190endef
2191
2192define transform-java-to-classes.jar
2193@echo "target Java: $(PRIVATE_MODULE) ($(PRIVATE_CLASS_INTERMEDIATES_DIR))"
2194$(call compile-java,$(TARGET_JAVAC),$(PRIVATE_BOOTCLASSPATH))
2195endef
2196
2197# Invoke Jack to compile java from source to dex and jack files.
2198#
2199# Some historical notes:
2200# - below we write the list of java files to java-source-list to avoid argument
2201#   list length problems with Cygwin
2202# - we filter out duplicate java file names because Jack doesn't like them.
2203define jack-java-to-dex
2204$(hide) rm -f $@
2205$(hide) rm -f $(PRIVATE_CLASSES_JACK)
2206$(hide) rm -rf $(PRIVATE_JACK_INTERMEDIATES_DIR)
2207$(hide) mkdir -p $(dir $@)
2208$(hide) mkdir -p $(dir $(PRIVATE_CLASSES_JACK))
2209$(hide) mkdir -p $(PRIVATE_JACK_INTERMEDIATES_DIR)
2210$(if $(PRIVATE_JACK_INCREMENTAL_DIR),$(hide) mkdir -p $(PRIVATE_JACK_INCREMENTAL_DIR))
2211$(call dump-words-to-file,$(PRIVATE_JAVA_SOURCES),$(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list)
2212$(hide) if [ -d "$(PRIVATE_SOURCE_INTERMEDIATES_DIR)" ]; then \
2213          find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name '*.java' >> $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list; \
2214fi
2215$(hide) tr ' ' '\n' < $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list \
2216    | $(NORMALIZE_PATH) | sort -u > $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq
2217$(if $(PRIVATE_JACK_PROGUARD_FLAGS), \
2218    $(hide) echo -basedirectory $(CURDIR) > $@.flags; \
2219    echo $(PRIVATE_JACK_PROGUARD_FLAGS) >> $@.flags; \
2220)
2221$(if $(PRIVATE_EXTRA_JAR_ARGS),
2222    $(hide) mkdir -p $@.res.tmp
2223    $(hide) $(call create-empty-package-at,$@.res.tmp.zip)
2224    $(hide) $(call add-java-resources-to,$@.res.tmp.zip)
2225    $(hide) unzip -qo $@.res.tmp.zip -d $@.res.tmp
2226    $(hide) rm $@.res.tmp.zip)
2227$(hide) if [ -s $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq ] ; then \
2228    export tmpEcjArg="@$(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq"; \
2229else \
2230    export tmpEcjArg=""; \
2231fi; \
2232$(call call-jack) \
2233    $(strip $(PRIVATE_JACK_FLAGS)) \
2234    $(strip $(PRIVATE_JACK_COVERAGE_OPTIONS)) \
2235    $(if $(NO_OPTIMIZE_DX), \
2236        -D jack.dex.optimize="false") \
2237    $(if $(PRIVATE_RMTYPEDEFS), \
2238        -D jack.android.remove-typedef="true") \
2239    $(addprefix --classpath ,$(strip \
2240        $(call normalize-path-list,$(PRIVATE_JACK_SHARED_LIBRARIES)))) \
2241    $(addprefix --import ,$(call reverse-list,$(PRIVATE_STATIC_JACK_LIBRARIES))) \
2242    $(if $(PRIVATE_EXTRA_JAR_ARGS),--import-resource $@.res.tmp) \
2243    -D jack.android.min-api-level=$(PRIVATE_JACK_MIN_SDK_VERSION) \
2244    -D jack.import.resource.policy=keep-first \
2245    -D jack.import.type.policy=keep-first \
2246    --output-jack $(PRIVATE_CLASSES_JACK) \
2247    $(if $(PRIVATE_JACK_INCREMENTAL_DIR),--incremental-folder $(PRIVATE_JACK_INCREMENTAL_DIR)) \
2248    --output-dex $(PRIVATE_JACK_INTERMEDIATES_DIR) \
2249    $(addprefix --config-jarjar ,$(strip $(PRIVATE_JARJAR_RULES))) \
2250    $(if $(PRIVATE_JACK_PROGUARD_FLAGS),--config-proguard $@.flags) \
2251    $$tmpEcjArg \
2252    || ( rm -rf $(PRIVATE_CLASSES_JACK); exit 41 )
2253$(hide) mv $(PRIVATE_JACK_INTERMEDIATES_DIR)/classes*.dex $(dir $@)
2254$(hide) rm -f $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list
2255$(if $(PRIVATE_EXTRA_JAR_ARGS),$(hide) rm -rf $@.res.tmp)
2256$(hide) mv $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq $(PRIVATE_JACK_INTERMEDIATES_DIR).java-source-list
2257$(if $(PRIVATE_JAR_PACKAGES), $(hide) echo unsupported options PRIVATE_JAR_PACKAGES in $@; exit 53)
2258$(if $(PRIVATE_JAR_EXCLUDE_PACKAGES), $(hide) echo unsupported options JAR_EXCLUDE_PACKAGES in $@; exit 53)
2259$(if $(PRIVATE_JAR_MANIFEST), $(hide) echo unsupported options JAR_MANIFEST in $@; exit 53)
2260endef
2261
2262# Invoke Jack to compile java source just to check it compiles correctly.
2263#
2264# Some historical notes:
2265# - below we write the list of java files to java-source-list to avoid argument
2266#   list length problems with Cygwin
2267# - we filter out duplicate java file names because Jack doesn't like them.
2268define jack-check-java
2269$(hide) rm -f $@
2270$(hide) rm -f $@.java-source-list
2271$(hide) rm -f $@.java-source-list-uniq
2272$(hide) mkdir -p $(dir $@)
2273$(if $(PRIVATE_JACK_INCREMENTAL_DIR),$(hide) mkdir -p $(PRIVATE_JACK_INCREMENTAL_DIR))
2274$(call dump-words-to-file,$(PRIVATE_JAVA_SOURCES),$@.java-source-list)
2275$(hide) if [ -d "$(PRIVATE_SOURCE_INTERMEDIATES_DIR)" ]; then \
2276          find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name '*.java' >> $@.java-source-list; \
2277fi
2278$(hide) tr ' ' '\n' < $@.java-source-list \
2279    | sort -u > $@.java-source-list-uniq
2280$(hide) if [ -s $@.java-source-list-uniq ] ; then \
2281	$(call call-jack) \
2282	    $(strip $(PRIVATE_JACK_FLAGS)) \
2283	    $(addprefix --classpath ,$(strip \
2284	        $(call normalize-path-list,$(call reverse-list,$(PRIVATE_STATIC_JACK_LIBRARIES)) $(PRIVATE_JACK_SHARED_LIBRARIES)))) \
2285	    -D jack.import.resource.policy=keep-first \
2286	    -D jack.android.min-api-level=$(PRIVATE_JACK_MIN_SDK_VERSION) \
2287	    -D jack.import.type.policy=keep-first \
2288	    $(if $(PRIVATE_JACK_INCREMENTAL_DIR),--incremental-folder $(PRIVATE_JACK_INCREMENTAL_DIR)) \
2289	    @$@.java-source-list-uniq; \
2290fi
2291touch $@
2292endef
2293
2294define transform-jar-to-jack
2295	$(hide) mkdir -p $(dir $@)
2296	$(hide) mkdir -p $@.tmpjill.res
2297	$(hide) unzip -qo $< -d $@.tmpjill.res
2298	$(hide) find $@.tmpjill.res -iname "*.class" -delete
2299	$(hide) $(call call-jack) \
2300	    $(PRIVATE_JACK_FLAGS) \
2301        -D jack.import.resource.policy=keep-first \
2302        -D jack.import.type.policy=keep-first \
2303        -D jack.android.min-api-level=$(PRIVATE_JACK_MIN_SDK_VERSION) \
2304	    --import $< \
2305	    --import-resource $@.tmpjill.res \
2306	    --output-jack $@
2307	$(hide) rm -rf $@.tmpjill.res
2308endef
2309
2310# Moves $1.tmp to $1 if necessary. This is designed to be used with
2311# .KATI_RESTAT. For kati, this function doesn't update the timestamp
2312# of $1 when $1.tmp is identical to $1 so that ninja won't rebuild
2313# targets which depend on $1.
2314define commit-change-for-toc
2315$(hide) if cmp -s $1.tmp $1 ; then \
2316 rm $1.tmp ; \
2317else \
2318 mv $1.tmp $1 ; \
2319fi
2320endef
2321
2322## Rule to create a table of contents from a .jar file.
2323## Must be called with $(eval).
2324# $(1): A .jar file
2325define _transform-jar-to-toc
2326$1.toc: $1 | $(IJAR)
2327	@echo Generating TOC: $$@
2328	$(hide) $(IJAR) $$< $$@.tmp
2329	$$(call commit-change-for-toc,$$@)
2330endef
2331
2332## Define a rule which generates .jar.toc and mark it as .KATI_RESTAT.
2333# $(1): A .jar file
2334define define-jar-to-toc-rule
2335$(eval $(call _transform-jar-to-toc,$1))\
2336$(eval .KATI_RESTAT: $1.toc)
2337endef
2338
2339ifeq (,$(TARGET_BUILD_APPS))
2340
2341## Rule to create a table of contents from a .dex file.
2342## Must be called with $(eval).
2343# $(1): The directory which contains classes*.dex files
2344define _transform-dex-to-toc
2345$1/classes.dex.toc: PRIVATE_INPUT_DEX_FILES := $1/classes*.dex
2346$1/classes.dex.toc: $1/classes.dex $(DEXDUMP)
2347	@echo Generating TOC: $$@
2348	$(hide) ANDROID_LOG_TAGS="*:e" $(DEXDUMP) -l xml $$(PRIVATE_INPUT_DEX_FILES) > $$@.tmp
2349	$$(call commit-change-for-toc,$$@)
2350endef
2351
2352## Define a rule which generates .dex.toc and mark it as .KATI_RESTAT.
2353# $(1): The directory which contains classes*.dex files
2354define define-dex-to-toc-rule
2355$(eval $(call _transform-dex-to-toc,$1))\
2356$(eval .KATI_RESTAT: $1/classes.dex.toc)
2357endef
2358
2359else
2360
2361# Turn off .toc optimization for apps build as we cannot build dexdump.
2362define define-dex-to-toc-rule
2363endef
2364
2365endif  # TARGET_BUILD_APPS
2366
2367
2368# Invoke Jack to compile java from source to jack files without shrink or obfuscation.
2369#
2370# Some historical notes:
2371# - below we write the list of java files to java-source-list to avoid argument
2372#   list length problems with Cygwin
2373# - we filter out duplicate java file names because Jack doesn't like them.
2374define java-to-jack
2375$(hide) rm -f $@
2376$(hide) rm -rf $(PRIVATE_JACK_INTERMEDIATES_DIR)
2377$(hide) mkdir -p $(dir $@)
2378$(hide) mkdir -p $(PRIVATE_JACK_INTERMEDIATES_DIR)
2379$(if $(PRIVATE_JACK_INCREMENTAL_DIR),$(hide) mkdir -p $(PRIVATE_JACK_INCREMENTAL_DIR))
2380$(call dump-words-to-file,$(PRIVATE_JAVA_SOURCES),$(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list)
2381$(hide) if [ -d "$(PRIVATE_SOURCE_INTERMEDIATES_DIR)" ]; then \
2382          find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name '*.java' >> $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list; \
2383fi
2384$(hide) tr ' ' '\n' < $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list \
2385    | $(NORMALIZE_PATH) | sort -u > $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq
2386$(if $(PRIVATE_JACK_PROGUARD_FLAGS), \
2387    $(hide) echo -basedirectory $(CURDIR) > $@.flags; \
2388    echo $(PRIVATE_JACK_PROGUARD_FLAGS) >> $@.flags; \
2389)
2390$(if $(PRIVATE_EXTRA_JAR_ARGS),
2391	$(hide) mkdir -p $@.res.tmp
2392	$(hide) $(call create-empty-package-at,$@.res.tmp.zip)
2393	$(hide) $(call add-java-resources-to,$@.res.tmp.zip)
2394	$(hide) unzip -qo $@.res.tmp.zip -d $@.res.tmp
2395	$(hide) rm $@.res.tmp.zip)
2396$(hide) if [ -s $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq ] ; then \
2397    export tmpEcjArg="@$(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq"; \
2398else \
2399    export tmpEcjArg=""; \
2400fi; \
2401$(call call-jack) \
2402    $(strip $(PRIVATE_JACK_FLAGS)) \
2403    $(if $(NO_OPTIMIZE_DX), \
2404        -D jack.dex.optimize="false") \
2405    $(addprefix --classpath ,$(strip \
2406        $(call normalize-path-list,$(PRIVATE_JACK_SHARED_LIBRARIES)))) \
2407    $(addprefix --import ,$(call reverse-list,$(PRIVATE_STATIC_JACK_LIBRARIES))) \
2408    $(if $(PRIVATE_EXTRA_JAR_ARGS),--import-resource $@.res.tmp) \
2409    -D jack.import.resource.policy=keep-first \
2410    -D jack.import.type.policy=keep-first \
2411    -D jack.android.min-api-level=$(PRIVATE_JACK_MIN_SDK_VERSION) \
2412    $(if $(PRIVATE_JACK_INCREMENTAL_DIR),--incremental-folder $(PRIVATE_JACK_INCREMENTAL_DIR)) \
2413    --output-jack $@ \
2414    $(addprefix --config-jarjar ,$(strip $(PRIVATE_JARJAR_RULES))) \
2415    $(if $(PRIVATE_JACK_PROGUARD_FLAGS),--config-proguard $@.flags) \
2416    $$tmpEcjArg \
2417    || ( rm -f $@ ; exit 41 )
2418$(hide) rm -f $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list
2419$(if $(PRIVATE_EXTRA_JAR_ARGS),$(hide) rm -rf $@.res.tmp)
2420$(hide) mv $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq $(PRIVATE_JACK_INTERMEDIATES_DIR).java-source-list
2421$(if $(PRIVATE_JAR_PACKAGES), $(hide) echo unsupported options PRIVATE_JAR_PACKAGES in $@; exit 53)
2422$(if $(PRIVATE_JAR_EXCLUDE_PACKAGES), $(hide) echo unsupported options JAR_EXCLUDE_PACKAGES in $@; exit 53)
2423$(if $(PRIVATE_JAR_MANIFEST), $(hide) echo unsupported options JAR_MANIFEST in $@; exit 53)
2424endef
2425
2426define transform-classes.jar-to-emma
2427$(hide) java -classpath $(EMMA_JAR) emma instr -outmode fullcopy -outfile \
2428    $(PRIVATE_EMMA_COVERAGE_FILE) -ip $< -d $(PRIVATE_EMMA_INTERMEDIATES_DIR) \
2429    $(addprefix -ix , $(PRIVATE_EMMA_COVERAGE_FILTER))
2430endef
2431
2432# Create a mostly-empty .jar file that we'll add to later.
2433# The MacOS jar tool doesn't like creating empty jar files,
2434# so we need to give it something.
2435# $(1) package to create
2436define create-empty-package-at
2437@mkdir -p $(dir $(1))
2438$(hide) touch $(dir $(1))zipdummy
2439$(hide) (cd $(dir $(1)) && jar cf $(notdir $(1)) zipdummy)
2440$(hide) zip -qd $(1) zipdummy
2441$(hide) rm $(dir $(1))zipdummy
2442endef
2443
2444# Create a mostly-empty .jar file that we'll add to later.
2445# The MacOS jar tool doesn't like creating empty jar files,
2446# so we need to give it something.
2447define create-empty-package
2448$(call create-empty-package-at,$@)
2449endef
2450
2451#TODO: we kinda want to build different asset packages for
2452#      different configurations, then combine them later (or something).
2453#      Per-locale, etc.
2454#      A list of dynamic and static parameters;  build layers for
2455#      dynamic params that lay over the static ones.
2456#TODO: update the manifest to point to the package file
2457#Note that the version numbers are given to aapt as simple default
2458#values; applications can override these by explicitly stating
2459#them in their manifest.
2460define add-assets-to-package
2461$(hide) $(AAPT) package -u $(PRIVATE_AAPT_FLAGS) \
2462    $(addprefix -c , $(PRIVATE_PRODUCT_AAPT_CONFIG)) \
2463    $(addprefix --preferred-density , $(PRIVATE_PRODUCT_AAPT_PREF_CONFIG)) \
2464    $(addprefix -M , $(PRIVATE_ANDROID_MANIFEST)) \
2465    $(addprefix -S , $(PRIVATE_RESOURCE_DIR)) \
2466    $(addprefix -A , $(PRIVATE_ASSET_DIR)) \
2467    $(addprefix -I , $(PRIVATE_AAPT_INCLUDES)) \
2468    $(addprefix --min-sdk-version , $(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
2469    $(addprefix --target-sdk-version , $(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
2470    $(if $(filter --product,$(PRIVATE_AAPT_FLAGS)),,$(addprefix --product , $(PRIVATE_TARGET_AAPT_CHARACTERISTICS))) \
2471    $(if $(filter --version-code,$(PRIVATE_AAPT_FLAGS)),,--version-code $(PLATFORM_SDK_VERSION)) \
2472    $(if $(filter --version-name,$(PRIVATE_AAPT_FLAGS)),,--version-name $(APPS_DEFAULT_VERSION_NAME)) \
2473    $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \
2474    $(addprefix --rename-instrumentation-target-package , $(PRIVATE_MANIFEST_INSTRUMENTATION_FOR)) \
2475    --skip-symbols-without-default-localization \
2476    -F $@
2477endef
2478
2479# We need the extra blank line, so that the command will be on a separate line.
2480# $(1): the ABI name
2481# $(2): the list of shared libraies
2482define _add-jni-shared-libs-to-package-per-abi
2483$(hide) cp $(2) $(dir $@)lib/$(1)
2484
2485endef
2486
2487# For apps_only build, don't uncompress/page-align the jni libraries,
2488# because the apk may be run on older platforms that don't support loading jni directly from apk.
2489ifdef TARGET_BUILD_APPS
2490JNI_COMPRESS_FLAGS :=
2491ZIPALIGN_PAGE_ALIGN_FLAGS :=
2492else
2493JNI_COMPRESS_FLAGS := -0
2494ZIPALIGN_PAGE_ALIGN_FLAGS := -p
2495endif
2496
2497define add-jni-shared-libs-to-package
2498$(hide) rm -rf $(dir $@)lib
2499$(hide) mkdir -p $(addprefix $(dir $@)lib/,$(PRIVATE_JNI_SHARED_LIBRARIES_ABI))
2500$(foreach abi,$(PRIVATE_JNI_SHARED_LIBRARIES_ABI),\
2501  $(call _add-jni-shared-libs-to-package-per-abi,$(abi),\
2502    $(patsubst $(abi):%,%,$(filter $(abi):%,$(PRIVATE_JNI_SHARED_LIBRARIES)))))
2503$(hide) (cd $(dir $@) && zip -qrX $(JNI_COMPRESS_FLAGS) $(notdir $@) lib)
2504$(hide) rm -rf $(dir $@)lib
2505endef
2506
2507#TODO: update the manifest to point to the dex file
2508define add-dex-to-package
2509$(call add-dex-to-package-arg,$@)
2510endef
2511
2512# $(1): the package file.
2513define add-dex-to-package-arg
2514$(hide) find $(dir $(PRIVATE_DEX_FILE)) -maxdepth 1 -name "classes*.dex" | sort | xargs zip -qjX $(1)
2515endef
2516
2517# Add java resources added by the current module.
2518# $(1) destination package
2519#
2520define add-java-resources-to
2521$(call dump-words-to-file, $(PRIVATE_EXTRA_JAR_ARGS), $(1).jar-arg-list)
2522$(hide) jar uf $(1) @$(1).jar-arg-list
2523@rm -f $(1).jar-arg-list
2524endef
2525
2526# Add resources carried by static Jack libraries.
2527#
2528define add-carried-jack-resources
2529 $(hide) if [ -d $(PRIVATE_JACK_INTERMEDIATES_DIR) ] ; then \
2530    find $(PRIVATE_JACK_INTERMEDIATES_DIR) -type f | sort \
2531        | sed -e "s?^$(PRIVATE_JACK_INTERMEDIATES_DIR)/? -C \"$(PRIVATE_JACK_INTERMEDIATES_DIR)\" \"?" -e "s/$$/\"/" \
2532        > $(dir $@)jack_res_jar_flags; \
2533    if [ -s $(dir $@)jack_res_jar_flags ] ; then \
2534        jar uf $@ @$(dir $@)jack_res_jar_flags; \
2535    fi; \
2536fi
2537endef
2538
2539# Returns the minSdkVersion of the specified APK as a decimal number. If the
2540# version is a codename, returns the current platform SDK version (always a
2541# decimal number) instead. If the APK does not specify a minSdkVersion, returns
2542# 0 to match how the Android platform interprets this situation at runtime.
2543#
2544# This currently substitutes any version which contains characters other than
2545# digits with the current platform's API Level number. This is because I
2546# couldn't figure out an easy way to perform the substitution only for the
2547# version codes listed in PLATFORM_VERSION_ALL_CODENAMES.
2548define get-package-min-sdk-version-int
2549$$(($(AAPT) dump badging $(1) 2>&1 | grep '^sdkVersion' || echo "sdkVersion:'0'") \
2550    | cut -d"'" -f2 | \
2551    sed -e s/^.*[^0-9].*$$/$(PLATFORM_SDK_VERSION)/)
2552endef
2553
2554# Sign a package using the specified key/cert.
2555#
2556define sign-package
2557$(call sign-package-arg,$@)
2558endef
2559
2560# $(1): the package file we are signing.
2561define sign-package-arg
2562$(hide) mv $(1) $(1).unsigned
2563$(hide) java -Djava.library.path=$(SIGNAPK_JNI_LIBRARY_PATH) -jar $(SIGNAPK_JAR) \
2564    --min-sdk-version $(call get-package-min-sdk-version-int,$@.unsigned) \
2565    $(PRIVATE_CERTIFICATE) $(PRIVATE_PRIVATE_KEY) \
2566    $(PRIVATE_ADDITIONAL_CERTIFICATES) $(1).unsigned $(1).signed
2567$(hide) mv $(1).signed $(1)
2568endef
2569
2570# Align STORED entries of a package on 4-byte boundaries to make them easier to mmap.
2571#
2572define align-package
2573$(hide) if ! $(ZIPALIGN) -c $(ZIPALIGN_PAGE_ALIGN_FLAGS) 4 $@ >/dev/null ; then \
2574  mv $@ $@.unaligned; \
2575  $(ZIPALIGN) \
2576    -f \
2577    $(ZIPALIGN_PAGE_ALIGN_FLAGS) \
2578    4 \
2579    $@.unaligned $@.aligned; \
2580  mv $@.aligned $@; \
2581  fi
2582endef
2583
2584# Remove dynamic timestamps from packages
2585#
2586define remove-timestamps-from-package
2587$(hide) $(ZIPTIME) $@
2588endef
2589
2590# Uncompress shared libraries embedded in an apk.
2591#
2592define uncompress-shared-libs
2593$(hide) if (zipinfo $@ $(PRIVATE_EMBEDDED_JNI_LIBS) 2>/dev/null | grep -v ' stor ' >/dev/null) ; then \
2594  rm -rf $(dir $@)uncompressedlibs && mkdir $(dir $@)uncompressedlibs; \
2595  unzip $@ $(PRIVATE_EMBEDDED_JNI_LIBS) -d $(dir $@)uncompressedlibs && \
2596  zip -d $@ 'lib/*.so' && \
2597  ( cd $(dir $@)uncompressedlibs && find lib -type f | sort | zip -D -X -0 ../$(notdir $@) -@ ) && \
2598  rm -rf $(dir $@)uncompressedlibs; \
2599  fi
2600endef
2601
2602# TODO(joeo): If we can ever upgrade to post 3.81 make and get the
2603# new prebuilt rules to work, we should change this to copy the
2604# resources to the out directory and then copy the resources.
2605
2606# Note: we intentionally don't clean PRIVATE_CLASS_INTERMEDIATES_DIR
2607# in transform-java-to-classes for the sake of vm-tests.
2608define transform-host-java-to-package
2609@echo "$($(PRIVATE_PREFIX)DISPLAY) Java: $(PRIVATE_MODULE) ($(PRIVATE_CLASS_INTERMEDIATES_DIR))"
2610$(call compile-java,$(HOST_JAVAC),$(PRIVATE_BOOTCLASSPATH))
2611endef
2612
2613###########################################################
2614## Commands for copying files
2615###########################################################
2616
2617# Define a rule to copy a header.  Used via $(eval) by copy_headers.make.
2618# $(1): source header
2619# $(2): destination header
2620define copy-one-header
2621$(2): $(1)
2622	@echo "Header: $$@"
2623	$$(copy-file-to-new-target-with-cp)
2624endef
2625
2626# Define a rule to copy a file.  For use via $(eval).
2627# $(1): source file
2628# $(2): destination file
2629define copy-one-file
2630$(2): $(1)
2631	@echo "Copy: $$@"
2632	$$(copy-file-to-target)
2633endef
2634
2635# Copies many files.
2636# $(1): The files to copy.  Each entry is a ':' separated src:dst pair
2637# Evaluates to the list of the dst files (ie suitable for a dependency list)
2638define copy-many-files
2639$(foreach f, $(1), $(strip \
2640    $(eval _cmf_tuple := $(subst :, ,$(f))) \
2641    $(eval _cmf_src := $(word 1,$(_cmf_tuple))) \
2642    $(eval _cmf_dest := $(word 2,$(_cmf_tuple))) \
2643    $(eval $(call copy-one-file,$(_cmf_src),$(_cmf_dest))) \
2644    $(_cmf_dest)))
2645endef
2646
2647# Copy the file only if it's a well-formed xml file. For use via $(eval).
2648# $(1): source file
2649# $(2): destination file, must end with .xml.
2650define copy-xml-file-checked
2651$(2): $(1)
2652	@echo "Copy xml: $$@"
2653	$(hide) xmllint $$< >/dev/null  # Don't print the xml file to stdout.
2654	$$(copy-file-to-target)
2655endef
2656
2657# The -t option to acp and the -p option to cp is
2658# required for OSX.  OSX has a ridiculous restriction
2659# where it's an error for a .a file's modification time
2660# to disagree with an internal timestamp, and this
2661# macro is used to install .a files (among other things).
2662
2663# Copy a single file from one place to another,
2664# preserving permissions and overwriting any existing
2665# file.
2666# When we used acp, it could not handle high resolution timestamps
2667# on file systems like ext4. Because of that, '-t' option was disabled
2668# and copy-file-to-target was identical to copy-file-to-new-target.
2669# Keep the behavior until we audit and ensure that switching this back
2670# won't break anything.
2671define copy-file-to-target
2672@mkdir -p $(dir $@)
2673$(hide) rm -f $@
2674$(hide) cp $< $@
2675endef
2676
2677# The same as copy-file-to-target, but use the local
2678# cp command instead of acp.
2679define copy-file-to-target-with-cp
2680@mkdir -p $(dir $@)
2681$(hide) rm -f $@
2682$(hide) cp -p $< $@
2683endef
2684
2685# The same as copy-file-to-target, but use the zipalign tool to do so.
2686define copy-file-to-target-with-zipalign
2687@mkdir -p $(dir $@)
2688$(hide) rm -f $@
2689$(hide) $(ZIPALIGN) -f 4 $< $@
2690endef
2691
2692# The same as copy-file-to-target, but strip out "# comment"-style
2693# comments (for config files and such).
2694define copy-file-to-target-strip-comments
2695@mkdir -p $(dir $@)
2696$(hide) rm -f $@
2697$(hide) sed -e 's/#.*$$//' -e 's/[ \t]*$$//' -e '/^$$/d' < $< > $@
2698endef
2699
2700# The same as copy-file-to-target, but don't preserve
2701# the old modification time.
2702define copy-file-to-new-target
2703@mkdir -p $(dir $@)
2704$(hide) rm -f $@
2705$(hide) cp $< $@
2706endef
2707
2708# The same as copy-file-to-new-target, but use the local
2709# cp command instead of acp.
2710define copy-file-to-new-target-with-cp
2711@mkdir -p $(dir $@)
2712$(hide) rm -f $@
2713$(hide) cp $< $@
2714endef
2715
2716# Copy a prebuilt file to a target location.
2717define transform-prebuilt-to-target
2718@echo "$(if $(PRIVATE_IS_HOST_MODULE),host,target) Prebuilt: $(PRIVATE_MODULE) ($@)"
2719$(copy-file-to-target)
2720endef
2721
2722# Copy a prebuilt file to a target location, using zipalign on it.
2723define transform-prebuilt-to-target-with-zipalign
2724@echo "$(if $(PRIVATE_IS_HOST_MODULE),host,target) Prebuilt APK: $(PRIVATE_MODULE) ($@)"
2725$(copy-file-to-target-with-zipalign)
2726endef
2727
2728# Copy a prebuilt file to a target location, stripping "# comment" comments.
2729define transform-prebuilt-to-target-strip-comments
2730@echo "$(if $(PRIVATE_IS_HOST_MODULE),host,target) Prebuilt: $(PRIVATE_MODULE) ($@)"
2731$(copy-file-to-target-strip-comments)
2732endef
2733
2734# Copy a list of files/directories to target location, with sub dir structure preserved.
2735# For example $(HOST_OUT_EXECUTABLES)/aapt -> $(staging)/bin/aapt .
2736# $(1): the source list of files/directories.
2737# $(2): the path prefix to strip. In the above example it would be $(HOST_OUT).
2738# $(3): the target location.
2739define copy-files-with-structure
2740$(foreach t,$(1),\
2741  $(eval s := $(patsubst $(2)%,%,$(t)))\
2742  $(hide) mkdir -p $(dir $(3)/$(s)); cp -Rf $(t) $(3)/$(s)$(newline))
2743endef
2744
2745# Define a rule to create a symlink to a file.
2746# $(1): full path to source
2747# $(2): source (may be relative)
2748# $(3): full path to destination
2749define symlink-file
2750$(eval $(_symlink-file))
2751endef
2752
2753# Order-only dependency because make/ninja will follow the link when checking
2754# the timestamp, so the file must exist
2755define _symlink-file
2756$(3): | $(1)
2757	@echo "Symlink: $$@ -> $(2)"
2758	@mkdir -p $(dir $$@)
2759	@rm -rf $$@
2760	$(hide) ln -sf $(2) $$@
2761endef
2762
2763###########################################################
2764## Commands to call Proguard
2765###########################################################
2766define transform-jar-to-proguard
2767@echo Proguard: $@
2768$(hide) $(PROGUARD) -injars $< -outjars $@ $(PRIVATE_PROGUARD_FLAGS) \
2769    $(addprefix -injars , $(PRIVATE_EXTRA_INPUT_JAR))
2770endef
2771
2772###########################################################
2773## Stuff source generated from one-off tools
2774###########################################################
2775
2776define transform-generated-source
2777@echo "target Generated: $(PRIVATE_MODULE) <= $<"
2778@mkdir -p $(dir $@)
2779$(hide) $(PRIVATE_CUSTOM_TOOL)
2780endef
2781
2782
2783###########################################################
2784## Assertions about attributes of the target
2785###########################################################
2786
2787# $(1): The file to check
2788ifndef get-file-size
2789$(error HOST_OS must define get-file-size)
2790endif
2791
2792# Convert a partition data size (eg, as reported in /proc/mtd) to the
2793# size of the image used to flash that partition (which includes a
2794# spare area for each page).
2795# $(1): the partition data size
2796define image-size-from-data-size
2797$(strip $(eval _isfds_value := $$(shell echo $$$$(($(1) / $(BOARD_NAND_PAGE_SIZE) * \
2798  ($(BOARD_NAND_PAGE_SIZE)+$(BOARD_NAND_SPARE_SIZE))))))\
2799$(if $(filter 0, $(_isfds_value)),$(shell echo $$(($(BOARD_NAND_PAGE_SIZE)+$(BOARD_NAND_SPARE_SIZE)))),$(_isfds_value))\
2800$(eval _isfds_value :=))
2801endef
2802
2803# $(1): The file(s) to check (often $@)
2804# $(2): The maximum total image size, in decimal bytes.
2805#    Make sure to take into account any reserved space needed for the FS.
2806#
2807# If $(2) is empty, evaluates to "true"
2808#
2809# Reserve bad blocks.  Make sure that MAX(1% of partition size, 2 blocks)
2810# is left over after the image has been flashed.  Round the 1% up to the
2811# next whole flash block size.
2812define assert-max-file-size
2813$(if $(2), \
2814  size=$$(for i in $(1); do $(call get-file-size,$$i); echo +; done; echo 0); \
2815  total=$$(( $$( echo "$$size" ) )); \
2816  printname=$$(echo -n "$(1)" | tr " " +); \
2817  img_blocksize=$(call image-size-from-data-size,$(BOARD_FLASH_BLOCK_SIZE)); \
2818  twoblocks=$$((img_blocksize * 2)); \
2819  onepct=$$((((($(2) / 100) - 1) / img_blocksize + 1) * img_blocksize)); \
2820  reserve=$$((twoblocks > onepct ? twoblocks : onepct)); \
2821  maxsize=$$(($(2) - reserve)); \
2822  echo "$$printname maxsize=$$maxsize blocksize=$$img_blocksize total=$$total reserve=$$reserve"; \
2823  if [ "$$total" -gt "$$maxsize" ]; then \
2824    echo "error: $$printname too large ($$total > [$(2) - $$reserve])"; \
2825    false; \
2826  elif [ "$$total" -gt $$((maxsize - 32768)) ]; then \
2827    echo "WARNING: $$printname approaching size limit ($$total now; limit $$maxsize)"; \
2828  fi \
2829 , \
2830  true \
2831 )
2832endef
2833
2834# Like assert-max-file-size, but the second argument is a partition
2835# size, which we'll convert to a max image size before checking it
2836# against the files.
2837#
2838# $(1): The file(s) to check (often $@)
2839# $(2): The partition size.
2840define assert-max-image-size
2841$(if $(2), \
2842  $(call assert-max-file-size,$(1),$(call image-size-from-data-size,$(2))))
2843endef
2844
2845
2846###########################################################
2847## Define device-specific radio files
2848###########################################################
2849INSTALLED_RADIOIMAGE_TARGET :=
2850
2851# Copy a radio image file to the output location, and add it to
2852# INSTALLED_RADIOIMAGE_TARGET.
2853# $(1): filename
2854define add-radio-file
2855  $(eval $(call add-radio-file-internal,$(1),$(notdir $(1))))
2856endef
2857define add-radio-file-internal
2858INSTALLED_RADIOIMAGE_TARGET += $$(PRODUCT_OUT)/$(2)
2859$$(PRODUCT_OUT)/$(2) : $$(LOCAL_PATH)/$(1)
2860	$$(transform-prebuilt-to-target)
2861endef
2862
2863# Version of add-radio-file that also arranges for the version of the
2864# file to be checked against the contents of
2865# $(TARGET_BOARD_INFO_FILE).
2866# $(1): filename
2867# $(2): name of version variable in board-info (eg, "version-baseband")
2868define add-radio-file-checked
2869  $(eval $(call add-radio-file-checked-internal,$(1),$(notdir $(1)),$(2)))
2870endef
2871define add-radio-file-checked-internal
2872INSTALLED_RADIOIMAGE_TARGET += $$(PRODUCT_OUT)/$(2)
2873BOARD_INFO_CHECK += $(3):$(LOCAL_PATH)/$(1)
2874$$(PRODUCT_OUT)/$(2) : $$(LOCAL_PATH)/$(1)
2875	$$(transform-prebuilt-to-target)
2876endef
2877
2878
2879###########################################################
2880# Override the package defined in $(1), setting the
2881# variables listed below differently.
2882#
2883#  $(1): The makefile to override (relative to the source
2884#        tree root)
2885#  $(2): Old LOCAL_PACKAGE_NAME value.
2886#  $(3): New LOCAL_PACKAGE_NAME value.
2887#  $(4): New LOCAL_MANIFEST_PACKAGE_NAME value.
2888#  $(5): New LOCAL_CERTIFICATE value.
2889#  $(6): New LOCAL_INSTRUMENTATION_FOR value.
2890#  $(7): New LOCAL_MANIFEST_INSTRUMENTATION_FOR value.
2891#
2892# Note that LOCAL_PACKAGE_OVERRIDES is NOT cleared in
2893# clear_vars.mk.
2894###########################################################
2895define inherit-package
2896  $(eval $(call inherit-package-internal,$(1),$(2),$(3),$(4),$(5),$(6),$(7)))
2897endef
2898
2899define inherit-package-internal
2900  LOCAL_PACKAGE_OVERRIDES \
2901      := $(strip $(1))||$(strip $(2))||$(strip $(3))||$(strip $(4))||&&$(strip $(5))||&&$(strip $(6))||&&$(strip $(7)) $(LOCAL_PACKAGE_OVERRIDES)
2902  include $(1)
2903  LOCAL_PACKAGE_OVERRIDES \
2904      := $(wordlist 1,$(words $(LOCAL_PACKAGE_OVERRIDES)), $(LOCAL_PACKAGE_OVERRIDES))
2905endef
2906
2907# To be used with inherit-package above
2908# Evalutes to true if the package was overridden
2909define set-inherited-package-variables
2910$(strip $(call set-inherited-package-variables-internal))
2911endef
2912
2913define keep-or-override
2914$(eval $(1) := $(if $(2),$(2),$($(1))))
2915endef
2916
2917define set-inherited-package-variables-internal
2918  $(eval _o := $(subst ||, ,$(lastword $(LOCAL_PACKAGE_OVERRIDES))))
2919  $(eval _n := $(subst ||, ,$(firstword $(LOCAL_PACKAGE_OVERRIDES))))
2920  $(if $(filter $(word 2,$(_n)),$(LOCAL_PACKAGE_NAME)), \
2921    $(eval LOCAL_PACKAGE_NAME := $(word 3,$(_o))) \
2922    $(eval LOCAL_MANIFEST_PACKAGE_NAME := $(word 4,$(_o))) \
2923    $(call keep-or-override,LOCAL_CERTIFICATE,$(patsubst &&%,%,$(word 5,$(_o)))) \
2924    $(call keep-or-override,LOCAL_INSTRUMENTATION_FOR,$(patsubst &&%,%,$(word 6,$(_o)))) \
2925    $(call keep-or-override,LOCAL_MANIFEST_INSTRUMENTATION_FOR,$(patsubst &&%,%,$(word 7,$(_o)))) \
2926    $(eval LOCAL_OVERRIDES_PACKAGES := $(sort $(LOCAL_OVERRIDES_PACKAGES) $(word 2,$(_o)))) \
2927    true \
2928  ,)
2929endef
2930
2931###########################################################
2932## API Check
2933###########################################################
2934
2935# eval this to define a rule that runs apicheck.
2936#
2937# Args:
2938#    $(1)  target
2939#    $(2)  stable api file
2940#    $(3)  api file to be tested
2941#    $(4)  stable removed api file
2942#    $(5)  removed api file to be tested
2943#    $(6)  arguments for apicheck
2944#    $(7)  command to run if apicheck failed
2945#    $(8)  target dependent on this api check
2946#    $(9)  additional dependencies
2947define check-api
2948$(TARGET_OUT_COMMON_INTERMEDIATES)/PACKAGING/$(strip $(1))-timestamp: $(2) $(3) $(4) $(APICHECK) $(9)
2949	@echo "Checking API:" $(1)
2950	$(hide) ( $(APICHECK_COMMAND) $(6) $(2) $(3) $(4) $(5) || ( $(7) ; exit 38 ) )
2951	$(hide) mkdir -p $$(dir $$@)
2952	$(hide) touch $$@
2953$(8): $(TARGET_OUT_COMMON_INTERMEDIATES)/PACKAGING/$(strip $(1))-timestamp
2954endef
2955
2956## Whether to build from source if prebuilt alternative exists
2957###########################################################
2958# $(1): module name
2959# $(2): LOCAL_PATH
2960# Expands to empty string if not from source.
2961ifeq (true,$(ANDROID_BUILD_FROM_SOURCE))
2962define if-build-from-source
2963true
2964endef
2965else
2966define if-build-from-source
2967$(if $(filter $(ANDROID_NO_PREBUILT_MODULES),$(1))$(filter \
2968    $(addsuffix %,$(ANDROID_NO_PREBUILT_PATHS)),$(2)),true)
2969endef
2970endif
2971
2972# Include makefile $(1) if build from source for module $(2)
2973# $(1): the makefile to include
2974# $(2): module name
2975# $(3): LOCAL_PATH
2976define include-if-build-from-source
2977$(if $(call if-build-from-source,$(2),$(3)),$(eval include $(1)))
2978endef
2979
2980# Return the arch for the source file of a prebuilt
2981# Return "none" if no matching arch found and return empty
2982# if the input is empty, so the result can be passed to
2983# LOCAL_MODULE_TARGET_ARCH.
2984# $(1) the list of archs supported by the prebuilt
2985define get-prebuilt-src-arch
2986$(strip $(if $(filter $(TARGET_ARCH),$(1)),$(TARGET_ARCH),\
2987  $(if $(filter $(TARGET_2ND_ARCH),$(1)),$(TARGET_2ND_ARCH),$(if $(1),none))))
2988endef
2989
2990###########################################################
2991## Other includes
2992###########################################################
2993
2994# -----------------------------------------------------------------
2995# Rules and functions to help copy important files to DIST_DIR
2996# when requested.
2997include $(BUILD_SYSTEM)/distdir.mk
2998
2999# Include any vendor specific definitions.mk file
3000-include $(TOPDIR)vendor/*/build/core/definitions.mk
3001-include $(TOPDIR)device/*/build/core/definitions.mk
3002-include $(TOPDIR)product/*/build/core/definitions.mk
3003
3004# broken:
3005#	$(foreach file,$^,$(if $(findstring,.a,$(suffix $file)),-l$(file),$(file)))
3006
3007###########################################################
3008## Misc notes
3009###########################################################
3010
3011#DEPDIR = .deps
3012#df = $(DEPDIR)/$(*F)
3013
3014#SRCS = foo.c bar.c ...
3015
3016#%.o : %.c
3017#	@$(MAKEDEPEND); \
3018#	  cp $(df).d $(df).P; \
3019#	  sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
3020#	      -e '/^$$/ d' -e 's/$$/ :/' < $(df).d >> $(df).P; \
3021#	  rm -f $(df).d
3022#	$(COMPILE.c) -o $@ $<
3023
3024#-include $(SRCS:%.c=$(DEPDIR)/%.P)
3025
3026
3027#%.o : %.c
3028#	$(COMPILE.c) -MD -o $@ $<
3029#	@cp $*.d $*.P; \
3030#	  sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
3031#	      -e '/^$$/ d' -e 's/$$/ :/' < $*.d >> $*.P; \
3032#	  rm -f $*.d
3033