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