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