1 # Copyright (C) 2009 The Android Open Source Project
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Common definitions for the Android NDK build system
18 # We use the GNU Make Standard Library
19 include $(BUILD_SYSTEM)/../gmsl/gmsl
21 # This is the Android NDK version number as a list of three items:
22 # major, minor, revision
26 # Used to output warnings and error from the library, it's possible to
27 # disable any warnings or errors by overriding these definitions
28 # manually or by setting GMSL_NO_WARNINGS or GMSL_NO_ERRORS
30 __ndk_name := Android NDK
31 __ndk_info = $(info $(__ndk_name): $1 $2 $3 $4 $5)
32 __ndk_warning = $(warning $(__ndk_name): $1 $2 $3 $4 $5)
33 __ndk_error = $(error $(__ndk_name): $1 $2 $3 $4 $5)
42 # If NDK_TRACE is enabled then calls to the library functions are
43 # traced to stdout using warning messages with their arguments
46 __ndk_tr1 = $(warning $0('$1'))
47 __ndk_tr2 = $(warning $0('$1','$2'))
48 __ndk_tr3 = $(warning $0('$1','$2','$3'))
55 # -----------------------------------------------------------------------------
57 # Arguments: 1: text to print when NDK_LOG is defined
59 # Usage : $(call ndk_log,<some text>)
60 # -----------------------------------------------------------------------------
62 ndk_log = $(info $(__ndk_name): $1)
68 # -----------------------------------------------------------------------------
70 # Returns : an empty macro
72 # -----------------------------------------------------------------------------
75 # -----------------------------------------------------------------------------
77 # Returns : a single space
79 # -----------------------------------------------------------------------------
80 space := $(empty) $(empty)
82 # -----------------------------------------------------------------------------
85 # Returns : the penultimate (next-to-last) element of a list
86 # Usage : $(call last2, <LIST>)
87 # -----------------------------------------------------------------------------
88 last2 = $(word $(words $1), x $1)
90 # -----------------------------------------------------------------------------
93 # Returns : the antepenultimate (second-next-to-last) element of a list
94 # Usage : $(call last3, <LIST>)
95 # -----------------------------------------------------------------------------
96 last3 = $(word $(words $1), x x $1)
98 # -----------------------------------------------------------------------------
99 # Macro : this-makefile
100 # Returns : the name of the current Makefile in the inclusion stack
101 # Usage : $(this-makefile)
102 # -----------------------------------------------------------------------------
103 this-makefile = $(lastword $(MAKEFILE_LIST))
105 # -----------------------------------------------------------------------------
106 # Macro : local-makefile
107 # Returns : the name of the last parsed Android.mk file
108 # Usage : $(local-makefile)
109 # -----------------------------------------------------------------------------
110 local-makefile = $(lastword $(filter %Android.mk,$(MAKEFILE_LIST)))
112 # -----------------------------------------------------------------------------
113 # Function : assert-defined
114 # Arguments: 1: list of variable names
116 # Usage : $(call assert-defined, VAR1 VAR2 VAR3...)
117 # Rationale: Checks that all variables listed in $1 are defined, or abort the
119 # -----------------------------------------------------------------------------
120 assert-defined = $(foreach __varname,$(strip $1),\
121 $(if $(strip $($(__varname))),,\
122 $(call __ndk_error, Assertion failure: $(__varname) is not defined)\
126 # -----------------------------------------------------------------------------
127 # Function : clear-vars
128 # Arguments: 1: list of variable names
129 # 2: file where the variable should be defined
131 # Usage : $(call clear-vars, VAR1 VAR2 VAR3...)
132 # Rationale: Clears/undefines all variables in argument list
133 # -----------------------------------------------------------------------------
134 clear-vars = $(foreach __varname,$1,$(eval $(__varname) := $(empty)))
136 # -----------------------------------------------------------------------------
137 # Function : check-required-vars
138 # Arguments: 1: list of variable names
139 # 2: file where the variable(s) should be defined
141 # Usage : $(call check-required-vars, VAR1 VAR2 VAR3..., <file>)
142 # Rationale: Checks that all required vars listed in $1 were defined by $2
143 # or abort the build with an error
144 # -----------------------------------------------------------------------------
145 check-required-vars = $(foreach __varname,$1,\
146 $(if $(strip $($(__varname))),,\
147 $(call __ndk_info, Required variable $(__varname) is not defined by $2)\
148 $(call __ndk_error,Aborting)\
152 # -----------------------------------------------------------------------------
153 # Function : modules-clear
156 # Usage : $(call modules-clear)
157 # Rationale: clears the list of defined modules known by the build system
158 # -----------------------------------------------------------------------------
159 modules-clear = $(eval __ndk_modules := $(empty_set))
161 # -----------------------------------------------------------------------------
162 # Function : modules-add
163 # Arguments: 1: module name
164 # 2: path to Android.mk where the module is defined
166 # Usage : $(call modules-add,<modulename>,<Android.mk path>)
167 # Rationale: add a new module. If it is already defined, print an error message
169 # -----------------------------------------------------------------------------
171 $(if $(call set_is_member,$(__ndk_modules),$1),\
172 $(call __ndk_info,Trying to define local module '$1' in $2.)\
173 $(call __ndk_info,But this module was already defined by $(__ndk_modules.$1).)\
174 $(call __ndk_error,Aborting.)\
176 $(eval __ndk_modules := $(call set_insert,$(__ndk_modules),$1))\
177 $(eval __ndk_modules.$1 := $2)\
179 # -----------------------------------------------------------------------------
180 # Function : check-user-define
181 # Arguments: 1: name of variable that must be defined by the user
182 # 2: name of Makefile where the variable should be defined
183 # 3: name/description of the Makefile where the check is done, which
184 # must be included by $2
186 # -----------------------------------------------------------------------------
187 check-user-define = $(if $(strip $($1)),,\
188 $(call __ndk_error,Missing $1 before including $3 in $2))
190 # -----------------------------------------------------------------------------
191 # This is used to check that LOCAL_MODULE is properly defined by an Android.mk
192 # file before including one of the $(BUILD_SHARED_LIBRARY), etc... files.
194 # Function : check-user-LOCAL_MODULE
195 # Arguments: 1: name/description of the included build Makefile where the
198 # Usage : $(call check-user-LOCAL_MODULE, BUILD_SHARED_LIBRARY)
199 # -----------------------------------------------------------------------------
200 check-defined-LOCAL_MODULE = \
201 $(call check-user-define,LOCAL_MODULE,$(local-makefile),$(1)) \
202 $(if $(call seq,$(words $(LOCAL_MODULE)),1),,\
203 $(call __ndk_info,LOCAL_MODULE definition in $(local-makefile) must not contain space)\
204 $(call __ndk_error,Please correct error. Aborting)\
207 # -----------------------------------------------------------------------------
208 # Strip any 'lib' prefix in front of a given string.
210 # Function : strip-lib-prefix
211 # Arguments: 1: module name
212 # Returns : module name, without any 'lib' prefix if any
213 # Usage : $(call strip-lib-prefix,$(LOCAL_MODULE))
214 # -----------------------------------------------------------------------------
215 strip-lib-prefix = $(1:lib%=%)
217 # -----------------------------------------------------------------------------
218 # This is used to strip any lib prefix from LOCAL_MODULE, then check that
219 # the corresponding module name is not already defined.
221 # Function : check-user-LOCAL_MODULE
222 # Arguments: 1: path of Android.mk where this LOCAL_MODULE is defined
224 # Usage : $(call check-LOCAL_MODULE,$(LOCAL_MAKEFILE))
225 # -----------------------------------------------------------------------------
226 check-LOCAL_MODULE = \
227 $(eval LOCAL_MODULE := $$(call strip-lib-prefix,$$(LOCAL_MODULE)))\
228 $(call modules-add,$(LOCAL_MODULE),$1)
230 # -----------------------------------------------------------------------------
232 # Returns : the directory of the current Makefile
234 # -----------------------------------------------------------------------------
235 my-dir = $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
237 # -----------------------------------------------------------------------------
238 # Function : all-makefiles-under
239 # Arguments: 1: directory path
240 # Returns : a list of all makefiles immediately below some directory
241 # Usage : $(call all-makefiles-under, <some path>)
242 # -----------------------------------------------------------------------------
243 all-makefiles-under = $(wildcard $1/*/Android.mk)
245 # -----------------------------------------------------------------------------
246 # Macro : all-subdir-makefiles
247 # Returns : list of all makefiles in subdirectories of the current Makefile's
249 # Usage : $(all-subdir-makefiles)
250 # -----------------------------------------------------------------------------
251 all-subdir-makefiles = $(call all-makefiles-under,$(call my-dir))
254 # =============================================================================
256 # Application.mk support
258 # =============================================================================
260 # the list of variables that *must* be defined in Application.mk files
261 NDK_APP_VARS_REQUIRED := APP_MODULES APP_PROJECT_PATH
263 # the list of variables that *may* be defined in Application.mk files
264 NDK_APP_VARS_OPTIONAL := APP_OPTIM APP_CPPFLAGS APP_CFLAGS APP_CXXFLAGS
266 # the list of all variables that may appear in an Application.mk file
267 NDK_APP_VARS := $(NDK_APP_VARS_REQUIRED) $(NDK_APP_VARS_OPTIONAL)
269 # =============================================================================
273 # =============================================================================
276 # =============================================================================
278 # Generated files support
280 # =============================================================================
283 # -----------------------------------------------------------------------------
284 # Function : host-static-library-path
285 # Arguments : 1: library module name (e.g. 'foo')
286 # Returns : location of generated host library name (e.g. '..../libfoo.a)
287 # Usage : $(call host-static-library-path,<modulename>)
288 # -----------------------------------------------------------------------------
289 host-static-library-path = $(HOST_OUT)/lib$1.a
291 # -----------------------------------------------------------------------------
292 # Function : host-executable-path
293 # Arguments : 1: executable module name (e.g. 'foo')
294 # Returns : location of generated host executable name (e.g. '..../foo)
295 # Usage : $(call host-executable-path,<modulename>)
296 # -----------------------------------------------------------------------------
297 host-executable-path = $(HOST_OUT)/$1$(HOST_EXE)
299 # -----------------------------------------------------------------------------
300 # Function : static-library-path
301 # Arguments : 1: library module name (e.g. 'foo')
302 # Returns : location of generated static library name (e.g. '..../libfoo.a)
303 # Usage : $(call static-library-path,<modulename>)
304 # -----------------------------------------------------------------------------
305 static-library-path = $(TARGET_OUT)/lib$1.a
307 # -----------------------------------------------------------------------------
308 # Function : shared-library-path
309 # Arguments : 1: library module name (e.g. 'foo')
310 # Returns : location of generated shared library name (e.g. '..../libfoo.so)
311 # Usage : $(call shared-library-path,<modulename>)
312 # -----------------------------------------------------------------------------
313 shared-library-path = $(TARGET_OUT)/lib$1.so
315 # -----------------------------------------------------------------------------
316 # Function : executable-path
317 # Arguments : 1: executable module name (e.g. 'foo')
318 # Returns : location of generated exectuable name (e.g. '..../foo)
319 # Usage : $(call executable-path,<modulename>)
320 # -----------------------------------------------------------------------------
321 executable-path = $(TARGET_OUT)/$1
323 # =============================================================================
325 # Build commands support
327 # =============================================================================
329 # -----------------------------------------------------------------------------
332 # Usage : $(hide)<make commands>
333 # Rationale: To be used as a prefix for Make build commands to hide them
334 # by default during the build. To show them, set V=1 in your
335 # environment or command-line.
340 # -->|$(hide) <build-commands>
342 # Where '-->|' stands for a single tab character.
344 # -----------------------------------------------------------------------------
351 # -----------------------------------------------------------------------------
352 # Template : ev-compile-c-source
353 # Arguments : 1: single C source file name (relative to LOCAL_PATH)
354 # 2: target object file (without path)
356 # Usage : $(eval $(call ev-compile-c-source,<srcfile>,<objfile>)
357 # Rationale : Internal template evaluated by compile-c-source and
359 # -----------------------------------------------------------------------------
360 define ev-compile-c-source
361 _SRC:=$$(LOCAL_PATH)/$(1)
362 _OBJ:=$$(LOCAL_OBJS_DIR)/$(2)
364 $$(_OBJ): PRIVATE_SRC := $$(_SRC)
365 $$(_OBJ): PRIVATE_OBJ := $$(_OBJ)
366 $$(_OBJ): PRIVATE_MODULE := $$(LOCAL_MODULE)
367 $$(_OBJ): PRIVATE_ARM_MODE := $$(LOCAL_ARM_MODE)
368 $$(_OBJ): PRIVATE_ARM_TEXT := $$(LOCAL_ARM_TEXT)
369 $$(_OBJ): PRIVATE_CC := $$($$(my)CC)
370 $$(_OBJ): PRIVATE_CFLAGS := $$($$(my)CFLAGS) \
371 $$($$(my)$(LOCAL_ARM_MODE)_$(LOCAL_BUILD_MODE)_CFLAGS) \
372 $$(LOCAL_C_INCLUDES:%=-I%) \
377 $$(_OBJ): $$(_SRC) $$(LOCAL_MAKEFILE) $$(NDK_APP_APPLICATION_MK)
378 @mkdir -p $$(dir $$(PRIVATE_OBJ))
379 @echo "Compile $$(PRIVATE_ARM_TEXT) : $$(PRIVATE_MODULE) <= $$(PRIVATE_SRC)"
380 $(hide) $$(PRIVATE_CC) $$(PRIVATE_CFLAGS) -c \
381 -MMD -MP -MF $$(PRIVATE_OBJ).d.tmp \
384 $$(call cmd-process-deps,$$(PRIVATE_OBJ))
386 LOCAL_OBJECTS += $$(_OBJ)
387 LOCAL_DEPENDENCY_DIRS += $$(dir $$(_OBJ))
390 # -----------------------------------------------------------------------------
391 # Function : compile-c-source
392 # Arguments : 1: single C source file name (relative to LOCAL_PATH)
394 # Usage : $(call compile-c-source,<srcfile>)
395 # Rationale : Setup everything required to build a single C source file
396 # -----------------------------------------------------------------------------
397 compile-c-source = $(eval $(call ev-compile-c-source,$1,$(1:%.c=%.o)))
399 # -----------------------------------------------------------------------------
400 # Function : compile-s-source
401 # Arguments : 1: single Assembly source file name (relative to LOCAL_PATH)
403 # Usage : $(call compile-s-source,<srcfile>)
404 # Rationale : Setup everything required to build a single Assembly source file
405 # -----------------------------------------------------------------------------
406 compile-s-source = $(eval $(call ev-compile-c-source,$1,$(1:%.S=%.o)))
409 # -----------------------------------------------------------------------------
410 # Template : ev-compile-cpp-source
411 # Arguments : 1: single C++ source file name (relative to LOCAL_PATH)
412 # 2: target object file (without path)
414 # Usage : $(eval $(call ev-compile-cpp-source,<srcfile>,<objfile>)
415 # Rationale : Internal template evaluated by compile-cpp-source
416 # -----------------------------------------------------------------------------
418 define ev-compile-cpp-source
419 _SRC:=$$(LOCAL_PATH)/$(1)
420 _OBJ:=$$(LOCAL_OBJS_DIR)/$(2)
422 $$(_OBJ): PRIVATE_SRC := $$(_SRC)
423 $$(_OBJ): PRIVATE_OBJ := $$(_OBJ)
424 $$(_OBJ): PRIVATE_MODULE := $$(LOCAL_MODULE)
425 $$(_OBJ): PRIVATE_ARM_MODE := $$(LOCAL_ARM_MODE)
426 $$(_OBJ): PRIVATE_ARM_TEXT := $$(LOCAL_ARM_TEXT)
427 $$(_OBJ): PRIVATE_CXX := $$($$(my)CXX)
428 $$(_OBJ): PRIVATE_CXXFLAGS := $$($$(my)CXXFLAGS) \
429 $$($$(my)$(LOCAL_ARM_MODE)_$(LOCAL_BUILD_MODE)_CFLAGS) \
430 $$(LOCAL_C_INCLUDES:%=-I%) \
436 $$(NDK_APP_CPPFLAGS) \
437 $$(NDK_APP_CXXFLAGS) \
439 $$(_OBJ): $$(_SRC) $$(LOCAL_MAKEFILE) $$(NDK_APP_APPLICATION_MK)
440 @mkdir -p $$(dir $$(PRIVATE_OBJ))
441 @echo "Compile++ $$(PRIVATE_ARM_TEXT): $$(PRIVATE_MODULE) <= $$(PRIVATE_SRC)"
442 $(hide) $$(PRIVATE_CXX) $$(PRIVATE_CXXFLAGS) -c \
443 -MMD -MP -MF $$(PRIVATE_OBJ).d.tmp \
446 $$(call cmd-process-deps,$$(PRIVATE_OBJ))
448 LOCAL_OBJECTS += $$(_OBJ)
449 LOCAL_DEPENDENCY_DIRS += $$(dir $$(_OBJ))
452 # -----------------------------------------------------------------------------
453 # Function : compile-cpp-source
454 # Arguments : 1: single C++ source file name (relative to LOCAL_PATH)
456 # Usage : $(call compile-c-source,<srcfile>)
457 # Rationale : Setup everything required to build a single C++ source file
458 # -----------------------------------------------------------------------------
459 compile-cpp-source = $(eval $(call ev-compile-cpp-source,$1,$(1:%$(LOCAL_CPP_EXTENSION)=%.o)))
461 # -----------------------------------------------------------------------------
462 # Command : cmd-process-deps
463 # Arguments : 1: object file path
465 # Usage : $(call cmd-process-deps,<objectfile>)
466 # Rationale : To be used as a Make build command to process the dependencies
467 # generated by the compiler (in <obj>.d.tmp) into ones suited
468 # for our build system. See the comments in build/core/mkdeps.sh
470 # -----------------------------------------------------------------------------
471 cmd-process-deps = $(hide) $(BUILD_SYSTEM)/mkdeps.sh $(1) $(1).d.tmp $(1).d
473 # -----------------------------------------------------------------------------
474 # Command : cmd-install-file
475 # Arguments : 1: source file
476 # 2: destination file
478 # Usage : $(call cmd-install-file,<srcfile>,<dstfile>)
479 # Rationale : To be used as a Make build command to copy/install a file to
481 # -----------------------------------------------------------------------------
482 define cmd-install-file