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