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
16# this script is used to record an application definition in the
17# NDK build system, before performing any build whatsoever.
18#
19# It is included repeatedly from build/core/main.mk and expects a
20# variable named '_application_mk' which points to a given Application.mk
21# file that will be included here. The latter must define a few variables
22# to describe the application to the build system, and the rest of the
23# code here will perform book-keeping and basic checks
24#
25
26$(call assert-defined, _application_mk _app)
27$(call ndk_log,Parsing $(_application_mk))
28
29$(call clear-vars, $(NDK_APP_VARS))
30
31# Check that NDK_DEBUG is properly defined. If it is
32# the only valid states are: undefined, 0, 1, false and true
33#
34# We set APP_DEBUG to <undefined>, 'true' or 'false'.
35#
36APP_DEBUG := $(strip $(NDK_DEBUG))
37ifeq ($(APP_DEBUG),0)
38  APP_DEBUG:= false
39endif
40ifeq ($(APP_DEBUG),1)
41  APP_DEBUG := true
42endif
43ifdef APP_DEBUG
44  ifneq (,$(filter-out true false,$(APP_DEBUG)))
45    $(call __ndk_warning,NDK_DEBUG is defined to the unsupported value '$(NDK_DEBUG)', will be ignored!)
46  endif
47endif
48
49include $(_application_mk)
50
51$(call check-required-vars,$(NDK_APP_VARS_REQUIRED),$(_application_mk))
52
53_map := NDK_APP.$(_app)
54
55# strip the 'lib' prefix in front of APP_MODULES modules
56APP_MODULES := $(call strip-lib-prefix,$(APP_MODULES))
57
58APP_PROJECT_PATH := $(strip $(APP_PROJECT_PATH))
59ifndef APP_PROJECT_PATH
60    APP_PROJECT_PATH := $(NDK_PROJECT_PATH)
61endif
62
63# check whether APP_PLATFORM is defined. If not, look for project.properties in
64# the $(APP_PROJECT_PATH) and extract the value with awk's help. If nothing is here,
65# revert to the default value (i.e. "android-3").
66#
67APP_PLATFORM := $(strip $(APP_PLATFORM))
68ifndef APP_PLATFORM
69    _local_props := $(strip $(wildcard $(APP_PROJECT_PATH)/project.properties))
70    ifndef _local_props
71        # NOTE: project.properties was called default.properties before
72        _local_props := $(strip $(wildcard $(APP_PROJECT_PATH)/default.properties))
73    endif
74    ifdef _local_props
75        APP_PLATFORM := $(strip $(shell $(HOST_AWK) -f $(BUILD_AWK)/extract-platform.awk $(_local_props)))
76        $(call ndk_log,  Found APP_PLATFORM=$(APP_PLATFORM) in $(_local_props))
77    else
78        APP_PLATFORM := android-3
79        $(call ndk_log,  Defaulted to APP_PLATFORM=$(APP_PLATFORM))
80    endif
81endif
82
83# SPECIAL CASES:
84# 1) android-6 and android-7 are the same thing as android-5
85# 2) android-10 .. 13 is the same thing as android-9
86ifneq (,$(filter android-6 android-7,$(APP_PLATFORM)))
87    APP_PLATFORM := android-5
88    $(call ndk_log,  Adjusting APP_PLATFORM to $(APP_PLATFORM))
89endif
90ifneq (,$(filter android-10 android-11 android-12 android-13,$(APP_PLATFORM)))
91    APP_PLATFORM := android-9
92    $(call ndk_log,  Adjusting APP_PLATFORM to $(APP_PLATFORM))
93endif
94
95# Check that the value of APP_PLATFORM corresponds to a known platform
96# If not, we're going to use the max supported platform value.
97#
98_bad_platform := $(strip $(filter-out $(NDK_ALL_PLATFORMS),$(APP_PLATFORM)))
99ifdef _bad_platform
100    $(call ndk_log,Application $(_app) targets unknown platform '$(_bad_platform)')
101    APP_PLATFORM := android-$(NDK_MAX_PLATFORM_LEVEL)
102    $(call ndk_log,Switching to $(APP_PLATFORM))
103endif
104
105# Check that the value of APP_ABI corresponds to known ABIs
106# 'all' is a special case that means 'all supported ABIs'
107#
108# It will be handled in setup-app.mk. We can't hope to change
109# the value of APP_ABI is the user enforces it on the command-line
110# with a call like:  ndk-build APP_ABI=all
111#
112# Because GNU Make makes the APP_ABI variable read-only (any assignments
113# to it will be ignored)
114#
115APP_ABI := $(strip $(APP_ABI))
116ifndef APP_ABI
117    # Default ABI is 'armeabi'
118    APP_ABI := armeabi
119endif
120ifneq ($(APP_ABI),all)
121    _bad_abis := $(strip $(filter-out $(NDK_ALL_ABIS),$(APP_ABIS)))
122    ifdef _bad_abis
123        $(call __ndk_info,Application $(_app) targets unknown ABI '$(_bad_abis)')
124        $(call __ndk_info,Please fix the APP_ABI definition in $(_application_mk))
125        $(call __ndk_info,to use a set of the following values: $(NDK_ALL_ABIS))
126        $(call __ndk_error,Aborting)
127    endif
128endif
129
130# If APP_BUILD_SCRIPT is defined, check that the file exists.
131# If undefined, look in $(APP_PROJECT_PATH)/jni/Android.mk
132#
133APP_BUILD_SCRIPT := $(strip $(APP_BUILD_SCRIPT))
134ifdef APP_BUILD_SCRIPT
135    _build_script := $(strip $(wildcard $(APP_BUILD_SCRIPT)))
136    ifndef _build_script
137        $(call __ndk_info,Your APP_BUILD_SCRIPT points to an unknown file: $(APP_BUILD_SCRIPT))
138        $(call __ndk_error,Aborting...)
139    endif
140    APP_BUILD_SCRIPT := $(_build_script)
141    $(call ndk_log,  Using build script $(APP_BUILD_SCRIPT))
142else
143    _build_script := $(strip $(wildcard $(APP_PROJECT_PATH)/jni/Android.mk))
144    ifndef _build_script
145        $(call __ndk_info,There is no Android.mk under $(APP_PROJECT_PATH)/jni)
146        $(call __ndk_info,If this is intentional, please define APP_BUILD_SCRIPT to point)
147        $(call __ndk_info,to a valid NDK build script.)
148        $(call __ndk_error,Aborting...)
149    endif
150    APP_BUILD_SCRIPT := $(_build_script)
151    $(call ndk_log,  Defaulted to APP_BUILD_SCRIPT=$(APP_BUILD_SCRIPT))
152endif
153
154# Determine whether the application should be debuggable.
155# - If APP_DEBUG is set to 'true', then it always should.
156# - If APP_DEBUG is set to 'false', then it never should
157# - Otherwise, extract the android:debuggable attribute from the manifest.
158#
159ifdef APP_DEBUG
160  APP_DEBUGGABLE := $(APP_DEBUG)
161  ifdef NDK_LOG
162    ifeq ($(APP_DEBUG),true)
163      $(call ndk_log,Application '$(_app)' forced debuggable through NDK_DEBUG)
164    else
165      $(call ndk_log,Application '$(_app)' forced *not* debuggable through NDK_DEBUG)
166    endif
167  endif
168else
169  # NOTE: To make unit-testing simpler, handle the case where there is no manifest.
170  APP_DEBUGGABLE := false
171  APP_MANIFEST := $(strip $(wildcard $(APP_PROJECT_PATH)/AndroidManifest.xml))
172  ifdef APP_MANIFEST
173    APP_DEBUGGABLE := $(shell $(HOST_AWK) -f $(BUILD_AWK)/extract-debuggable.awk $(APP_MANIFEST))
174  endif
175  ifdef NDK_LOG
176    ifeq ($(APP_DEBUGGABLE),true)
177      $(call ndk_log,Application '$(_app)' *is* debuggable)
178    else
179      $(call ndk_log,Application '$(_app)' is not debuggable)
180    endif
181  endif
182endif
183
184# LOCAL_BUILD_MODE will be either release or debug
185#
186# If APP_OPTIM is defined in the Application.mk, just use this.
187#
188# Otherwise, set to 'debug' if android:debuggable is set to TRUE,
189# and to 'release' if not.
190#
191ifneq ($(APP_OPTIM),)
192    # check that APP_OPTIM, if defined, is either 'release' or 'debug'
193    $(if $(filter-out release debug,$(APP_OPTIM)),\
194        $(call __ndk_info, The APP_OPTIM defined in $(_application_mk) must only be 'release' or 'debug')\
195        $(call __ndk_error,Aborting)\
196    )
197    $(call ndk_log,Selecting optimization mode through Application.mk: $(APP_OPTIM))
198else
199    ifeq ($(APP_DEBUGGABLE),true)
200        $(call ndk_log,Selecting debug optimization mode (app is debuggable))
201        APP_OPTIM := debug
202    else
203        $(call ndk_log,Selecting release optimization mode (app is not debuggable))
204        APP_OPTIM := release
205    endif
206endif
207
208# set release/debug build flags. We always use the -g flag because
209# we generate symbol versions of the binaries that are later stripped
210# when they are copied to the final project's libs/<abi> directory.
211#
212ifeq ($(APP_OPTIM),debug)
213  APP_CFLAGS := -O0 -g $(APP_CFLAGS)
214else
215  APP_CFLAGS := -O2 -DNDEBUG -g $(APP_CFLAGS)
216endif
217
218# Check that APP_STL is defined. If not, use the default value (system)
219# otherwise, check that the name is correct.
220APP_STL := $(strip $(APP_STL))
221ifndef APP_STL
222    APP_STL := system
223else
224    $(call ndk-stl-check,$(APP_STL))
225endif
226
227
228
229$(if $(call get,$(_map),defined),\
230  $(call __ndk_info,Weird, the application $(_app) is already defined by $(call get,$(_map),defined))\
231  $(call __ndk_error,Aborting)\
232)
233
234$(call set,$(_map),defined,$(_application_mk))
235
236# Record all app-specific variable definitions
237$(foreach __name,$(NDK_APP_VARS),\
238  $(call set,$(_map),$(__name),$($(__name)))\
239)
240
241# Record the Application.mk for debugging
242$(call set,$(_map),Application.mk,$(_application_mk))
243
244NDK_ALL_APPS += $(_app)
245