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