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