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