OSDN Git Service

Fix LOCAL_CFLAGS/CPPFLAGS handling to match full Android build system.
[android-x86/development.git] / ndk / build / core / definitions.mk
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
19 include $(BUILD_SYSTEM)/../gmsl/gmsl
20
21 # This is the Android NDK version number as a list of three items:
22 # major, minor, revision
23 #
24 ndk_version := 1 0 0
25
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
29
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)
34
35 ifdef NDK_NO_WARNINGS
36 __ndk_warning :=
37 endif
38 ifdef NDK_NO_ERRORS
39 __ndk_error :=
40 endif
41
42 # If NDK_TRACE is enabled then calls to the library functions are
43 # traced to stdout using warning messages with their arguments
44
45 ifdef NDK_TRACE
46 __ndk_tr1 = $(warning $0('$1'))
47 __ndk_tr2 = $(warning $0('$1','$2'))
48 __ndk_tr3 = $(warning $0('$1','$2','$3'))
49 else
50 __ndk_tr1 :=
51 __ndk_tr2 :=
52 __ndk_tr3 :=
53 endif
54
55 # -----------------------------------------------------------------------------
56 # Function : ndk_log
57 # Arguments: 1: text to print when NDK_LOG is defined
58 # Returns  : None
59 # Usage    : $(call ndk_log,<some text>)
60 # -----------------------------------------------------------------------------
61 ifdef NDK_LOG
62 ndk_log = $(info $(__ndk_name): $1)
63 else
64 ndk_log :=
65 endif
66
67
68 # -----------------------------------------------------------------------------
69 # Macro    : empty
70 # Returns  : an empty macro
71 # Usage    : $(empty)
72 # -----------------------------------------------------------------------------
73 empty :=
74
75 # -----------------------------------------------------------------------------
76 # Macro    : space
77 # Returns  : a single space
78 # Usage    : $(space)
79 # -----------------------------------------------------------------------------
80 space  := $(empty) $(empty)
81
82 # -----------------------------------------------------------------------------
83 # Function : last2
84 # Arguments: a list
85 # Returns  : the penultimate (next-to-last) element of a list
86 # Usage    : $(call last2, <LIST>)
87 # -----------------------------------------------------------------------------
88 last2 = $(word $(words $1), x $1)
89
90 # -----------------------------------------------------------------------------
91 # Function : last3
92 # Arguments: a list
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)
97
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))
104
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)))
111
112 # -----------------------------------------------------------------------------
113 # Function : assert-defined
114 # Arguments: 1: list of variable names
115 # Returns  : None
116 # Usage    : $(call assert-defined, VAR1 VAR2 VAR3...)
117 # Rationale: Checks that all variables listed in $1 are defined, or abort the
118 #            build
119 # -----------------------------------------------------------------------------
120 assert-defined = $(foreach __varname,$(strip $1),\
121   $(if $(strip $($(__varname))),,\
122     $(call __ndk_error, Assertion failure: $(__varname) is not defined)\
123   )\
124 )
125
126 # -----------------------------------------------------------------------------
127 # Function : clear-vars
128 # Arguments: 1: list of variable names
129 #            2: file where the variable should be defined
130 # Returns  : None
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)))
135
136 # -----------------------------------------------------------------------------
137 # Function : check-required-vars
138 # Arguments: 1: list of variable names
139 #            2: file where the variable(s) should be defined
140 # Returns  : None
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)\
149   )\
150 )
151
152 # -----------------------------------------------------------------------------
153 # Function : modules-clear
154 # Arguments: None
155 # Returns  : None
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))
160
161 # -----------------------------------------------------------------------------
162 # Function : modules-add
163 # Arguments: 1: module name
164 #            2: path to Android.mk where the module is defined
165 # Returns  : None
166 # Usage    : $(call modules-add,<modulename>,<Android.mk path>)
167 # Rationale: add a new module. If it is already defined, print an error message
168 #            and abort.
169 # -----------------------------------------------------------------------------
170 modules-add = \
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.)\
175   )\
176   $(eval __ndk_modules := $(call set_insert,$(__ndk_modules),$1))\
177   $(eval __ndk_modules.$1 := $2)\
178
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
185 # Returns  : None
186 # -----------------------------------------------------------------------------
187 check-user-define = $(if $(strip $($1)),,\
188   $(call __ndk_error,Missing $1 before including $3 in $2))
189
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.
193 #
194 # Function : check-user-LOCAL_MODULE
195 # Arguments: 1: name/description of the included build Makefile where the
196 #               check is done
197 # Returns  : None
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)\
205   )
206
207 # -----------------------------------------------------------------------------
208 # Strip any 'lib' prefix in front of a given string.
209 #
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%=%)
216
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.
220 #
221 # Function : check-user-LOCAL_MODULE
222 # Arguments: 1: path of Android.mk where this LOCAL_MODULE is defined
223 # Returns  : None
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)
229
230 # -----------------------------------------------------------------------------
231 # Macro    : my-dir
232 # Returns  : the directory of the current Makefile
233 # Usage    : $(my-dir)
234 # -----------------------------------------------------------------------------
235 my-dir = $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
236
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)
244
245 # -----------------------------------------------------------------------------
246 # Macro    : all-subdir-makefiles
247 # Returns  : list of all makefiles in subdirectories of the current Makefile's
248 #            location
249 # Usage    : $(all-subdir-makefiles)
250 # -----------------------------------------------------------------------------
251 all-subdir-makefiles = $(call all-makefiles-under,$(call my-dir))
252
253
254 # =============================================================================
255 #
256 # Application.mk support
257 #
258 # =============================================================================
259
260 # the list of variables that *must* be defined in Application.mk files
261 NDK_APP_VARS_REQUIRED := APP_MODULES APP_PROJECT_PATH
262
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
265
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)
268
269 # =============================================================================
270 #
271 # Android.mk support
272 #
273 # =============================================================================
274
275
276 # =============================================================================
277 #
278 # Generated files support
279 #
280 # =============================================================================
281
282
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
290
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)
298
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
306
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
314
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
322
323 # =============================================================================
324 #
325 # Build commands support
326 #
327 # =============================================================================
328
329 # -----------------------------------------------------------------------------
330 # Macro    : hide
331 # Returns  : nothing
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.
336 #
337 #            For example:
338 #
339 #                foo.o: foo.c
340 #                -->|$(hide) <build-commands>
341 #
342 #            Where '-->|' stands for a single tab character.
343 #
344 # -----------------------------------------------------------------------------
345 ifeq ($(V),1)
346 hide = $(empty)
347 else
348 hide = @
349 endif
350
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)
355 # Returns   : None
356 # Usage     : $(eval $(call ev-compile-c-source,<srcfile>,<objfile>)
357 # Rationale : Internal template evaluated by compile-c-source and
358 #             compile-s-source
359 # -----------------------------------------------------------------------------
360 define  ev-compile-c-source
361 _SRC:=$$(LOCAL_PATH)/$(1)
362 _OBJ:=$$(LOCAL_OBJS_DIR)/$(2)
363
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%) \
373                               -I$$(LOCAL_PATH) \
374                               $$(LOCAL_CFLAGS) \
375                               $$(NDK_APP_CFLAGS)
376
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 \
382         $$(PRIVATE_SRC) \
383         -o $$(PRIVATE_OBJ)
384         $$(call cmd-process-deps,$$(PRIVATE_OBJ))
385
386 LOCAL_OBJECTS         += $$(_OBJ)
387 LOCAL_DEPENDENCY_DIRS += $$(dir $$(_OBJ))
388 endef
389
390 # -----------------------------------------------------------------------------
391 # Function  : compile-c-source
392 # Arguments : 1: single C source file name (relative to LOCAL_PATH)
393 # Returns   : None
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)))
398
399 # -----------------------------------------------------------------------------
400 # Function  : compile-s-source
401 # Arguments : 1: single Assembly source file name (relative to LOCAL_PATH)
402 # Returns   : None
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)))
407
408
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)
413 # Returns   : None
414 # Usage     : $(eval $(call ev-compile-cpp-source,<srcfile>,<objfile>)
415 # Rationale : Internal template evaluated by compile-cpp-source
416 # -----------------------------------------------------------------------------
417
418 define  ev-compile-cpp-source
419 _SRC:=$$(LOCAL_PATH)/$(1)
420 _OBJ:=$$(LOCAL_OBJS_DIR)/$(2)
421
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%) \
431                               -I$$(LOCAL_PATH) \
432                               $$(LOCAL_CFLAGS) \
433                               $$(LOCAL_CPPFLAGS) \
434                               $$(LOCAL_CXXFLAGS) \
435                               $$(NDK_APP_CFLAGS) \
436                               $$(NDK_APP_CPPFLAGS) \
437                               $$(NDK_APP_CXXFLAGS) \
438
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 \
444         $$(PRIVATE_SRC) \
445         -o $$(PRIVATE_OBJ)
446         $$(call cmd-process-deps,$$(PRIVATE_OBJ))
447
448 LOCAL_OBJECTS         += $$(_OBJ)
449 LOCAL_DEPENDENCY_DIRS += $$(dir $$(_OBJ))
450 endef
451
452 # -----------------------------------------------------------------------------
453 # Function  : compile-cpp-source
454 # Arguments : 1: single C++ source file name (relative to LOCAL_PATH)
455 # Returns   : None
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)))
460
461 # -----------------------------------------------------------------------------
462 # Command   : cmd-process-deps
463 # Arguments : 1: object file path
464 # Returns   : None
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
469 #             for more details.
470 # -----------------------------------------------------------------------------
471 cmd-process-deps = $(hide) $(BUILD_SYSTEM)/mkdeps.sh $(1) $(1).d.tmp $(1).d
472
473 # -----------------------------------------------------------------------------
474 # Command   : cmd-install-file
475 # Arguments : 1: source file
476 #             2: destination file
477 # Returns   : None
478 # Usage     : $(call cmd-install-file,<srcfile>,<dstfile>)
479 # Rationale : To be used as a Make build command to copy/install a file to
480 #             a given location.
481 # -----------------------------------------------------------------------------
482 define cmd-install-file
483 @mkdir -p $(dir $2)
484 $(hide) cp -fp $1 $2
485 endef