• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (C) 2009 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14#
15# Common definitions for the Android NDK build system
16#
17
18# We use the GNU Make Standard Library
19include $(NDK_ROOT)/build/gmsl/gmsl
20
21include $(BUILD_SYSTEM)/definitions-tests.mk
22include $(BUILD_SYSTEM)/definitions-utils.mk
23include $(BUILD_SYSTEM)/definitions-host.mk
24include $(BUILD_SYSTEM)/definitions-graph.mk
25
26# -----------------------------------------------------------------------------
27# Macro    : this-makefile
28# Returns  : the name of the current Makefile in the inclusion stack
29# Usage    : $(this-makefile)
30# -----------------------------------------------------------------------------
31this-makefile = $(lastword $(MAKEFILE_LIST))
32
33# -----------------------------------------------------------------------------
34# Macro    : local-makefile
35# Returns  : the name of the last parsed Android.mk file
36# Usage    : $(local-makefile)
37# -----------------------------------------------------------------------------
38local-makefile = $(lastword $(filter %Android.mk,$(MAKEFILE_LIST)))
39
40# -----------------------------------------------------------------------------
41# Function : assert-defined
42# Arguments: 1: list of variable names
43# Returns  : None
44# Usage    : $(call assert-defined, VAR1 VAR2 VAR3...)
45# Rationale: Checks that all variables listed in $1 are defined, or abort the
46#            build
47# -----------------------------------------------------------------------------
48assert-defined = $(foreach __varname,$(strip $1),\
49  $(if $(strip $($(__varname))),,\
50    $(call __ndk_error, Assertion failure: $(__varname) is not defined)\
51  )\
52)
53
54# -----------------------------------------------------------------------------
55# Function : check-required-vars
56# Arguments: 1: list of variable names
57#            2: file where the variable(s) should be defined
58# Returns  : None
59# Usage    : $(call check-required-vars, VAR1 VAR2 VAR3..., <file>)
60# Rationale: Checks that all required vars listed in $1 were defined by $2
61#            or abort the build with an error
62# -----------------------------------------------------------------------------
63check-required-vars = $(foreach __varname,$1,\
64  $(if $(strip $($(__varname))),,\
65    $(call __ndk_info, Required variable $(__varname) is not defined by $2)\
66    $(call __ndk_error,Aborting)\
67  )\
68)
69
70# The list of default C++ extensions supported by GCC.
71default-c++-extensions := .cc .cp .cxx .cpp .CPP .c++ .C
72
73# -----------------------------------------------------------------------------
74# Function : generate-dir
75# Arguments: 1: directory path
76# Returns  : Generate a rule, but not dependency, to create a directory with
77#            host-mkdir.
78# Usage    : $(call generate-dir,<path>)
79# -----------------------------------------------------------------------------
80define ev-generate-dir
81__ndk_dir := $1
82ifeq (,$$(__ndk_dir_flag__$$(__ndk_dir)))
83# Note that the following doesn't work because path in windows may contain
84# ':' if ndk-build is called inside jni/ directory when path is expanded
85# to full-path, eg. C:/path/to/project/jni/
86#
87#    __ndk_dir_flag__$1 := true
88#
89__ndk_dir_flag__$$(__ndk_dir) := true
90$1:
91	@$$(call host-mkdir,$$@)
92endif
93endef
94
95generate-dir = $(eval $(call ev-generate-dir,$1))
96
97# -----------------------------------------------------------------------------
98# Function : generate-file-dir
99# Arguments: 1: file path
100# Returns  : Generate a dependency and a rule to ensure that the parent
101#            directory of the input file path will be created before it.
102#            This is used to enforce a call to host-mkdir.
103# Usage    : $(call generate-file-dir,<file>)
104# Rationale: Many object files will be stored in the same output directory.
105#            Introducing a dependency on the latter avoids calling mkdir -p
106#            for every one of them.
107#
108# -----------------------------------------------------------------------------
109
110define ev-generate-file-dir
111__ndk_file_dir := $(call parent-dir,$1)
112$$(call generate-dir,$$(__ndk_file_dir))
113$1:| $$(__ndk_file_dir)
114endef
115
116generate-file-dir = $(eval $(call ev-generate-file-dir,$1))
117
118# -----------------------------------------------------------------------------
119# Function : generate-list-file
120# Arguments: 1: list of strings (possibly very long)
121#            2: file name
122# Returns  : write the content of a possibly very long string list to a file.
123#            this shall be used in commands and will work around limitations
124#            of host command-line lengths.
125# Usage    : $(call host-echo-to-file,<string-list>,<file>)
126# Rationale: When there is a very large number of objects and/or libraries at
127#            link time, the size of the command becomes too large for the
128#            host system's maximum. Various tools however support the
129#            @<listfile> syntax, where <listfile> is the path of a file
130#            which content will be parsed as if they were options.
131#
132#            This function is used to generate such a list file from a long
133#            list of strings in input.
134#
135# -----------------------------------------------------------------------------
136
137# Helper functions because the GNU Make $(word ...) function does
138# not accept a 0 index, so we need to bump any of these to 1 when
139# we find them.
140#
141index-is-zero = $(filter 0 00 000 0000 00000 000000 0000000,$1)
142bump-0-to-1 = $(if $(call index-is-zero,$1),1,$1)
143
144-test-bump-0-to-1 = \
145  $(call test-expect,$(call bump-0-to-1))\
146  $(call test-expect,1,$(call bump-0-to-1,0))\
147  $(call test-expect,1,$(call bump-0-to-1,1))\
148  $(call test-expect,2,$(call bump-0-to-1,2))\
149  $(call test-expect,1,$(call bump-0-to-1,00))\
150  $(call test-expect,1,$(call bump-0-to-1,000))\
151  $(call test-expect,1,$(call bump-0-to-1,0000))\
152  $(call test-expect,1,$(call bump-0-to-1,00000))\
153  $(call test-expect,1,$(call bump-0-to-1,000000))\
154  $(call test-expect,10,$(call bump-0-to-1,10))\
155  $(call test-expect,100,$(call bump-0-to-1,100))
156
157# Same as $(wordlist ...) except the start index, if 0, is bumped to 1
158index-word-list = $(wordlist $(call bump-0-to-1,$1),$2,$3)
159
160-test-index-word-list = \
161  $(call test-expect,,$(call index-word-list,1,1))\
162  $(call test-expect,a b,$(call index-word-list,0,2,a b c d))\
163  $(call test-expect,b c,$(call index-word-list,2,3,a b c d))\
164
165# NOTE: With GNU Make $1 and $(1) are equivalent, which means
166#       that $10 is equivalent to $(1)0, and *not* $(10).
167
168# Used to generate a slice of up to 10 items starting from index $1,
169# If $1 is 0, it will be bumped to 1 (and only 9 items will be printed)
170# $1: start (tenth) index. Can be 0
171# $2: word list
172#
173define list-file-start-gen-10
174	$$(hide) $$(HOST_ECHO_N) "$(call index-word-list,$10,$19,$2) " >> $$@
175endef
176
177# Used to generate a slice of always 10 items starting from index $1
178# $1: start (tenth) index. CANNOT BE 0
179# $2: word list
180define list-file-always-gen-10
181	$$(hide) $$(HOST_ECHO_N) "$(wordlist $10,$19,$2) " >> $$@
182endef
183
184# Same as list-file-always-gen-10, except that the word list might be
185# empty at position $10 (i.e. $(1)0)
186define list-file-maybe-gen-10
187ifneq ($(word $10,$2),)
188	$$(hide) $$(HOST_ECHO_N) "$(wordlist $10,$19,$2) " >> $$@
189endif
190endef
191
192define list-file-start-gen-100
193$(call list-file-start-gen-10,$10,$2)
194$(call list-file-always-gen-10,$11,$2)
195$(call list-file-always-gen-10,$12,$2)
196$(call list-file-always-gen-10,$13,$2)
197$(call list-file-always-gen-10,$14,$2)
198$(call list-file-always-gen-10,$15,$2)
199$(call list-file-always-gen-10,$16,$2)
200$(call list-file-always-gen-10,$17,$2)
201$(call list-file-always-gen-10,$18,$2)
202$(call list-file-always-gen-10,$19,$2)
203endef
204
205define list-file-always-gen-100
206$(call list-file-always-gen-10,$10,$2)
207$(call list-file-always-gen-10,$11,$2)
208$(call list-file-always-gen-10,$12,$2)
209$(call list-file-always-gen-10,$13,$2)
210$(call list-file-always-gen-10,$14,$2)
211$(call list-file-always-gen-10,$15,$2)
212$(call list-file-always-gen-10,$16,$2)
213$(call list-file-always-gen-10,$17,$2)
214$(call list-file-always-gen-10,$18,$2)
215$(call list-file-always-gen-10,$19,$2)
216endef
217
218define list-file-maybe-gen-100
219ifneq ($(word $(call bump-0-to-1,$100),$2),)
220ifneq ($(word $199,$2),)
221$(call list-file-start-gen-10,$10,$2)
222$(call list-file-always-gen-10,$11,$2)
223$(call list-file-always-gen-10,$12,$2)
224$(call list-file-always-gen-10,$13,$2)
225$(call list-file-always-gen-10,$14,$2)
226$(call list-file-always-gen-10,$15,$2)
227$(call list-file-always-gen-10,$16,$2)
228$(call list-file-always-gen-10,$17,$2)
229$(call list-file-always-gen-10,$18,$2)
230$(call list-file-always-gen-10,$19,$2)
231else
232ifneq ($(word $150,$2),)
233$(call list-file-start-gen-10,$10,$2)
234$(call list-file-always-gen-10,$11,$2)
235$(call list-file-always-gen-10,$12,$2)
236$(call list-file-always-gen-10,$13,$2)
237$(call list-file-always-gen-10,$14,$2)
238$(call list-file-maybe-gen-10,$15,$2)
239$(call list-file-maybe-gen-10,$16,$2)
240$(call list-file-maybe-gen-10,$17,$2)
241$(call list-file-maybe-gen-10,$18,$2)
242$(call list-file-maybe-gen-10,$19,$2)
243else
244$(call list-file-start-gen-10,$10,$2)
245$(call list-file-maybe-gen-10,$11,$2)
246$(call list-file-maybe-gen-10,$12,$2)
247$(call list-file-maybe-gen-10,$13,$2)
248$(call list-file-maybe-gen-10,$14,$2)
249endif
250endif
251endif
252endef
253
254define list-file-maybe-gen-1000
255ifneq ($(word $(call bump-0-to-1,$1000),$2),)
256ifneq ($(word $1999,$2),)
257$(call list-file-start-gen-100,$10,$2)
258$(call list-file-always-gen-100,$11,$2)
259$(call list-file-always-gen-100,$12,$2)
260$(call list-file-always-gen-100,$13,$2)
261$(call list-file-always-gen-100,$14,$2)
262$(call list-file-always-gen-100,$15,$2)
263$(call list-file-always-gen-100,$16,$2)
264$(call list-file-always-gen-100,$17,$2)
265$(call list-file-always-gen-100,$18,$2)
266$(call list-file-always-gen-100,$19,$2)
267else
268ifneq ($(word $1500,$2),)
269$(call list-file-start-gen-100,$10,$2)
270$(call list-file-always-gen-100,$11,$2)
271$(call list-file-always-gen-100,$12,$2)
272$(call list-file-always-gen-100,$13,$2)
273$(call list-file-always-gen-100,$14,$2)
274$(call list-file-maybe-gen-100,$15,$2)
275$(call list-file-maybe-gen-100,$16,$2)
276$(call list-file-maybe-gen-100,$17,$2)
277$(call list-file-maybe-gen-100,$18,$2)
278$(call list-file-maybe-gen-100,$19,$2)
279else
280$(call list-file-start-gen-100,$10,$2)
281$(call list-file-maybe-gen-100,$11,$2)
282$(call list-file-maybe-gen-100,$12,$2)
283$(call list-file-maybe-gen-100,$13,$2)
284$(call list-file-maybe-gen-100,$14,$2)
285endif
286endif
287endif
288endef
289
290
291define generate-list-file-ev
292__list_file := $2
293
294.PHONY: $$(__list_file).tmp
295
296$$(call generate-file-dir,$$(__list_file).tmp)
297
298$$(__list_file).tmp:
299	$$(hide) $$(HOST_ECHO_N) "" > $$@
300$(call list-file-maybe-gen-1000,0,$1)
301$(call list-file-maybe-gen-1000,1,$1)
302$(call list-file-maybe-gen-1000,2,$1)
303$(call list-file-maybe-gen-1000,3,$1)
304$(call list-file-maybe-gen-1000,4,$1)
305$(call list-file-maybe-gen-1000,5,$1)
306
307$$(__list_file): $$(__list_file).tmp
308	$$(hide) $$(call host-copy-if-differ,$$@.tmp,$$@)
309	$$(hide) $$(call host-rm,$$@.tmp)
310
311endef
312
313generate-list-file = $(eval $(call generate-list-file-ev,$1,$2))
314
315# -----------------------------------------------------------------------------
316# Function : link-whole-archives
317# Arguments: 1: list of whole static libraries
318# Returns  : linker flags to use the whole static libraries
319# Usage    : $(call link-whole-archives,<libraries>)
320# Rationale: This function is used to put the list of whole static libraries
321#            inside a -Wl,--whole-archive ... -Wl,--no-whole-archive block.
322#            If the list is empty, it returns an empty string.
323#            This function also calls host-path to translate the library
324#            paths.
325# -----------------------------------------------------------------------------
326link-whole-archives = $(if $(strip $1),$(call link-whole-archive-flags,$1))
327link-whole-archive-flags = -Wl,--whole-archive $(call host-path,$1) -Wl,--no-whole-archive
328
329-test-link-whole-archive = \
330  $(call test-expect,,$(call link-whole-archives))\
331  $(eval _start := -Wl,--whole-archive)\
332  $(eval _end := -Wl,--no-whole-archive)\
333  $(call test-expect,$(_start) foo $(_end),$(call link-whole-archives,foo))\
334  $(call test-expect,$(_start) foo bar $(_end),$(call link-whole-archives,foo bar))
335
336# =============================================================================
337#
338# Modules database
339#
340# The following declarations are used to manage the list of modules
341# defined in application's Android.mk files.
342#
343# Technical note:
344#    We use __ndk_modules to hold the list of all modules corresponding
345#    to a given application.
346#
347#    For each module 'foo', __ndk_modules.foo.<field> is used
348#    to store module-specific information.
349#
350#        type         -> type of module (e.g. 'static', 'shared', ...)
351#        depends      -> list of other modules this module depends on
352#
353#    Also, LOCAL_XXXX values defined for a module are recorded in XXXX, e.g.:
354#
355#        PATH   -> recorded LOCAL_PATH for the module
356#        CFLAGS -> recorded LOCAL_CFLAGS for the module
357#        ...
358#
359#    Some of these are created by build scripts like BUILD_STATIC_LIBRARY:
360#
361#        MAKEFILE -> The Android.mk where the module is defined.
362#        LDFLAGS  -> Final linker flags
363#        OBJECTS  -> List of module objects
364#        BUILT_MODULE -> location of module built file (e.g. obj/<app>/<abi>/libfoo.so)
365#
366#    Note that some modules are never installed (e.g. static libraries).
367#
368# =============================================================================
369
370# The list of LOCAL_XXXX variables that are recorded for each module definition
371# These are documented by docs/ANDROID-MK.TXT. Exception is LOCAL_MODULE
372#
373modules-LOCALS := \
374    MODULE \
375    MODULE_FILENAME \
376    PATH \
377    SRC_FILES \
378    CPP_EXTENSION \
379    C_INCLUDES \
380    CFLAGS \
381    CXXFLAGS \
382    CPPFLAGS \
383    STATIC_LIBRARIES \
384    WHOLE_STATIC_LIBRARIES \
385    SHARED_LIBRARIES \
386    LDLIBS \
387    ALLOW_UNDEFINED_SYMBOLS \
388    ARM_MODE \
389    ARM_NEON \
390    DISABLE_NO_EXECUTE \
391    DISABLE_RELRO \
392    DISABLE_FORMAT_STRING_CHECKS \
393    EXPORT_CFLAGS \
394    EXPORT_CPPFLAGS \
395    EXPORT_LDLIBS \
396    EXPORT_C_INCLUDES \
397    FILTER_ASM \
398    CPP_FEATURES \
399    SHORT_COMMANDS \
400    BUILT_MODULE_NOT_COPIED \
401    THIN_ARCHIVE \
402
403# The following are generated by the build scripts themselves
404
405# LOCAL_MAKEFILE will contain the path to the Android.mk defining the module
406modules-LOCALS += MAKEFILE
407
408# LOCAL_LDFLAGS will contain the set of final linker flags for the module
409modules-LOCALS += LDFLAGS
410
411# LOCAL_OBJECTS will contain the list of object files generated from the
412# module's sources, if any.
413modules-LOCALS += OBJECTS
414
415# LOCAL_BUILT_MODULE will contain the location of the symbolic version of
416# the generated module (i.e. the one containing all symbols used during
417# native debugging). It is generally under $PROJECT/obj/local/
418modules-LOCALS += BUILT_MODULE
419
420# LOCAL_OBJS_DIR will contain the location where the object files for
421# this module will be stored. Usually $PROJECT/obj/local/<module>/obj
422modules-LOCALS += OBJS_DIR
423
424# LOCAL_INSTALLED will contain the location of the installed version
425# of the module. Usually $PROJECT/libs/<abi>/<prefix><module><suffix>
426# where <prefix> and <suffix> depend on the module class.
427modules-LOCALS += INSTALLED
428
429# LOCAL_MODULE_CLASS will contain the type of the module
430# (e.g. STATIC_LIBRARY, SHARED_LIBRARY, etc...)
431modules-LOCALS += MODULE_CLASS
432
433# the list of managed fields per module
434modules-fields = depends \
435                 $(modules-LOCALS)
436
437# -----------------------------------------------------------------------------
438# Function : modules-clear
439# Arguments: None
440# Returns  : None
441# Usage    : $(call modules-clear)
442# Rationale: clears the list of defined modules known by the build system
443# -----------------------------------------------------------------------------
444modules-clear = \
445    $(foreach __mod,$(__ndk_modules),\
446        $(foreach __field,$(modules-fields),\
447            $(eval __ndk_modules.$(__mod).$(__field) := $(empty))\
448        )\
449    )\
450    $(eval __ndk_modules := $(empty_set)) \
451    $(eval __ndk_top_modules := $(empty)) \
452    $(eval __ndk_import_list := $(empty)) \
453    $(eval __ndk_import_depth := $(empty))
454
455# -----------------------------------------------------------------------------
456# Function : modules-get-list
457# Arguments: None
458# Returns  : The list of all recorded modules
459# Usage    : $(call modules-get-list)
460# -----------------------------------------------------------------------------
461modules-get-list = $(__ndk_modules)
462
463# -----------------------------------------------------------------------------
464# Function : modules-get-top-list
465# Arguments: None
466# Returns  : The list of all recorded non-imported modules
467# Usage    : $(call modules-get-top-list)
468# -----------------------------------------------------------------------------
469modules-get-top-list = $(__ndk_top_modules)
470
471# -----------------------------------------------------------------------------
472# Function : module-add
473# Arguments: 1: module name
474# Returns  : None
475# Usage    : $(call module-add,<modulename>)
476# Rationale: add a new module. If it is already defined, print an error message
477#            and abort. This will record all LOCAL_XXX variables for the module.
478# -----------------------------------------------------------------------------
479module-add = \
480  $(call assert-defined,LOCAL_MAKEFILE LOCAL_BUILT_MODULE LOCAL_OBJS_DIR LOCAL_MODULE_CLASS)\
481  $(if $(call set_is_member,$(__ndk_modules),$1),\
482    $(call __ndk_info,Trying to define local module '$1' in $(LOCAL_MAKEFILE).)\
483    $(call __ndk_info,But this module was already defined by $(__ndk_modules.$1.MAKEFILE).)\
484    $(call __ndk_error,Aborting.)\
485  )\
486  $(eval __ndk_modules := $(call set_insert,$(__ndk_modules),$1))\
487  $(if $(strip $(__ndk_import_depth)),,\
488    $(eval __ndk_top_modules := $(call set_insert,$(__ndk_top_modules),$1))\
489  )\
490  $(if $(call module-class-is-installable,$(LOCAL_MODULE_CLASS)),\
491    $(eval LOCAL_INSTALLED := $(NDK_APP_DST_DIR)/$(notdir $(LOCAL_BUILT_MODULE))),\
492    $(eval LOCAL_INSTALLED := $(LOCAL_BUILT_MODULE))\
493  )\
494  $(foreach __field,STATIC_LIBRARIES WHOLE_STATIC_LIBRARIES SHARED_LIBRARIES,\
495    $(eval LOCAL_$(__field) := $(call strip-lib-prefix,$(LOCAL_$(__field)))))\
496  $(foreach __local,$(modules-LOCALS),\
497    $(eval __ndk_modules.$1.$(__local) := $(LOCAL_$(__local)))\
498  )\
499  $(call module-handle-c++-features,$1)
500
501
502# Retrieve the class of module $1
503module-get-class = $(__ndk_modules.$1.MODULE_CLASS)
504
505# Retrieve built location of module $1
506module-get-built = $(__ndk_modules.$1.BUILT_MODULE)
507
508# Returns $(true) is module $1 is installable
509# An installable module is one that will be copied to $PROJECT/libs/<abi>/
510# (e.g. shared libraries).
511#
512module-is-installable = $(call module-class-is-installable,$(call module-get-class,$1))
513
514# Returns $(true) if module $1 is a copyable prebuilt
515# A copyable prebuilt module is one that will be copied to $NDK_OUT/<abi>/
516# at build time. At the moment, this is only used for prebuilt shared
517# libraries, since it helps ndk-gdb.
518#
519module-is-copyable = $(call module-class-is-copyable,$(call module-get-class,$1))
520
521# -----------------------------------------------------------------------------
522# Function : module-get-export
523# Arguments: 1: module name
524#            2: export variable name without LOCAL_EXPORT_ prefix (e.g. 'CFLAGS')
525# Returns  : Exported value
526# Usage    : $(call module-get-export,<modulename>,<varname>)
527# Rationale: Return the recorded value of LOCAL_EXPORT_$2, if any, for module $1
528# -----------------------------------------------------------------------------
529module-get-export = $(__ndk_modules.$1.EXPORT_$2)
530
531# -----------------------------------------------------------------------------
532# Function : module-get-listed-export
533# Arguments: 1: list of module names
534#            2: export variable name without LOCAL_EXPORT_ prefix (e.g. 'CFLAGS')
535# Returns  : Exported values
536# Usage    : $(call module-get-listed-export,<module-list>,<varname>)
537# Rationale: Return the recorded value of LOCAL_EXPORT_$2, if any, for modules
538#            listed in $1.
539# -----------------------------------------------------------------------------
540module-get-listed-export = $(strip \
541    $(foreach __listed_module,$1,\
542        $(call module-get-export,$(__listed_module),$2)\
543    ))
544
545# -----------------------------------------------------------------------------
546# Function : modules-restore-locals
547# Arguments: 1: module name
548# Returns  : None
549# Usage    : $(call module-restore-locals,<modulename>)
550# Rationale: Restore the recorded LOCAL_XXX definitions for a given module.
551# -----------------------------------------------------------------------------
552module-restore-locals = \
553    $(foreach __local,$(modules-LOCALS),\
554        $(eval LOCAL_$(__local) := $(__ndk_modules.$1.$(__local)))\
555    )
556
557# Dump all module information. Only use this for debugging
558modules-dump-database = \
559    $(info Modules [$(TARGET_ARCH_ABI)]: $(__ndk_modules)) \
560    $(foreach __mod,$(__ndk_modules),\
561        $(info $(space4)$(__mod):)\
562        $(foreach __field,$(modules-fields),\
563            $(eval __fieldval := $(strip $(__ndk_modules.$(__mod).$(__field))))\
564            $(if $(__fieldval),\
565                $(if $(filter 1,$(words $(__fieldval))),\
566                    $(info $(space4)$(space4)$(__field): $(__fieldval)),\
567                    $(info $(space4)$(space4)$(__field): )\
568                    $(foreach __fielditem,$(__fieldval),\
569                        $(info $(space4)$(space4)$(space4)$(__fielditem))\
570                    )\
571                )\
572            )\
573        )\
574    )\
575    $(info Top modules: $(__ndk_top_modules))\
576    $(info --- end of modules list)
577
578
579# -----------------------------------------------------------------------------
580# Function : module-add-static-depends
581# Arguments: 1: module name
582#            2: list/set of static library modules this module depends on.
583# Returns  : None
584# Usage    : $(call module-add-static-depends,<modulename>,<list of module names>)
585# Rationale: Record that a module depends on a set of static libraries.
586#            Use module-get-static-dependencies to retrieve final list.
587# -----------------------------------------------------------------------------
588module-add-static-depends = \
589    $(call module-add-depends-any,$1,$2,depends) \
590
591# -----------------------------------------------------------------------------
592# Function : module-add-shared-depends
593# Arguments: 1: module name
594#            2: list/set of shared library modules this module depends on.
595# Returns  : None
596# Usage    : $(call module-add-shared-depends,<modulename>,<list of module names>)
597# Rationale: Record that a module depends on a set of shared libraries.
598#            Use modulge-get-shared-dependencies to retrieve final list.
599# -----------------------------------------------------------------------------
600module-add-shared-depends = \
601    $(call module-add-depends-any,$1,$2,depends) \
602
603# Used internally by module-add-static-depends and module-add-shared-depends
604# NOTE: this function must not modify the existing dependency order when new depends are added.
605#
606module-add-depends-any = \
607    $(eval __ndk_modules.$1.$3 += $(filter-out $(__ndk_modules.$1.$3),$2))
608
609
610# -----------------------------------------------------------------------------
611# Returns non-empty if a module is a static library
612# Arguments: 1: module name
613# Returns     : non-empty iff the module is a static library.
614# Usage       : $(if $(call module-is-static-library,<name>),...)
615# -----------------------------------------------------------------------------
616module-is-static-library = $(strip \
617  $(filter STATIC_LIBRARY PREBUILT_STATIC_LIBRARY,\
618    $(call module-get-class,$1)))
619
620# -----------------------------------------------------------------------------
621# Returns non-empty if a module is a shared library
622# Arguments: 1: module name
623# Returns     : non-empty iff the module is a shared library.
624# Usage       : $(if $(call module-is-shared-library,<name>),...)
625# -----------------------------------------------------------------------------
626module-is-shared-library = $(strip \
627  $(filter SHARED_LIBRARY PREBUILT_SHARED_LIBRARY,\
628    $(call module-get-class,$1)))
629
630# -----------------------------------------------------------------------------
631# Filter a list of module names to retain only the static libraries.
632# Arguments: 1: module name list
633# Returns     : input list modules which are static libraries.
634# -----------------------------------------------------------------------------
635module-filter-static-libraries = $(call filter-by,$1,module-is-static-library)
636
637# -----------------------------------------------------------------------------
638# Filter a list of module names to retain only the shared libraries.
639# Arguments: 1: module name list
640# Returns     : input list modules which are shared libraries.
641# -----------------------------------------------------------------------------
642module-filter-shared-libraries = $(call filter-by,$1,module-is-shared-library)
643
644# -----------------------------------------------------------------------------
645# Return the LOCAL_STATIC_LIBRARIES for a given module.
646# Arguments: 1: module name
647# Returns     : List of static library modules.
648# -----------------------------------------------------------------------------
649module-get-static-libs = $(__ndk_modules.$1.STATIC_LIBRARIES)
650
651# -----------------------------------------------------------------------------
652# Return the LOCAL_WHOLE_STATIC_LIBRARIES for a given module.
653# Arguments: 1: module name
654# Returns     : List of whole static library modules.
655# -----------------------------------------------------------------------------
656module-get-whole-static-libs = $(__ndk_modules.$1.WHOLE_STATIC_LIBRARIES)
657
658# -----------------------------------------------------------------------------
659# Return all static libraries for a given module.
660# Arguments: 1: module name
661# Returns     : List of static library modules (whole or not).
662# -----------------------------------------------------------------------------
663module-get-all-static-libs = $(strip \
664  $(__ndk_modules.$1.STATIC_LIBRARIES) \
665  $(__ndk_modules.$1.WHOLE_STATIC_LIBRARIES))
666
667# -----------------------------------------------------------------------------
668# Return the list of LOCAL_SHARED_LIBRARIES for a given module.
669# Arguments: 1: module name
670# Returns     : List of shared library modules.
671# -----------------------------------------------------------------------------
672module-get-shared-libs = $(__ndk_modules.$1.SHARED_LIBRARIES)
673
674# -----------------------------------------------------------------------------
675# Return the list of all libraries a modules depends directly on.
676# This is the concatenation of its LOCAL_STATIC_LIBRARIES,
677# LOCAL_WHOLE_STATIC_LIBRARIES, and LOCAL_SHARED_LIBRARIES variables.
678# Arguments: 1: module name
679# Returns     : List of library modules (static or shared).
680# -----------------------------------------------------------------------------
681module-get-direct-libs = $(strip \
682  $(__ndk_modules.$1.STATIC_LIBRARIES) \
683  $(__ndk_modules.$1.WHOLE_STATIC_LIBRARIES) \
684  $(__ndk_modules.$1.SHARED_LIBRARIES))
685
686
687# -----------------------------------------------------------------------------
688# Computes the full closure of a module and its dependencies. Order is
689# defined by a breadth-first walk of the graph.
690# $1 will be the first item in the result.
691#
692# Arguments: 1: module name
693# Returns     : List of all modules $1 depends on.
694#
695# Note: Do not use this to determine build dependencies. The returned list
696#       is much too large for this. For example consider the following
697#       dependency graph:
698#
699#   main.exe -> libA.a -> libfoo.so -> libB.a
700#
701#       This function will return all four modules in the result, while
702#       at link time building main.exe only requires the first three.
703#
704# -----------------------------------------------------------------------------
705module-get-all-dependencies = $(call -ndk-mod-get-closure,$1,module-get-depends)
706
707# -----------------------------------------------------------------------------
708# Compute the list of all static and shared libraries required to link a
709# given module.
710#
711# Note that the result is topologically ordered, i.e. if library A depends
712# on library B, then A will always appear after B in the result.
713#
714# Arguments: 1: module name
715# Returns     : List of all library $1 depends at link time.
716#
717# Note: This doesn't differentiate between regular and whole static
718#       libraries. Use module-extract-whole-static-libs to filter the
719#       result returned by this function.
720# -----------------------------------------------------------------------------
721module-get-link-libs = $(strip \
722  $(eval _ndk_mod_link_module := $1) \
723  $(call -ndk-mod-get-topological-depends,$1,-ndk-mod-link-deps))
724
725# Special dependency function used by module-get-link-libs.
726# The rules to follow are the following:
727#  - if $1 is the link module, or if it is a static library, then all
728#    direct dependencies.
729#  - otherwise, the module is a shared library, don't add build deps.
730-ndk-mod-link-deps = \
731  $(if $(call seq,$1,$(_ndk_mod_link_module))$(call module-is-static-library,$1),\
732    $(call module-get-direct-libs,$1))
733
734# -----------------------------------------------------------------------------
735# This function is used to extract the list of static libraries that need
736# to be linked as whole, i.e. placed in a special section on the final
737# link command.
738# Arguments: $1: module name.
739#            $2: list of all static link-time libraries (regular or whole).
740# Returns  : list of static libraries from '$2' that need to be linked
741#            as whole.
742# -----------------------------------------------------------------------------
743module-extract-whole-static-libs = $(strip \
744  $(eval _ndk_mod_whole_all := $(call map,module-get-whole-static-libs,$1 $2))\
745  $(eval _ndk_mod_whole_result := $(filter $(_ndk_mod_whole_all),$2))\
746  $(_ndk_mod_whole_result))
747
748# Used to recompute all dependencies once all module information has been recorded.
749#
750modules-compute-dependencies = \
751    $(foreach __module,$(__ndk_modules),\
752        $(call module-compute-depends,$(__module))\
753    )
754
755module-compute-depends = \
756    $(call module-add-static-depends,$1,$(__ndk_modules.$1.STATIC_LIBRARIES))\
757    $(call module-add-static-depends,$1,$(__ndk_modules.$1.WHOLE_STATIC_LIBRARIES))\
758    $(call module-add-shared-depends,$1,$(__ndk_modules.$1.SHARED_LIBRARIES))\
759
760module-get-installed = $(__ndk_modules.$1.INSTALLED)
761
762module-get-depends = $(__ndk_modules.$1.depends)
763
764# -----------------------------------------------------------------------------
765# Function : modules-get-all-installable
766# Arguments: 1: list of module names
767# Returns  : List of all the installable modules $1 depends on transitively.
768# Usage    : $(call modules-all-get-installable,<list of module names>)
769# Rationale: This computes the closure of all installable module dependencies starting from $1
770# -----------------------------------------------------------------------------
771# For now, only the closure of LOCAL_SHARED_LIBRARIES is enough
772modules-get-all-installable = $(strip \
773    $(foreach __alldep,$(call module-get-all-dependencies,$1),\
774        $(if $(call module-is-installable,$(__alldep)),$(__alldep))\
775    ))
776
777# Return the C++ extension(s) of a given module
778# $1: module name
779module-get-c++-extensions = $(strip \
780    $(if $(__ndk_modules.$1.CPP_EXTENSION),\
781        $(__ndk_modules.$1.CPP_EXTENSION),\
782        $(default-c++-extensions)\
783    ))
784
785# Return the list of C++ sources of a given module
786#
787module-get-c++-sources = \
788    $(eval __files := $(__ndk_modules.$1.SRC_FILES:%.neon=%)) \
789    $(eval __files := $(__files:%.arm=%)) \
790    $(eval __extensions := $(call module-get-c++-extensions,$1))\
791    $(filter $(foreach __extension,$(__extensions),%$(__extension)),$(__files))
792
793# Returns true if a module has C++ sources
794#
795module-has-c++-sources = $(strip $(call module-get-c++-sources,$1))
796
797
798# Add C++ dependencies to any module that has C++ sources.
799# $1: list of C++ runtime static libraries (if any)
800# $2: list of C++ runtime shared libraries (if any)
801#
802modules-add-c++-dependencies = \
803    $(foreach __module,$(__ndk_modules),\
804        $(if $(call module-has-c++-sources,$(__module)),\
805            $(call ndk_log,Module '$(__module)' has C++ sources)\
806            $(call module-add-c++-deps,$(__module),$1,$2),\
807        )\
808    )
809
810
811# Return the compiler flags used to compile a C++ module
812# Order matters and should match the one used by the build command
813module-get-c++-flags = $(strip \
814    $(__ndk_modules.$1.CFLAGS) \
815    $(__ndk_modules.$1.CPPFLAGS) \
816    $(__ndk_modules.$1.CXXFLAGS))
817
818# This function is used to remove certain flags from a module compiler flags
819# $1: Module name
820# $2: List of flags to remove
821#
822module-filter-out-compiler-flags = \
823    $(eval __ndk_modules.$1.CFLAGS   := $(filter-out $2,$(__ndk_modules.$1.CFLAGS)))\
824    $(eval __ndk_modules.$1.CPPFLAGS := $(filter-out $2,$(__ndk_modules.$1.CPPFLAGS)))\
825    $(eval __ndk_modules.$1.CXXFLAGS := $(filter-out $2,$(__ndk_modules.$1.CXXFLAGS)))
826
827# Return true if a module's compiler flags enable rtti
828# We just look at -frtti and -fno-rtti on the command-line
829# and keep the last one of these flags.
830module-flags-have-rtti = $(strip \
831        $(filter -frtti,\
832            $(lastword $(filter -frtti -fno-rtti,$(call module-get-c++-flags,$1)))\
833        )\
834    )
835
836# Same with C++ exception support (i.e. -fexceptions and -fno-exceptions)
837#
838module-flags-have-exceptions = $(strip \
839        $(filter -fexceptions,\
840            $(lastword $(filter -fexceptions -fno-execeptions,$(call module-get-c++-flags,$1)))\
841        )\
842    )
843
844# Handle the definition of LOCAL_CPP_FEATURES, i.e.:
845#
846#  - If it is defined, check that it only contains valid values
847#  - If it is undefined, try to compute its value automatically by
848#    looking at the C++ compiler flags used to build the module
849#
850# After this, we remove all features flags from the module's command-line
851# And add only the correct ones back in LOCAL_CPP_FLAGS
852#
853module-handle-c++-features = \
854    $(if $(strip $(__ndk_modules.$1.CPP_FEATURES)),\
855        $(eval __cxxbad := $(filter-out rtti exceptions,$(__ndk_modules.$1.CPP_FEATURES)))\
856        $(if $(__cxxbad),\
857            $(call __ndk_info,WARNING: Ignoring invalid values in LOCAL_CPP_FEATURES definition in $(__ndk_modules.$1.MAKEFILE): $(__cxxbad))\
858            $(eval __ndk_modules.$1.CPP_FEATURES := $(strip $(filter-out $(__cxxbad),$(__ndk_modules.$1.CPP_FEATURES))))\
859        )\
860    ,\
861        $(eval __ndk_modules.$1.CPP_FEATURES := $(strip \
862            $(if $(call module-flags-have-rtti,$1),rtti) \
863            $(if $(call module-flags-have-exceptions,$1),exceptions) \
864        )) \
865    )\
866    $(call module-filter-out-compiler-flags,$1,-frtti -fno-rtti -fexceptions -fno-exceptions)\
867
868# Returns true if a module or its dependencies have specific C++ features
869# (i.e. RTTI or Exceptions)
870#
871# $1: module name
872# $2: list of features (e.g. 'rtti' or 'exceptions')
873#
874module-has-c++-features = $(strip \
875    $(eval __cxxdeps  := $(call module-get-all-dependencies,$1))\
876    $(eval __cxxflags := $(foreach __cxxdep,$(__cxxdeps),$(__ndk_modules.$(__cxxdep).CPP_FEATURES)))\
877    $(if $(filter $2,$(__cxxflags)),true,)\
878    )
879
880# Add standard C++ dependencies to a given module
881#
882# $1: module name
883# $2: list of C++ runtime static libraries (if any)
884# $3: list of C++ runtime shared libraries (if any)
885#
886module-add-c++-deps = \
887    $(if $(call strip,$2),$(call ndk_log,Add dependency '$(call strip,$2)' to module '$1'))\
888    $(eval __ndk_modules.$1.STATIC_LIBRARIES += $(2))\
889    $(if $(call strip,$3),$(call ndk_log,Add dependency '$(call strip,$3)' to module '$1'))\
890    $(eval __ndk_modules.$1.SHARED_LIBRARIES += $(3))
891
892
893# =============================================================================
894#
895# Utility functions
896#
897# =============================================================================
898
899# -----------------------------------------------------------------------------
900# Function : pretty-dir
901# Arguments: 1: path
902# Returns  : Remove NDK_PROJECT_PATH prefix from a given path. This can be
903#            used to perform pretty-printing for logs.
904# -----------------------------------------------------------------------------
905pretty-dir = $(patsubst $(NDK_ROOT)/%,<NDK>/%,\
906                 $(patsubst $(NDK_PROJECT_PATH)/%,%,$1))
907
908# Note: NDK_PROJECT_PATH is typically defined after this test is run.
909-test-pretty-dir = \
910  $(eval NDK_PROJECT_PATH ?= .)\
911  $(call test-expect,foo,$(call pretty-dir,foo))\
912  $(call test-expect,foo,$(call pretty-dir,$(NDK_PROJECT_PATH)/foo))\
913  $(call test-expect,foo/bar,$(call pretty-dir,$(NDK_PROJECT_PATH)/foo/bar))\
914  $(call test-expect,<NDK>/foo,$(call pretty-dir,$(NDK_ROOT)/foo))\
915  $(call test-expect,<NDK>/foo/bar,$(call pretty-dir,$(NDK_ROOT)/foo/bar))
916
917# -----------------------------------------------------------------------------
918# Function : check-user-define
919# Arguments: 1: name of variable that must be defined by the user
920#            2: name of Makefile where the variable should be defined
921#            3: name/description of the Makefile where the check is done, which
922#               must be included by $2
923# Returns  : None
924# -----------------------------------------------------------------------------
925check-user-define = $(if $(strip $($1)),,\
926  $(call __ndk_error,Missing $1 before including $3 in $2))
927
928# -----------------------------------------------------------------------------
929# This is used to check that LOCAL_MODULE is properly defined by an Android.mk
930# file before including one of the $(BUILD_SHARED_LIBRARY), etc... files.
931#
932# Function : check-user-LOCAL_MODULE
933# Arguments: 1: name/description of the included build Makefile where the
934#               check is done
935# Returns  : None
936# Usage    : $(call check-user-LOCAL_MODULE, BUILD_SHARED_LIBRARY)
937# -----------------------------------------------------------------------------
938check-defined-LOCAL_MODULE = \
939  $(call check-user-define,LOCAL_MODULE,$(local-makefile),$(1)) \
940  $(if $(call seq,$(words $(LOCAL_MODULE)),1),,\
941    $(call __ndk_info,LOCAL_MODULE definition in $(local-makefile) must not contain space)\
942    $(call __ndk_error,Please correct error. Aborting)\
943  )
944
945# -----------------------------------------------------------------------------
946# This is used to check that LOCAL_MODULE_FILENAME, if defined, is correct.
947#
948# Function : check-user-LOCAL_MODULE_FILENAME
949# Returns  : None
950# Usage    : $(call check-user-LOCAL_MODULE_FILENAME)
951# -----------------------------------------------------------------------------
952check-LOCAL_MODULE_FILENAME = \
953  $(if $(strip $(LOCAL_MODULE_FILENAME)),\
954    $(if $(call seq,$(words $(LOCAL_MODULE_FILENAME)),1),,\
955        $(call __ndk_info,$(LOCAL_MAKEFILE):$(LOCAL_MODULE): LOCAL_MODULE_FILENAME must not contain spaces)\
956        $(call __ndk_error,Plase correct error. Aborting)\
957    )\
958    $(if $(filter %$(TARGET_LIB_EXTENSION) %$(TARGET_SONAME_EXTENSION),$(LOCAL_MODULE_FILENAME)),\
959        $(call __ndk_info,$(LOCAL_MAKEFILE):$(LOCAL_MODULE): LOCAL_MODULE_FILENAME should not include file extensions)\
960    )\
961  )
962
963# -----------------------------------------------------------------------------
964# Function  : handle-module-filename
965# Arguments : 1: default file prefix
966#             2: file suffix
967# Returns   : None
968# Usage     : $(call handle-module-filename,<prefix>,<suffix>)
969# Rationale : To be used to check and or set the module's filename through
970#             the LOCAL_MODULE_FILENAME variable.
971# -----------------------------------------------------------------------------
972handle-module-filename = $(eval $(call ev-handle-module-filename,$1,$2))
973
974#
975# Check that LOCAL_MODULE_FILENAME is properly defined
976# - with one single item
977# - without a library file extension
978# - with no directory separators
979#
980define ev-check-module-filename
981ifneq (1,$$(words $$(LOCAL_MODULE_FILENAME)))
982    $$(call __ndk_info,$$(LOCAL_MAKEFILE):$$(LOCAL_MODULE): LOCAL_MODULE_FILENAME must not contain any space)
983    $$(call __ndk_error,Aborting)
984endif
985ifneq (,$$(filter %$$(TARGET_LIB_EXTENSION) %$$(TARGET_SONAME_EXTENSION),$$(LOCAL_MODULE_FILENAME)))
986    $$(call __ndk_info,$$(LOCAL_MAKEFILE):$$(LOCAL_MODULE): LOCAL_MODULE_FILENAME must not contain a file extension)
987    $$(call __ndk_error,Aborting)
988endif
989ifneq (1,$$(words $$(subst /, ,$$(LOCAL_MODULE_FILENAME))))
990    $$(call __ndk_info,$$(LOCAL_MAKEFILE):$$(LOCAL_MODULE): LOCAL_MODULE_FILENAME must not contain directory separators)
991    $$(call __ndk_error,Aborting)
992endif
993endef
994
995#
996# Check the definition of LOCAL_MODULE_FILENAME. If none exists,
997# infer it from the LOCAL_MODULE name.
998#
999# $1: default file prefix
1000# $2: default file suffix
1001#
1002define ev-handle-module-filename
1003LOCAL_MODULE_FILENAME := $$(strip $$(LOCAL_MODULE_FILENAME))
1004ifndef LOCAL_MODULE_FILENAME
1005    LOCAL_MODULE_FILENAME := $1$$(LOCAL_MODULE)
1006endif
1007$$(eval $$(call ev-check-module-filename))
1008LOCAL_MODULE_FILENAME := $$(LOCAL_MODULE_FILENAME)$2
1009endef
1010
1011handle-prebuilt-module-filename = $(eval $(call ev-handle-prebuilt-module-filename,$1))
1012
1013#
1014# Check the definition of LOCAL_MODULE_FILENAME for a _prebuilt_ module.
1015# If none exists, infer it from $(LOCAL_SRC_FILES)
1016#
1017# $1: default file suffix
1018#
1019define ev-handle-prebuilt-module-filename
1020LOCAL_MODULE_FILENAME := $$(strip $$(LOCAL_MODULE_FILENAME))
1021ifndef LOCAL_MODULE_FILENAME
1022    LOCAL_MODULE_FILENAME := $$(notdir $(LOCAL_SRC_FILES))
1023    LOCAL_MODULE_FILENAME := $$(LOCAL_MODULE_FILENAME:%$$(TARGET_LIB_EXTENSION)=%)
1024    LOCAL_MODULE_FILENAME := $$(LOCAL_MODULE_FILENAME:%$$(TARGET_SONAME_EXTENSION)=%)
1025endif
1026LOCAL_MODULE_FILENAME := $$(LOCAL_MODULE_FILENAME)$1
1027$$(eval $$(call ev-check-module-filename))
1028endef
1029
1030
1031# -----------------------------------------------------------------------------
1032# Function  : handle-module-built
1033# Returns   : None
1034# Usage     : $(call handle-module-built)
1035# Rationale : To be used to automatically compute the location of the generated
1036#             binary file, and the directory where to place its object files.
1037# -----------------------------------------------------------------------------
1038handle-module-built = \
1039    $(eval LOCAL_BUILT_MODULE := $(TARGET_OUT)/$(LOCAL_MODULE_FILENAME))\
1040    $(eval LOCAL_OBJS_DIR     := $(TARGET_OBJS)/$(LOCAL_MODULE))
1041
1042# -----------------------------------------------------------------------------
1043# Compute the real path of a prebuilt file.
1044#
1045# Function : local-prebuilt-path
1046# Arguments: 1: prebuilt path (as listed in $(LOCAL_SRC_FILES))
1047# Returns  : full path. If $1 begins with a /, the path is considered
1048#            absolute and returned as-is. Otherwise, $(LOCAL_PATH)/$1 is
1049#            returned instead.
1050# Usage    : $(call local-prebuilt-path,$(LOCAL_SRC_FILES))
1051# -----------------------------------------------------------------------------
1052local-prebuilt-path = $(call local-source-file-path,$1)
1053
1054# -----------------------------------------------------------------------------
1055# This is used to strip any lib prefix from LOCAL_MODULE, then check that
1056# the corresponding module name is not already defined.
1057#
1058# Function : check-user-LOCAL_MODULE
1059# Arguments: 1: path of Android.mk where this LOCAL_MODULE is defined
1060# Returns  : None
1061# Usage    : $(call check-LOCAL_MODULE,$(LOCAL_MAKEFILE))
1062# -----------------------------------------------------------------------------
1063check-LOCAL_MODULE = \
1064  $(eval LOCAL_MODULE := $$(call strip-lib-prefix,$$(LOCAL_MODULE)))
1065
1066# -----------------------------------------------------------------------------
1067# Macro    : my-dir
1068# Returns  : the directory of the current Makefile
1069# Usage    : $(my-dir)
1070# -----------------------------------------------------------------------------
1071my-dir = $(call parent-dir,$(lastword $(MAKEFILE_LIST)))
1072
1073# -----------------------------------------------------------------------------
1074# Function : all-makefiles-under
1075# Arguments: 1: directory path
1076# Returns  : a list of all makefiles immediately below some directory
1077# Usage    : $(call all-makefiles-under, <some path>)
1078# -----------------------------------------------------------------------------
1079all-makefiles-under = $(wildcard $1/*/Android.mk)
1080
1081# -----------------------------------------------------------------------------
1082# Macro    : all-subdir-makefiles
1083# Returns  : list of all makefiles in subdirectories of the current Makefile's
1084#            location
1085# Usage    : $(all-subdir-makefiles)
1086# -----------------------------------------------------------------------------
1087all-subdir-makefiles = $(call all-makefiles-under,$(call my-dir))
1088
1089# =============================================================================
1090#
1091# Source file tagging support.
1092#
1093# Each source file listed in LOCAL_SRC_FILES can have any number of
1094# 'tags' associated to it. A tag name must not contain space, and its
1095# usage can vary.
1096#
1097# For example, the 'debug' tag is used to sources that must be built
1098# in debug mode, the 'arm' tag is used for sources that must be built
1099# using the 32-bit instruction set on ARM platforms, and 'neon' is used
1100# for sources that must be built with ARM Advanced SIMD (a.k.a. NEON)
1101# support.
1102#
1103# More tags might be introduced in the future.
1104#
1105#  LOCAL_SRC_TAGS contains the list of all tags used (initially empty)
1106#  LOCAL_SRC_FILES contains the list of all source files.
1107#  LOCAL_SRC_TAG.<tagname> contains the set of source file names tagged
1108#      with <tagname>
1109#  LOCAL_SRC_FILES_TAGS.<filename> contains the set of tags for a given
1110#      source file name
1111#
1112# Tags are processed by a toolchain-specific function (e.g. TARGET-compute-cflags)
1113# which will call various functions to compute source-file specific settings.
1114# These are currently stored as:
1115#
1116#  LOCAL_SRC_FILES_TARGET_CFLAGS.<filename> contains the list of
1117#      target-specific C compiler flags used to compile a given
1118#      source file. This is set by the function TARGET-set-cflags
1119#      defined in the toolchain's setup.mk script.
1120#
1121#  LOCAL_SRC_FILES_TEXT.<filename> contains the 'text' that will be
1122#      displayed along the label of the build output line. For example
1123#      'thumb' or 'arm  ' with ARM-based toolchains.
1124#
1125# =============================================================================
1126
1127# -----------------------------------------------------------------------------
1128# Macro    : clear-all-src-tags
1129# Returns  : remove all source file tags and associated data.
1130# Usage    : $(clear-all-src-tags)
1131# -----------------------------------------------------------------------------
1132clear-all-src-tags = \
1133$(foreach __tag,$(LOCAL_SRC_TAGS), \
1134    $(eval LOCAL_SRC_TAG.$(__tag) := $(empty)) \
1135) \
1136$(foreach __src,$(LOCAL_SRC_FILES), \
1137    $(eval LOCAL_SRC_FILES_TAGS.$(__src) := $(empty)) \
1138    $(eval LOCAL_SRC_FILES_TARGET_CFLAGS.$(__src) := $(empty)) \
1139    $(eval LOCAL_SRC_FILES_TEXT.$(__src) := $(empty)) \
1140) \
1141$(eval LOCAL_SRC_TAGS := $(empty_set))
1142
1143# -----------------------------------------------------------------------------
1144# Macro    : tag-src-files
1145# Arguments: 1: list of source files to tag
1146#            2: tag name (must not contain space)
1147# Usage    : $(call tag-src-files,<list-of-source-files>,<tagname>)
1148# Rationale: Add a tag to a list of source files
1149# -----------------------------------------------------------------------------
1150tag-src-files = \
1151$(eval LOCAL_SRC_TAGS := $(call set_insert,$2,$(LOCAL_SRC_TAGS))) \
1152$(eval LOCAL_SRC_TAG.$2 := $(call set_union,$1,$(LOCAL_SRC_TAG.$2))) \
1153$(foreach __src,$1, \
1154    $(eval LOCAL_SRC_FILES_TAGS.$(__src) += $2) \
1155)
1156
1157# -----------------------------------------------------------------------------
1158# Macro    : get-src-files-with-tag
1159# Arguments: 1: tag name
1160# Usage    : $(call get-src-files-with-tag,<tagname>)
1161# Return   : The list of source file names that have been tagged with <tagname>
1162# -----------------------------------------------------------------------------
1163get-src-files-with-tag = $(LOCAL_SRC_TAG.$1)
1164
1165# -----------------------------------------------------------------------------
1166# Macro    : get-src-files-without-tag
1167# Arguments: 1: tag name
1168# Usage    : $(call get-src-files-without-tag,<tagname>)
1169# Return   : The list of source file names that have NOT been tagged with <tagname>
1170# -----------------------------------------------------------------------------
1171get-src-files-without-tag = $(filter-out $(LOCAL_SRC_TAG.$1),$(LOCAL_SRC_FILES))
1172
1173# -----------------------------------------------------------------------------
1174# Macro    : set-src-files-target-cflags
1175# Arguments: 1: list of source files
1176#            2: list of compiler flags
1177# Usage    : $(call set-src-files-target-cflags,<sources>,<flags>)
1178# Rationale: Set or replace the set of compiler flags that will be applied
1179#            when building a given set of source files. This function should
1180#            normally be called from the toolchain-specific function that
1181#            computes all compiler flags for all source files.
1182# -----------------------------------------------------------------------------
1183set-src-files-target-cflags = $(foreach __src,$1,$(eval LOCAL_SRC_FILES_TARGET_CFLAGS.$(__src) := $2))
1184
1185# -----------------------------------------------------------------------------
1186# Macro    : add-src-files-target-cflags
1187# Arguments: 1: list of source files
1188#            2: list of compiler flags
1189# Usage    : $(call add-src-files-target-cflags,<sources>,<flags>)
1190# Rationale: A variant of set-src-files-target-cflags that can be used
1191#            to append, instead of replace, compiler flags for specific
1192#            source files.
1193# -----------------------------------------------------------------------------
1194add-src-files-target-cflags = $(foreach __src,$1,$(eval LOCAL_SRC_FILES_TARGET_CFLAGS.$(__src) += $2))
1195
1196# -----------------------------------------------------------------------------
1197# Macro    : get-src-file-target-cflags
1198# Arguments: 1: single source file name
1199# Usage    : $(call get-src-file-target-cflags,<source>)
1200# Rationale: Return the set of target-specific compiler flags that must be
1201#            applied to a given source file. These must be set prior to this
1202#            call using set-src-files-target-cflags or add-src-files-target-cflags
1203# -----------------------------------------------------------------------------
1204get-src-file-target-cflags = $(LOCAL_SRC_FILES_TARGET_CFLAGS.$1)
1205
1206# -----------------------------------------------------------------------------
1207# Macro    : set-src-files-text
1208# Arguments: 1: list of source files
1209#            2: text
1210# Usage    : $(call set-src-files-text,<sources>,<text>)
1211# Rationale: Set or replace the 'text' associated to a set of source files.
1212#            The text is a very short string that complements the build
1213#            label. For example, it will be either 'thumb' or 'arm  ' for
1214#            ARM-based toolchains. This function must be called by the
1215#            toolchain-specific functions that processes all source files.
1216# -----------------------------------------------------------------------------
1217set-src-files-text = $(foreach __src,$1,$(eval LOCAL_SRC_FILES_TEXT.$(__src) := $2))
1218
1219# -----------------------------------------------------------------------------
1220# Macro    : get-src-file-text
1221# Arguments: 1: single source file
1222# Usage    : $(call get-src-file-text,<source>)
1223# Rationale: Return the 'text' associated to a given source file when
1224#            set-src-files-text was called.
1225# -----------------------------------------------------------------------------
1226get-src-file-text = $(LOCAL_SRC_FILES_TEXT.$1)
1227
1228# This should only be called for debugging the source files tagging system
1229dump-src-file-tags = \
1230$(info LOCAL_SRC_TAGS := $(LOCAL_SRC_TAGS)) \
1231$(info LOCAL_SRC_FILES = $(LOCAL_SRC_FILES)) \
1232$(foreach __tag,$(LOCAL_SRC_TAGS),$(info LOCAL_SRC_TAG.$(__tag) = $(LOCAL_SRC_TAG.$(__tag)))) \
1233$(foreach __src,$(LOCAL_SRC_FILES),$(info LOCAL_SRC_FILES_TAGS.$(__src) = $(LOCAL_SRC_FILES_TAGS.$(__src)))) \
1234$(info WITH arm = $(call get-src-files-with-tag,arm)) \
1235$(info WITHOUT arm = $(call get-src-files-without-tag,arm)) \
1236$(foreach __src,$(LOCAL_SRC_FILES),$(info LOCAL_SRC_FILES_TARGET_CFLAGS.$(__src) = $(LOCAL_SRC_FILES_TARGET_CFLAGS.$(__src)))) \
1237$(foreach __src,$(LOCAL_SRC_FILES),$(info LOCAL_SRC_FILES_TEXT.$(__src) = $(LOCAL_SRC_FILES_TEXT.$(__src)))) \
1238
1239
1240# =============================================================================
1241#
1242# Application.mk support
1243#
1244# =============================================================================
1245
1246# the list of variables that *must* be defined in Application.mk files
1247NDK_APP_VARS_REQUIRED :=
1248
1249# the list of variables that *may* be defined in Application.mk files
1250NDK_APP_VARS_OPTIONAL := APP_OPTIM APP_CPPFLAGS APP_CFLAGS APP_CXXFLAGS APP_LDFLAGS \
1251                         APP_PLATFORM APP_BUILD_SCRIPT APP_ABI APP_MODULES \
1252                         APP_PROJECT_PATH APP_STL APP_SHORT_COMMANDS \
1253                         APP_PIE APP_THIN_ARCHIVE
1254
1255# the list of all variables that may appear in an Application.mk file
1256# or defined by the build scripts.
1257NDK_APP_VARS := $(NDK_APP_VARS_REQUIRED) \
1258                $(NDK_APP_VARS_OPTIONAL) \
1259                APP_DEBUG \
1260                APP_DEBUGGABLE \
1261                APP_MANIFEST
1262
1263# =============================================================================
1264#
1265# Android.mk support
1266#
1267# =============================================================================
1268
1269# =============================================================================
1270#
1271# Build commands support
1272#
1273# =============================================================================
1274
1275get-object-name = $(strip \
1276    $(subst ../,__/,\
1277        $(eval __obj := $1)\
1278        $(foreach __ext,.c .s .S $(LOCAL_CPP_EXTENSION),\
1279            $(eval __obj := $(__obj:%$(__ext)=%$(TARGET_OBJ_EXTENSION)))\
1280        )\
1281        $(__obj)\
1282    ))
1283
1284-test-get-object-name = \
1285  $(eval TARGET_OBJ_EXTENSION=.o)\
1286  $(eval LOCAL_CPP_EXTENSION ?= .cpp)\
1287  $(call test-expect,foo.o,$(call get-object-name,foo.c))\
1288  $(call test-expect,bar.o,$(call get-object-name,bar.s))\
1289  $(call test-expect,zoo.o,$(call get-object-name,zoo.S))\
1290  $(call test-expect,tot.o,$(call get-object-name,tot.cpp))
1291
1292# -----------------------------------------------------------------------------
1293# Macro    : hide
1294# Returns  : nothing
1295# Usage    : $(hide)<make commands>
1296# Rationale: To be used as a prefix for Make build commands to hide them
1297#            by default during the build. To show them, set V=1 in your
1298#            environment or command-line.
1299#
1300#            For example:
1301#
1302#                foo.o: foo.c
1303#                -->|$(hide) <build-commands>
1304#
1305#            Where '-->|' stands for a single tab character.
1306#
1307# -----------------------------------------------------------------------------
1308ifeq ($(V),1)
1309hide = $(empty)
1310else
1311hide = @
1312endif
1313
1314
1315# -----------------------------------------------------------------------------
1316# Function  : local-source-file-path
1317# Parameters: $1: source file (as listed in LOCAL_SRC_FILES)
1318# Returns   : full source file path of $1
1319# Usage     : $(call local-source-file-path,$1)
1320# Rationale : Used to compute the full path of a source listed in
1321#             LOCAL_SRC_FILES. If it is an absolute path, then this
1322#             returns the input, otherwise, prepends $(LOCAL_PATH)/
1323#             to the result.
1324# -----------------------------------------------------------------------------
1325local-source-file-path = $(if $(call host-path-is-absolute,$1),$1,$(LOCAL_PATH)/$1)
1326
1327# cmd-convert-deps
1328#
1329# On Cygwin, we need to convert the .d dependency file generated by
1330# the gcc toolchain by transforming any Windows paths inside it into
1331# Cygwin paths that GNU Make can understand (i.e. C:/Foo => /cygdrive/c/Foo)
1332#
1333# To do that, we will force the compiler to write the dependency file to
1334# <foo>.d.org, which will will later convert through a clever sed script
1335# that is auto-generated by our build system.
1336#
1337# The script is invoked with:
1338#
1339#    $(NDK_DEPENDENCIES_CONVERTER) foo.d
1340#
1341# It will look if foo.d.org exists, and if so, process it
1342# to generate foo.d, then remove the original foo.d.org.
1343#
1344# On other systems, we simply tell the compiler to write to the .d file directly.
1345#
1346# NOTE: In certain cases, no dependency file will be generated by the
1347#       compiler (e.g. when compiling an assembly file as foo.s)
1348#
1349# convert-deps is used to compute the name of the compiler-generated dependency file
1350# cmd-convert-deps is a command used to convert it to a Cygwin-specific path
1351#
1352ifeq ($(HOST_OS),cygwin)
1353convert-deps = $1.org
1354cmd-convert-deps = && $(NDK_DEPENDENCIES_CONVERTER) $1
1355else
1356convert-deps = $1
1357cmd-convert-deps =
1358endif
1359
1360# This assumes that many variables have been pre-defined:
1361# _SRC: source file
1362# _OBJ: destination file
1363# _CC: 'compiler' command
1364# _FLAGS: 'compiler' flags
1365# _TEXT: Display text (e.g. "Compile++ thumb", must be EXACTLY 15 chars long)
1366#
1367define ev-build-file
1368$$(_OBJ): PRIVATE_SRC      := $$(_SRC)
1369$$(_OBJ): PRIVATE_OBJ      := $$(_OBJ)
1370$$(_OBJ): PRIVATE_DEPS     := $$(call host-path,$$(_OBJ).d)
1371$$(_OBJ): PRIVATE_MODULE   := $$(LOCAL_MODULE)
1372$$(_OBJ): PRIVATE_TEXT     := "$$(_TEXT)"
1373$$(_OBJ): PRIVATE_CC       := $$(_CC)
1374$$(_OBJ): PRIVATE_CFLAGS   := $$(_FLAGS)
1375
1376ifeq ($$(LOCAL_SHORT_COMMANDS),true)
1377_OPTIONS_LISTFILE := $$(_OBJ).cflags
1378$$(_OBJ): $$(call generate-list-file,$$(_FLAGS),$$(_OPTIONS_LISTFILE))
1379$$(_OBJ): PRIVATE_CFLAGS := @$$(call host-path,$$(_OPTIONS_LISTFILE))
1380$$(_OBJ): $$(_OPTIONS_LISTFILE)
1381endif
1382
1383$$(call generate-file-dir,$$(_OBJ))
1384
1385$$(_OBJ): $$(_SRC) $$(LOCAL_MAKEFILE) $$(NDK_APP_APPLICATION_MK) $$(NDK_DEPENDENCIES_CONVERTER)
1386	@$$(HOST_ECHO) "$$(PRIVATE_TEXT)  : $$(PRIVATE_MODULE) <= $$(notdir $$(PRIVATE_SRC))"
1387	$$(hide) $$(PRIVATE_CC) -MMD -MP -MF $$(call convert-deps,$$(PRIVATE_DEPS)) $$(PRIVATE_CFLAGS) $$(call host-path,$$(PRIVATE_SRC)) -o $$(call host-path,$$(PRIVATE_OBJ)) \
1388	$$(call cmd-convert-deps,$$(PRIVATE_DEPS))
1389endef
1390
1391# This assumes the same things than ev-build-file, but will handle
1392# the definition of LOCAL_FILTER_ASM as well.
1393define ev-build-source-file
1394LOCAL_DEPENDENCY_DIRS += $$(dir $$(_OBJ))
1395ifndef LOCAL_FILTER_ASM
1396  # Trivial case: Directly generate an object file
1397  $$(eval $$(call ev-build-file))
1398else
1399  # This is where things get hairy, we first transform
1400  # the source into an assembler file, send it to the
1401  # filter, then generate a final object file from it.
1402  #
1403
1404  # First, remember the original settings and compute
1405  # the location of our temporary files.
1406  #
1407  _ORG_SRC := $$(_SRC)
1408  _ORG_OBJ := $$(_OBJ)
1409  _ORG_FLAGS := $$(_FLAGS)
1410  _ORG_TEXT  := $$(_TEXT)
1411
1412  _OBJ_ASM_ORIGINAL := $$(patsubst %$$(TARGET_OBJ_EXTENSION),%.s,$$(_ORG_OBJ))
1413  _OBJ_ASM_FILTERED := $$(patsubst %$$(TARGET_OBJ_EXTENSION),%.filtered.s,$$(_ORG_OBJ))
1414
1415  # If the source file is a plain assembler file, we're going to
1416  # use it directly in our filter.
1417  ifneq (,$$(filter %.s,$$(_SRC)))
1418    _OBJ_ASM_ORIGINAL := $$(_SRC)
1419  endif
1420
1421  #$$(info SRC=$$(_SRC) OBJ=$$(_OBJ) OBJ_ORIGINAL=$$(_OBJ_ASM_ORIGINAL) OBJ_FILTERED=$$(_OBJ_ASM_FILTERED))
1422
1423  # We need to transform the source into an assembly file, instead of
1424  # an object. The proper way to do that depends on the file extension.
1425  #
1426  # For C and C++ source files, simply replace the -c by an -S in the
1427  # compilation command (this forces the compiler to generate an
1428  # assembly file).
1429  #
1430  # For assembler templates (which end in .S), replace the -c with -E
1431  # to send it to the preprocessor instead.
1432  #
1433  # Don't do anything for plain assembly files (which end in .s)
1434  #
1435  ifeq (,$$(filter %.s,$$(_SRC)))
1436    _OBJ   := $$(_OBJ_ASM_ORIGINAL)
1437    ifneq (,$$(filter %.S,$$(_SRC)))
1438      _FLAGS := $$(patsubst -c,-E,$$(_ORG_FLAGS))
1439    else
1440      _FLAGS := $$(patsubst -c,-S,$$(_ORG_FLAGS))
1441    endif
1442    $$(eval $$(call ev-build-file))
1443  endif
1444
1445  # Next, process the assembly file with the filter
1446  $$(_OBJ_ASM_FILTERED): PRIVATE_SRC    := $$(_OBJ_ASM_ORIGINAL)
1447  $$(_OBJ_ASM_FILTERED): PRIVATE_DST    := $$(_OBJ_ASM_FILTERED)
1448  $$(_OBJ_ASM_FILTERED): PRIVATE_FILTER := $$(LOCAL_FILTER_ASM)
1449  $$(_OBJ_ASM_FILTERED): PRIVATE_MODULE := $$(LOCAL_MODULE)
1450  $$(_OBJ_ASM_FILTERED): $$(_OBJ_ASM_ORIGINAL)
1451	@$$(HOST_ECHO) "AsmFilter      : $$(PRIVATE_MODULE) <= $$(notdir $$(PRIVATE_SRC))"
1452	$$(hide) $$(PRIVATE_FILTER) $$(PRIVATE_SRC) $$(PRIVATE_DST)
1453
1454  # Then, generate the final object, we need to keep assembler-specific
1455  # flags which look like -Wa,<option>:
1456  _SRC   := $$(_OBJ_ASM_FILTERED)
1457  _OBJ   := $$(_ORG_OBJ)
1458  _FLAGS := $$(filter -Wa%,$$(_ORG_FLAGS)) -c
1459  _TEXT  := "Assembly     "
1460  $$(eval $$(call ev-build-file))
1461endif
1462endef
1463
1464# -----------------------------------------------------------------------------
1465# Template  : ev-compile-c-source
1466# Arguments : 1: single C source file name (relative to LOCAL_PATH)
1467#             2: target object file (without path)
1468# Returns   : None
1469# Usage     : $(eval $(call ev-compile-c-source,<srcfile>,<objfile>)
1470# Rationale : Internal template evaluated by compile-c-source and
1471#             compile-s-source
1472# -----------------------------------------------------------------------------
1473define  ev-compile-c-source
1474_SRC:=$$(call local-source-file-path,$(1))
1475_OBJ:=$$(LOCAL_OBJS_DIR:%/=%)/$(2)
1476
1477_FLAGS := $$($$(my)CFLAGS) \
1478          $$(call get-src-file-target-cflags,$(1)) \
1479          $$(call host-c-includes,$$(LOCAL_C_INCLUDES) $$(LOCAL_PATH)) \
1480          $$(LOCAL_CFLAGS) \
1481          $$(NDK_APP_CFLAGS) \
1482          $$(call host-c-includes,$$($(my)C_INCLUDES)) \
1483          -c \
1484
1485_TEXT := "Compile $$(call get-src-file-text,$1)"
1486_CC   := $$(NDK_CCACHE) $$(TARGET_CC)
1487
1488$$(eval $$(call ev-build-source-file))
1489endef
1490
1491# -----------------------------------------------------------------------------
1492# Function  : compile-c-source
1493# Arguments : 1: single C source file name (relative to LOCAL_PATH)
1494#             2: object file
1495# Returns   : None
1496# Usage     : $(call compile-c-source,<srcfile>,<objfile>)
1497# Rationale : Setup everything required to build a single C source file
1498# -----------------------------------------------------------------------------
1499compile-c-source = $(eval $(call ev-compile-c-source,$1,$2))
1500
1501# -----------------------------------------------------------------------------
1502# Function  : compile-s-source
1503# Arguments : 1: single Assembly source file name (relative to LOCAL_PATH)
1504#             2: object file
1505# Returns   : None
1506# Usage     : $(call compile-s-source,<srcfile>,<objfile>)
1507# Rationale : Setup everything required to build a single Assembly source file
1508# -----------------------------------------------------------------------------
1509compile-s-source = $(eval $(call ev-compile-c-source,$1,$2))
1510
1511
1512# -----------------------------------------------------------------------------
1513# Template  : ev-compile-cpp-source
1514# Arguments : 1: single C++ source file name (relative to LOCAL_PATH)
1515#             2: target object file (without path)
1516# Returns   : None
1517# Usage     : $(eval $(call ev-compile-cpp-source,<srcfile>,<objfile>)
1518# Rationale : Internal template evaluated by compile-cpp-source
1519# -----------------------------------------------------------------------------
1520
1521define  ev-compile-cpp-source
1522_SRC:=$$(call local-source-file-path,$(1))
1523_OBJ:=$$(LOCAL_OBJS_DIR:%/=%)/$(2)
1524_FLAGS := $$($$(my)CXXFLAGS) \
1525          $$(call get-src-file-target-cflags,$(1)) \
1526          $$(call host-c-includes, $$(LOCAL_C_INCLUDES) $$(LOCAL_PATH)) \
1527          $$(LOCAL_CFLAGS) \
1528          $$(LOCAL_CPPFLAGS) \
1529          $$(LOCAL_CXXFLAGS) \
1530          $$(NDK_APP_CFLAGS) \
1531          $$(NDK_APP_CPPFLAGS) \
1532          $$(NDK_APP_CXXFLAGS) \
1533          $$(call host-c-includes,$$($(my)C_INCLUDES)) \
1534          -c \
1535
1536_CC   := $$(NDK_CCACHE) $$($$(my)CXX)
1537_TEXT := "Compile++ $$(call get-src-file-text,$1)"
1538
1539$$(eval $$(call ev-build-source-file))
1540endef
1541
1542# -----------------------------------------------------------------------------
1543# Function  : compile-cpp-source
1544# Arguments : 1: single C++ source file name (relative to LOCAL_PATH)
1545#           : 2: object file name
1546# Returns   : None
1547# Usage     : $(call compile-c-source,<srcfile>)
1548# Rationale : Setup everything required to build a single C++ source file
1549# -----------------------------------------------------------------------------
1550compile-cpp-source = $(eval $(call ev-compile-cpp-source,$1,$2))
1551
1552#
1553#  Module imports
1554#
1555
1556# Initialize import list
1557import-init = $(eval __ndk_import_dirs :=)
1558
1559# Add an optional single directory to the list of import paths
1560#
1561import-add-path-optional = \
1562  $(if $(strip $(wildcard $1)),\
1563    $(call ndk_log,Adding import directory: $1)\
1564    $(eval __ndk_import_dirs += $1)\
1565  )\
1566
1567# Add a directory to the list of import paths
1568# This will warn if the directory does not exist
1569#
1570import-add-path = \
1571  $(if $(strip $(wildcard $1)),\
1572    $(call ndk_log,Adding import directory: $1)\
1573    $(eval __ndk_import_dirs += $1)\
1574  ,\
1575    $(call __ndk_info,WARNING: Ignoring unknown import directory: $1)\
1576  )\
1577
1578import-find-module = $(strip \
1579      $(eval __imported_module :=)\
1580      $(foreach __import_dir,$(__ndk_import_dirs),\
1581        $(if $(__imported_module),,\
1582          $(call ndk_log,  Probing $(__import_dir)/$1/Android.mk)\
1583          $(if $(strip $(wildcard $(__import_dir)/$1/Android.mk)),\
1584            $(eval __imported_module := $(__import_dir)/$1)\
1585          )\
1586        )\
1587      )\
1588      $(__imported_module)\
1589    )
1590
1591# described in docs/IMPORT-MODULE.TXT
1592# $1: tag name for the lookup
1593#
1594# Small technical note on __ndk_import_depth: we use this variable to
1595# record the depth of recursive import-module calls. The variable is
1596# initially empty, and we append a "x" to it each time import-module is
1597# called. I.e. for three recursive calls to import-module, we would get
1598# the values:
1599#
1600#   first call:   x
1601#   second call:  xx
1602#   third call:   xxx
1603#
1604# This is used in module-add to add the top-level modules (i.e. those
1605# that are not added with import-module) to __ndk_top_modules, corresponding
1606# to the default list of wanted modules (see setup-toolchain.mk).
1607#
1608import-module = \
1609    $(eval __import_tag := $(strip $1))\
1610    $(if $(call seq,$(words $(__import_tag)),1),,\
1611      $(call __ndk_info,$(call local-makefile): Cannot import module with spaces in tag: '$(__import_tag)')\
1612    )\
1613    $(if $(call set_is_member,$(__ndk_import_list),$(__import_tag)),\
1614      $(call ndk_log,Skipping duplicate import for module with tag '$(__import_tag)')\
1615    ,\
1616      $(call ndk_log,Looking for imported module with tag '$(__import_tag)')\
1617      $(eval __imported_path := $(call import-find-module,$(__import_tag)))\
1618      $(if $(__imported_path),\
1619        $(call ndk_log,    Found in $(__imported_path))\
1620        $(eval __ndk_import_depth := $(__ndk_import_depth)x) \
1621        $(eval __ndk_import_list := $(call set_insert,$(__ndk_import_list),$(__import_tag)))\
1622        $(eval include $(__imported_path)/Android.mk)\
1623        $(eval __ndk_import_depth := $(__ndk_import_depth:%x=%))\
1624      ,\
1625        $(call __ndk_info,$(call local-makefile): Cannot find module with tag '$(__import_tag)' in import path)\
1626        $(call __ndk_info,Are you sure your NDK_MODULE_PATH variable is properly defined ?)\
1627        $(call __ndk_info,The following directories were searched:)\
1628        $(for __import_dir,$(__ndk_import_dirs),\
1629          $(call __ndk_info,    $(__import_dir))\
1630        )\
1631        $(call __ndk_error,Aborting.)\
1632      )\
1633   )
1634
1635# Only used for debugging
1636#
1637import-debug = \
1638    $(info IMPORT DIRECTORIES:)\
1639    $(foreach __dir,$(__ndk_import_dirs),\
1640      $(info -- $(__dir))\
1641    )\
1642
1643#
1644#  Module classes
1645#
1646NDK_MODULE_CLASSES :=
1647
1648# Register a new module class
1649# $1: class name (e.g. STATIC_LIBRARY)
1650# $2: optional file prefix (e.g. 'lib')
1651# $3: optional file suffix (e.g. '.so')
1652#
1653module-class-register = \
1654    $(eval NDK_MODULE_CLASSES += $1) \
1655    $(eval NDK_MODULE_CLASS.$1.FILE_PREFIX := $2) \
1656    $(eval NDK_MODULE_CLASS.$1.FILE_SUFFIX := $3) \
1657    $(eval NDK_MODULE_CLASS.$1.INSTALLABLE := $(false)) \
1658
1659# Same a module-class-register, for installable modules
1660#
1661# An installable module is one that will be copied to $PROJECT/libs/<abi>/
1662# during the NDK build.
1663#
1664# $1: class name
1665# $2: optional file prefix
1666# $3: optional file suffix
1667#
1668module-class-register-installable = \
1669    $(call module-class-register,$1,$2,$3) \
1670    $(eval NDK_MODULE_CLASS.$1.INSTALLABLE := $(true))
1671
1672# Returns $(true) if $1 is a valid/registered LOCAL_MODULE_CLASS value
1673#
1674module-class-check = $(call set_is_member,$(NDK_MODULE_CLASSES),$1)
1675
1676# Returns $(true) if $1 corresponds to an installable module class
1677#
1678module-class-is-installable = $(if $(NDK_MODULE_CLASS.$1.INSTALLABLE),$(true),$(false))
1679
1680# Returns $(true) if $1 corresponds to a copyable prebuilt module class
1681#
1682module-class-is-copyable = $(if $(call seq,$1,PREBUILT_SHARED_LIBRARY),$(true),$(false))
1683
1684#
1685# Register valid module classes
1686#
1687
1688# static libraries:
1689# <foo> -> lib<foo>.a by default
1690$(call module-class-register,STATIC_LIBRARY,lib,$(TARGET_LIB_EXTENSION))
1691
1692# shared libraries:
1693# <foo> -> lib<foo>.so
1694# a shared library is installable.
1695$(call module-class-register-installable,SHARED_LIBRARY,lib,$(TARGET_SONAME_EXTENSION))
1696
1697# executable
1698# <foo> -> <foo>
1699# an executable is installable.
1700$(call module-class-register-installable,EXECUTABLE,,)
1701
1702# prebuilt shared library
1703# <foo> -> <foo>  (we assume it is already well-named)
1704# it is installable
1705$(call module-class-register-installable,PREBUILT_SHARED_LIBRARY,,)
1706
1707# prebuilt static library
1708# <foo> -> <foo> (we assume it is already well-named)
1709$(call module-class-register,PREBUILT_STATIC_LIBRARY,,)
1710
1711#
1712# C++ STL support
1713#
1714
1715# The list of registered STL implementations we support
1716NDK_STL_LIST :=
1717
1718# Used internally to register a given STL implementation, see below.
1719#
1720# $1: STL name as it appears in APP_STL (e.g. system)
1721# $2: STL module name (e.g. cxx-stl/system)
1722# $3: list of static libraries all modules will depend on
1723# $4: list of shared libraries all modules will depend on
1724#
1725ndk-stl-register = \
1726    $(eval __ndk_stl := $(strip $1)) \
1727    $(eval NDK_STL_LIST += $(__ndk_stl)) \
1728    $(eval NDK_STL.$(__ndk_stl).IMPORT_MODULE := $(strip $2)) \
1729    $(eval NDK_STL.$(__ndk_stl).STATIC_LIBS := $(strip $(call strip-lib-prefix,$3))) \
1730    $(eval NDK_STL.$(__ndk_stl).SHARED_LIBS := $(strip $(call strip-lib-prefix,$4)))
1731
1732# Called to check that the value of APP_STL is a valid one.
1733# $1: STL name as it apperas in APP_STL (e.g. 'system')
1734#
1735ndk-stl-check = \
1736    $(if $(call set_is_member,$(NDK_STL_LIST),$1),,\
1737        $(call __ndk_info,Invalid APP_STL value: $1)\
1738        $(call __ndk_info,Please use one of the following instead: $(NDK_STL_LIST))\
1739        $(call __ndk_error,Aborting))
1740
1741# Called before the top-level Android.mk is parsed to
1742# select the STL implementation.
1743# $1: STL name as it appears in APP_STL (e.g. system)
1744#
1745ndk-stl-select = \
1746    $(call import-module,$(NDK_STL.$1.IMPORT_MODULE))
1747
1748# Called after all Android.mk files are parsed to add
1749# proper STL dependencies to every C++ module.
1750# $1: STL name as it appears in APP_STL (e.g. system)
1751#
1752ndk-stl-add-dependencies = \
1753    $(call modules-add-c++-dependencies,\
1754        $(NDK_STL.$1.STATIC_LIBS),\
1755        $(NDK_STL.$1.SHARED_LIBS))
1756
1757#
1758#
1759
1760# Register the 'system' STL implementation
1761#
1762$(call ndk-stl-register,\
1763    system,\
1764    cxx-stl/system,\
1765    libstdc++,\
1766    )
1767
1768# Register the 'stlport_static' STL implementation
1769#
1770$(call ndk-stl-register,\
1771    stlport_static,\
1772    cxx-stl/stlport,\
1773    stlport_static,\
1774    )
1775
1776# Register the 'stlport_shared' STL implementation
1777#
1778$(call ndk-stl-register,\
1779    stlport_shared,\
1780    cxx-stl/stlport,\
1781    ,\
1782    stlport_shared\
1783    )
1784
1785# Register the 'gnustl_static' STL implementation
1786#
1787$(call ndk-stl-register,\
1788    gnustl_static,\
1789    cxx-stl/gnu-libstdc++,\
1790    gnustl_static,\
1791    \
1792    )
1793
1794$(call ndk-stl-register,\
1795    gnustl_shared,\
1796    cxx-stl/gnu-libstdc++,\
1797    ,\
1798    gnustl_shared\
1799    )
1800
1801# Register the static version of the GAbi++ C++ runtime
1802#
1803$(call ndk-stl-register,\
1804    gabi++_static,\
1805    cxx-stl/gabi++,\
1806    gabi++_static,\
1807    )
1808
1809# Register the shared version of the GAbi++ C++ runtime
1810#
1811$(call ndk-stl-register,\
1812    gabi++_shared,\
1813    cxx-stl/gabi++,\
1814    gabi++_shared,\
1815    )
1816
1817# The 'none' APP_STL value corresponds to no C++ support at
1818# all. Used by some of the STLport and GAbi++ test projects.
1819#
1820$(call ndk-stl-register,\
1821    none,\
1822    cxx-stl/system,\
1823    )
1824
1825ifneq (,$(NDK_UNIT_TESTS))
1826$(call ndk-run-all-tests)
1827endif
1828