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