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 $(BUILD_SYSTEM)/../gmsl/gmsl 20 21# This is the Android NDK version number as a list of three items: 22# major, minor, revision 23# 24ndk_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 35ifdef NDK_NO_WARNINGS 36__ndk_warning := 37endif 38ifdef NDK_NO_ERRORS 39__ndk_error := 40endif 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 45ifdef NDK_TRACE 46__ndk_tr1 = $(warning $0('$1')) 47__ndk_tr2 = $(warning $0('$1','$2')) 48__ndk_tr3 = $(warning $0('$1','$2','$3')) 49else 50__ndk_tr1 := 51__ndk_tr2 := 52__ndk_tr3 := 53endif 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# ----------------------------------------------------------------------------- 61ifdef NDK_LOG 62ndk_log = $(info $(__ndk_name): $1) 63else 64ndk_log := 65endif 66 67 68# ----------------------------------------------------------------------------- 69# Macro : empty 70# Returns : an empty macro 71# Usage : $(empty) 72# ----------------------------------------------------------------------------- 73empty := 74 75# ----------------------------------------------------------------------------- 76# Macro : space 77# Returns : a single space 78# Usage : $(space) 79# ----------------------------------------------------------------------------- 80space := $(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# ----------------------------------------------------------------------------- 88last2 = $(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# ----------------------------------------------------------------------------- 96last3 = $(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# ----------------------------------------------------------------------------- 103this-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# ----------------------------------------------------------------------------- 110local-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# ----------------------------------------------------------------------------- 120assert-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# ----------------------------------------------------------------------------- 134clear-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# ----------------------------------------------------------------------------- 145check-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# ----------------------------------------------------------------------------- 159modules-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# ----------------------------------------------------------------------------- 170modules-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# ----------------------------------------------------------------------------- 187check-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# ----------------------------------------------------------------------------- 200check-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# ----------------------------------------------------------------------------- 215strip-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# ----------------------------------------------------------------------------- 226check-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# ----------------------------------------------------------------------------- 235my-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# ----------------------------------------------------------------------------- 243all-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# ----------------------------------------------------------------------------- 251all-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 261NDK_APP_VARS_REQUIRED := APP_MODULES APP_PROJECT_PATH 262 263# the list of variables that *may* be defined in Application.mk files 264NDK_APP_VARS_OPTIONAL := APP_OPTIM APP_CPPFLAGS APP_CFLAGS APP_CXXFLAGS \ 265 APP_PLATFORM APP_BUILD_SCRIPT 266 267# the list of all variables that may appear in an Application.mk file 268NDK_APP_VARS := $(NDK_APP_VARS_REQUIRED) $(NDK_APP_VARS_OPTIONAL) 269 270# ============================================================================= 271# 272# Android.mk support 273# 274# ============================================================================= 275 276 277# ============================================================================= 278# 279# Generated files support 280# 281# ============================================================================= 282 283 284# ----------------------------------------------------------------------------- 285# Function : host-static-library-path 286# Arguments : 1: library module name (e.g. 'foo') 287# Returns : location of generated host library name (e.g. '..../libfoo.a) 288# Usage : $(call host-static-library-path,<modulename>) 289# ----------------------------------------------------------------------------- 290host-static-library-path = $(HOST_OUT)/lib$1.a 291 292# ----------------------------------------------------------------------------- 293# Function : host-executable-path 294# Arguments : 1: executable module name (e.g. 'foo') 295# Returns : location of generated host executable name (e.g. '..../foo) 296# Usage : $(call host-executable-path,<modulename>) 297# ----------------------------------------------------------------------------- 298host-executable-path = $(HOST_OUT)/$1$(HOST_EXE) 299 300# ----------------------------------------------------------------------------- 301# Function : static-library-path 302# Arguments : 1: library module name (e.g. 'foo') 303# Returns : location of generated static library name (e.g. '..../libfoo.a) 304# Usage : $(call static-library-path,<modulename>) 305# ----------------------------------------------------------------------------- 306static-library-path = $(TARGET_OUT)/lib$1.a 307 308# ----------------------------------------------------------------------------- 309# Function : shared-library-path 310# Arguments : 1: library module name (e.g. 'foo') 311# Returns : location of generated shared library name (e.g. '..../libfoo.so) 312# Usage : $(call shared-library-path,<modulename>) 313# ----------------------------------------------------------------------------- 314shared-library-path = $(TARGET_OUT)/lib$1.so 315 316# ----------------------------------------------------------------------------- 317# Function : executable-path 318# Arguments : 1: executable module name (e.g. 'foo') 319# Returns : location of generated exectuable name (e.g. '..../foo) 320# Usage : $(call executable-path,<modulename>) 321# ----------------------------------------------------------------------------- 322executable-path = $(TARGET_OUT)/$1 323 324# ============================================================================= 325# 326# Build commands support 327# 328# ============================================================================= 329 330# ----------------------------------------------------------------------------- 331# Macro : hide 332# Returns : nothing 333# Usage : $(hide)<make commands> 334# Rationale: To be used as a prefix for Make build commands to hide them 335# by default during the build. To show them, set V=1 in your 336# environment or command-line. 337# 338# For example: 339# 340# foo.o: foo.c 341# -->|$(hide) <build-commands> 342# 343# Where '-->|' stands for a single tab character. 344# 345# ----------------------------------------------------------------------------- 346ifeq ($(V),1) 347hide = $(empty) 348else 349hide = @ 350endif 351 352# ----------------------------------------------------------------------------- 353# Template : ev-compile-c-source 354# Arguments : 1: single C source file name (relative to LOCAL_PATH) 355# 2: target object file (without path) 356# Returns : None 357# Usage : $(eval $(call ev-compile-c-source,<srcfile>,<objfile>) 358# Rationale : Internal template evaluated by compile-c-source and 359# compile-s-source 360# ----------------------------------------------------------------------------- 361define ev-compile-c-source 362_SRC:=$$(LOCAL_PATH)/$(1) 363_OBJ:=$$(LOCAL_OBJS_DIR)/$(2) 364 365$$(_OBJ): PRIVATE_SRC := $$(_SRC) 366$$(_OBJ): PRIVATE_OBJ := $$(_OBJ) 367$$(_OBJ): PRIVATE_MODULE := $$(LOCAL_MODULE) 368$$(_OBJ): PRIVATE_ARM_MODE := $$(LOCAL_ARM_MODE) 369$$(_OBJ): PRIVATE_ARM_TEXT := $$(LOCAL_ARM_TEXT) 370$$(_OBJ): PRIVATE_CC := $$($$(my)CC) 371$$(_OBJ): PRIVATE_CFLAGS := $$($$(my)CFLAGS) \ 372 $$($$(my)$(LOCAL_ARM_MODE)_$(LOCAL_BUILD_MODE)_CFLAGS) \ 373 $$(LOCAL_C_INCLUDES:%=-I%) \ 374 -I$$(LOCAL_PATH) \ 375 $$(LOCAL_CFLAGS) \ 376 $$(NDK_APP_CFLAGS) 377 378$$(_OBJ): $$(_SRC) $$(LOCAL_MAKEFILE) $$(NDK_APP_APPLICATION_MK) 379 @mkdir -p $$(dir $$(PRIVATE_OBJ)) 380 @echo "Compile $$(PRIVATE_ARM_TEXT) : $$(PRIVATE_MODULE) <= $$(PRIVATE_SRC)" 381 $(hide) $$(PRIVATE_CC) $$(PRIVATE_CFLAGS) -c \ 382 -MMD -MP -MF $$(PRIVATE_OBJ).d.tmp \ 383 $$(PRIVATE_SRC) \ 384 -o $$(PRIVATE_OBJ) 385 $$(call cmd-process-deps,$$(PRIVATE_OBJ)) 386 387LOCAL_OBJECTS += $$(_OBJ) 388LOCAL_DEPENDENCY_DIRS += $$(dir $$(_OBJ)) 389endef 390 391# ----------------------------------------------------------------------------- 392# Function : compile-c-source 393# Arguments : 1: single C source file name (relative to LOCAL_PATH) 394# Returns : None 395# Usage : $(call compile-c-source,<srcfile>) 396# Rationale : Setup everything required to build a single C source file 397# ----------------------------------------------------------------------------- 398compile-c-source = $(eval $(call ev-compile-c-source,$1,$(1:%.c=%.o))) 399 400# ----------------------------------------------------------------------------- 401# Function : compile-s-source 402# Arguments : 1: single Assembly source file name (relative to LOCAL_PATH) 403# Returns : None 404# Usage : $(call compile-s-source,<srcfile>) 405# Rationale : Setup everything required to build a single Assembly source file 406# ----------------------------------------------------------------------------- 407compile-s-source = $(eval $(call ev-compile-c-source,$1,$(1:%.S=%.o))) 408 409 410# ----------------------------------------------------------------------------- 411# Template : ev-compile-cpp-source 412# Arguments : 1: single C++ source file name (relative to LOCAL_PATH) 413# 2: target object file (without path) 414# Returns : None 415# Usage : $(eval $(call ev-compile-cpp-source,<srcfile>,<objfile>) 416# Rationale : Internal template evaluated by compile-cpp-source 417# ----------------------------------------------------------------------------- 418 419define ev-compile-cpp-source 420_SRC:=$$(LOCAL_PATH)/$(1) 421_OBJ:=$$(LOCAL_OBJS_DIR)/$(2) 422 423$$(_OBJ): PRIVATE_SRC := $$(_SRC) 424$$(_OBJ): PRIVATE_OBJ := $$(_OBJ) 425$$(_OBJ): PRIVATE_MODULE := $$(LOCAL_MODULE) 426$$(_OBJ): PRIVATE_ARM_MODE := $$(LOCAL_ARM_MODE) 427$$(_OBJ): PRIVATE_ARM_TEXT := $$(LOCAL_ARM_TEXT) 428$$(_OBJ): PRIVATE_CXX := $$($$(my)CXX) 429$$(_OBJ): PRIVATE_CXXFLAGS := $$($$(my)CXXFLAGS) \ 430 $$($$(my)$(LOCAL_ARM_MODE)_$(LOCAL_BUILD_MODE)_CFLAGS) \ 431 $$(LOCAL_C_INCLUDES:%=-I%) \ 432 -I$$(LOCAL_PATH) \ 433 $$(LOCAL_CFLAGS) \ 434 $$(LOCAL_CPPFLAGS) \ 435 $$(LOCAL_CXXFLAGS) \ 436 $$(NDK_APP_CFLAGS) \ 437 $$(NDK_APP_CPPFLAGS) \ 438 $$(NDK_APP_CXXFLAGS) \ 439 440$$(_OBJ): $$(_SRC) $$(LOCAL_MAKEFILE) $$(NDK_APP_APPLICATION_MK) 441 @mkdir -p $$(dir $$(PRIVATE_OBJ)) 442 @echo "Compile++ $$(PRIVATE_ARM_TEXT): $$(PRIVATE_MODULE) <= $$(PRIVATE_SRC)" 443 $(hide) $$(PRIVATE_CXX) $$(PRIVATE_CXXFLAGS) -c \ 444 -MMD -MP -MF $$(PRIVATE_OBJ).d.tmp \ 445 $$(PRIVATE_SRC) \ 446 -o $$(PRIVATE_OBJ) 447 $$(call cmd-process-deps,$$(PRIVATE_OBJ)) 448 449LOCAL_OBJECTS += $$(_OBJ) 450LOCAL_DEPENDENCY_DIRS += $$(dir $$(_OBJ)) 451endef 452 453# ----------------------------------------------------------------------------- 454# Function : compile-cpp-source 455# Arguments : 1: single C++ source file name (relative to LOCAL_PATH) 456# Returns : None 457# Usage : $(call compile-c-source,<srcfile>) 458# Rationale : Setup everything required to build a single C++ source file 459# ----------------------------------------------------------------------------- 460compile-cpp-source = $(eval $(call ev-compile-cpp-source,$1,$(1:%$(LOCAL_CPP_EXTENSION)=%.o))) 461 462# ----------------------------------------------------------------------------- 463# Command : cmd-process-deps 464# Arguments : 1: object file path 465# Returns : None 466# Usage : $(call cmd-process-deps,<objectfile>) 467# Rationale : To be used as a Make build command to process the dependencies 468# generated by the compiler (in <obj>.d.tmp) into ones suited 469# for our build system. See the comments in build/core/mkdeps.sh 470# for more details. 471# ----------------------------------------------------------------------------- 472cmd-process-deps = $(hide) $(BUILD_SYSTEM)/mkdeps.sh $(1) $(1).d.tmp $(1).d 473 474# ----------------------------------------------------------------------------- 475# Command : cmd-install-file 476# Arguments : 1: source file 477# 2: destination file 478# Returns : None 479# Usage : $(call cmd-install-file,<srcfile>,<dstfile>) 480# Rationale : To be used as a Make build command to copy/install a file to 481# a given location. 482# ----------------------------------------------------------------------------- 483define cmd-install-file 484@mkdir -p $(dir $2) 485$(hide) cp -fp $1 $2 486endef 487