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