• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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# The relative paths of the non-module targets in the system.
41ALL_NON_MODULES:=
42NON_MODULES_WITHOUT_LICENSE_METADATA:=
43
44# Full paths to targets that should be added to the "make droid"
45# set of installed targets.
46ALL_DEFAULT_INSTALLED_MODULES:=
47
48# The list of tags that have been defined by
49# LOCAL_MODULE_TAGS.  Each word in this variable maps
50# to a corresponding ALL_MODULE_TAGS.<tagname> variable
51# that contains all of the INSTALLED_MODULEs with that tag.
52ALL_MODULE_TAGS:=
53
54# Similar to ALL_MODULE_TAGS, but contains the short names
55# of all targets for a particular tag.  The top-level variable
56# won't have the list of tags;  ust ALL_MODULE_TAGS to get
57# the list of all known tags.  (This means that this variable
58# will always be empty; it's just here as a placeholder for
59# its sub-variables.)
60ALL_MODULE_NAME_TAGS:=
61
62# Full path to all asm, C, C++, lex and yacc generated C files.
63# These all have an order-only dependency on the copied headers
64ALL_C_CPP_ETC_OBJECTS:=
65
66# These files go into the SDK
67ALL_SDK_FILES:=
68
69# Files for dalvik.  This is often build without building the rest of the OS.
70INTERNAL_DALVIK_MODULES:=
71
72# All findbugs xml files
73ALL_FINDBUGS_FILES:=
74
75# GPL module license files
76ALL_GPL_MODULE_LICENSE_FILES:=
77
78# Packages with certificate violation
79CERTIFICATE_VIOLATION_MODULES :=
80
81# Target and host installed module's dependencies on shared libraries.
82# They are list of "<module_name>:<installed_file>:lib1,lib2...".
83TARGET_DEPENDENCIES_ON_SHARED_LIBRARIES :=
84$(TARGET_2ND_ARCH_VAR_PREFIX)TARGET_DEPENDENCIES_ON_SHARED_LIBRARIES :=
85HOST_DEPENDENCIES_ON_SHARED_LIBRARIES :=
86$(HOST_2ND_ARCH_VAR_PREFIX)HOST_DEPENDENCIES_ON_SHARED_LIBRARIES :=
87HOST_CROSS_DEPENDENCIES_ON_SHARED_LIBRARIES :=
88$(HOST_CROSS_2ND_ARCH_VAR_PREFIX)HOST_CROSS_DEPENDENCIES_ON_SHARED_LIBRARIES :=
89
90# Generated class file names for Android resource.
91# They are escaped and quoted so can be passed safely to a bash command.
92ANDROID_RESOURCE_GENERATED_CLASSES := 'R.class' 'R$$*.class' 'Manifest.class' 'Manifest$$*.class'
93
94# Display names for various build targets
95TARGET_DISPLAY := target
96HOST_DISPLAY := host
97HOST_CROSS_DISPLAY := host cross
98
99# All installed initrc files
100ALL_INIT_RC_INSTALLED_PAIRS :=
101
102# All installed vintf manifest fragments for a partition at
103ALL_VINTF_MANIFEST_FRAGMENTS_LIST:=
104
105# All tests that should be skipped in presubmit check.
106ALL_DISABLED_PRESUBMIT_TESTS :=
107
108# All compatibility suites mentioned in LOCAL_COMPATIBILITY_SUITE
109ALL_COMPATIBILITY_SUITES :=
110
111# All compatibility suite files to dist.
112ALL_COMPATIBILITY_DIST_FILES :=
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
525###########################################################
526## Sometimes a notice dependency will reference an unadorned
527## module name that only appears in ALL_MODULES adorned with
528## an ARCH suffix or a `host_cross_` prefix.
529##
530## After all of the modules are processed in base_rules.mk,
531## replace all such dependencies with every matching adorned
532## module name.
533###########################################################
534
535define fix-notice-deps
536$(strip \
537  $(eval _all_module_refs := \
538    $(sort \
539      $(foreach m,$(sort $(ALL_MODULES)), \
540        $(call word-colon,1,$(ALL_MODULES.$(m).NOTICE_DEPS)) \
541      ) \
542    ) \
543  ) \
544  $(foreach m, $(_all_module_refs), \
545    $(eval _lookup.$(m) := \
546      $(sort \
547        $(if $(strip $(ALL_MODULES.$(m).PATH)), \
548          $(m), \
549          $(filter $(m)_32 $(m)_64 host_cross_$(m) host_cross_$(m)_32 host_cross_$(m)_64, $(ALL_MODULES)) \
550        ) \
551      ) \
552    ) \
553  ) \
554  $(foreach m, $(ALL_MODULES), \
555    $(eval ALL_MODULES.$(m).NOTICE_DEPS := \
556      $(sort \
557         $(foreach d,$(sort $(ALL_MODULES.$(m).NOTICE_DEPS)), \
558           $(foreach n,$(_lookup.$(call word-colon,1,$(d))),$(n):$(call wordlist-colon,2,9999,$(d))) \
559        ) \
560      ) \
561    ) \
562  ) \
563)
564endef
565
566###########################################################
567## Target directory for license metadata files.
568###########################################################
569define license-metadata-dir
570$(call generated-sources-dir-for,META,lic,)
571endef
572
573###########################################################
574# License metadata targets corresponding to targets in $(1)
575###########################################################
576define corresponding-license-metadata
577$(strip $(foreach target, $(sort $(1)), \
578  $(if $(strip $(ALL_MODULES.$(target).META_LIC)), \
579    $(ALL_MODULES.$(target).META_LIC), \
580    $(if $(strip $(ALL_TARGETS.$(target).META_LIC)), \
581      $(ALL_TARGETS.$(target).META_LIC), \
582      $(call append-path,$(call license-metadata-dir),$(patsubst $(OUT_DIR)%,out%,$(target).meta_lic))))))
583endef
584
585###########################################################
586## License metadata build rule for my_register_name $(1)
587###########################################################
588define license-metadata-rule
589$(foreach meta_lic, $(ALL_MODULES.$(1).DELAYED_META_LIC),$(call _license-metadata-rule,$(1),$(meta_lic)))
590endef
591
592$(KATI_obsolete_var notice-rule, This function has been removed)
593
594define _license-metadata-rule
595$(strip $(eval _srcs := $(strip $(foreach d,$(ALL_MODULES.$(1).NOTICE_DEPS),$(if $(strip $(ALL_MODULES.$(call word-colon,1,$(d)).INSTALLED)), $(ALL_MODULES.$(call word-colon,1,$(d)).INSTALLED),$(if $(strip $(ALL_MODULES.$(call word-colon,1,$(d)).BUILT)), $(ALL_MODULES.$(call word-colon,1,$(d)).BUILT), $(call word-colon,1,$d)))))))
596$(strip $(eval _deps := $(sort $(filter-out $(2)%,\
597   $(foreach d,$(ALL_MODULES.$(1).NOTICE_DEPS),\
598     $(addsuffix :$(call wordlist-colon,2,9999,$(d)), \
599       $(foreach dt,$(ALL_MODULES.$(d).BUILT) $(ALL_MODULES.$(d).INSTALLED),\
600         $(ALL_TARGETS.$(dt).META_LIC))))))))
601$(strip $(eval _notices := $(sort $(ALL_MODULES.$(1).NOTICES))))
602$(strip $(eval _tgts := $(sort $(ALL_MODULES.$(1).BUILT))))
603$(strip $(eval _inst := $(sort $(ALL_MODULES.$(1).INSTALLED))))
604$(strip $(eval _path := $(sort $(ALL_MODULES.$(1).PATH))))
605$(strip $(eval _map := $(strip $(foreach _m,$(sort $(ALL_MODULES.$(1).LICENSE_INSTALL_MAP)), \
606  $(eval _s := $(call word-colon,1,$(_m))) \
607  $(eval _d := $(call word-colon,2,$(_m))) \
608  $(eval _ns := $(if $(strip $(ALL_MODULES.$(_s).INSTALLED)),$(ALL_MODULES.$(_s).INSTALLED),$(if $(strip $(ALL_MODULES.$(_s).BUILT)),$(ALL_MODULES.$(_s).BUILT),$(_s)))) \
609  $(foreach ns,$(_ns),$(ns):$(_d) ) \
610))))
611
612$(2): PRIVATE_KINDS := $(sort $(ALL_MODULES.$(1).LICENSE_KINDS))
613$(2): PRIVATE_CONDITIONS := $(sort $(ALL_MODULES.$(1).LICENSE_CONDITIONS))
614$(2): PRIVATE_NOTICES := $(_notices)
615$(2): PRIVATE_NOTICE_DEPS := $(_deps)
616$(2): PRIVATE_SOURCES := $(_srcs)
617$(2): PRIVATE_TARGETS := $(_tgts)
618$(2): PRIVATE_INSTALLED := $(_inst)
619$(2): PRIVATE_PATH := $(_path)
620$(2): PRIVATE_IS_CONTAINER := $(ALL_MODULES.$(1).IS_CONTAINER)
621$(2): PRIVATE_PACKAGE_NAME := $(strip $(ALL_MODULES.$(1).LICENSE_PACKAGE_NAME))
622$(2): PRIVATE_INSTALL_MAP := $(_map)
623$(2): PRIVATE_MODULE_TYPE := $(ALL_MODULES.$(1).MODULE_TYPE)
624$(2): PRIVATE_MODULE_CLASS := $(ALL_MODULES.$(1).MODULE_CLASS)
625$(2): PRIVATE_INSTALL_MAP := $(_map)
626$(2): PRIVATE_ARGUMENT_FILE := $(call intermediates-dir-for,PACKAGING,notice)/$(2)/arguments
627$(2): $(BUILD_LICENSE_METADATA)
628$(2) : $(foreach d,$(_deps),$(call word-colon,1,$(d))) $(foreach n,$(_notices),$(call word-colon,1,$(n)) )
629	rm -f $$@
630	mkdir -p $$(dir $$@)
631	mkdir -p $$(dir $$(PRIVATE_ARGUMENT_FILE))
632	$$(call dump-words-to-file,\
633	    $$(addprefix -mt ,$$(PRIVATE_MODULE_TYPE))\
634	    $$(addprefix -mc ,$$(PRIVATE_MODULE_CLASS))\
635	    $$(addprefix -k ,$$(PRIVATE_KINDS))\
636	    $$(addprefix -c ,$$(PRIVATE_CONDITIONS))\
637	    $$(addprefix -n ,$$(PRIVATE_NOTICES))\
638	    $$(addprefix -d ,$$(PRIVATE_NOTICE_DEPS))\
639	    $$(addprefix -s ,$$(PRIVATE_SOURCES))\
640	    $$(addprefix -m ,$$(PRIVATE_INSTALL_MAP))\
641	    $$(addprefix -t ,$$(PRIVATE_TARGETS))\
642	    $$(addprefix -i ,$$(PRIVATE_INSTALLED))\
643	    $$(addprefix -r ,$$(PRIVATE_PATH)),\
644	    $$(PRIVATE_ARGUMENT_FILE))
645	OUT_DIR=$(OUT_DIR) $(BUILD_LICENSE_METADATA) \
646	  $$(if $$(PRIVATE_IS_CONTAINER),-is_container) \
647	  -p '$$(PRIVATE_PACKAGE_NAME)' \
648	  @$$(PRIVATE_ARGUMENT_FILE) \
649	  -o $$@
650endef
651
652
653###########################################################
654## License metadata build rule for non-module target $(1)
655###########################################################
656define non-module-license-metadata-rule
657$(strip $(eval _dir := $(call license-metadata-dir)))
658$(strip $(eval _tgt := $(strip $(1))))
659$(strip $(eval _meta := $(call append-path,$(_dir),$(patsubst $(OUT_DIR)%,out%,$(_tgt).meta_lic))))
660$(strip $(eval _deps := $(sort $(filter-out 0p: :,$(foreach d,$(strip $(ALL_NON_MODULES.$(_tgt).DEPENDENCIES)),$(ALL_TARGETS.$(call word-colon,1,$(d)).META_LIC):$(call wordlist-colon,2,9999,$(d)))))))
661$(strip $(eval _notices := $(sort $(ALL_NON_MODULES.$(_tgt).NOTICES))))
662$(strip $(eval _path := $(sort $(ALL_NON_MODULES.$(_tgt).PATH))))
663$(strip $(eval _install_map := $(ALL_NON_MODULES.$(_tgt).ROOT_MAPPINGS)))
664$(strip $(eval \
665  $$(foreach d,$(strip $(ALL_NON_MODULES.$(_tgt).DEPENDENCIES)), \
666    $$(if $$(strip $$(ALL_TARGETS.$$(d).META_LIC)), \
667      , \
668      $$(eval NON_MODULES_WITHOUT_LICENSE_METADATA += $$(d))) \
669  )) \
670)
671
672$(_meta): PRIVATE_KINDS := $(sort $(ALL_NON_MODULES.$(_tgt).LICENSE_KINDS))
673$(_meta): PRIVATE_CONDITIONS := $(sort $(ALL_NON_MODULES.$(_tgt).LICENSE_CONDITIONS))
674$(_meta): PRIVATE_NOTICES := $(_notices)
675$(_meta): PRIVATE_NOTICE_DEPS := $(_deps)
676$(_meta): PRIVATE_SOURCES := $(ALL_NON_MODULES.$(_tgt).DEPENDENCIES)
677$(_meta): PRIVATE_TARGETS := $(_tgt)
678$(_meta): PRIVATE_PATH := $(_path)
679$(_meta): PRIVATE_IS_CONTAINER := $(ALL_NON_MODULES.$(_tgt).IS_CONTAINER)
680$(_meta): PRIVATE_PACKAGE_NAME := $(strip $(ALL_NON_MODULES.$(_tgt).LICENSE_PACKAGE_NAME))
681$(_meta): PRIVATE_INSTALL_MAP := $(strip $(_install_map))
682$(_meta): PRIVATE_ARGUMENT_FILE := $(call intermediates-dir-for,PACKAGING,notice)/$(_meta)/arguments
683$(_meta): $(BUILD_LICENSE_METADATA)
684$(_meta) : $(foreach d,$(_deps),$(call word-colon,1,$(d))) $(foreach n,$(_notices),$(call word-colon,1,$(n)) )
685	rm -f $$@
686	mkdir -p $$(dir $$@)
687	mkdir -p $$(dir $$(PRIVATE_ARGUMENT_FILE))
688	$$(call dump-words-to-file,\
689	    $$(addprefix -k ,$$(PRIVATE_KINDS))\
690	    $$(addprefix -c ,$$(PRIVATE_CONDITIONS))\
691	    $$(addprefix -n ,$$(PRIVATE_NOTICES))\
692	    $$(addprefix -d ,$$(PRIVATE_NOTICE_DEPS))\
693	    $$(addprefix -s ,$$(PRIVATE_SOURCES))\
694	    $$(addprefix -m ,$$(PRIVATE_INSTALL_MAP))\
695	    $$(addprefix -t ,$$(PRIVATE_TARGETS))\
696	    $$(addprefix -r ,$$(PRIVATE_PATH)),\
697	    $$(PRIVATE_ARGUMENT_FILE))
698	OUT_DIR=$(OUT_DIR) $(BUILD_LICENSE_METADATA) \
699          -mt raw -mc unknown \
700	  $$(if $$(PRIVATE_IS_CONTAINER),-is_container) \
701	  $$(addprefix -r ,$$(PRIVATE_PATH)) \
702	  @$$(PRIVATE_ARGUMENT_FILE) \
703	  -o $$@
704
705endef
706
707###########################################################
708## Declare the license metadata for non-module target $(1).
709##
710## $(2) -- license kinds e.g. SPDX-license-identifier-Apache-2.0
711## $(3) -- license conditions e.g. notice by_exception_only
712## $(4) -- license text filenames (notices)
713## $(5) -- package name
714## $(6) -- project path
715###########################################################
716define declare-license-metadata
717$(strip \
718  $(eval _tgt := $(subst //,/,$(strip $(1)))) \
719  $(eval ALL_NON_MODULES += $(_tgt)) \
720  $(eval ALL_NON_MODULES.$(_tgt).LICENSE_KINDS := $(strip $(2))) \
721  $(eval ALL_NON_MODULES.$(_tgt).LICENSE_CONDITIONS := $(strip $(3))) \
722  $(eval ALL_NON_MODULES.$(_tgt).NOTICES := $(strip $(4))) \
723  $(eval ALL_NON_MODULES.$(_tgt).LICENSE_PACKAGE_NAME := $(strip $(5))) \
724  $(eval ALL_NON_MODULES.$(_tgt).PATH := $(strip $(6))) \
725)
726endef
727
728###########################################################
729## Declare that non-module targets copied from project $(1) and
730## optionally ending in $(2) have the following license
731## metadata:
732##
733## $(3) -- license kinds e.g. SPDX-license-identifier-Apache-2.0
734## $(4) -- license conditions e.g. notice by_exception_only
735## $(5) -- license text filenames (notices)
736## $(6) -- package name
737###########################################################
738define declare-copy-files-license-metadata
739$(strip \
740  $(foreach _pair,$(filter $(1)%$(2),$(PRODUCT_COPY_FILES)),$(eval $(call declare-license-metadata,$(PRODUCT_OUT)/$(call word-colon,2,$(_pair)),$(3),$(4),$(5),$(6),$(1)))) \
741)
742endef
743
744###########################################################
745## Declare the license metadata for non-module container-type target $(1).
746##
747## Container-type targets are targets like .zip files that
748## merely aggregate other files.
749##
750## $(2) -- license kinds e.g. SPDX-license-identifier-Apache-2.0
751## $(3) -- license conditions e.g. notice by_exception_only
752## $(4) -- license text filenames (notices)
753## $(5) -- package name
754## $(6) -- project path
755###########################################################
756define declare-container-license-metadata
757$(strip \
758  $(eval _tgt := $(subst //,/,$(strip $(1)))) \
759  $(eval ALL_NON_MODULES += $(_tgt)) \
760  $(eval ALL_NON_MODULES.$(_tgt).LICENSE_KINDS := $(strip $(2))) \
761  $(eval ALL_NON_MODULES.$(_tgt).LICENSE_CONDITIONS := $(strip $(3))) \
762  $(eval ALL_NON_MODULES.$(_tgt).NOTICES := $(strip $(4))) \
763  $(eval ALL_NON_MODULES.$(_tgt).LICENSE_PACKAGE_NAME := $(strip $(5))) \
764  $(eval ALL_NON_MODULES.$(_tgt).PATH := $(strip $(6))) \
765  $(eval ALL_NON_MODULES.$(_tgt).IS_CONTAINER := true) \
766)
767endef
768
769###########################################################
770## Declare that non-module target $(1) is a non-copyrightable file.
771##
772## e.g. an information-only file merely listing other files.
773###########################################################
774define declare-0p-target
775$(strip \
776  $(eval _tgt := $(subst //,/,$(strip $(1)))) \
777  $(eval ALL_0P_TARGETS += $(_tgt)) \
778)
779endef
780
781###########################################################
782## Declare that non-module targets copied from project $(1) and
783## optionally ending in $(2) are non-copyrightable files.
784##
785## e.g. an information-only file merely listing other files.
786###########################################################
787define declare-0p-copy-files
788$(strip \
789  $(foreach _pair,$(filter $(1)%$(2),$(PRODUCT_COPY_FILES)),$(eval $(call declare-0p-target,$(PRODUCT_OUT)/$(call word-colon,2,$(_pair))))) \
790)
791endef
792
793###########################################################
794## Declare non-module target $(1) to have a first-party license
795## (Android Apache 2.0)
796##
797## $(2) -- project path
798###########################################################
799define declare-1p-target
800$(call declare-license-metadata,$(1),SPDX-license-identifier-Apache-2.0,notice,build/soong/licenses/LICENSE,Android,$(2))
801endef
802
803###########################################################
804## Declare that non-module targets copied from project $(1) and
805## optionally ending in $(2) are first-party licensed
806## (Android Apache 2.0)
807###########################################################
808define declare-1p-copy-files
809$(foreach _pair,$(filter $(1)%$(2),$(PRODUCT_COPY_FILES)),$(call declare-1p-target,$(PRODUCT_OUT)/$(call word-colon,2,$(_pair)),$(1)))
810endef
811
812###########################################################
813## Declare non-module container-type target $(1) to have a
814## first-party license (Android Apache 2.0).
815##
816## Container-type targets are targets like .zip files that
817## merely aggregate other files.
818##
819## $92) -- project path
820###########################################################
821define declare-1p-container
822$(call declare-container-license-metadata,$(1),SPDX-license-identifier-Apache-2.0,notice,build/soong/licenses/LICENSE,Android,$(2))
823endef
824
825###########################################################
826## Declare license dependencies $(2) for non-module target $(1)
827###########################################################
828define declare-license-deps
829$(strip \
830  $(eval _tgt := $(strip $(1))) \
831  $(eval ALL_NON_MODULES += $(_tgt)) \
832  $(eval ALL_NON_MODULES.$(_tgt).DEPENDENCIES := $(strip $(ALL_NON_MODULES.$(_tgt).DEPENDENCIES) $(2))) \
833)
834endef
835
836###########################################################
837## Declare license dependencies $(2) for non-module container-type target $(1)
838##
839## Container-type targets are targets like .zip files that
840## merely aggregate other files.
841##
842## $(3) -- root mappings space-separated source:target
843###########################################################
844define declare-container-license-deps
845$(strip \
846  $(eval _tgt := $(strip $(1))) \
847  $(eval ALL_NON_MODULES += $(_tgt)) \
848  $(eval ALL_NON_MODULES.$(_tgt).DEPENDENCIES := $(strip $(ALL_NON_MODULES.$(_tgt).DEPENDENCIES) $(2))) \
849  $(eval ALL_NON_MODULES.$(_tgt).IS_CONTAINER := true) \
850  $(eval ALL_NON_MODULES.$(_tgt).ROOT_MAPPINGS := $(strip $(ALL_NON_MODULES.$(_tgt).ROOT_MAPPINGS) $(3))) \
851)
852endef
853
854###########################################################
855## Declares the rule to report targets with no license metadata.
856###########################################################
857define report-missing-licenses-rule
858.PHONY: reportmissinglicenses
859reportmissinglicenses: PRIVATE_NON_MODULES:=$(sort $(NON_MODULES_WITHOUT_LICENSE_METADATA))
860reportmissinglicenses: PRIVATE_COPIED_FILES:=$(sort $(filter $(NON_MODULES_WITHOUT_LICENSE_METADATA),$(foreach _pair,$(PRODUCT_COPY_FILES), $(PRODUCT_OUT)/$(call word-colon,2,$(_pair)))))
861reportmissinglicenses:
862	@echo Reporting $$(words $$(PRIVATE_NON_MODULES)) targets without license metadata
863	$$(foreach t,$$(PRIVATE_NON_MODULES),if ! [ -h $$(t) ]; then echo No license metadata for $$(t) >&2; fi;)
864	$$(foreach t,$$(PRIVATE_COPIED_FILES),if ! [ -h $$(t) ]; then echo No license metadata for copied file $$(t) >&2; fi;)
865
866endef
867
868
869###########################################################
870# Returns the unique list of built license metadata files.
871###########################################################
872define all-license-metadata
873$(sort \
874  $(foreach t,$(ALL_NON_MODULES),$(if $(filter 0p,$(ALL_TARGETS.$(t).META_LIC)),, $(ALL_TARGETS.$(t).META_LIC))) \
875  $(foreach m,$(ALL_MODULES), $(ALL_MODULES.$(m).META_LIC)) \
876)
877endef
878
879###########################################################
880# Declares the rule to report all library names used in any notice files.
881###########################################################
882define report-all-notice-library-names-rule
883$(strip $(eval _all := $(call all-license-metadata)))
884
885.PHONY: reportallnoticelibrarynames
886reportallnoticelibrarynames: PRIVATE_LIST_FILE := $(call license-metadata-dir)/filelist
887reportallnoticelibrarynames: | $(COMPLIANCENOTICE_SHIPPEDLIBS)
888reportallnoticelibrarynames: $(_all)
889	@echo Reporting notice library names for at least $$(words $(_all)) license metadata files
890	$(hide) rm -f $$(PRIVATE_LIST_FILE)
891	$(hide) mkdir -p $$(dir $$(PRIVATE_LIST_FILE))
892	$(hide) find out -name '*meta_lic' -type f -printf '"%p"\n' >$$(PRIVATE_LIST_FILE)
893	OUT_DIR=$(OUT_DIR) $(COMPLIANCENOTICE_SHIPPEDLIBS) @$$(PRIVATE_LIST_FILE)
894endef
895
896###########################################################
897# Declares the rule to build all license metadata.
898###########################################################
899define build-all-license-metadata-rule
900$(strip $(eval _all := $(call all-license-metadata)))
901
902.PHONY: alllicensemetadata
903alllicensemetadata: $(_all)
904	@echo Building all $(words $(_all)) license metadata files
905endef
906
907
908###########################################################
909## Declares a license metadata build rule for ALL_MODULES
910###########################################################
911define build-license-metadata
912$(strip \
913  $(strip $(eval _dir := $(call license-metadata-dir))) \
914  $(foreach t,$(sort $(ALL_0P_TARGETS)), \
915    $(eval ALL_TARGETS.$(t).META_LIC := 0p) \
916  ) \
917  $(foreach t,$(sort $(ALL_NON_MODULES)), \
918    $(eval ALL_TARGETS.$(t).META_LIC := $(call append-path,$(_dir),$(patsubst $(OUT_DIR)%,out%,$(t).meta_lic))) \
919  ) \
920  $(foreach t,$(sort $(ALL_NON_MODULES)),$(eval $(call non-module-license-metadata-rule,$(t)))) \
921  $(foreach m,$(sort $(ALL_MODULES)),$(eval $(call license-metadata-rule,$(m)))) \
922  $(eval $(call report-missing-licenses-rule)) \
923  $(eval $(call report-all-notice-library-names-rule)) \
924  $(eval $(call build-all-license-metadata-rule)))
925endef
926
927###########################################################
928## Returns correct _idfPrefix from the list:
929##   { HOST, HOST_CROSS, TARGET }
930###########################################################
931# the following rules checked in order:
932# ($1 is in {HOST_CROSS} => $1;
933# ($1 is empty) => TARGET;
934# ($2 is not empty) => HOST_CROSS;
935# => HOST;
936define find-idf-prefix
937$(strip \
938    $(eval _idf_pfx_:=$(strip $(filter HOST_CROSS,$(1)))) \
939    $(eval _idf_pfx_:=$(if $(strip $(1)),$(if $(_idf_pfx_),$(_idf_pfx_),$(if $(strip $(2)),HOST_CROSS,HOST)),TARGET)) \
940    $(_idf_pfx_)
941)
942endef
943
944###########################################################
945## The intermediates directory.  Where object files go for
946## a given target.  We could technically get away without
947## the "_intermediates" suffix on the directory, but it's
948## nice to be able to grep for that string to find out if
949## anyone's abusing the system.
950###########################################################
951
952# $(1): target class, like "APPS"
953# $(2): target name, like "NotePad"
954# $(3): { HOST, HOST_CROSS, <empty (TARGET)>, <other non-empty (HOST)> }
955# $(4): if non-empty, force the intermediates to be COMMON
956# $(5): if non-empty, force the intermediates to be for the 2nd arch
957# $(6): if non-empty, force the intermediates to be for the host cross os
958define intermediates-dir-for
959$(strip \
960    $(eval _idfClass := $(strip $(1))) \
961    $(if $(_idfClass),, \
962        $(error $(LOCAL_PATH): Class not defined in call to intermediates-dir-for)) \
963    $(eval _idfName := $(strip $(2))) \
964    $(if $(_idfName),, \
965        $(error $(LOCAL_PATH): Name not defined in call to intermediates-dir-for)) \
966    $(eval _idfPrefix := $(call find-idf-prefix,$(3),$(6))) \
967    $(eval _idf2ndArchPrefix := $(if $(strip $(5)),$(TARGET_2ND_ARCH_VAR_PREFIX))) \
968    $(if $(filter $(_idfPrefix)_$(_idfClass),$(COMMON_MODULE_CLASSES))$(4), \
969        $(eval _idfIntBase := $($(_idfPrefix)_OUT_COMMON_INTERMEDIATES)) \
970      ,$(if $(filter $(_idfClass),$(PER_ARCH_MODULE_CLASSES)),\
971          $(eval _idfIntBase := $($(_idf2ndArchPrefix)$(_idfPrefix)_OUT_INTERMEDIATES)) \
972       ,$(eval _idfIntBase := $($(_idfPrefix)_OUT_INTERMEDIATES)) \
973       ) \
974     ) \
975    $(_idfIntBase)/$(_idfClass)/$(_idfName)_intermediates \
976)
977endef
978
979# Uses LOCAL_MODULE_CLASS, LOCAL_MODULE, and LOCAL_IS_HOST_MODULE
980# to determine the intermediates directory.
981#
982# $(1): if non-empty, force the intermediates to be COMMON
983# $(2): if non-empty, force the intermediates to be for the 2nd arch
984# $(3): if non-empty, force the intermediates to be for the host cross os
985define local-intermediates-dir
986$(strip \
987    $(if $(strip $(LOCAL_MODULE_CLASS)),, \
988        $(error $(LOCAL_PATH): LOCAL_MODULE_CLASS not defined before call to local-intermediates-dir)) \
989    $(if $(strip $(LOCAL_MODULE)),, \
990        $(error $(LOCAL_PATH): LOCAL_MODULE not defined before call to local-intermediates-dir)) \
991    $(call intermediates-dir-for,$(LOCAL_MODULE_CLASS),$(LOCAL_MODULE),$(if $(strip $(LOCAL_IS_HOST_MODULE)),HOST),$(1),$(2),$(3)) \
992)
993endef
994
995###########################################################
996## The generated sources directory.  Placing generated
997## source files directly in the intermediates directory
998## causes problems for multiarch builds, where there are
999## two intermediates directories for a single target. Put
1000## them in a separate directory, and they will be copied to
1001## each intermediates directory automatically.
1002###########################################################
1003
1004# $(1): target class, like "APPS"
1005# $(2): target name, like "NotePad"
1006# $(3): { HOST, HOST_CROSS, <empty (TARGET)>, <other non-empty (HOST)> }
1007# $(4): if non-empty, force the generated sources to be COMMON
1008define generated-sources-dir-for
1009$(strip \
1010    $(eval _idfClass := $(strip $(1))) \
1011    $(if $(_idfClass),, \
1012        $(error $(LOCAL_PATH): Class not defined in call to generated-sources-dir-for)) \
1013    $(eval _idfName := $(strip $(2))) \
1014    $(if $(_idfName),, \
1015        $(error $(LOCAL_PATH): Name not defined in call to generated-sources-dir-for)) \
1016    $(eval _idfPrefix := $(call find-idf-prefix,$(3),)) \
1017    $(if $(filter $(_idfPrefix)_$(_idfClass),$(COMMON_MODULE_CLASSES))$(4), \
1018        $(eval _idfIntBase := $($(_idfPrefix)_OUT_COMMON_GEN)) \
1019      , \
1020        $(eval _idfIntBase := $($(_idfPrefix)_OUT_GEN)) \
1021     ) \
1022    $(_idfIntBase)/$(_idfClass)/$(_idfName)_intermediates \
1023)
1024endef
1025
1026# Uses LOCAL_MODULE_CLASS, LOCAL_MODULE, and LOCAL_IS_HOST_MODULE
1027# to determine the generated sources directory.
1028#
1029# $(1): if non-empty, force the intermediates to be COMMON
1030define local-generated-sources-dir
1031$(strip \
1032    $(if $(strip $(LOCAL_MODULE_CLASS)),, \
1033        $(error $(LOCAL_PATH): LOCAL_MODULE_CLASS not defined before call to local-generated-sources-dir)) \
1034    $(if $(strip $(LOCAL_MODULE)),, \
1035        $(error $(LOCAL_PATH): LOCAL_MODULE not defined before call to local-generated-sources-dir)) \
1036    $(call generated-sources-dir-for,$(LOCAL_MODULE_CLASS),$(LOCAL_MODULE),$(if $(strip $(LOCAL_IS_HOST_MODULE)),HOST),$(1)) \
1037)
1038endef
1039
1040###########################################################
1041## The packaging directory for a module.  Similar to intermedates, but
1042## in a location that will be wiped by an m installclean.
1043###########################################################
1044
1045# $(1): subdir in PACKAGING
1046# $(2): target class, like "APPS"
1047# $(3): target name, like "NotePad"
1048# $(4): { HOST, HOST_CROSS, <empty (TARGET)>, <other non-empty (HOST)> }
1049define packaging-dir-for
1050$(strip \
1051    $(eval _pdfClass := $(strip $(2))) \
1052    $(if $(_pdfClass),, \
1053        $(error $(LOCAL_PATH): Class not defined in call to generated-sources-dir-for)) \
1054    $(eval _pdfName := $(strip $(3))) \
1055    $(if $(_pdfName),, \
1056        $(error $(LOCAL_PATH): Name not defined in call to generated-sources-dir-for)) \
1057    $(call intermediates-dir-for,PACKAGING,$(1),$(4))/$(_pdfClass)/$(_pdfName)_intermediates \
1058)
1059endef
1060
1061# Uses LOCAL_MODULE_CLASS, LOCAL_MODULE, and LOCAL_IS_HOST_MODULE
1062# to determine the packaging directory.
1063#
1064# $(1): subdir in PACKAGING
1065define local-packaging-dir
1066$(strip \
1067    $(if $(strip $(LOCAL_MODULE_CLASS)),, \
1068        $(error $(LOCAL_PATH): LOCAL_MODULE_CLASS not defined before call to local-generated-sources-dir)) \
1069    $(if $(strip $(LOCAL_MODULE)),, \
1070        $(error $(LOCAL_PATH): LOCAL_MODULE not defined before call to local-generated-sources-dir)) \
1071    $(call packaging-dir-for,$(1),$(LOCAL_MODULE_CLASS),$(LOCAL_MODULE),$(if $(strip $(LOCAL_IS_HOST_MODULE)),HOST)) \
1072)
1073endef
1074
1075
1076###########################################################
1077## Convert a list of short module names (e.g., "framework", "Browser")
1078## into the list of files that are built for those modules.
1079## NOTE: this won't return reliable results until after all
1080## sub-makefiles have been included.
1081## $(1): target list
1082###########################################################
1083
1084define module-built-files
1085$(foreach module,$(1),$(ALL_MODULES.$(module).BUILT))
1086endef
1087
1088###########################################################
1089## Convert a list of short modules names (e.g., "framework", "Browser")
1090## into the list of files that are installed for those modules.
1091## NOTE: this won't return reliable results until after all
1092## sub-makefiles have been included.
1093## $(1): target list
1094###########################################################
1095
1096define module-installed-files
1097$(foreach module,$(1),$(ALL_MODULES.$(module).INSTALLED))
1098endef
1099
1100###########################################################
1101## Convert a list of short modules names (e.g., "framework", "Browser")
1102## into the list of files that are built *for the target* for those modules.
1103## NOTE: this won't return reliable results until after all
1104## sub-makefiles have been included.
1105## $(1): target list
1106###########################################################
1107
1108define module-target-built-files
1109$(foreach module,$(1),$(ALL_MODULES.$(module).TARGET_BUILT))
1110endef
1111
1112###########################################################
1113## Convert a list of short modules names (e.g., "framework", "Browser")
1114## into the list of files that should be used when linking
1115## against that module as a public API.
1116## TODO: Allow this for more than JAVA_LIBRARIES modules
1117## NOTE: this won't return reliable results until after all
1118## sub-makefiles have been included.
1119## $(1): target list
1120###########################################################
1121
1122define module-stubs-files
1123$(foreach module,$(1),$(if $(filter $(module),$(JAVA_SDK_LIBRARIES)),\
1124$(call java-lib-files,$(module).stubs),$(ALL_MODULES.$(module).STUBS)))
1125endef
1126
1127###########################################################
1128## Evaluates to the timestamp file for a doc module, which
1129## is the dependency that should be used.
1130## $(1): doc module
1131###########################################################
1132
1133define doc-timestamp-for
1134$(OUT_DOCS)/$(strip $(1))-timestamp
1135endef
1136
1137
1138###########################################################
1139## Convert "core ext framework" to "out/.../javalib.jar ..."
1140## $(1): library list
1141## $(2): Non-empty if IS_HOST_MODULE
1142###########################################################
1143
1144# Get the jar files (you can pass to "javac -classpath") of static or shared
1145# Java libraries that you want to link against.
1146# $(1): library name list
1147# $(2): Non-empty if IS_HOST_MODULE
1148define java-lib-files
1149$(foreach lib,$(1),$(call intermediates-dir-for,JAVA_LIBRARIES,$(lib),$(2),COMMON)/classes.jar)
1150endef
1151
1152# Get the header jar files (you can pass to "javac -classpath") of static or shared
1153# Java libraries that you want to link against.
1154# $(1): library name list
1155# $(2): Non-empty if IS_HOST_MODULE
1156ifneq ($(TURBINE_ENABLED),false)
1157define java-lib-header-files
1158$(foreach lib,$(1),$(call intermediates-dir-for,JAVA_LIBRARIES,$(lib),$(2),COMMON)/classes-header.jar)
1159endef
1160else
1161define java-lib-header-files
1162$(call java-lib-files,$(1),$(2))
1163endef
1164endif
1165
1166# Get the dependency files (you can put on the right side of "|" of a build rule)
1167# of the Java libraries.
1168# $(1): library name list
1169# $(2): Non-empty if IS_HOST_MODULE
1170# Historically for target Java libraries we used a different file (javalib.jar)
1171# as the dependency.
1172# Now we can use classes.jar as dependency, so java-lib-deps is the same
1173# as java-lib-files.
1174define java-lib-deps
1175$(call java-lib-files,$(1),$(2))
1176endef
1177
1178# Get the jar files (you can pass to "javac -classpath") of static or shared
1179# APK libraries that you want to link against.
1180# $(1): library name list
1181define app-lib-files
1182$(foreach lib,$(1),$(call intermediates-dir-for,APPS,$(lib),,COMMON)/classes.jar)
1183endef
1184
1185# Get the header jar files (you can pass to "javac -classpath") of static or shared
1186# APK libraries that you want to link against.
1187# $(1): library name list
1188ifneq ($(TURBINE_ENABLED),false)
1189define app-lib-header-files
1190$(foreach lib,$(1),$(call intermediates-dir-for,APPS,$(lib),,COMMON)/classes-header.jar)
1191endef
1192else
1193define app-lib-header-files
1194$(call app-lib-files,$(1))
1195endef
1196endif
1197
1198# Get the exported-sdk-libs files which collectively give you the list of exported java sdk
1199# lib names that are (transitively) exported from the given set of java libs
1200# $(1): library name list
1201define exported-sdk-libs-files
1202$(foreach lib,$(1),$(call intermediates-dir-for,JAVA_LIBRARIES,$(lib),,COMMON)/exported-sdk-libs)
1203endef
1204
1205###########################################################
1206## MODULE_TAG set operations
1207###########################################################
1208
1209# Given a list of tags, return the targets that specify
1210# any of those tags.
1211# $(1): tag list
1212define modules-for-tag-list
1213$(sort $(foreach tag,$(1),$(foreach m,$(ALL_MODULE_NAME_TAGS.$(tag)),$(ALL_MODULES.$(m).INSTALLED))))
1214endef
1215
1216# Same as modules-for-tag-list, but operates on
1217# ALL_MODULE_NAME_TAGS.
1218# $(1): tag list
1219define module-names-for-tag-list
1220$(sort $(foreach tag,$(1),$(ALL_MODULE_NAME_TAGS.$(tag))))
1221endef
1222
1223# Given an accept and reject list, find the matching
1224# set of targets.  If a target has multiple tags and
1225# any of them are rejected, the target is rejected.
1226# Reject overrides accept.
1227# $(1): list of tags to accept
1228# $(2): list of tags to reject
1229#TODO(dbort): do $(if $(strip $(1)),$(1),$(ALL_MODULE_TAGS))
1230#TODO(jbq): as of 20100106 nobody uses the second parameter
1231define get-tagged-modules
1232$(filter-out \
1233  $(call modules-for-tag-list,$(2)), \
1234    $(call modules-for-tag-list,$(1)))
1235endef
1236
1237###########################################################
1238## Append a leaf to a base path.  Properly deals with
1239## base paths ending in /.
1240##
1241## $(1): base path
1242## $(2): leaf path
1243###########################################################
1244
1245define append-path
1246$(subst //,/,$(1)/$(2))
1247endef
1248
1249
1250###########################################################
1251## Color-coded warnings and errors
1252## Use echo-(warning|error) in a build rule
1253## Use pretty-(warning|error) instead of $(warning)/$(error)
1254###########################################################
1255ESC_BOLD := \033[1m
1256ESC_WARNING := \033[35m
1257ESC_ERROR := \033[31m
1258ESC_RESET := \033[0m
1259
1260# $(1): path (and optionally line) information
1261# $(2): message to print
1262define echo-warning
1263echo -e "$(ESC_BOLD)$(1): $(ESC_WARNING)warning:$(ESC_RESET)$(ESC_BOLD)" '$(subst ','\'',$(2))'  "$(ESC_RESET)" >&2
1264endef
1265
1266# $(1): path (and optionally line) information
1267# $(2): message to print
1268define echo-error
1269echo -e "$(ESC_BOLD)$(1): $(ESC_ERROR)error:$(ESC_RESET)$(ESC_BOLD)" '$(subst ','\'',$(2))'  "$(ESC_RESET)" >&2
1270endef
1271
1272###########################################################
1273## Legacy showcommands compatibility
1274###########################################################
1275
1276define pretty
1277@echo $1
1278endef
1279
1280###########################################################
1281## Commands for including the dependency files the compiler generates
1282###########################################################
1283# $(1): the .P file
1284# $(2): the main build target
1285define include-depfile
1286$(eval $(2) : .KATI_DEPFILE := $1)
1287endef
1288
1289# $(1): object files
1290define include-depfiles-for-objs
1291$(foreach obj, $(1), $(call include-depfile, $(obj:%.o=%.d), $(obj)))
1292endef
1293
1294###########################################################
1295## Track source files compiled to objects
1296###########################################################
1297# $(1): list of sources
1298# $(2): list of matching objects
1299define track-src-file-obj
1300$(eval $(call _track-src-file-obj,$(1)))
1301endef
1302define _track-src-file-obj
1303i := w
1304$(foreach s,$(1),
1305my_tracked_src_files += $(s)
1306my_src_file_obj_$(s) := $$(word $$(words $$(i)),$$(2))
1307i += w)
1308endef
1309
1310# $(1): list of sources
1311# $(2): list of matching generated sources
1312define track-src-file-gen
1313$(eval $(call _track-src-file-gen,$(2)))
1314endef
1315define _track-src-file-gen
1316i := w
1317$(foreach s,$(1),
1318my_tracked_gen_files += $(s)
1319my_src_file_gen_$(s) := $$(word $$(words $$(i)),$$(1))
1320i += w)
1321endef
1322
1323# $(1): list of generated sources
1324# $(2): list of matching objects
1325define track-gen-file-obj
1326$(call track-src-file-obj,$(foreach f,$(1),\
1327  $(or $(my_src_file_gen_$(f)),$(f))),$(2))
1328endef
1329
1330###########################################################
1331## Commands for running lex
1332###########################################################
1333
1334define transform-l-to-c-or-cpp
1335@echo "Lex: $(PRIVATE_MODULE) <= $<"
1336@mkdir -p $(dir $@)
1337M4=$(M4) $(LEX) -o$@ $<
1338endef
1339
1340###########################################################
1341## Commands for running yacc
1342##
1343###########################################################
1344
1345define transform-y-to-c-or-cpp
1346@echo "Yacc: $(PRIVATE_MODULE) <= $<"
1347@mkdir -p $(dir $@)
1348M4=$(M4) $(YACC) $(PRIVATE_YACCFLAGS) \
1349  --defines=$(basename $@).h \
1350  -o $@ $<
1351endef
1352
1353###########################################################
1354## Commands to compile RenderScript to Java
1355###########################################################
1356
1357## Merge multiple .d files generated by llvm-rs-cc. This is necessary
1358## because ninja can handle only a single depfile per build target.
1359## .d files generated by llvm-rs-cc define .stamp, .bc, and optionally
1360## .java as build targets. However, there's no way to let ninja know
1361## dependencies to .bc files and .java files, so we give up build
1362## targets for them. As we write the .stamp file as the target by
1363## ourselves, the awk script removes the first lines before the colon
1364## and append a backslash to the last line to concatenate contents of
1365## multiple files.
1366# $(1): .d files to be merged
1367# $(2): merged .d file
1368define _merge-renderscript-d
1369$(hide) echo '$@: $(backslash)' > $2
1370$(foreach d,$1, \
1371  $(hide) awk 'start { sub(/( \\)?$$/, " \\"); print } /:/ { start=1 }' < $d >> $2$(newline))
1372$(hide) echo >> $2
1373endef
1374
1375# b/37755219
1376RS_CC_ASAN_OPTIONS := ASAN_OPTIONS=detect_leaks=0:detect_container_overflow=0
1377
1378define transform-renderscripts-to-java-and-bc
1379@echo "RenderScript: $(PRIVATE_MODULE) <= $(PRIVATE_RS_SOURCE_FILES)"
1380$(hide) rm -rf $(PRIVATE_RS_OUTPUT_DIR)
1381$(hide) mkdir -p $(PRIVATE_RS_OUTPUT_DIR)/res/raw
1382$(hide) mkdir -p $(PRIVATE_RS_OUTPUT_DIR)/src
1383$(hide) $(RS_CC_ASAN_OPTIONS) $(PRIVATE_RS_CC) \
1384  -o $(PRIVATE_RS_OUTPUT_DIR)/res/raw \
1385  -p $(PRIVATE_RS_OUTPUT_DIR)/src \
1386  -d $(PRIVATE_RS_OUTPUT_DIR) \
1387  -a $@ -MD \
1388  $(addprefix -target-api , $(PRIVATE_RS_TARGET_API)) \
1389  $(PRIVATE_RS_FLAGS) \
1390  $(foreach inc,$(PRIVATE_RS_INCLUDES),$(addprefix -I , $(inc))) \
1391  $(PRIVATE_RS_SOURCE_FILES)
1392$(SOONG_ZIP) -o $@ -C $(PRIVATE_RS_OUTPUT_DIR)/src -D $(PRIVATE_RS_OUTPUT_DIR)/src
1393$(SOONG_ZIP) -o $(PRIVATE_RS_OUTPUT_RES_ZIP) -C $(PRIVATE_RS_OUTPUT_DIR)/res -D $(PRIVATE_RS_OUTPUT_DIR)/res
1394$(call _merge-renderscript-d,$(PRIVATE_DEP_FILES),$@.d)
1395endef
1396
1397define transform-bc-to-so
1398@echo "Renderscript compatibility: $(notdir $@) <= $(notdir $<)"
1399$(hide) mkdir -p $(dir $@)
1400$(hide) $(BCC_COMPAT) -O3 -o $(dir $@)/$(notdir $(<:.bc=.o)) -fPIC -shared \
1401  -rt-path $(RS_PREBUILT_CLCORE) -mtriple $(RS_COMPAT_TRIPLE) $<
1402$(hide) $(PRIVATE_CXX_LINK) -fuse-ld=lld -target $(CLANG_TARGET_TRIPLE) -shared -Wl,-soname,$(notdir $@) -nostdlib \
1403  -Wl,-rpath,\$$ORIGIN/../lib \
1404  $(dir $@)/$(notdir $(<:.bc=.o)) \
1405  $(RS_PREBUILT_COMPILER_RT) \
1406  -o $@ $(CLANG_TARGET_GLOBAL_LLDFLAGS) -Wl,--hash-style=sysv \
1407  -L $(SOONG_OUT_DIR)/ndk/platforms/android-$(PRIVATE_SDK_VERSION)/arch-$(TARGET_ARCH)/usr/lib64 \
1408  -L $(SOONG_OUT_DIR)/ndk/platforms/android-$(PRIVATE_SDK_VERSION)/arch-$(TARGET_ARCH)/usr/lib \
1409  $(call intermediates-dir-for,SHARED_LIBRARIES,libRSSupport)/libRSSupport.so \
1410  -lm -lc
1411endef
1412
1413###########################################################
1414## Commands to compile RenderScript to C++
1415###########################################################
1416
1417define transform-renderscripts-to-cpp-and-bc
1418@echo "RenderScript: $(PRIVATE_MODULE) <= $(PRIVATE_RS_SOURCE_FILES)"
1419$(hide) rm -rf $(PRIVATE_RS_OUTPUT_DIR)
1420$(hide) mkdir -p $(PRIVATE_RS_OUTPUT_DIR)/
1421$(hide) $(RS_CC_ASAN_OPTIONS) $(PRIVATE_RS_CC) \
1422  -o $(PRIVATE_RS_OUTPUT_DIR)/ \
1423  -d $(PRIVATE_RS_OUTPUT_DIR) \
1424  -a $@ -MD \
1425  -reflect-c++ \
1426  $(addprefix -target-api , $(PRIVATE_RS_TARGET_API)) \
1427  $(PRIVATE_RS_FLAGS) \
1428  $(addprefix -I , $(PRIVATE_RS_INCLUDES)) \
1429  $(PRIVATE_RS_SOURCE_FILES)
1430$(call _merge-renderscript-d,$(PRIVATE_DEP_FILES),$@.d)
1431$(hide) mkdir -p $(dir $@)
1432$(hide) touch $@
1433endef
1434
1435
1436###########################################################
1437## Commands for running aidl
1438###########################################################
1439
1440define transform-aidl-to-java
1441@mkdir -p $(dir $@)
1442@echo "Aidl: $(PRIVATE_MODULE) <= $<"
1443$(hide) $(AIDL) -d$(patsubst %.java,%.P,$@) $(PRIVATE_AIDL_FLAGS) $< $@
1444endef
1445#$(AIDL) $(PRIVATE_AIDL_FLAGS) $< - | indent -nut -br -npcs -l1000 > $@
1446
1447define transform-aidl-to-cpp
1448@mkdir -p $(dir $@)
1449@mkdir -p $(PRIVATE_HEADER_OUTPUT_DIR)
1450@echo "Generating C++ from AIDL: $(PRIVATE_MODULE) <= $<"
1451$(hide) $(AIDL_CPP) -d$(basename $@).aidl.d --ninja $(PRIVATE_AIDL_FLAGS) \
1452    $< $(PRIVATE_HEADER_OUTPUT_DIR) $@
1453endef
1454
1455## Given a .aidl file path, generate the rule to compile it a .java file
1456# $(1): a .aidl source file
1457# $(2): a directory to place the generated .java files in
1458# $(3): name of a variable to add the path to the generated source file to
1459#
1460# You must call this with $(eval).
1461define define-aidl-java-rule
1462define-aidl-java-rule-src := $(patsubst %.aidl,%.java,$(subst ../,dotdot/,$(addprefix $(2)/,$(1))))
1463$$(define-aidl-java-rule-src) : $(call clean-path,$(LOCAL_PATH)/$(1)) $(AIDL)
1464	$$(transform-aidl-to-java)
1465$(3) += $$(define-aidl-java-rule-src)
1466endef
1467
1468## Given a .aidl file path generate the rule to compile it a .cpp file.
1469# $(1): a .aidl source file
1470# $(2): a directory to place the generated .cpp files in
1471# $(3): name of a variable to add the path to the generated source file to
1472#
1473# You must call this with $(eval).
1474define define-aidl-cpp-rule
1475define-aidl-cpp-rule-src := $(patsubst %.aidl,%$(LOCAL_CPP_EXTENSION),$(subst ../,dotdot/,$(addprefix $(2)/,$(1))))
1476$$(define-aidl-cpp-rule-src) : $(call clean-path,$(LOCAL_PATH)/$(1)) $(AIDL_CPP)
1477	$$(transform-aidl-to-cpp)
1478$(3) += $$(define-aidl-cpp-rule-src)
1479endef
1480
1481###########################################################
1482## Commands for running vts
1483###########################################################
1484
1485define transform-vts-to-cpp
1486@mkdir -p $(dir $@)
1487@mkdir -p $(PRIVATE_HEADER_OUTPUT_DIR)
1488@echo "Generating C++ from VTS: $(PRIVATE_MODULE) <= $<"
1489$(hide) $(VTSC) -TODO_b/120496070 $(PRIVATE_VTS_FLAGS) \
1490    $< $(PRIVATE_HEADER_OUTPUT_DIR) $@
1491endef
1492
1493## Given a .vts file path generate the rule to compile it a .cpp file.
1494# $(1): a .vts source file
1495# $(2): a directory to place the generated .cpp files in
1496# $(3): name of a variable to add the path to the generated source file to
1497#
1498# You must call this with $(eval).
1499define define-vts-cpp-rule
1500define-vts-cpp-rule-src := $(patsubst %.vts,%$(LOCAL_CPP_EXTENSION),$(subst ../,dotdot/,$(addprefix $(2)/,$(1))))
1501$$(define-vts-cpp-rule-src) : $(LOCAL_PATH)/$(1) $(VTSC)
1502	$$(transform-vts-to-cpp)
1503$(3) += $$(define-vts-cpp-rule-src)
1504endef
1505
1506###########################################################
1507## Commands for running java-event-log-tags.py
1508###########################################################
1509
1510define transform-logtags-to-java
1511@mkdir -p $(dir $@)
1512@echo "logtags: $@ <= $<"
1513$(hide) $(JAVATAGS) -o $@ $< $(PRIVATE_MERGED_TAG)
1514endef
1515
1516
1517###########################################################
1518## Commands for running protoc to compile .proto into .java
1519###########################################################
1520
1521define transform-proto-to-java
1522@mkdir -p $(dir $@)
1523@echo "Protoc: $@ <= $(PRIVATE_PROTO_SRC_FILES)"
1524@rm -rf $(PRIVATE_PROTO_JAVA_OUTPUT_DIR)
1525@mkdir -p $(PRIVATE_PROTO_JAVA_OUTPUT_DIR)
1526$(hide) for f in $(PRIVATE_PROTO_SRC_FILES); do \
1527        $(PROTOC) \
1528        $(addprefix --proto_path=, $(PRIVATE_PROTO_INCLUDES)) \
1529        $(PRIVATE_PROTO_JAVA_OUTPUT_OPTION)="$(PRIVATE_PROTO_JAVA_OUTPUT_PARAMS):$(PRIVATE_PROTO_JAVA_OUTPUT_DIR)" \
1530        $(PRIVATE_PROTOC_FLAGS) \
1531        $$f || exit 33; \
1532        done
1533$(SOONG_ZIP) -o $@ -C $(PRIVATE_PROTO_JAVA_OUTPUT_DIR) -D $(PRIVATE_PROTO_JAVA_OUTPUT_DIR)
1534endef
1535
1536######################################################################
1537## Commands for running protoc to compile .proto into .pb.cc (or.pb.c) and .pb.h
1538######################################################################
1539
1540define transform-proto-to-cc
1541@echo "Protoc: $@ <= $<"
1542@mkdir -p $(dir $@)
1543$(hide) \
1544  $(PROTOC) \
1545  $(addprefix --proto_path=, $(PRIVATE_PROTO_INCLUDES)) \
1546  $(PRIVATE_PROTOC_FLAGS) \
1547  $<
1548@# aprotoc outputs only .cc. Rename it to .cpp if necessary.
1549$(if $(PRIVATE_RENAME_CPP_EXT),\
1550  $(hide) mv $(basename $@).cc $@)
1551endef
1552
1553###########################################################
1554## Helper to set include paths form transform-*-to-o
1555###########################################################
1556define c-includes
1557$(addprefix -I , $(PRIVATE_C_INCLUDES)) \
1558$(foreach i,$(PRIVATE_IMPORTED_INCLUDES),$(EXPORTS.$(i)))\
1559$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),,\
1560    $(addprefix -I ,\
1561        $(filter-out $(PRIVATE_C_INCLUDES), \
1562            $(PRIVATE_GLOBAL_C_INCLUDES))) \
1563    $(addprefix -isystem ,\
1564        $(filter-out $(PRIVATE_C_INCLUDES), \
1565            $(PRIVATE_GLOBAL_C_SYSTEM_INCLUDES))))
1566endef
1567
1568###########################################################
1569## Commands for running gcc to compile a C++ file
1570###########################################################
1571
1572define transform-cpp-to-o-compiler-args
1573$(c-includes) \
1574-c \
1575$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1576    $(PRIVATE_TARGET_GLOBAL_CFLAGS) \
1577    $(PRIVATE_TARGET_GLOBAL_CPPFLAGS) \
1578    $(PRIVATE_ARM_CFLAGS) \
1579 ) \
1580$(PRIVATE_RTTI_FLAG) \
1581$(PRIVATE_CFLAGS) \
1582$(PRIVATE_CPPFLAGS) \
1583$(PRIVATE_DEBUG_CFLAGS) \
1584$(PRIVATE_CFLAGS_NO_OVERRIDE) \
1585$(PRIVATE_CPPFLAGS_NO_OVERRIDE)
1586endef
1587
1588# PATH_TO_CLANG_TIDY is defined in build/soong
1589define call-clang-tidy
1590$(PATH_TO_CLANG_TIDY) \
1591  $(PRIVATE_TIDY_FLAGS) \
1592  -checks=$(PRIVATE_TIDY_CHECKS)
1593endef
1594
1595define clang-tidy-cpp
1596$(hide) $(call-clang-tidy) $< -- $(transform-cpp-to-o-compiler-args)
1597endef
1598
1599ifneq (,$(filter 1 true,$(WITH_TIDY_ONLY)))
1600define transform-cpp-to-o
1601$(if $(PRIVATE_TIDY_CHECKS),
1602  @echo "$($(PRIVATE_PREFIX)DISPLAY) tidy $(PRIVATE_ARM_MODE) C++: $<"
1603  $(clang-tidy-cpp))
1604endef
1605else
1606define transform-cpp-to-o
1607@echo "$($(PRIVATE_PREFIX)DISPLAY) $(PRIVATE_ARM_MODE) C++: $(PRIVATE_MODULE) <= $<"
1608@mkdir -p $(dir $@)
1609$(if $(PRIVATE_TIDY_CHECKS),$(clang-tidy-cpp))
1610$(hide) $(RELATIVE_PWD) $(PRIVATE_CXX) \
1611  $(transform-cpp-to-o-compiler-args) \
1612  -MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1613endef
1614endif
1615
1616
1617###########################################################
1618## Commands for running gcc to compile a C file
1619###########################################################
1620
1621# $(1): extra flags
1622define transform-c-or-s-to-o-compiler-args
1623$(c-includes) \
1624-c \
1625$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1626    $(PRIVATE_TARGET_GLOBAL_CFLAGS) \
1627    $(PRIVATE_TARGET_GLOBAL_CONLYFLAGS) \
1628    $(PRIVATE_ARM_CFLAGS) \
1629 ) \
1630 $(1)
1631endef
1632
1633define transform-c-to-o-compiler-args
1634$(call transform-c-or-s-to-o-compiler-args, \
1635  $(PRIVATE_CFLAGS) \
1636  $(PRIVATE_CONLYFLAGS) \
1637  $(PRIVATE_DEBUG_CFLAGS) \
1638  $(PRIVATE_CFLAGS_NO_OVERRIDE))
1639endef
1640
1641define clang-tidy-c
1642$(hide) $(call-clang-tidy) $< -- $(transform-c-to-o-compiler-args)
1643endef
1644
1645ifneq (,$(filter 1 true,$(WITH_TIDY_ONLY)))
1646define transform-c-to-o
1647$(if $(PRIVATE_TIDY_CHECKS),
1648  @echo "$($(PRIVATE_PREFIX)DISPLAY) tidy $(PRIVATE_ARM_MODE) C: $<"
1649  $(clang-tidy-c))
1650endef
1651else
1652define transform-c-to-o
1653@echo "$($(PRIVATE_PREFIX)DISPLAY) $(PRIVATE_ARM_MODE) C: $(PRIVATE_MODULE) <= $<"
1654@mkdir -p $(dir $@)
1655$(if $(PRIVATE_TIDY_CHECKS),$(clang-tidy-c))
1656$(hide) $(RELATIVE_PWD) $(PRIVATE_CC) \
1657  $(transform-c-to-o-compiler-args) \
1658  -MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1659endef
1660endif
1661
1662define transform-s-to-o
1663@echo "$($(PRIVATE_PREFIX)DISPLAY) asm: $(PRIVATE_MODULE) <= $<"
1664@mkdir -p $(dir $@)
1665$(RELATIVE_PWD) $(PRIVATE_CC) \
1666  $(call transform-c-or-s-to-o-compiler-args, $(PRIVATE_ASFLAGS)) \
1667  -MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1668endef
1669
1670# YASM compilation
1671define transform-asm-to-o
1672@mkdir -p $(dir $@)
1673$(hide) $(YASM) \
1674    $(addprefix -I , $(PRIVATE_C_INCLUDES)) \
1675    $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_GLOBAL_YASM_FLAGS) \
1676    $(PRIVATE_ASFLAGS) \
1677    -o $@ $<
1678endef
1679
1680###########################################################
1681## Commands for running gcc to compile an Objective-C file
1682## This should never happen for target builds but this
1683## will error at build time.
1684###########################################################
1685
1686define transform-m-to-o
1687@echo "$($(PRIVATE_PREFIX)DISPLAY) ObjC: $(PRIVATE_MODULE) <= $<"
1688$(call transform-c-or-s-to-o, $(PRIVATE_CFLAGS) $(PRIVATE_DEBUG_CFLAGS))
1689endef
1690
1691###########################################################
1692## Commands for running gcc to compile a host C++ file
1693###########################################################
1694
1695define transform-host-cpp-to-o-compiler-args
1696$(c-includes) \
1697-c \
1698$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1699    $(PRIVATE_HOST_GLOBAL_CFLAGS) \
1700    $(PRIVATE_HOST_GLOBAL_CPPFLAGS) \
1701 ) \
1702$(PRIVATE_CFLAGS) \
1703$(PRIVATE_CPPFLAGS) \
1704$(PRIVATE_DEBUG_CFLAGS) \
1705$(PRIVATE_CFLAGS_NO_OVERRIDE) \
1706$(PRIVATE_CPPFLAGS_NO_OVERRIDE)
1707endef
1708
1709define clang-tidy-host-cpp
1710$(hide) $(call-clang-tidy) $< -- $(transform-host-cpp-to-o-compiler-args)
1711endef
1712
1713ifneq (,$(filter 1 true,$(WITH_TIDY_ONLY)))
1714define transform-host-cpp-to-o
1715$(if $(PRIVATE_TIDY_CHECKS),
1716  @echo "tidy $($(PRIVATE_PREFIX)DISPLAY) C++: $<"
1717  $(clang-tidy-host-cpp))
1718endef
1719else
1720define transform-host-cpp-to-o
1721@echo "$($(PRIVATE_PREFIX)DISPLAY) C++: $(PRIVATE_MODULE) <= $<"
1722@mkdir -p $(dir $@)
1723$(if $(PRIVATE_TIDY_CHECKS),$(clang-tidy-host-cpp))
1724$(hide) $(RELATIVE_PWD) $(PRIVATE_CXX) \
1725  $(transform-host-cpp-to-o-compiler-args) \
1726  -MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1727endef
1728endif
1729
1730
1731###########################################################
1732## Commands for running gcc to compile a host C file
1733###########################################################
1734
1735define transform-host-c-or-s-to-o-common-args
1736$(c-includes) \
1737-c \
1738$(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
1739    $(PRIVATE_HOST_GLOBAL_CFLAGS) \
1740    $(PRIVATE_HOST_GLOBAL_CONLYFLAGS) \
1741 )
1742endef
1743
1744# $(1): extra flags
1745define transform-host-c-or-s-to-o
1746@mkdir -p $(dir $@)
1747$(hide) $(RELATIVE_PWD) $(PRIVATE_CC) \
1748  $(transform-host-c-or-s-to-o-common-args) \
1749  $(1) \
1750  -MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1751endef
1752
1753define transform-host-c-to-o-compiler-args
1754  $(transform-host-c-or-s-to-o-common-args) \
1755  $(PRIVATE_CFLAGS) $(PRIVATE_CONLYFLAGS) \
1756  $(PRIVATE_DEBUG_CFLAGS) $(PRIVATE_CFLAGS_NO_OVERRIDE)
1757endef
1758
1759define clang-tidy-host-c
1760$(hide) $(call-clang-tidy) $< -- $(transform-host-c-to-o-compiler-args)
1761endef
1762
1763ifneq (,$(filter 1 true,$(WITH_TIDY_ONLY)))
1764define transform-host-c-to-o
1765$(if $(PRIVATE_TIDY_CHECKS),
1766  @echo "tidy $($(PRIVATE_PREFIX)DISPLAY) C: $<"
1767  $(clang-tidy-host-c))
1768endef
1769else
1770define transform-host-c-to-o
1771@echo "$($(PRIVATE_PREFIX)DISPLAY) C: $(PRIVATE_MODULE) <= $<"
1772@mkdir -p $(dir $@)
1773$(if $(PRIVATE_TIDY_CHECKS), $(clang-tidy-host-c))
1774$(hide) $(RELATIVE_PWD) $(PRIVATE_CC) \
1775  $(transform-host-c-to-o-compiler-args) \
1776  -MD -MF $(patsubst %.o,%.d,$@) -o $@ $<
1777endef
1778endif
1779
1780define transform-host-s-to-o
1781@echo "$($(PRIVATE_PREFIX)DISPLAY) asm: $(PRIVATE_MODULE) <= $<"
1782$(call transform-host-c-or-s-to-o, $(PRIVATE_ASFLAGS))
1783endef
1784
1785###########################################################
1786## Commands for running gcc to compile a host Objective-C file
1787###########################################################
1788
1789define transform-host-m-to-o
1790@echo "$($(PRIVATE_PREFIX)DISPLAY) ObjC: $(PRIVATE_MODULE) <= $<"
1791$(call transform-host-c-or-s-to-o, $(PRIVATE_CFLAGS) $(PRIVATE_DEBUG_CFLAGS) $(PRIVATE_CFLAGS_NO_OVERRIDE))
1792endef
1793
1794###########################################################
1795## Commands for running gcc to compile a host Objective-C++ file
1796###########################################################
1797
1798define transform-host-mm-to-o
1799$(transform-host-cpp-to-o)
1800endef
1801
1802
1803###########################################################
1804## Rules to compile a single C/C++ source with ../ in the path
1805###########################################################
1806# Replace "../" in object paths with $(DOTDOT_REPLACEMENT).
1807DOTDOT_REPLACEMENT := dotdot/
1808
1809## Rule to compile a C++ source file with ../ in the path.
1810## Must be called with $(eval).
1811# $(1): the C++ source file in LOCAL_SRC_FILES.
1812# $(2): the additional dependencies.
1813# $(3): the variable name to collect the output object file.
1814# $(4): the ninja pool to use for the rule
1815define compile-dotdot-cpp-file
1816o := $(intermediates)/$(patsubst %$(LOCAL_CPP_EXTENSION),%.o,$(subst ../,$(DOTDOT_REPLACEMENT),$(1)))
1817$$(o) : .KATI_NINJA_POOL := $(4)
1818$$(o) : $(TOPDIR)$(LOCAL_PATH)/$(1) $(2) $(CLANG_CXX)
1819	$$(transform-$$(PRIVATE_HOST)cpp-to-o)
1820$$(call include-depfiles-for-objs, $$(o))
1821$(3) += $$(o)
1822endef
1823
1824## Rule to compile a C source file with ../ in the path.
1825## Must be called with $(eval).
1826# $(1): the C source file in LOCAL_SRC_FILES.
1827# $(2): the additional dependencies.
1828# $(3): the variable name to collect the output object file.
1829# $(4): the ninja pool to use for the rule
1830define compile-dotdot-c-file
1831o := $(intermediates)/$(patsubst %.c,%.o,$(subst ../,$(DOTDOT_REPLACEMENT),$(1)))
1832$$(o) : .KATI_NINJA_POOL := $(4)
1833$$(o) : $(TOPDIR)$(LOCAL_PATH)/$(1) $(2) $(CLANG)
1834	$$(transform-$$(PRIVATE_HOST)c-to-o)
1835$$(call include-depfiles-for-objs, $$(o))
1836$(3) += $$(o)
1837endef
1838
1839## Rule to compile a .S source file with ../ in the path.
1840## Must be called with $(eval).
1841# $(1): the .S source file in LOCAL_SRC_FILES.
1842# $(2): the additional dependencies.
1843# $(3): the variable name to collect the output object file.
1844# $(4): the ninja pool to use for the rule
1845define compile-dotdot-s-file
1846o := $(intermediates)/$(patsubst %.S,%.o,$(subst ../,$(DOTDOT_REPLACEMENT),$(1)))
1847$$(o) : .KATI_NINJA_POOL := $(4)
1848$$(o) : $(TOPDIR)$(LOCAL_PATH)/$(1) $(2) $(CLANG)
1849	$$(transform-$$(PRIVATE_HOST)s-to-o)
1850$$(call include-depfiles-for-objs, $$(o))
1851$(3) += $$(o)
1852endef
1853
1854## Rule to compile a .s source file with ../ in the path.
1855## Must be called with $(eval).
1856# $(1): the .s source file in LOCAL_SRC_FILES.
1857# $(2): the additional dependencies.
1858# $(3): the variable name to collect the output object file.
1859# $(4): the ninja pool to use for the rule
1860define compile-dotdot-s-file-no-deps
1861o := $(intermediates)/$(patsubst %.s,%.o,$(subst ../,$(DOTDOT_REPLACEMENT),$(1)))
1862$$(o) : .KATI_NINJA_POOL := $(4)
1863$$(o) : $(TOPDIR)$(LOCAL_PATH)/$(1) $(2) $(CLANG)
1864	$$(transform-$$(PRIVATE_HOST)s-to-o)
1865$(3) += $$(o)
1866endef
1867
1868###########################################################
1869## Commands for running ar
1870###########################################################
1871
1872define _concat-if-arg2-not-empty
1873$(if $(2),$(hide) $(1) $(2))
1874endef
1875
1876# Split long argument list into smaller groups and call the command repeatedly
1877# Call the command at least once even if there are no arguments, as otherwise
1878# the output file won't be created.
1879#
1880# $(1): the command without arguments
1881# $(2): the arguments
1882define split-long-arguments
1883$(hide) $(1) $(wordlist 1,500,$(2))
1884$(call _concat-if-arg2-not-empty,$(1),$(wordlist 501,1000,$(2)))
1885$(call _concat-if-arg2-not-empty,$(1),$(wordlist 1001,1500,$(2)))
1886$(call _concat-if-arg2-not-empty,$(1),$(wordlist 1501,2000,$(2)))
1887$(call _concat-if-arg2-not-empty,$(1),$(wordlist 2001,2500,$(2)))
1888$(call _concat-if-arg2-not-empty,$(1),$(wordlist 2501,3000,$(2)))
1889$(call _concat-if-arg2-not-empty,$(1),$(wordlist 3001,99999,$(2)))
1890endef
1891
1892# $(1): the full path of the source static library.
1893# $(2): the full path of the destination static library.
1894define _extract-and-include-single-target-whole-static-lib
1895$(hide) ldir=$(PRIVATE_INTERMEDIATES_DIR)/WHOLE/$(basename $(notdir $(1)))_objs;\
1896    rm -rf $$ldir; \
1897    mkdir -p $$ldir; \
1898    cp $(1) $$ldir; \
1899    lib_to_include=$$ldir/$(notdir $(1)); \
1900    filelist=; \
1901    subdir=0; \
1902    for f in `$($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_AR) t $(1)`; do \
1903        if [ -e $$ldir/$$f ]; then \
1904            mkdir $$ldir/$$subdir; \
1905            ext=$$subdir/; \
1906            subdir=$$((subdir+1)); \
1907            $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_AR) m $$lib_to_include $$f; \
1908        else \
1909            ext=; \
1910        fi; \
1911        $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_AR) p $$lib_to_include $$f > $$ldir/$$ext$$f; \
1912        filelist="$$filelist $$ldir/$$ext$$f"; \
1913    done ; \
1914    $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_AR) $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_GLOBAL_ARFLAGS) \
1915        $(PRIVATE_ARFLAGS) $(2) $$filelist
1916
1917endef
1918
1919# $(1): the full path of the source static library.
1920# $(2): the full path of the destination static library.
1921define extract-and-include-whole-static-libs-first
1922$(if $(strip $(1)),
1923$(hide) cp $(1) $(2))
1924endef
1925
1926# $(1): the full path of the destination static library.
1927define extract-and-include-target-whole-static-libs
1928$(call extract-and-include-whole-static-libs-first, $(firstword $(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)),$(1))
1929$(foreach lib,$(wordlist 2,999,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)), \
1930    $(call _extract-and-include-single-target-whole-static-lib, $(lib), $(1)))
1931endef
1932
1933# Explicitly delete the archive first so that ar doesn't
1934# try to add to an existing archive.
1935define transform-o-to-static-lib
1936@echo "$($(PRIVATE_PREFIX)DISPLAY) StaticLib: $(PRIVATE_MODULE) ($@)"
1937@mkdir -p $(dir $@)
1938@rm -f $@ $@.tmp
1939$(call extract-and-include-target-whole-static-libs,$@.tmp)
1940$(call split-long-arguments,$($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_AR) \
1941    $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_GLOBAL_ARFLAGS) \
1942    $(PRIVATE_ARFLAGS) \
1943    $@.tmp,$(PRIVATE_ALL_OBJECTS))
1944$(hide) mv -f $@.tmp $@
1945endef
1946
1947###########################################################
1948## Commands for running host ar
1949###########################################################
1950
1951# $(1): the full path of the source static library.
1952# $(2): the full path of the destination static library.
1953define _extract-and-include-single-host-whole-static-lib
1954$(hide) ldir=$(PRIVATE_INTERMEDIATES_DIR)/WHOLE/$(basename $(notdir $(1)))_objs;\
1955    rm -rf $$ldir; \
1956    mkdir -p $$ldir; \
1957    cp $(1) $$ldir; \
1958    lib_to_include=$$ldir/$(notdir $(1)); \
1959    filelist=; \
1960    subdir=0; \
1961    for f in `$($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)AR) t $(1) | \grep '\.o$$'`; do \
1962        if [ -e $$ldir/$$f ]; then \
1963           mkdir $$ldir/$$subdir; \
1964           ext=$$subdir/; \
1965           subdir=$$((subdir+1)); \
1966           $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)AR) m $$lib_to_include $$f; \
1967        else \
1968           ext=; \
1969        fi; \
1970        $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)AR) p $$lib_to_include $$f > $$ldir/$$ext$$f; \
1971        filelist="$$filelist $$ldir/$$ext$$f"; \
1972    done ; \
1973    $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)AR) $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)GLOBAL_ARFLAGS) \
1974        $(2) $$filelist
1975
1976endef
1977
1978define extract-and-include-host-whole-static-libs
1979$(call extract-and-include-whole-static-libs-first, $(firstword $(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)),$(1))
1980$(foreach lib,$(wordlist 2,999,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)), \
1981    $(call _extract-and-include-single-host-whole-static-lib, $(lib),$(1)))
1982endef
1983
1984ifeq ($(HOST_OS),darwin)
1985# On Darwin the host ar fails if there is nothing to add to .a at all.
1986# We work around by adding a dummy.o and then deleting it.
1987define create-dummy.o-if-no-objs
1988$(if $(PRIVATE_ALL_OBJECTS),,$(hide) touch $(dir $(1))dummy.o)
1989endef
1990
1991define get-dummy.o-if-no-objs
1992$(if $(PRIVATE_ALL_OBJECTS),,$(dir $(1))dummy.o)
1993endef
1994
1995define delete-dummy.o-if-no-objs
1996$(if $(PRIVATE_ALL_OBJECTS),,$(hide) $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)AR) d $(1) $(dir $(1))dummy.o \
1997  && rm -f $(dir $(1))dummy.o)
1998endef
1999else
2000create-dummy.o-if-no-objs =
2001get-dummy.o-if-no-objs =
2002delete-dummy.o-if-no-objs =
2003endif  # HOST_OS is darwin
2004
2005# Explicitly delete the archive first so that ar doesn't
2006# try to add to an existing archive.
2007define transform-host-o-to-static-lib
2008@echo "$($(PRIVATE_PREFIX)DISPLAY) StaticLib: $(PRIVATE_MODULE) ($@)"
2009@mkdir -p $(dir $@)
2010@rm -f $@ $@.tmp
2011$(call extract-and-include-host-whole-static-libs,$@.tmp)
2012$(call create-dummy.o-if-no-objs,$@.tmp)
2013$(call split-long-arguments,$($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)AR) \
2014    $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)GLOBAL_ARFLAGS) $@.tmp,\
2015    $(PRIVATE_ALL_OBJECTS) $(call get-dummy.o-if-no-objs,$@.tmp))
2016$(call delete-dummy.o-if-no-objs,$@.tmp)
2017$(hide) mv -f $@.tmp $@
2018endef
2019
2020
2021###########################################################
2022## Commands for running gcc to link a shared library or package
2023###########################################################
2024
2025# ld just seems to be so finicky with command order that we allow
2026# it to be overriden en-masse see combo/linux-arm.make for an example.
2027ifneq ($(HOST_CUSTOM_LD_COMMAND),true)
2028define transform-host-o-to-shared-lib-inner
2029$(hide) $(PRIVATE_CXX_LINK) \
2030  -Wl,-rpath,\$$ORIGIN/../$(notdir $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)OUT_SHARED_LIBRARIES)) \
2031  -Wl,-rpath,\$$ORIGIN/$(notdir $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)OUT_SHARED_LIBRARIES)) \
2032  -shared -Wl,-soname,$(notdir $@) \
2033  $(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
2034     $(PRIVATE_HOST_GLOBAL_LDFLAGS) \
2035  ) \
2036  $(PRIVATE_LDFLAGS) \
2037  $(PRIVATE_ALL_OBJECTS) \
2038  -Wl,--whole-archive \
2039  $(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES) \
2040  -Wl,--no-whole-archive \
2041  $(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--start-group) \
2042  $(PRIVATE_ALL_STATIC_LIBRARIES) \
2043  $(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--end-group) \
2044  $(if $(filter true,$(NATIVE_COVERAGE)),$(PRIVATE_HOST_LIBPROFILE_RT)) \
2045  $(PRIVATE_ALL_SHARED_LIBRARIES) \
2046  -o $@ \
2047  $(PRIVATE_LDLIBS)
2048endef
2049endif
2050
2051define transform-host-o-to-shared-lib
2052@echo "$($(PRIVATE_PREFIX)DISPLAY) SharedLib: $(PRIVATE_MODULE) ($@)"
2053@mkdir -p $(dir $@)
2054$(transform-host-o-to-shared-lib-inner)
2055endef
2056
2057define transform-host-o-to-package
2058@echo "$($(PRIVATE_PREFIX)DISPLAY) Package: $(PRIVATE_MODULE) ($@)"
2059@mkdir -p $(dir $@)
2060$(transform-host-o-to-shared-lib-inner)
2061endef
2062
2063
2064###########################################################
2065## Commands for running gcc to link a shared library or package
2066###########################################################
2067
2068define transform-o-to-shared-lib-inner
2069$(hide) $(PRIVATE_CXX_LINK) \
2070  -nostdlib -Wl,-soname,$(notdir $@) \
2071  -Wl,--gc-sections \
2072  -shared \
2073  $(PRIVATE_TARGET_CRTBEGIN_SO_O) \
2074  $(PRIVATE_ALL_OBJECTS) \
2075  -Wl,--whole-archive \
2076  $(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES) \
2077  -Wl,--no-whole-archive \
2078  $(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--start-group) \
2079  $(PRIVATE_ALL_STATIC_LIBRARIES) \
2080  $(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--end-group) \
2081  $(if $(filter true,$(NATIVE_COVERAGE)),$(PRIVATE_TARGET_COVERAGE_LIB)) \
2082  $(PRIVATE_TARGET_LIBCRT_BUILTINS) \
2083  $(PRIVATE_TARGET_GLOBAL_LDFLAGS) \
2084  $(PRIVATE_LDFLAGS) \
2085  $(PRIVATE_ALL_SHARED_LIBRARIES) \
2086  -o $@ \
2087  $(PRIVATE_TARGET_CRTEND_SO_O) \
2088  $(PRIVATE_LDLIBS)
2089endef
2090
2091define transform-o-to-shared-lib
2092@echo "$($(PRIVATE_PREFIX)DISPLAY) SharedLib: $(PRIVATE_MODULE) ($@)"
2093@mkdir -p $(dir $@)
2094$(transform-o-to-shared-lib-inner)
2095endef
2096
2097###########################################################
2098## Commands for running gcc to link an executable
2099###########################################################
2100
2101define transform-o-to-executable-inner
2102$(hide) $(PRIVATE_CXX_LINK) -pie \
2103  -nostdlib -Bdynamic \
2104  -Wl,-dynamic-linker,$(PRIVATE_LINKER) \
2105  -Wl,--gc-sections \
2106  -Wl,-z,nocopyreloc \
2107  $(PRIVATE_TARGET_CRTBEGIN_DYNAMIC_O) \
2108  $(PRIVATE_ALL_OBJECTS) \
2109  -Wl,--whole-archive \
2110  $(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES) \
2111  -Wl,--no-whole-archive \
2112  $(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--start-group) \
2113  $(PRIVATE_ALL_STATIC_LIBRARIES) \
2114  $(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--end-group) \
2115  $(if $(filter true,$(NATIVE_COVERAGE)),$(PRIVATE_TARGET_COVERAGE_LIB)) \
2116  $(PRIVATE_TARGET_LIBCRT_BUILTINS) \
2117  $(PRIVATE_TARGET_GLOBAL_LDFLAGS) \
2118  $(PRIVATE_LDFLAGS) \
2119  $(PRIVATE_ALL_SHARED_LIBRARIES) \
2120  -o $@ \
2121  $(PRIVATE_TARGET_CRTEND_O) \
2122  $(PRIVATE_LDLIBS)
2123endef
2124
2125define transform-o-to-executable
2126@echo "$($(PRIVATE_PREFIX)DISPLAY) Executable: $(PRIVATE_MODULE) ($@)"
2127@mkdir -p $(dir $@)
2128$(transform-o-to-executable-inner)
2129endef
2130
2131
2132###########################################################
2133## Commands for linking a static executable. In practice,
2134## we only use this on arm, so the other platforms don't
2135## have transform-o-to-static-executable defined.
2136## Clang driver needs -static to create static executable.
2137## However, bionic/linker uses -shared to overwrite.
2138## Linker for x86 targets does not allow coexistance of -static and -shared,
2139## so we add -static only if -shared is not used.
2140###########################################################
2141
2142define transform-o-to-static-executable-inner
2143$(hide) $(PRIVATE_CXX_LINK) \
2144  -nostdlib -Bstatic \
2145  $(if $(filter $(PRIVATE_LDFLAGS),-shared),,-static) \
2146  -Wl,--gc-sections \
2147  -o $@ \
2148  $(PRIVATE_TARGET_CRTBEGIN_STATIC_O) \
2149  $(PRIVATE_TARGET_GLOBAL_LDFLAGS) \
2150  $(PRIVATE_LDFLAGS) \
2151  $(PRIVATE_ALL_OBJECTS) \
2152  -Wl,--whole-archive \
2153  $(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES) \
2154  -Wl,--no-whole-archive \
2155  $(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)) \
2156  -Wl,--start-group \
2157  $(filter %libc.a %libc.hwasan.a,$(PRIVATE_ALL_STATIC_LIBRARIES)) \
2158  $(filter %libc_nomalloc.a %libc_nomalloc.hwasan.a,$(PRIVATE_ALL_STATIC_LIBRARIES)) \
2159  $(if $(filter true,$(NATIVE_COVERAGE)),$(PRIVATE_TARGET_COVERAGE_LIB)) \
2160  $(filter %libcompiler_rt.a %libcompiler_rt.hwasan.a,$(PRIVATE_ALL_STATIC_LIBRARIES)) \
2161  $(PRIVATE_TARGET_LIBCRT_BUILTINS) \
2162  -Wl,--end-group \
2163  $(PRIVATE_TARGET_CRTEND_O)
2164endef
2165
2166define transform-o-to-static-executable
2167@echo "$($(PRIVATE_PREFIX)DISPLAY) StaticExecutable: $(PRIVATE_MODULE) ($@)"
2168@mkdir -p $(dir $@)
2169$(transform-o-to-static-executable-inner)
2170endef
2171
2172
2173###########################################################
2174## Commands for running gcc to link a host executable
2175###########################################################
2176
2177ifneq ($(HOST_CUSTOM_LD_COMMAND),true)
2178define transform-host-o-to-executable-inner
2179$(hide) $(PRIVATE_CXX_LINK) \
2180  $(PRIVATE_ALL_OBJECTS) \
2181  -Wl,--whole-archive \
2182  $(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES) \
2183  -Wl,--no-whole-archive \
2184  $(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--start-group) \
2185  $(PRIVATE_ALL_STATIC_LIBRARIES) \
2186  $(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--end-group) \
2187  $(if $(filter true,$(NATIVE_COVERAGE)),$(PRIVATE_HOST_LIBPROFILE_RT)) \
2188  $(PRIVATE_ALL_SHARED_LIBRARIES) \
2189  $(foreach path,$(PRIVATE_RPATHS), \
2190    -Wl,-rpath,\$$ORIGIN/$(path)) \
2191  $(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
2192      $(PRIVATE_HOST_GLOBAL_LDFLAGS) \
2193  ) \
2194  $(PRIVATE_LDFLAGS) \
2195  -o $@ \
2196  $(PRIVATE_LDLIBS)
2197endef
2198endif
2199
2200define transform-host-o-to-executable
2201@echo "$($(PRIVATE_PREFIX)DISPLAY) Executable: $(PRIVATE_MODULE) ($@)"
2202@mkdir -p $(dir $@)
2203$(transform-host-o-to-executable-inner)
2204endef
2205
2206###########################################################
2207## Commands for packaging native coverage files
2208###########################################################
2209define package-coverage-files
2210  @rm -f $@ $@.lst $@.premerged
2211  @touch $@.lst
2212  $(foreach obj,$(strip $(PRIVATE_ALL_OBJECTS)), $(hide) echo $(obj) >> $@.lst$(newline))
2213  $(hide) $(SOONG_ZIP) -o $@.premerged -C $(OUT_DIR) -l $@.lst
2214  $(hide) $(MERGE_ZIPS) -ignore-duplicates $@ $@.premerged $(strip $(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES))
2215endef
2216
2217###########################################################
2218## Commands for running javac to make .class files
2219###########################################################
2220
2221# b/37750224
2222AAPT_ASAN_OPTIONS := ASAN_OPTIONS=detect_leaks=0
2223
2224# Search for generated R.java in $1, copy the found R.java as $2.
2225define find-generated-R.java
2226$(hide) for GENERATED_R_FILE in `find $(1) \
2227  -name R.java 2> /dev/null`; do \
2228    cp $$GENERATED_R_FILE $(2) || exit 32; \
2229  done;
2230@# Ensure that the target file is always created, i.e. also in case we did not
2231@# enter the GENERATED_R_FILE-loop above. This avoids unnecessary rebuilding.
2232$(hide) touch $(2)
2233endef
2234
2235###########################################################
2236# AAPT2 compilation and link
2237###########################################################
2238define aapt2-compile-one-resource-file
2239@mkdir -p $(dir $@)
2240$(hide) $(AAPT2) compile -o $(dir $@) $(PRIVATE_AAPT2_CFLAGS) $<
2241endef
2242
2243define aapt2-compile-resource-dirs
2244@mkdir -p $(dir $@)
2245$(hide) $(AAPT2) compile -o $@ $(addprefix --dir ,$(PRIVATE_SOURCE_RES_DIRS)) \
2246  $(PRIVATE_AAPT2_CFLAGS)
2247endef
2248
2249# TODO(b/74574557): use aapt2 compile --zip if it gets implemented
2250define aapt2-compile-resource-zips
2251@mkdir -p $(dir $@)
2252$(ZIPSYNC) -d $@.contents -l $@.list $(PRIVATE_SOURCE_RES_ZIPS)
2253$(hide) $(AAPT2) compile -o $@ --dir $@.contents $(PRIVATE_AAPT2_CFLAGS)
2254endef
2255
2256# Set up rule to compile one resource file with aapt2.
2257# Must be called with $(eval).
2258# $(1): the source file
2259# $(2): the output file
2260define aapt2-compile-one-resource-file-rule
2261$(2) : $(1) $(AAPT2)
2262	@echo "AAPT2 compile $$@ <- $$<"
2263	$$(call aapt2-compile-one-resource-file)
2264endef
2265
2266# Convert input resource file path to output file path.
2267# values-[config]/<file>.xml -> values-[config]_<file>.arsc.flat;
2268# For other resource file, just replace the last "/" with "_" and
2269# add .flat extension.
2270#
2271# $(1): the input resource file path
2272# $(2): the base dir of the output file path
2273# Returns: the compiled output file path
2274define aapt2-compiled-resource-out-file
2275$(strip \
2276  $(eval _p_w := $(strip $(subst /,$(space),$(dir $(call clean-path,$(1))))))
2277  $(2)/$(subst $(space),/,$(_p_w))_$(if $(filter values%,$(lastword $(_p_w))),$(patsubst %.xml,%.arsc,$(notdir $(1))),$(notdir $(1))).flat)
2278endef
2279
2280define aapt2-link
2281@mkdir -p $(dir $@)
2282rm -rf $(PRIVATE_JAVA_GEN_DIR)
2283mkdir -p $(PRIVATE_JAVA_GEN_DIR)
2284$(call dump-words-to-file,$(PRIVATE_RES_FLAT),$(dir $@)aapt2-flat-list)
2285$(call dump-words-to-file,$(PRIVATE_OVERLAY_FLAT),$(dir $@)aapt2-flat-overlay-list)
2286$(hide) $(AAPT2) link -o $@ \
2287  $(PRIVATE_AAPT_FLAGS) \
2288  $(if $(PRIVATE_STATIC_LIBRARY_EXTRA_PACKAGES),$$(cat $(PRIVATE_STATIC_LIBRARY_EXTRA_PACKAGES))) \
2289  $(addprefix --manifest ,$(PRIVATE_ANDROID_MANIFEST)) \
2290  $(addprefix -I ,$(PRIVATE_AAPT_INCLUDES)) \
2291  $(addprefix -I ,$(PRIVATE_SHARED_ANDROID_LIBRARIES)) \
2292  $(addprefix -A ,$(foreach d,$(PRIVATE_ASSET_DIR),$(call clean-path,$(d)))) \
2293  $(addprefix --java ,$(PRIVATE_JAVA_GEN_DIR)) \
2294  $(addprefix --proguard ,$(PRIVATE_PROGUARD_OPTIONS_FILE)) \
2295  $(addprefix --min-sdk-version ,$(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
2296  $(addprefix --target-sdk-version ,$(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
2297  $(if $(filter --product,$(PRIVATE_AAPT_FLAGS)),,$(addprefix --product ,$(PRIVATE_TARGET_AAPT_CHARACTERISTICS))) \
2298  $(addprefix -c ,$(PRIVATE_PRODUCT_AAPT_CONFIG)) \
2299  $(addprefix --preferred-density ,$(PRIVATE_PRODUCT_AAPT_PREF_CONFIG)) \
2300  $(if $(filter --version-code,$(PRIVATE_AAPT_FLAGS)),,--version-code $(PLATFORM_SDK_VERSION)) \
2301  $(if $(filter --version-name,$(PRIVATE_AAPT_FLAGS)),,--version-name $(APPS_DEFAULT_VERSION_NAME)) \
2302  $(addprefix --rename-manifest-package ,$(PRIVATE_MANIFEST_PACKAGE_NAME)) \
2303  $(addprefix --rename-instrumentation-target-package ,$(PRIVATE_MANIFEST_INSTRUMENTATION_FOR)) \
2304  -R \@$(dir $@)aapt2-flat-overlay-list \
2305  \@$(dir $@)aapt2-flat-list
2306$(SOONG_ZIP) -o $(PRIVATE_SRCJAR) -C $(PRIVATE_JAVA_GEN_DIR) -D $(PRIVATE_JAVA_GEN_DIR)
2307$(EXTRACT_JAR_PACKAGES) -i $(PRIVATE_SRCJAR) -o $(PRIVATE_AAPT_EXTRA_PACKAGES) --prefix '--extra-packages '
2308endef
2309
2310define _create-default-manifest-file
2311$(1):
2312	rm -f $1
2313	(echo '<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="missing.manifest">' && \
2314	 echo '    <uses-sdk android:minSdkVersion="$(2)" />' && \
2315	 echo '</manifest>' ) > $1
2316endef
2317
2318define create-default-manifest-file
2319  $(eval $(call _create-default-manifest-file,$(1),$(2)))
2320endef
2321
2322
2323###########################################################
2324xlint_unchecked := -Xlint:unchecked
2325
2326# emit-line, <word list>, <output file>
2327define emit-line
2328   $(if $(1),echo -n '$(strip $(1)) ' >> $(2))
2329endef
2330
2331# dump-words-to-file, <word list>, <output file>
2332define dump-words-to-file
2333        @rm -f $(2)
2334        @touch $(2)
2335        @$(call emit-line,$(wordlist 1,500,$(1)),$(2))
2336        @$(call emit-line,$(wordlist 501,1000,$(1)),$(2))
2337        @$(call emit-line,$(wordlist 1001,1500,$(1)),$(2))
2338        @$(call emit-line,$(wordlist 1501,2000,$(1)),$(2))
2339        @$(call emit-line,$(wordlist 2001,2500,$(1)),$(2))
2340        @$(call emit-line,$(wordlist 2501,3000,$(1)),$(2))
2341        @$(call emit-line,$(wordlist 3001,3500,$(1)),$(2))
2342        @$(call emit-line,$(wordlist 3501,4000,$(1)),$(2))
2343        @$(call emit-line,$(wordlist 4001,4500,$(1)),$(2))
2344        @$(call emit-line,$(wordlist 4501,5000,$(1)),$(2))
2345        @$(call emit-line,$(wordlist 5001,5500,$(1)),$(2))
2346        @$(call emit-line,$(wordlist 5501,6000,$(1)),$(2))
2347        @$(call emit-line,$(wordlist 6001,6500,$(1)),$(2))
2348        @$(call emit-line,$(wordlist 6501,7000,$(1)),$(2))
2349        @$(call emit-line,$(wordlist 7001,7500,$(1)),$(2))
2350        @$(call emit-line,$(wordlist 7501,8000,$(1)),$(2))
2351        @$(call emit-line,$(wordlist 8001,8500,$(1)),$(2))
2352        @$(call emit-line,$(wordlist 8501,9000,$(1)),$(2))
2353        @$(call emit-line,$(wordlist 9001,9500,$(1)),$(2))
2354        @$(call emit-line,$(wordlist 9501,10000,$(1)),$(2))
2355        @$(call emit-line,$(wordlist 10001,10500,$(1)),$(2))
2356        @$(call emit-line,$(wordlist 10501,11000,$(1)),$(2))
2357        @$(call emit-line,$(wordlist 11001,11500,$(1)),$(2))
2358        @$(call emit-line,$(wordlist 11501,12000,$(1)),$(2))
2359        @$(call emit-line,$(wordlist 12001,12500,$(1)),$(2))
2360        @$(call emit-line,$(wordlist 12501,13000,$(1)),$(2))
2361        @$(call emit-line,$(wordlist 13001,13500,$(1)),$(2))
2362        @$(call emit-line,$(wordlist 13501,14000,$(1)),$(2))
2363        @$(call emit-line,$(wordlist 14001,14500,$(1)),$(2))
2364        @$(call emit-line,$(wordlist 14501,15000,$(1)),$(2))
2365        @$(call emit-line,$(wordlist 15001,15500,$(1)),$(2))
2366        @$(call emit-line,$(wordlist 15501,16000,$(1)),$(2))
2367        @$(call emit-line,$(wordlist 16001,16500,$(1)),$(2))
2368        @$(call emit-line,$(wordlist 16501,17000,$(1)),$(2))
2369        @$(call emit-line,$(wordlist 17001,17500,$(1)),$(2))
2370        @$(call emit-line,$(wordlist 17501,18000,$(1)),$(2))
2371        @$(call emit-line,$(wordlist 18001,18500,$(1)),$(2))
2372        @$(call emit-line,$(wordlist 18501,19000,$(1)),$(2))
2373        @$(call emit-line,$(wordlist 19001,19500,$(1)),$(2))
2374        @$(call emit-line,$(wordlist 19501,20000,$(1)),$(2))
2375        @$(call emit-line,$(wordlist 20001,20500,$(1)),$(2))
2376        @$(call emit-line,$(wordlist 20501,21000,$(1)),$(2))
2377        @$(call emit-line,$(wordlist 21001,21500,$(1)),$(2))
2378        @$(call emit-line,$(wordlist 21501,22000,$(1)),$(2))
2379        @$(call emit-line,$(wordlist 22001,22500,$(1)),$(2))
2380        @$(call emit-line,$(wordlist 22501,23000,$(1)),$(2))
2381        @$(call emit-line,$(wordlist 23001,23500,$(1)),$(2))
2382        @$(call emit-line,$(wordlist 23501,24000,$(1)),$(2))
2383        @$(call emit-line,$(wordlist 24001,24500,$(1)),$(2))
2384        @$(call emit-line,$(wordlist 24501,25000,$(1)),$(2))
2385        @$(call emit-line,$(wordlist 25001,25500,$(1)),$(2))
2386        @$(call emit-line,$(wordlist 25501,26000,$(1)),$(2))
2387        @$(call emit-line,$(wordlist 26001,26500,$(1)),$(2))
2388        @$(call emit-line,$(wordlist 26501,27000,$(1)),$(2))
2389        @$(call emit-line,$(wordlist 27001,27500,$(1)),$(2))
2390        @$(call emit-line,$(wordlist 27501,28000,$(1)),$(2))
2391        @$(call emit-line,$(wordlist 28001,28500,$(1)),$(2))
2392        @$(call emit-line,$(wordlist 28501,29000,$(1)),$(2))
2393        @$(call emit-line,$(wordlist 29001,29500,$(1)),$(2))
2394        @$(call emit-line,$(wordlist 29501,30000,$(1)),$(2))
2395        @$(call emit-line,$(wordlist 30001,30500,$(1)),$(2))
2396        @$(call emit-line,$(wordlist 30501,31000,$(1)),$(2))
2397        @$(call emit-line,$(wordlist 31001,31500,$(1)),$(2))
2398        @$(call emit-line,$(wordlist 31501,32000,$(1)),$(2))
2399        @$(call emit-line,$(wordlist 32001,32500,$(1)),$(2))
2400        @$(call emit-line,$(wordlist 32501,33000,$(1)),$(2))
2401        @$(call emit-line,$(wordlist 33001,33500,$(1)),$(2))
2402        @$(call emit-line,$(wordlist 33501,34000,$(1)),$(2))
2403        @$(call emit-line,$(wordlist 34001,34500,$(1)),$(2))
2404        @$(call emit-line,$(wordlist 34501,35000,$(1)),$(2))
2405        @$(call emit-line,$(wordlist 35001,35500,$(1)),$(2))
2406        @$(call emit-line,$(wordlist 35501,36000,$(1)),$(2))
2407        @$(call emit-line,$(wordlist 36001,36500,$(1)),$(2))
2408        @$(call emit-line,$(wordlist 36501,37000,$(1)),$(2))
2409        @$(call emit-line,$(wordlist 37001,37500,$(1)),$(2))
2410        @$(call emit-line,$(wordlist 37501,38000,$(1)),$(2))
2411        @$(call emit-line,$(wordlist 38001,38500,$(1)),$(2))
2412        @$(call emit-line,$(wordlist 38501,39000,$(1)),$(2))
2413        @$(call emit-line,$(wordlist 39001,39500,$(1)),$(2))
2414        @$(if $(wordlist 39501,39502,$(1)),$(error Too many words ($(words $(1)))))
2415endef
2416# Return jar arguments to compress files in a given directory
2417# $(1): directory
2418#
2419# Returns an @-file argument that contains the output of a subshell
2420# that looks like -C $(1) path/to/file1 -C $(1) path/to/file2
2421# Also adds "-C out/empty ." which avoids errors in jar when
2422# there are no files in the directory.
2423define jar-args-sorted-files-in-directory
2424    @<(find $(1) -type f | sort | $(JAR_ARGS) $(1); echo "-C $(EMPTY_DIRECTORY) .")
2425endef
2426
2427# append additional Java sources(resources/Proto sources, and etc) to $(1).
2428define fetch-additional-java-source
2429$(hide) if [ -d "$(PRIVATE_SOURCE_INTERMEDIATES_DIR)" ]; then \
2430    find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name '*.java' -and -not -name '.*' >> $(1); \
2431fi
2432endef
2433
2434# Some historical notes:
2435# - below we write the list of java files to java-source-list to avoid argument
2436#   list length problems with Cygwin
2437# - we filter out duplicate java file names because eclipse's compiler
2438#   doesn't like them.
2439define write-java-source-list
2440@echo "$($(PRIVATE_PREFIX)DISPLAY) Java source list: $(PRIVATE_MODULE)"
2441$(hide) rm -f $@
2442$(call dump-words-to-file,$(sort $(PRIVATE_JAVA_SOURCES)),$@.tmp)
2443$(call fetch-additional-java-source,$@.tmp)
2444$(hide) tr ' ' '\n' < $@.tmp | $(NORMALIZE_PATH) | sort -u > $@
2445endef
2446
2447# Common definition to invoke javac on the host and target.
2448#
2449# $(1): javac
2450# $(2): classpath_libs
2451define compile-java
2452$(hide) rm -f $@
2453$(hide) rm -rf $(PRIVATE_CLASS_INTERMEDIATES_DIR) $(PRIVATE_ANNO_INTERMEDIATES_DIR)
2454$(hide) mkdir -p $(dir $@)
2455$(hide) mkdir -p $(PRIVATE_CLASS_INTERMEDIATES_DIR) $(PRIVATE_ANNO_INTERMEDIATES_DIR)
2456$(if $(PRIVATE_SRCJARS),\
2457    $(ZIPSYNC) -d $(PRIVATE_SRCJAR_INTERMEDIATES_DIR) -l $(PRIVATE_SRCJAR_LIST_FILE) -f "*.java" $(PRIVATE_SRCJARS))
2458$(hide) if [ -s $(PRIVATE_JAVA_SOURCE_LIST) $(if $(PRIVATE_SRCJARS),-o -s $(PRIVATE_SRCJAR_LIST_FILE) )] ; then \
2459    $(SOONG_JAVAC_WRAPPER) $(JAVAC_WRAPPER) $(1) -encoding UTF-8 \
2460    $(if $(findstring true,$(PRIVATE_WARNINGS_ENABLE)),$(xlint_unchecked),) \
2461    $(if $(PRIVATE_USE_SYSTEM_MODULES), \
2462      $(addprefix --system=,$(PRIVATE_SYSTEM_MODULES_DIR)), \
2463      $(addprefix -bootclasspath ,$(strip \
2464          $(call normalize-path-list,$(PRIVATE_BOOTCLASSPATH)) \
2465          $(PRIVATE_EMPTY_BOOTCLASSPATH)))) \
2466    $(if $(PRIVATE_USE_SYSTEM_MODULES), \
2467      $(if $(PRIVATE_PATCH_MODULE), \
2468        --patch-module=$(PRIVATE_PATCH_MODULE)=$(call normalize-path-list,. $(2)))) \
2469    $(addprefix -classpath ,$(call normalize-path-list,$(strip \
2470      $(if $(PRIVATE_USE_SYSTEM_MODULES), \
2471        $(filter-out $(PRIVATE_SYSTEM_MODULES_LIBS),$(PRIVATE_BOOTCLASSPATH))) \
2472      $(2)))) \
2473    $(if $(findstring true,$(PRIVATE_WARNINGS_ENABLE)),$(xlint_unchecked),) \
2474    -d $(PRIVATE_CLASS_INTERMEDIATES_DIR) -s $(PRIVATE_ANNO_INTERMEDIATES_DIR) \
2475    $(PRIVATE_JAVACFLAGS) \
2476    \@$(PRIVATE_JAVA_SOURCE_LIST) \
2477    $(if $(PRIVATE_SRCJARS),\@$(PRIVATE_SRCJAR_LIST_FILE)) \
2478    || ( rm -rf $(PRIVATE_CLASS_INTERMEDIATES_DIR) ; exit 41 ) \
2479fi
2480$(if $(PRIVATE_JAVA_LAYERS_FILE), $(hide) build/make/tools/java-layers.py \
2481    $(PRIVATE_JAVA_LAYERS_FILE) @$(PRIVATE_JAVA_SOURCE_LIST),)
2482$(if $(PRIVATE_JAR_EXCLUDE_FILES), $(hide) find $(PRIVATE_CLASS_INTERMEDIATES_DIR) \
2483    -name $(word 1, $(PRIVATE_JAR_EXCLUDE_FILES)) \
2484    $(addprefix -o -name , $(wordlist 2, 999, $(PRIVATE_JAR_EXCLUDE_FILES))) \
2485    | xargs rm -rf)
2486$(if $(PRIVATE_JAR_PACKAGES), \
2487    $(hide) find $(PRIVATE_CLASS_INTERMEDIATES_DIR) -mindepth 1 -type f \
2488        $(foreach pkg, $(PRIVATE_JAR_PACKAGES), \
2489            -not -path $(PRIVATE_CLASS_INTERMEDIATES_DIR)/$(subst .,/,$(pkg))/\*) -delete ; \
2490        find $(PRIVATE_CLASS_INTERMEDIATES_DIR) -empty -delete)
2491$(if $(PRIVATE_JAR_EXCLUDE_PACKAGES), $(hide) rm -rf \
2492    $(foreach pkg, $(PRIVATE_JAR_EXCLUDE_PACKAGES), \
2493        $(PRIVATE_CLASS_INTERMEDIATES_DIR)/$(subst .,/,$(pkg))))
2494$(hide) $(SOONG_ZIP) -jar -o $@ -C $(PRIVATE_CLASS_INTERMEDIATES_DIR) -D $(PRIVATE_CLASS_INTERMEDIATES_DIR)
2495$(if $(PRIVATE_EXTRA_JAR_ARGS),$(call add-java-resources-to,$@))
2496endef
2497
2498define transform-java-to-header.jar
2499@echo "$($(PRIVATE_PREFIX)DISPLAY) Turbine: $(PRIVATE_MODULE)"
2500@mkdir -p $(dir $@)
2501@rm -rf $(dir $@)/classes-turbine
2502@mkdir $(dir $@)/classes-turbine
2503$(hide) if [ -s $(PRIVATE_JAVA_SOURCE_LIST) -o -n "$(PRIVATE_SRCJARS)" ] ; then \
2504    $(JAVA) -jar $(TURBINE) \
2505    --output $@.premerged --temp_dir $(dir $@)/classes-turbine \
2506    --sources \@$(PRIVATE_JAVA_SOURCE_LIST) --source_jars $(PRIVATE_SRCJARS) \
2507    --javacopts $(PRIVATE_JAVACFLAGS) $(COMMON_JDK_FLAGS) -- \
2508    $(if $(PRIVATE_USE_SYSTEM_MODULES), \
2509      --system $(PRIVATE_SYSTEM_MODULES_DIR), \
2510      --bootclasspath $(strip $(PRIVATE_BOOTCLASSPATH))) \
2511    --classpath $(strip $(if $(PRIVATE_USE_SYSTEM_MODULES), \
2512        $(filter-out $(PRIVATE_SYSTEM_MODULES_LIBS),$(PRIVATE_BOOTCLASSPATH))) \
2513      $(PRIVATE_ALL_JAVA_HEADER_LIBRARIES)) \
2514    || ( rm -rf $(dir $@)/classes-turbine ; exit 41 ) && \
2515    $(MERGE_ZIPS) -j --ignore-duplicates -stripDir META-INF $@.tmp $@.premerged $(PRIVATE_STATIC_JAVA_HEADER_LIBRARIES) ; \
2516else \
2517    $(MERGE_ZIPS) -j --ignore-duplicates -stripDir META-INF $@.tmp $(PRIVATE_STATIC_JAVA_HEADER_LIBRARIES) ; \
2518fi
2519$(hide) $(ZIPTIME) $@.tmp
2520$(hide) $(call commit-change-for-toc,$@)
2521endef
2522
2523# Runs jarjar on an input file.  Jarjar doesn't exit with a nonzero return code
2524# when there is a syntax error in a rules file and doesn't write the output
2525# file, so removes the output file before running jarjar and check if it exists
2526# after running jarjar.
2527define transform-jarjar
2528echo $($(PRIVATE_PREFIX)DISPLAY) JarJar: $@
2529rm -f $@
2530$(JAVA) -jar $(JARJAR) process $(PRIVATE_JARJAR_RULES) $< $@
2531[ -e $@ ] || (echo "Missing output file"; exit 1)
2532endef
2533
2534# Moves $1.tmp to $1 if necessary. This is designed to be used with
2535# .KATI_RESTAT. For kati, this function doesn't update the timestamp
2536# of $1 when $1.tmp is identical to $1 so that ninja won't rebuild
2537# targets which depend on $1.
2538define commit-change-for-toc
2539$(hide) if cmp -s $1.tmp $1 ; then \
2540 rm $1.tmp ; \
2541else \
2542 mv $1.tmp $1 ; \
2543fi
2544endef
2545
2546ifeq (,$(TARGET_BUILD_APPS))
2547
2548## Rule to create a table of contents from a .dex file.
2549## Must be called with $(eval).
2550# $(1): The directory which contains classes*.dex files
2551define _transform-dex-to-toc
2552$1/classes.dex.toc: PRIVATE_INPUT_DEX_FILES := $1/classes*.dex
2553$1/classes.dex.toc: $1/classes.dex $(DEXDUMP)
2554	@echo Generating TOC: $$@
2555	$(hide) ANDROID_LOG_TAGS="*:e" $(DEXDUMP) -l xml $$(PRIVATE_INPUT_DEX_FILES) > $$@.tmp
2556	$$(call commit-change-for-toc,$$@)
2557endef
2558
2559## Define a rule which generates .dex.toc and mark it as .KATI_RESTAT.
2560# $(1): The directory which contains classes*.dex files
2561define define-dex-to-toc-rule
2562$(eval $(call _transform-dex-to-toc,$1))\
2563$(eval .KATI_RESTAT: $1/classes.dex.toc)
2564endef
2565
2566else
2567
2568# Turn off .toc optimization for apps build as we cannot build dexdump.
2569define define-dex-to-toc-rule
2570endef
2571
2572endif  # TARGET_BUILD_APPS
2573
2574
2575# Takes an sdk version that might be PLATFORM_VERSION_CODENAME (for example P),
2576# returns a number greater than the highest existing sdk version if it is, or
2577# the input if it is not.
2578define codename-or-sdk-to-sdk
2579$(if $(filter $(1),$(PLATFORM_VERSION_CODENAME)),10000,$(1))
2580endef
2581
2582# Uses LOCAL_SDK_VERSION and PLATFORM_SDK_VERSION to determine a compileSdkVersion
2583# in the form of a number or a codename (28 or P)
2584define module-sdk-version
2585$(strip \
2586  $(if $(filter-out current system_current test_current core_current,$(LOCAL_SDK_VERSION)), \
2587    $(call get-numeric-sdk-version,$(LOCAL_SDK_VERSION)), \
2588    $(PLATFORM_SDK_VERSION)))
2589endef
2590
2591# Uses LOCAL_SDK_VERSION and DEFAULT_APP_TARGET_SDK to determine
2592# a targetSdkVersion in the form of a number or a codename (28 or P).
2593define module-target-sdk-version
2594$(strip \
2595  $(if $(filter-out current system_current test_current core_current,$(LOCAL_SDK_VERSION)), \
2596    $(call get-numeric-sdk-version,$(LOCAL_SDK_VERSION)), \
2597    $(DEFAULT_APP_TARGET_SDK)))
2598endef
2599
2600# Uses LOCAL_MIN_SDK_VERSION, LOCAL_SDK_VERSION and DEFAULT_APP_TARGET_SDK to determine
2601# a minSdkVersion in the form of a number or a codename (28 or P).
2602define module-min-sdk-version
2603$(if $(LOCAL_MIN_SDK_VERSION),$(LOCAL_MIN_SDK_VERSION),$(call module-target-sdk-version))
2604endef
2605
2606
2607define transform-classes.jar-to-dex
2608@echo "target Dex: $(PRIVATE_MODULE)"
2609@mkdir -p $(dir $@)tmp
2610$(hide) rm -f $(dir $@)classes*.dex $(dir $@)d8_input.jar
2611$(hide) $(ZIP2ZIP) -j -i $< -o $(dir $@)d8_input.jar "**/*.class"
2612$(hide) $(D8_WRAPPER) $(DX_COMMAND) $(DEX_FLAGS) \
2613    --output $(dir $@)tmp \
2614    $(addprefix --lib ,$(PRIVATE_D8_LIBS)) \
2615    --min-api $(PRIVATE_MIN_SDK_VERSION) \
2616    $(subst --main-dex-list=, --main-dex-list , \
2617        $(filter-out --core-library --multi-dex --minimal-main-dex,$(PRIVATE_DX_FLAGS))) \
2618    $(dir $@)d8_input.jar
2619$(hide) mv $(dir $@)tmp/* $(dir $@)
2620$(hide) rm -f $(dir $@)d8_input.jar
2621$(hide) rm -rf $(dir $@)tmp
2622endef
2623
2624# We need the extra blank line, so that the command will be on a separate line.
2625# $(1): the package
2626# $(2): the ABI name
2627# $(3): the list of shared libraies
2628define _add-jni-shared-libs-to-package-per-abi
2629$(hide) cp $(3) $(dir $(1))lib/$(2)
2630
2631endef
2632
2633# $(1): the package file
2634# $(2): if true, uncompress jni libs
2635define create-jni-shared-libs-package
2636rm -rf $(dir $(1))lib
2637mkdir -p $(addprefix $(dir $(1))lib/,$(PRIVATE_JNI_SHARED_LIBRARIES_ABI))
2638$(foreach abi,$(PRIVATE_JNI_SHARED_LIBRARIES_ABI),\
2639  $(call _add-jni-shared-libs-to-package-per-abi,$(1),$(abi),\
2640    $(patsubst $(abi):%,%,$(filter $(abi):%,$(PRIVATE_JNI_SHARED_LIBRARIES)))))
2641$(SOONG_ZIP) $(if $(2),-L 0) -o $(1) -C $(dir $(1)) -D $(dir $(1))lib
2642rm -rf $(dir $(1))lib
2643endef
2644
2645# $(1): the jar file.
2646# $(2): the classes.dex file.
2647define create-dex-jar
2648find $(dir $(2)) -maxdepth 1 -name "classes*.dex" | sort > $(1).lst
2649$(SOONG_ZIP) -o $(1) -C $(dir $(2)) -l $(1).lst
2650endef
2651
2652# Add java resources added by the current module to an existing package.
2653# $(1) destination package.
2654define add-java-resources-to
2655  $(call _java-resources,$(1),u)
2656endef
2657
2658# Add java resources added by the current module to a new jar.
2659# $(1) destination jar.
2660define create-java-resources-jar
2661  $(call _java-resources,$(1),c)
2662endef
2663
2664define _java-resources
2665$(call dump-words-to-file, $(PRIVATE_EXTRA_JAR_ARGS), $(1).jar-arg-list)
2666$(hide) $(JAR) $(2)f $(1) @$(1).jar-arg-list
2667@rm -f $(1).jar-arg-list
2668endef
2669
2670# Add resources (non .class files) from a jar to a package
2671# $(1): the package file
2672# $(2): the jar file
2673# $(3): temporary directory
2674define add-jar-resources-to-package
2675  rm -rf $(3)
2676  mkdir -p $(3)
2677  zipinfo -1 $(2) > /dev/null
2678  unzip -qo $(2) -d $(3) $$(zipinfo -1 $(2) | grep -v -E "\.class$$")
2679  $(JAR) uf $(1) $(call jar-args-sorted-files-in-directory,$(3))
2680endef
2681
2682# $(1): the output resources jar.
2683# $(2): the input jar
2684define extract-resources-jar
2685  $(ZIP2ZIP) -i $(2) -o $(1) -x '**/*.class' -x '**/*/'
2686endef
2687
2688# Sign a package using the specified key/cert.
2689#
2690define sign-package
2691$(call sign-package-arg,$@)
2692endef
2693
2694# $(1): the package file we are signing.
2695define sign-package-arg
2696$(hide) mv $(1) $(1).unsigned
2697$(hide) $(JAVA) -Djava.library.path=$$(dirname $(SIGNAPK_JNI_LIBRARY_PATH)) -jar $(SIGNAPK_JAR) \
2698    $(if $(strip $(PRIVATE_CERTIFICATE_LINEAGE)), --lineage $(PRIVATE_CERTIFICATE_LINEAGE)) \
2699    $(if $(strip $(PRIVATE_ROTATION_MIN_SDK_VERSION)), --rotation-min-sdk-version $(PRIVATE_ROTATION_MIN_SDK_VERSION)) \
2700    $(PRIVATE_CERTIFICATE) $(PRIVATE_PRIVATE_KEY) \
2701    $(PRIVATE_ADDITIONAL_CERTIFICATES) $(1).unsigned $(1).signed
2702$(hide) mv $(1).signed $(1)
2703endef
2704
2705# Align STORED entries of a package on 4-byte boundaries to make them easier to mmap.
2706#
2707define align-package
2708$(hide) if ! $(ZIPALIGN) -c -p 4 $@ >/dev/null ; then \
2709  mv $@ $@.unaligned; \
2710  $(ZIPALIGN) \
2711    -f \
2712    -p \
2713    4 \
2714    $@.unaligned $@.aligned; \
2715  mv $@.aligned $@; \
2716  fi
2717endef
2718
2719# Verifies ZIP alignment of a package.
2720#
2721define check-package-alignment
2722$(hide) if ! $(ZIPALIGN) -c -p 4 $@ >/dev/null ; then \
2723    $(call echo-error,$@,Improper package alignment); \
2724    exit 1; \
2725  fi
2726endef
2727
2728# Compress a package using the standard gzip algorithm.
2729define compress-package
2730$(hide) \
2731  mv $@ $@.uncompressed; \
2732  $(MINIGZIP) -c $@.uncompressed > $@.compressed; \
2733  rm -f $@.uncompressed; \
2734  mv $@.compressed $@;
2735endef
2736
2737ifeq ($(HOST_OS),linux)
2738# Runs appcompat and store logs in $(PRODUCT_OUT)/appcompat
2739define extract-package
2740$(AAPT2) dump resources $@ | awk -F ' |=' '/^Package/{print $$3}' >> $(PRODUCT_OUT)/appcompat/$(PRIVATE_MODULE).log &&
2741endef
2742define appcompat-header
2743$(hide) \
2744  mkdir -p $(PRODUCT_OUT)/appcompat && \
2745  rm -f $(PRODUCT_OUT)/appcompat/$(PRIVATE_MODULE).log && \
2746  echo -n "Package name: " >> $(PRODUCT_OUT)/appcompat/$(PRIVATE_MODULE).log && \
2747  $(extract-package) \
2748  echo "Module name in Android tree: $(PRIVATE_MODULE)" >> $(PRODUCT_OUT)/appcompat/$(PRIVATE_MODULE).log && \
2749  echo "Local path in Android tree: $(PRIVATE_PATH)" >> $(PRODUCT_OUT)/appcompat/$(PRIVATE_MODULE).log && \
2750  echo "Install path on $(TARGET_PRODUCT)-$(TARGET_BUILD_VARIANT): $(PRIVATE_INSTALLED_MODULE)" >> $(PRODUCT_OUT)/appcompat/$(PRIVATE_MODULE).log && \
2751  echo >> $(PRODUCT_OUT)/appcompat/$(PRIVATE_MODULE).log
2752endef
2753ART_VERIDEX_APPCOMPAT_SCRIPT:=$(HOST_OUT)/bin/appcompat.sh
2754define run-appcompat
2755$(hide) \
2756  echo "appcompat.sh output:" >> $(PRODUCT_OUT)/appcompat/$(PRIVATE_MODULE).log && \
2757  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
2758endef
2759appcompat-files = \
2760  $(AAPT2) \
2761  $(ART_VERIDEX_APPCOMPAT_SCRIPT) \
2762  $(INTERNAL_PLATFORM_HIDDENAPI_FLAGS) \
2763  $(HOST_OUT_EXECUTABLES)/veridex \
2764  $(TARGET_OUT_COMMON_INTERMEDIATES)/PACKAGING/core_dex_intermediates/classes.dex \
2765  $(TARGET_OUT_COMMON_INTERMEDIATES)/PACKAGING/oahl_dex_intermediates/classes.dex
2766else
2767appcompat-header =
2768run-appcompat =
2769appcompat-files =
2770endif  # HOST_OS == linux
2771.KATI_READONLY: appcompat-header run-appcompat appcompat-files
2772
2773# Remove dynamic timestamps from packages
2774#
2775define remove-timestamps-from-package
2776$(hide) $(ZIPTIME) $@
2777endef
2778
2779# Uncompress dex files embedded in an apk.
2780#
2781define uncompress-dexs
2782  if (zipinfo $@ '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then \
2783    $(ZIP2ZIP) -i $@ -o $@.tmp -0 "classes*.dex" && \
2784    mv -f $@.tmp $@ ; \
2785  fi
2786endef
2787
2788# Uncompress shared JNI libraries embedded in an apk.
2789#
2790define uncompress-prebuilt-embedded-jni-libs
2791  if (zipinfo $@ 'lib/*.so' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then \
2792    $(ZIP2ZIP) -i $@ -o $@.tmp -0 'lib/**/*.so' && mv -f $@.tmp $@ ; \
2793  fi
2794endef
2795
2796# Verifies shared JNI libraries and dex files in an apk are uncompressed.
2797#
2798define check-jni-dex-compression
2799  if (zipinfo $@ 'lib/*.so' '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then \
2800    $(call echo-error,$@,Contains compressed JNI libraries and/or dex files); \
2801    exit 1; \
2802  fi
2803endef
2804
2805# Remove unwanted shared JNI libraries embedded in an apk.
2806#
2807define remove-unwanted-prebuilt-embedded-jni-libs
2808  $(if $(PRIVATE_EMBEDDED_JNI_LIBS), \
2809    $(ZIP2ZIP) -i $@ -o $@.tmp \
2810      -x 'lib/**/*.so' $(addprefix -X ,$(PRIVATE_EMBEDDED_JNI_LIBS)) && \
2811    mv -f $@.tmp $@)
2812endef
2813
2814# TODO(joeo): If we can ever upgrade to post 3.81 make and get the
2815# new prebuilt rules to work, we should change this to copy the
2816# resources to the out directory and then copy the resources.
2817
2818# Note: we intentionally don't clean PRIVATE_CLASS_INTERMEDIATES_DIR
2819# in transform-java-to-classes for the sake of vm-tests.
2820define transform-host-java-to-package
2821@echo "Host Java: $(PRIVATE_MODULE) ($(PRIVATE_CLASS_INTERMEDIATES_DIR))"
2822$(call compile-java,$(HOST_JAVAC),$(PRIVATE_ALL_JAVA_LIBRARIES))
2823endef
2824
2825# Note: we intentionally don't clean PRIVATE_CLASS_INTERMEDIATES_DIR
2826# in transform-java-to-classes for the sake of vm-tests.
2827define transform-host-java-to-dalvik-package
2828@echo "Dalvik Java: $(PRIVATE_MODULE) ($(PRIVATE_CLASS_INTERMEDIATES_DIR))"
2829$(call compile-java,$(HOST_JAVAC),$(PRIVATE_ALL_JAVA_HEADER_LIBRARIES))
2830endef
2831
2832###########################################################
2833## Commands for copying files
2834###########################################################
2835
2836# Define a rule to copy a header.  Used via $(eval) by copy_headers.make.
2837# $(1): source header
2838# $(2): destination header
2839define copy-one-header
2840$(2): $(1)
2841	@echo "Header: $$@"
2842	$$(copy-file-to-new-target-with-cp)
2843endef
2844
2845# Define a rule to copy a file.  For use via $(eval).
2846# $(1): source file
2847# $(2): destination file
2848define copy-one-file
2849$(2): $(1)
2850	@echo "Copy: $$@"
2851	$$(copy-file-to-target)
2852endef
2853
2854define copy-and-uncompress-dexs
2855$(2): $(1) $(ZIPALIGN) $(ZIP2ZIP)
2856	@echo "Uncompress dexs in: $$@"
2857	$$(copy-file-to-target)
2858	$$(uncompress-dexs)
2859	$$(align-package)
2860endef
2861
2862# Create copy pair for compatibility suite
2863# Filter out $(LOCAL_INSTALLED_MODULE) to prevent overriding target
2864# $(1): source path
2865# $(2): destination path
2866# The format of copy pair is src:dst
2867define compat-copy-pair
2868$(if $(filter-out $(2), $(LOCAL_INSTALLED_MODULE)), $(1):$(2))
2869endef
2870
2871# Create copy pair for $(1) $(2)
2872# If $(2) is substring of $(3) do nothing.
2873# $(1): source path
2874# $(2): destination path
2875# $(3): filter-out target
2876# The format of copy pair is src:dst
2877define filter-copy-pair
2878$(if $(findstring $(2), $(3)),,$(1):$(2))
2879endef
2880
2881# Copies many files.
2882# $(1): The files to copy.  Each entry is a ':' separated src:dst pair
2883# $(2): An optional directory to prepend to the destination
2884# Evaluates to the list of the dst files (ie suitable for a dependency list)
2885define copy-many-files
2886$(foreach f, $(1), $(strip \
2887    $(eval _cmf_tuple := $(subst :, ,$(f))) \
2888    $(eval _cmf_src := $(word 1,$(_cmf_tuple))) \
2889    $(eval _cmf_dest := $(word 2,$(_cmf_tuple))) \
2890    $(if $(strip $(2)), \
2891      $(eval _cmf_dest := $(patsubst %/,%,$(strip $(2)))/$(patsubst /%,%,$(_cmf_dest)))) \
2892    $(if $(filter-out $(_cmf_src), $(_cmf_dest)), \
2893      $(eval $(call copy-one-file,$(_cmf_src),$(_cmf_dest)))) \
2894    $(_cmf_dest)))
2895endef
2896
2897# Copy the file only if it's a well-formed init script file. For use via $(eval).
2898# $(1): source file
2899# $(2): destination file
2900define copy-init-script-file-checked
2901ifdef TARGET_BUILD_UNBUNDLED
2902# TODO (b/185624993): Remove the chck on TARGET_BUILD_UNBUNDLED when host_init_verifier can run
2903# without requiring the HIDL interface map.
2904$(2): $(1)
2905else ifneq ($(HOST_OS),darwin)
2906# Host init verifier doesn't exist on darwin.
2907$(2): \
2908	$(1) \
2909	$(HOST_INIT_VERIFIER) \
2910	$(call intermediates-dir-for,ETC,passwd_system)/passwd_system \
2911	$(call intermediates-dir-for,ETC,passwd_system_ext)/passwd_system_ext \
2912	$(call intermediates-dir-for,ETC,passwd_vendor)/passwd_vendor \
2913	$(call intermediates-dir-for,ETC,passwd_odm)/passwd_odm \
2914	$(call intermediates-dir-for,ETC,passwd_product)/passwd_product \
2915	$(call intermediates-dir-for,ETC,plat_property_contexts)/plat_property_contexts \
2916	$(call intermediates-dir-for,ETC,system_ext_property_contexts)/system_ext_property_contexts \
2917	$(call intermediates-dir-for,ETC,product_property_contexts)/product_property_contexts \
2918	$(call intermediates-dir-for,ETC,vendor_property_contexts)/vendor_property_contexts \
2919	$(call intermediates-dir-for,ETC,odm_property_contexts)/odm_property_contexts
2920	$(hide) $(HOST_INIT_VERIFIER) \
2921	  -p $(call intermediates-dir-for,ETC,passwd_system)/passwd_system \
2922	  -p $(call intermediates-dir-for,ETC,passwd_system_ext)/passwd_system_ext \
2923	  -p $(call intermediates-dir-for,ETC,passwd_vendor)/passwd_vendor \
2924	  -p $(call intermediates-dir-for,ETC,passwd_odm)/passwd_odm \
2925	  -p $(call intermediates-dir-for,ETC,passwd_product)/passwd_product \
2926	  --property-contexts=$(call intermediates-dir-for,ETC,plat_property_contexts)/plat_property_contexts \
2927	  --property-contexts=$(call intermediates-dir-for,ETC,system_ext_property_contexts)/system_ext_property_contexts \
2928	  --property-contexts=$(call intermediates-dir-for,ETC,product_property_contexts)/product_property_contexts \
2929	  --property-contexts=$(call intermediates-dir-for,ETC,vendor_property_contexts)/vendor_property_contexts \
2930	  --property-contexts=$(call intermediates-dir-for,ETC,odm_property_contexts)/odm_property_contexts \
2931	  $$<
2932else
2933$(2): $(1)
2934endif
2935	@echo "Copy init script: $$@"
2936	$$(copy-file-to-target)
2937endef
2938
2939# Copies many init script files and check they are well-formed.
2940# $(1): The init script files to copy.  Each entry is a ':' separated src:dst pair.
2941# Evaluates to the list of the dst files. (ie suitable for a dependency list.)
2942define copy-many-init-script-files-checked
2943$(foreach f, $(1), $(strip \
2944    $(eval _cmf_tuple := $(subst :, ,$(f))) \
2945    $(eval _cmf_src := $(word 1,$(_cmf_tuple))) \
2946    $(eval _cmf_dest := $(word 2,$(_cmf_tuple))) \
2947    $(eval $(call copy-init-script-file-checked,$(_cmf_src),$(_cmf_dest))) \
2948    $(_cmf_dest)))
2949endef
2950
2951# Copy the file only if it's a well-formed xml file. For use via $(eval).
2952# $(1): source file
2953# $(2): destination file, must end with .xml.
2954define copy-xml-file-checked
2955$(2): $(1) $(XMLLINT)
2956	@echo "Copy xml: $$@"
2957	$(hide) $(XMLLINT) $$< >/dev/null  # Don't print the xml file to stdout.
2958	$$(copy-file-to-target)
2959endef
2960
2961# Copies many xml files and check they are well-formed.
2962# $(1): The xml files to copy.  Each entry is a ':' separated src:dst pair.
2963# Evaluates to the list of the dst files. (ie suitable for a dependency list.)
2964define copy-many-xml-files-checked
2965$(foreach f, $(1), $(strip \
2966    $(eval _cmf_tuple := $(subst :, ,$(f))) \
2967    $(eval _cmf_src := $(word 1,$(_cmf_tuple))) \
2968    $(eval _cmf_dest := $(word 2,$(_cmf_tuple))) \
2969    $(eval $(call copy-xml-file-checked,$(_cmf_src),$(_cmf_dest))) \
2970    $(_cmf_dest)))
2971endef
2972
2973# Copy the file only if it is a well-formed manifest file. For use viea $(eval)
2974# $(1): source file
2975# $(2): destination file
2976define copy-vintf-manifest-checked
2977$(2): $(1) $(HOST_OUT_EXECUTABLES)/assemble_vintf
2978	@echo "Copy xml: $$@"
2979	$(hide) $(HOST_OUT_EXECUTABLES)/assemble_vintf -i $$< >/dev/null  # Don't print the xml file to stdout.
2980	$$(copy-file-to-target)
2981endef
2982
2983# Copies many vintf manifest files checked.
2984# $(1): The files to copy.  Each entry is a ':' separated src:dst pair
2985# Evaluates to the list of the dst files (ie suitable for a dependency list)
2986define copy-many-vintf-manifest-files-checked
2987$(foreach f, $(1), $(strip \
2988    $(eval _cmf_tuple := $(subst :, ,$(f))) \
2989    $(eval _cmf_src := $(word 1,$(_cmf_tuple))) \
2990    $(eval _cmf_dest := $(word 2,$(_cmf_tuple))) \
2991    $(eval $(call copy-vintf-manifest-checked,$(_cmf_src),$(_cmf_dest))) \
2992    $(_cmf_dest)))
2993endef
2994
2995# Copy the file only if it's not an ELF file. For use via $(eval).
2996# $(1): source file
2997# $(2): destination file
2998# $(3): message to print on error
2999define copy-non-elf-file-checked
3000$(eval check_non_elf_file_timestamp := \
3001    $(call intermediates-dir-for,FAKE,check-non-elf-file-timestamps)/$(2).timestamp)
3002$(check_non_elf_file_timestamp): $(1) $(LLVM_READOBJ)
3003	@echo "Check non-ELF: $$<"
3004	$(hide) mkdir -p "$$(dir $$@)"
3005	$(hide) rm -f "$$@"
3006	$(hide) \
3007	    if $(LLVM_READOBJ) -h "$$<" >/dev/null 2>&1; then \
3008	        $(call echo-error,$(2),$(3)); \
3009	        $(call echo-error,$(2),found ELF file: $$<); \
3010	        false; \
3011	    fi
3012	$(hide) touch "$$@"
3013
3014$(2): $(1) $(check_non_elf_file_timestamp)
3015	@echo "Copy non-ELF: $$@"
3016	$$(copy-file-to-target)
3017
3018check-elf-prebuilt-product-copy-files: $(check_non_elf_file_timestamp)
3019endef
3020
3021# The -t option to acp and the -p option to cp is
3022# required for OSX.  OSX has a ridiculous restriction
3023# where it's an error for a .a file's modification time
3024# to disagree with an internal timestamp, and this
3025# macro is used to install .a files (among other things).
3026
3027# Copy a single file from one place to another,
3028# preserving permissions and overwriting any existing
3029# file.
3030# When we used acp, it could not handle high resolution timestamps
3031# on file systems like ext4. Because of that, '-t' option was disabled
3032# and copy-file-to-target was identical to copy-file-to-new-target.
3033# Keep the behavior until we audit and ensure that switching this back
3034# won't break anything.
3035define copy-file-to-target
3036@mkdir -p $(dir $@)
3037$(hide) rm -f $@
3038$(hide) cp "$<" "$@"
3039endef
3040
3041# The same as copy-file-to-target, but use the local
3042# cp command instead of acp.
3043define copy-file-to-target-with-cp
3044@mkdir -p $(dir $@)
3045$(hide) rm -f $@
3046$(hide) cp -p "$<" "$@"
3047endef
3048
3049# The same as copy-file-to-target, but strip out "# comment"-style
3050# comments (for config files and such).
3051define copy-file-to-target-strip-comments
3052@mkdir -p $(dir $@)
3053$(hide) rm -f $@
3054$(hide) sed -e 's/#.*$$//' -e 's/[ \t]*$$//' -e '/^$$/d' < $< > $@
3055endef
3056
3057# The same as copy-file-to-target, but don't preserve
3058# the old modification time.
3059define copy-file-to-new-target
3060@mkdir -p $(dir $@)
3061$(hide) rm -f $@
3062$(hide) cp $< $@
3063endef
3064
3065# The same as copy-file-to-new-target, but use the local
3066# cp command instead of acp.
3067define copy-file-to-new-target-with-cp
3068@mkdir -p $(dir $@)
3069$(hide) rm -f $@
3070$(hide) cp $< $@
3071endef
3072
3073# The same as copy-file-to-new-target, but preserve symlinks. Symlinks are
3074# converted to absolute to not break.
3075define copy-file-or-link-to-new-target
3076@mkdir -p $(dir $@)
3077$(hide) rm -f $@
3078$(hide) if [ -h $< ]; then \
3079  ln -s $$(realpath $<) $@; \
3080else \
3081  cp $< $@; \
3082fi
3083endef
3084
3085# Copy a prebuilt file to a target location.
3086define transform-prebuilt-to-target
3087@echo "$($(PRIVATE_PREFIX)DISPLAY) Prebuilt: $(PRIVATE_MODULE) ($@)"
3088$(copy-file-to-target)
3089endef
3090
3091# Copy a prebuilt file to a target location, stripping "# comment" comments.
3092define transform-prebuilt-to-target-strip-comments
3093@echo "$($(PRIVATE_PREFIX)DISPLAY) Prebuilt: $(PRIVATE_MODULE) ($@)"
3094$(copy-file-to-target-strip-comments)
3095endef
3096
3097# Copy a prebuilt file to a target location, but preserve symlinks rather than
3098# dereference them.
3099define copy-or-link-prebuilt-to-target
3100@echo "$($(PRIVATE_PREFIX)DISPLAY) Prebuilt: $(PRIVATE_MODULE) ($@)"
3101$(copy-file-or-link-to-new-target)
3102endef
3103
3104# Copy a list of files/directories to target location, with sub dir structure preserved.
3105# For example $(HOST_OUT_EXECUTABLES)/aapt -> $(staging)/bin/aapt .
3106# $(1): the source list of files/directories.
3107# $(2): the path prefix to strip. In the above example it would be $(HOST_OUT).
3108# $(3): the target location.
3109define copy-files-with-structure
3110$(foreach t,$(1),\
3111  $(eval s := $(patsubst $(2)%,%,$(t)))\
3112  $(hide) mkdir -p $(dir $(3)/$(s)); cp -Rf $(t) $(3)/$(s)$(newline))
3113endef
3114
3115# Define a rule to create a symlink to a file.
3116# $(1): any dependencies
3117# $(2): source (may be relative)
3118# $(3): full path to destination
3119define symlink-file
3120$(eval $(_symlink-file))
3121$(eval $(call declare-license-metadata,$(3),,,,,,))
3122$(eval $(call declare-license-deps,$(3),$(1)))
3123endef
3124
3125define _symlink-file
3126$(3): $(1)
3127	@echo "Symlink: $$@ -> $(2)"
3128	@mkdir -p $$(dir $$@)
3129	@rm -rf $$@
3130	$(hide) ln -sf $(2) $$@
3131$(3): .KATI_SYMLINK_OUTPUTS := $(3)
3132endef
3133
3134# Copy an apk to a target location while removing classes*.dex
3135# $(1): source file
3136# $(2): destination file
3137# $(3): LOCAL_STRIP_DEX, if non-empty then strip classes*.dex
3138define dexpreopt-copy-jar
3139$(2): $(1)
3140	@echo "Copy: $$@"
3141	$$(copy-file-to-target)
3142	$(if $(3),$$(call dexpreopt-remove-classes.dex,$$@))
3143endef
3144
3145# $(1): the .jar or .apk to remove classes.dex. Note that if all dex files
3146# are uncompressed in the archive, then dexopt will not do a copy of the dex
3147# files and we should not strip.
3148define dexpreopt-remove-classes.dex
3149$(hide) if (zipinfo $1 '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then \
3150zip --quiet --delete $(1) classes.dex; \
3151dex_index=2; \
3152while zip --quiet --delete $(1) classes$${dex_index}.dex > /dev/null; do \
3153  let dex_index=dex_index+1; \
3154done \
3155fi
3156endef
3157
3158# Copy an unstripped binary to the symbols directory while also extracting
3159# a hash mapping to the mapping directory.
3160# $(1): unstripped intermediates file
3161# $(2): path in symbols directory
3162define copy-unstripped-elf-file-with-mapping
3163$(call _copy-symbols-file-with-mapping,$(1),$(2),\
3164  elf,$(patsubst $(TARGET_OUT_UNSTRIPPED)/%,$(call intermediates-dir-for,PACKAGING,elf_symbol_mapping)/%,$(2).textproto))
3165endef
3166
3167# Copy an R8 dictionary to the packaging directory while also extracting
3168# a hash mapping to the mapping directory.
3169# $(1): unstripped intermediates file
3170# $(2): path in packaging directory
3171# $(3): path in mappings packaging directory
3172define copy-r8-dictionary-file-with-mapping
3173$(call _copy-symbols-file-with-mapping,$(1),$(2),r8,$(3))
3174endef
3175
3176# Copy an unstripped binary or R8 dictionary to the symbols directory
3177# while also extracting a hash mapping to the mapping directory.
3178# $(1): unstripped intermediates file
3179# $(2): path in symbols directory
3180# $(3): file type (elf or r8)
3181# $(4): path in the mappings directory
3182define _copy-symbols-file-with-mapping
3183$(2): .KATI_IMPLICIT_OUTPUTS := $(4)
3184$(2): $(SYMBOLS_MAP)
3185$(2): $(1)
3186	@echo "Copy symbols with mapping: $$@"
3187	$$(copy-file-to-target)
3188	$(SYMBOLS_MAP) -$(strip $(3)) $(2) -write_if_changed $(4)
3189.KATI_RESTAT: $(2)
3190endef
3191
3192# Returns the directory to copy proguard dictionaries into
3193define local-proguard-dictionary-directory
3194$(call intermediates-dir-for,PACKAGING,proguard_dictionary)/out/target/common/obj/$(LOCAL_MODULE_CLASS)/$(LOCAL_MODULE)_intermediates
3195endef
3196
3197# Returns the directory to copy proguard dictionary mappings into
3198define local-proguard-dictionary-mapping-directory
3199$(call intermediates-dir-for,PACKAGING,proguard_dictionary_mapping)/out/target/common/obj/$(LOCAL_MODULE_CLASS)/$(LOCAL_MODULE)_intermediates
3200endef
3201
3202
3203###########################################################
3204## Commands to call R8
3205###########################################################
3206
3207# Use --debug flag for eng builds by default
3208ifeq (eng,$(TARGET_BUILD_VARIANT))
3209R8_DEBUG_MODE := --debug
3210else
3211R8_DEBUG_MODE :=
3212endif
3213
3214define transform-jar-to-dex-r8
3215@echo R8: $@
3216$(hide) rm -f $(PRIVATE_PROGUARD_DICTIONARY)
3217$(hide) $(R8_WRAPPER) $(R8_COMPAT_PROGUARD) $(DEX_FLAGS) \
3218    -injars '$<' \
3219    --min-api $(PRIVATE_MIN_SDK_VERSION) \
3220    --no-data-resources \
3221    --force-proguard-compatibility --output $(subst classes.dex,,$@) \
3222    $(R8_DEBUG_MODE) \
3223    $(PRIVATE_PROGUARD_FLAGS) \
3224    $(addprefix -injars , $(PRIVATE_EXTRA_INPUT_JAR)) \
3225    $(PRIVATE_DX_FLAGS) \
3226    -ignorewarnings
3227$(hide) touch $(PRIVATE_PROGUARD_DICTIONARY)
3228endef
3229
3230###########################################################
3231## Stuff source generated from one-off tools
3232###########################################################
3233
3234define transform-generated-source
3235@echo "$($(PRIVATE_PREFIX)DISPLAY) Generated: $(PRIVATE_MODULE) <= $<"
3236@mkdir -p $(dir $@)
3237$(hide) $(PRIVATE_CUSTOM_TOOL)
3238endef
3239
3240
3241###########################################################
3242## Assertions about attributes of the target
3243###########################################################
3244
3245# $(1): The file to check
3246define get-file-size
3247stat -c "%s" "$(1)" | tr -d '\n'
3248endef
3249
3250# $(1): The file(s) to check (often $@)
3251# $(2): The partition size.
3252define assert-max-image-size
3253$(if $(2), \
3254  size=$$(for i in $(1); do $(call get-file-size,$$i); echo +; done; echo 0); \
3255  total=$$(( $$( echo "$$size" ) )); \
3256  printname=$$(echo -n "$(1)" | tr " " +); \
3257  maxsize=$$(($(2))); \
3258  if [ "$$total" -gt "$$maxsize" ]; then \
3259    echo "error: $$printname too large ($$total > $$maxsize)"; \
3260    false; \
3261  elif [ "$$total" -gt $$((maxsize - 32768)) ]; then \
3262    echo "WARNING: $$printname approaching size limit ($$total now; limit $$maxsize)"; \
3263  fi \
3264 , \
3265  true \
3266 )
3267endef
3268
3269
3270###########################################################
3271## Define device-specific radio files
3272###########################################################
3273INSTALLED_RADIOIMAGE_TARGET :=
3274
3275# Copy a radio image file to the output location, and add it to
3276# INSTALLED_RADIOIMAGE_TARGET.
3277# $(1): filename
3278define add-radio-file
3279  $(eval $(call add-radio-file-internal,$(1),$(notdir $(1))))
3280endef
3281define add-radio-file-internal
3282INSTALLED_RADIOIMAGE_TARGET += $$(PRODUCT_OUT)/$(2)
3283$$(PRODUCT_OUT)/$(2) : $$(LOCAL_PATH)/$(1)
3284	$$(transform-prebuilt-to-target)
3285endef
3286
3287# Version of add-radio-file that also arranges for the version of the
3288# file to be checked against the contents of
3289# $(TARGET_BOARD_INFO_FILE).
3290# $(1): filename
3291# $(2): name of version variable in board-info (eg, "version-baseband")
3292define add-radio-file-checked
3293  $(eval $(call add-radio-file-checked-internal,$(1),$(notdir $(1)),$(2)))
3294endef
3295define add-radio-file-checked-internal
3296INSTALLED_RADIOIMAGE_TARGET += $$(PRODUCT_OUT)/$(2)
3297BOARD_INFO_CHECK += $(3):$(LOCAL_PATH)/$(1)
3298$$(PRODUCT_OUT)/$(2) : $$(LOCAL_PATH)/$(1)
3299	$$(transform-prebuilt-to-target)
3300endef
3301
3302## Whether to build from source if prebuilt alternative exists
3303###########################################################
3304# $(1): module name
3305# $(2): LOCAL_PATH
3306# Expands to empty string if not from source.
3307ifeq (true,$(ANDROID_BUILD_FROM_SOURCE))
3308define if-build-from-source
3309true
3310endef
3311else
3312define if-build-from-source
3313$(if $(filter $(ANDROID_NO_PREBUILT_MODULES),$(1))$(filter \
3314    $(addsuffix %,$(ANDROID_NO_PREBUILT_PATHS)),$(2)),true)
3315endef
3316endif
3317
3318# Include makefile $(1) if build from source for module $(2)
3319# $(1): the makefile to include
3320# $(2): module name
3321# $(3): LOCAL_PATH
3322define include-if-build-from-source
3323$(if $(call if-build-from-source,$(2),$(3)),$(eval include $(1)))
3324endef
3325
3326# Return the arch for the source file of a prebuilt
3327# Return "none" if no matching arch found and return empty
3328# if the input is empty, so the result can be passed to
3329# LOCAL_MODULE_TARGET_ARCH.
3330# $(1) the list of archs supported by the prebuilt
3331define get-prebuilt-src-arch
3332$(strip $(if $(filter $(TARGET_ARCH),$(1)),$(TARGET_ARCH),\
3333  $(if $(filter $(TARGET_2ND_ARCH),$(1)),$(TARGET_2ND_ARCH),$(if $(1),none))))
3334endef
3335
3336# ###############################################################
3337# Set up statistics gathering
3338# ###############################################################
3339STATS.MODULE_TYPE := \
3340  HOST_STATIC_LIBRARY \
3341  HOST_SHARED_LIBRARY \
3342  STATIC_LIBRARY \
3343  SHARED_LIBRARY \
3344  EXECUTABLE \
3345  HOST_EXECUTABLE \
3346  PACKAGE \
3347  PHONY_PACKAGE \
3348  HOST_PREBUILT \
3349  PREBUILT \
3350  MULTI_PREBUILT \
3351  JAVA_LIBRARY \
3352  STATIC_JAVA_LIBRARY \
3353  HOST_JAVA_LIBRARY \
3354  DROIDDOC \
3355  COPY_HEADERS \
3356  NATIVE_TEST \
3357  NATIVE_BENCHMARK \
3358  HOST_NATIVE_TEST \
3359  FUZZ_TEST \
3360  HOST_FUZZ_TEST \
3361  STATIC_TEST_LIBRARY \
3362  HOST_STATIC_TEST_LIBRARY \
3363  NOTICE_FILE \
3364  HOST_DALVIK_JAVA_LIBRARY \
3365  HOST_DALVIK_STATIC_JAVA_LIBRARY \
3366  base_rules \
3367  HEADER_LIBRARY \
3368  HOST_TEST_CONFIG \
3369  TARGET_TEST_CONFIG
3370
3371$(foreach s,$(STATS.MODULE_TYPE),$(eval STATS.MODULE_TYPE.$(s) :=))
3372define record-module-type
3373$(strip $(if $(LOCAL_RECORDED_MODULE_TYPE),,
3374  $(if $(filter-out $(SOONG_ANDROID_MK),$(LOCAL_MODULE_MAKEFILE)),
3375    $(if $(filter $(1),$(STATS.MODULE_TYPE)),
3376      $(eval LOCAL_RECORDED_MODULE_TYPE := true)
3377        $(eval STATS.MODULE_TYPE.$(1) += 1),
3378      $(error Invalid module type: $(1))))))
3379endef
3380
3381###########################################################
3382## Compatibility suite tools
3383###########################################################
3384
3385# Return a list of output directories for a given suite and the current LOCAL_MODULE.
3386# Can be passed a subdirectory to use for the common testcase directory.
3387define compatibility_suite_dirs
3388  $(strip \
3389    $(if $(COMPATIBILITY_TESTCASES_OUT_$(1)), \
3390      $(if $(COMPATIBILITY_TESTCASES_OUT_INCLUDE_MODULE_FOLDER_$(1))$(LOCAL_COMPATIBILITY_PER_TESTCASE_DIRECTORY),\
3391        $(COMPATIBILITY_TESTCASES_OUT_$(1))/$(LOCAL_MODULE)$(2),\
3392        $(COMPATIBILITY_TESTCASES_OUT_$(1)))) \
3393    $($(my_prefix)OUT_TESTCASES)/$(LOCAL_MODULE)$(2))
3394endef
3395
3396# For each suite:
3397# 1. Copy the files to the many suite output directories.
3398#    And for test config files, we'll check the .xml is well-formed before copy.
3399# 2. Add all the files to each suite's dependent files list.
3400# 3. Do the dependency addition to my_all_targets.
3401# 4. Save the module name to COMPATIBILITY.$(suite).MODULES for each suite.
3402# 5. Collect files to dist to ALL_COMPATIBILITY_DIST_FILES.
3403# Requires for each suite: use my_compat_dist_config_$(suite) to define the test config.
3404#    and use my_compat_dist_$(suite) to define the others.
3405define create-suite-dependencies
3406$(foreach suite, $(LOCAL_COMPATIBILITY_SUITE), \
3407  $(eval $(if $(strip $(module_license_metadata)),\
3408    $$(foreach f,$$(my_compat_dist_$(suite)),$$(eval ALL_TARGETS.$$(call word-colon,2,$$(f)).META_LIC := $(module_license_metadata))),\
3409    $$(eval my_test_data += $$(foreach f,$$(my_compat_dist_$(suite)), $$(call word-colon,2,$$(f)))) \
3410  )) \
3411  $(eval $(if $(strip $(module_license_metadata)),\
3412    $$(foreach f,$$(my_compat_dist_config_$(suite)),$$(eval ALL_TARGETS.$$(call word-colon,2,$$(f)).META_LIC := $(module_license_metadata))),\
3413    $$(eval my_test_config += $$(foreach f,$$(my_compat_dist_config_$(suite)), $$(call word-colon,2,$$(f)))) \
3414  )) \
3415  $(if $(filter $(suite),$(ALL_COMPATIBILITY_SUITES)),,\
3416    $(eval ALL_COMPATIBILITY_SUITES += $(suite)) \
3417    $(eval COMPATIBILITY.$(suite).FILES :=) \
3418    $(eval COMPATIBILITY.$(suite).MODULES :=)) \
3419  $(eval COMPATIBILITY.$(suite).FILES += \
3420    $$(foreach f,$$(my_compat_dist_$(suite)),$$(call word-colon,2,$$(f))) \
3421    $$(foreach f,$$(my_compat_dist_config_$(suite)),$$(call word-colon,2,$$(f))) \
3422    $$(my_compat_dist_test_data_$(suite))) \
3423  $(eval ALL_COMPATIBILITY_DIST_FILES += $$(my_compat_dist_$(suite))) \
3424  $(eval COMPATIBILITY.$(suite).MODULES += $$(my_register_name))) \
3425$(eval $(my_all_targets) : \
3426  $(sort $(foreach suite,$(LOCAL_COMPATIBILITY_SUITE), \
3427    $(foreach f,$(my_compat_dist_$(suite)), $(call word-colon,2,$(f))))) \
3428  $(call copy-many-xml-files-checked, \
3429    $(sort $(foreach suite,$(LOCAL_COMPATIBILITY_SUITE),$(my_compat_dist_config_$(suite))))))
3430endef
3431
3432###########################################################
3433## Path Cleaning
3434###########################################################
3435
3436# Remove "dir .." combinations (but keep ".. ..")
3437#
3438# $(1): The expanded path, where / is converted to ' ' to work with $(word)
3439define _clean-path-strip-dotdot
3440$(strip \
3441  $(if $(word 2,$(1)),
3442    $(if $(call streq,$(word 2,$(1)),..),
3443      $(if $(call streq,$(word 1,$(1)),..),
3444        $(word 1,$(1)) $(call _clean-path-strip-dotdot,$(wordlist 2,$(words $(1)),$(1)))
3445      ,
3446        $(call _clean-path-strip-dotdot,$(wordlist 3,$(words $(1)),$(1)))
3447      )
3448    ,
3449      $(word 1,$(1)) $(call _clean-path-strip-dotdot,$(wordlist 2,$(words $(1)),$(1)))
3450    )
3451  ,
3452    $(1)
3453  )
3454)
3455endef
3456
3457# Remove any leading .. from the path (in case of /..)
3458#
3459# Should only be called if the original path started with /
3460# $(1): The expanded path, where / is converted to ' ' to work with $(word)
3461define _clean-path-strip-root-dotdots
3462$(strip $(if $(call streq,$(firstword $(1)),..),
3463  $(call _clean-path-strip-root-dotdots,$(wordlist 2,$(words $(1)),$(1))),
3464  $(1)))
3465endef
3466
3467# Call _clean-path-strip-dotdot until the path stops changing
3468# $(1): Non-empty if this path started with a /
3469# $(2): The expanded path, where / is converted to ' ' to work with $(word)
3470define _clean-path-expanded
3471$(strip \
3472  $(eval _ep := $(call _clean-path-strip-dotdot,$(2)))
3473  $(if $(1),$(eval _ep := $(call _clean-path-strip-root-dotdots,$(_ep))))
3474  $(if $(call streq,$(2),$(_ep)),
3475    $(_ep),
3476    $(call _clean-path-expanded,$(1),$(_ep))))
3477endef
3478
3479# Clean the file path -- remove //, dir/.., extra .
3480#
3481# This should be the same semantics as golang's filepath.Clean
3482#
3483# $(1): The file path to clean
3484define clean-path
3485$(strip \
3486  $(if $(call streq,$(words $(1)),1),
3487    $(eval _rooted := $(filter /%,$(1)))
3488    $(eval _expanded_path := $(filter-out .,$(subst /,$(space),$(1))))
3489    $(eval _path := $(if $(_rooted),/)$(subst $(space),/,$(call _clean-path-expanded,$(_rooted),$(_expanded_path))))
3490    $(if $(_path),
3491      $(_path),
3492      .
3493     )
3494  ,
3495    $(if $(call streq,$(words $(1)),0),
3496      .,
3497      $(error Call clean-path with only one path (without spaces))
3498    )
3499  )
3500)
3501endef
3502
3503ifeq ($(TEST_MAKE_clean_path),true)
3504  define my_test
3505    $(if $(call streq,$(call clean-path,$(1)),$(2)),,
3506      $(eval my_failed := true)
3507      $(warning clean-path test '$(1)': expected '$(2)', got '$(call clean-path,$(1))'))
3508  endef
3509  my_failed :=
3510
3511  # Already clean
3512  $(call my_test,abc,abc)
3513  $(call my_test,abc/def,abc/def)
3514  $(call my_test,a/b/c,a/b/c)
3515  $(call my_test,.,.)
3516  $(call my_test,..,..)
3517  $(call my_test,../..,../..)
3518  $(call my_test,../../abc,../../abc)
3519  $(call my_test,/abc,/abc)
3520  $(call my_test,/,/)
3521
3522  # Empty is current dir
3523  $(call my_test,,.)
3524
3525  # Remove trailing slash
3526  $(call my_test,abc/,abc)
3527  $(call my_test,abc/def/,abc/def)
3528  $(call my_test,a/b/c/,a/b/c)
3529  $(call my_test,./,.)
3530  $(call my_test,../,..)
3531  $(call my_test,../../,../..)
3532  $(call my_test,/abc/,/abc)
3533
3534  # Remove doubled slash
3535  $(call my_test,abc//def//ghi,abc/def/ghi)
3536  $(call my_test,//abc,/abc)
3537  $(call my_test,///abc,/abc)
3538  $(call my_test,//abc//,/abc)
3539  $(call my_test,abc//,abc)
3540
3541  # Remove . elements
3542  $(call my_test,abc/./def,abc/def)
3543  $(call my_test,/./abc/def,/abc/def)
3544  $(call my_test,abc/.,abc)
3545
3546  # Remove .. elements
3547  $(call my_test,abc/def/ghi/../jkl,abc/def/jkl)
3548  $(call my_test,abc/def/../ghi/../jkl,abc/jkl)
3549  $(call my_test,abc/def/..,abc)
3550  $(call my_test,abc/def/../..,.)
3551  $(call my_test,/abc/def/../..,/)
3552  $(call my_test,abc/def/../../..,..)
3553  $(call my_test,/abc/def/../../..,/)
3554  $(call my_test,abc/def/../../../ghi/jkl/../../../mno,../../mno)
3555  $(call my_test,/../abc,/abc)
3556
3557  # Combinations
3558  $(call my_test,abc/./../def,def)
3559  $(call my_test,abc//./../def,def)
3560  $(call my_test,abc/../../././../def,../../def)
3561
3562  ifdef my_failed
3563    $(error failed clean-path test)
3564  endif
3565endif
3566
3567###########################################################
3568## Given a filepath, returns nonempty if the path cannot be
3569## validated to be contained in the current directory
3570## This is, this function checks for '/' and '..'
3571##
3572## $(1): path to validate
3573define try-validate-path-is-subdir
3574$(strip \
3575    $(if $(filter /%,$(1)),
3576        $(1) starts with a slash
3577    )
3578    $(if $(filter ../%,$(call clean-path,$(1))),
3579        $(1) escapes its parent using '..'
3580    )
3581    $(if $(strip $(1)),
3582    ,
3583        '$(1)' is empty
3584    )
3585)
3586endef
3587
3588define validate-path-is-subdir
3589$(if $(call try-validate-path-is-subdir,$(1)),
3590  $(call pretty-error, Illegal path: $(call try-validate-path-is-subdir,$(1)))
3591)
3592endef
3593
3594###########################################################
3595## Given a space-delimited list of filepaths, returns
3596## nonempty if any cannot be validated to be contained in
3597## the current directory
3598##
3599## $(1): path list to validate
3600define try-validate-paths-are-subdirs
3601$(strip \
3602  $(foreach my_path,$(1),\
3603    $(call try-validate-path-is-subdir,$(my_path))\
3604  )
3605)
3606endef
3607
3608define validate-paths-are-subdirs
3609$(if $(call try-validate-paths-are-subdirs,$(1)),
3610    $(call pretty-error,Illegal paths:\'$(call try-validate-paths-are-subdirs,$(1))\')
3611)
3612endef
3613
3614###########################################################
3615## Tests of try-validate-path-is-subdir
3616##     and  try-validate-paths-are-subdirs
3617define test-validate-paths-are-subdirs
3618$(eval my_error := $(call try-validate-path-is-subdir,/tmp)) \
3619$(if $(call streq,$(my_error),/tmp starts with a slash),
3620,
3621  $(error incorrect error message for path /tmp. Got '$(my_error)')
3622) \
3623$(eval my_error := $(call try-validate-path-is-subdir,../sibling)) \
3624$(if $(call streq,$(my_error),../sibling escapes its parent using '..'),
3625,
3626  $(error incorrect error message for path ../sibling. Got '$(my_error)')
3627) \
3628$(eval my_error := $(call try-validate-path-is-subdir,child/../../sibling)) \
3629$(if $(call streq,$(my_error),child/../../sibling escapes its parent using '..'),
3630,
3631  $(error incorrect error message for path child/../../sibling. Got '$(my_error)')
3632) \
3633$(eval my_error := $(call try-validate-path-is-subdir,)) \
3634$(if $(call streq,$(my_error),'' is empty),
3635,
3636  $(error incorrect error message for empty path ''. Got '$(my_error)')
3637) \
3638$(eval my_error := $(call try-validate-path-is-subdir,subdir/subsubdir)) \
3639$(if $(call streq,$(my_error),),
3640,
3641  $(error rejected valid path 'subdir/subsubdir'. Got '$(my_error)')
3642)
3643
3644$(eval my_error := $(call try-validate-paths-are-subdirs,a/b /c/d e/f))
3645$(if $(call streq,$(my_error),/c/d starts with a slash),
3646,
3647  $(error incorrect error message for path list 'a/b /c/d e/f'. Got '$(my_error)')
3648)
3649$(eval my_error := $(call try-validate-paths-are-subdirs,a/b c/d))
3650$(if $(call streq,$(my_error),),
3651,
3652  $(error rejected valid path list 'a/b c/d'. Got '$(my_error)')
3653)
3654endef
3655# run test
3656$(strip $(call test-validate-paths-are-subdirs))
3657
3658###########################################################
3659## Validate jacoco class filters and convert them to
3660## file arguments
3661## Jacoco class filters are comma-separated lists of class
3662## files (android.app.Application), and may have '*' as the
3663## last character to match all classes in a package
3664## including subpackages.
3665define jacoco-class-filter-to-file-args
3666$(strip $(call jacoco-validate-file-args,\
3667  $(subst $(comma),$(space),\
3668    $(subst .,/,\
3669      $(strip $(1))))))
3670endef
3671
3672define jacoco-validate-file-args
3673$(strip $(1)\
3674  $(call validate-paths-are-subdirs,$(1))
3675  $(foreach arg,$(1),\
3676    $(if $(findstring ?,$(arg)),$(call pretty-error,\
3677      '?' filters are not supported in LOCAL_JACK_COVERAGE_INCLUDE_FILTER or LOCAL_JACK_COVERAGE_EXCLUDE_FILTER))\
3678    $(if $(findstring *,$(patsubst %*,%,$(arg))),$(call pretty-error,\
3679      '*' is only supported at the end of a filter in LOCAL_JACK_COVERAGE_INCLUDE_FILTER or LOCAL_JACK_COVERAGE_EXCLUDE_FILTER))\
3680  ))
3681endef
3682
3683###########################################################
3684## Other includes
3685###########################################################
3686
3687# Include any vendor specific definitions.mk file
3688-include $(TOPDIR)vendor/*/build/core/definitions.mk
3689-include $(TOPDIR)device/*/build/core/definitions.mk
3690-include $(TOPDIR)product/*/build/core/definitions.mk
3691
3692# broken:
3693#	$(foreach file,$^,$(if $(findstring,.a,$(suffix $file)),-l$(file),$(file)))
3694
3695###########################################################
3696## Misc notes
3697###########################################################
3698
3699#DEPDIR = .deps
3700#df = $(DEPDIR)/$(*F)
3701
3702#SRCS = foo.c bar.c ...
3703
3704#%.o : %.c
3705#	@$(MAKEDEPEND); \
3706#	  cp $(df).d $(df).P; \
3707#	  sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
3708#	      -e '/^$$/ d' -e 's/$$/ :/' < $(df).d >> $(df).P; \
3709#	  rm -f $(df).d
3710#	$(COMPILE.c) -o $@ $<
3711
3712#-include $(SRCS:%.c=$(DEPDIR)/%.P)
3713
3714
3715#%.o : %.c
3716#	$(COMPILE.c) -MD -o $@ $<
3717#	@cp $*.d $*.P; \
3718#	  sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
3719#	      -e '/^$$/ d' -e 's/$$/ :/' < $*.d >> $*.P; \
3720#	  rm -f $*.d
3721
3722
3723###########################################################
3724# Append the information to generate a RRO package for the
3725# source module.
3726#
3727#  $(1): Source module name.
3728#  $(2): Whether $(3) is a manifest package name or not.
3729#  $(3): Manifest package name if $(2) is true.
3730#        Otherwise, android manifest file path of the
3731#        source module.
3732#  $(4): Whether LOCAL_EXPORT_PACKAGE_RESOURCES is set or
3733#        not for the source module.
3734#  $(5): Resource overlay list.
3735#  $(6): Target partition
3736###########################################################
3737define append_enforce_rro_sources
3738  $(eval ENFORCE_RRO_SOURCES += \
3739      $(strip $(1))||$(strip $(2))||$(strip $(3))||$(strip $(4))||$(call normalize-path-list, $(strip $(5)))||$(strip $(6)) \
3740  )
3741endef
3742
3743###########################################################
3744# Generate all RRO packages for source modules stored in
3745# ENFORCE_RRO_SOURCES
3746###########################################################
3747define generate_all_enforce_rro_packages
3748$(foreach source,$(ENFORCE_RRO_SOURCES), \
3749  $(eval _o := $(subst ||,$(space),$(source))) \
3750  $(eval enforce_rro_source_module := $(word 1,$(_o))) \
3751  $(eval enforce_rro_source_is_manifest_package_name := $(word 2,$(_o))) \
3752  $(eval enforce_rro_source_manifest_package_info := $(word 3,$(_o))) \
3753  $(eval enforce_rro_use_res_lib := $(word 4,$(_o))) \
3754  $(eval enforce_rro_source_overlays := $(subst :, ,$(word 5,$(_o)))) \
3755  $(eval enforce_rro_partition := $(word 6,$(_o))) \
3756  $(eval include $(BUILD_SYSTEM)/generate_enforce_rro.mk) \
3757  $(eval ALL_MODULES.$$(enforce_rro_source_module).REQUIRED_FROM_TARGET += $$(LOCAL_PACKAGE_NAME)) \
3758)
3759endef
3760
3761###########################################################
3762## Find system_$(VER) in LOCAL_SDK_VERSION
3763## note: system_server_* is excluded. It's a different API surface
3764##
3765## $(1): LOCAL_SDK_VERSION
3766###########################################################
3767define has-system-sdk-version
3768$(filter-out system_server_%,$(filter system_%,$(1)))
3769endef
3770
3771###########################################################
3772## Get numerical version in LOCAL_SDK_VERSION
3773##
3774## $(1): LOCAL_SDK_VERSION
3775###########################################################
3776define get-numeric-sdk-version
3777$(filter-out current,\
3778  $(if $(call has-system-sdk-version,$(1)),$(patsubst system_%,%,$(1)),$(1)))
3779endef
3780
3781###########################################################
3782## Verify module name meets character requirements:
3783##   a-z A-Z 0-9
3784##   _.+-,@~
3785##
3786## This is a subset of bazel's target name restrictions:
3787##   https://docs.bazel.build/versions/master/build-ref.html#name
3788##
3789## Kati has problems with '=': https://github.com/google/kati/issues/138
3790###########################################################
3791define verify-module-name
3792$(if $(filter-out $(LOCAL_MODULE),$(subst /,,$(LOCAL_MODULE))), \
3793  $(call pretty-warning,Module name contains a /$(comma) use LOCAL_MODULE_STEM and LOCAL_MODULE_RELATIVE_PATH instead)) \
3794$(if $(call _invalid-name-chars,$(LOCAL_MODULE)), \
3795  $(call pretty-error,Invalid characters in module name: $(call _invalid-name-chars,$(LOCAL_MODULE))))
3796endef
3797define _invalid-name-chars
3798$(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)))))))))))))))))))))))))))))))))))))))))))))
3799endef
3800.KATI_READONLY := verify-module-name _invalid-name-chars
3801
3802###########################################################
3803## Verify module stem meets character requirements:
3804##   a-z A-Z 0-9
3805##   _.+-,@~
3806##
3807## This is a subset of bazel's target name restrictions:
3808##   https://docs.bazel.build/versions/master/build-ref.html#name
3809##
3810## $(1): The module stem variable to check
3811###########################################################
3812define verify-module-stem
3813$(if $(filter-out $($(1)),$(subst /,,$($(1)))), \
3814  $(call pretty-warning,Module stem \($(1)\) contains a /$(comma) use LOCAL_MODULE_RELATIVE_PATH instead)) \
3815$(if $(call _invalid-name-chars,$($(1))), \
3816  $(call pretty-error,Invalid characters in module stem \($(1)\): $(call _invalid-name-chars,$($(1)))))
3817endef
3818.KATI_READONLY := verify-module-stem
3819
3820$(KATI_obsolete_var \
3821  create-empty-package \
3822  initialize-package-file \
3823  add-jni-shared-libs-to-package \
3824  inherit-package,\
3825  These functions have been removed)
3826
3827###########################################################
3828## Verify the variants of a VNDK library are identical
3829##
3830## $(1): Path to the core variant shared library file.
3831## $(2): Path to the vendor variant shared library file.
3832## $(3): TOOLS_PREFIX
3833###########################################################
3834LIBRARY_IDENTITY_CHECK_SCRIPT := build/make/tools/check_identical_lib.sh
3835define verify-vndk-libs-identical
3836@echo "Checking VNDK vendor variant: $(2)"
3837$(hide) CLANG_BIN="$(LLVM_PREBUILTS_PATH)" \
3838  CROSS_COMPILE="$(strip $(3))" \
3839  XZ="$(XZ)" \
3840  $(LIBRARY_IDENTITY_CHECK_SCRIPT) $(SOONG_STRIP_PATH) $(1) $(2)
3841endef
3842
3843# Convert Soong libraries that have SDK variant
3844define use_soong_sdk_libraries
3845  $(foreach l,$(1),$(if $(filter $(l),$(SOONG_SDK_VARIANT_MODULES)),\
3846      $(l).sdk,$(l)))
3847endef
3848