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