OSDN Git Service

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