1# Copyright (C) 2010 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 file is designed to be called from the 'ndk-build' script
17# or similar wrapper tool.
18#
19
20# Detect the NDK installation path by processing this Makefile's location.
21# This assumes we are located under $NDK_ROOT/build/core/main.mk
22#
23NDK_ROOT := $(dir $(lastword $(MAKEFILE_LIST)))
24NDK_ROOT := $(strip $(NDK_ROOT:%build/core/=%))
25NDK_ROOT := $(subst \,/,$(NDK_ROOT))
26NDK_ROOT := $(NDK_ROOT:%/=%)
27ifeq ($(NDK_ROOT),)
28    # for the case when we're invoked from the NDK install path
29    NDK_ROOT := .
30endif
31ifdef NDK_LOG
32    $(info Android NDK: NDK installation path auto-detected: '$(NDK_ROOT)')
33endif
34ifneq ($(words $(NDK_ROOT)),1)
35    $(info Android NDK: You NDK installation path contains spaces.)
36    $(info Android NDK: Please re-install to a different location to fix the issue !)
37    $(error Aborting.)
38endif
39
40include $(NDK_ROOT)/build/core/init.mk
41
42# ====================================================================
43#
44# If NDK_PROJECT_PATH is not defined, find the application's project
45# path by looking at the manifest file in the current directory or
46# any of its parents. If none is found, try again with 'jni/Android.mk'
47#
48# Note that we first look at the current directory to avoid using
49# absolute NDK_PROJECT_PATH values. This reduces the length of all
50# source, object and binary paths that are passed to build commands.
51#
52# It turns out that some people use ndk-build to generate static
53# libraries without a full Android project tree.
54#
55# ====================================================================
56
57ifeq ($(HOST_OS),windows)
58# On Windows, defining host-dir-parent is a bit more tricky because the
59# GNU Make $(dir ...) function doesn't return an empty string when it
60# reaches the top of the directory tree, and we want to enforce this to
61# avoid infinite loops.
62#
63#   $(dir C:)     -> C:       (empty expected)
64#   $(dir C:/)    -> C:/      (empty expected)
65#   $(dir C:\)    -> C:\      (empty expected)
66#   $(dir C:/foo) -> C:/      (correct)
67#   $(dir C:\foo) -> C:\      (correct)
68#
69host-dir-parent = $(strip \
70    $(eval __host_dir_node := $(patsubst %/,%,$(subst \,/,$1)))\
71    $(eval __host_dir_parent := $(dir $(__host_dir_node)))\
72    $(filter-out $1,$(__host_dir_parent))\
73    )
74else
75host-dir-parent = $(patsubst %/,%,$(dir $1))
76endif
77
78find-project-dir = $(strip $(call find-project-dir-inner,$(abspath $1),$2))
79
80find-project-dir-inner = \
81    $(eval __found_project_path := )\
82    $(eval __find_project_path := $1)\
83    $(eval __find_project_file := $2)\
84    $(call find-project-dir-inner-2)\
85    $(__found_project_path)
86
87find-project-dir-inner-2 = \
88    $(call ndk_log,Looking for $(__find_project_file) in $(__find_project_path))\
89    $(eval __find_project_manifest := $(strip $(wildcard $(__find_project_path)/$(__find_project_file))))\
90    $(if $(__find_project_manifest),\
91        $(call ndk_log,    Found it !)\
92        $(eval __found_project_path := $(__find_project_path))\
93        ,\
94        $(eval __find_project_parent := $(call host-dir-parent,$(__find_project_path)))\
95        $(if $(__find_project_parent),\
96            $(eval __find_project_path := $(__find_project_parent))\
97            $(call find-project-dir-inner-2)\
98        )\
99    )
100
101NDK_PROJECT_PATH := $(strip $(NDK_PROJECT_PATH))
102
103# To keep paths as short as possible during the build, we first look if the
104# current directory is the top of our project path. If this is the case, we
105# will define NDK_PROJECT_PATH to simply '.'
106#
107# Otherwise, we will use find-project-dir which will first get the absolute
108# path of the current directory the climb back the hierarchy until we find
109# something. The result will always be a much longer definition for
110# NDK_PROJECT_PATH
111#
112ifndef NDK_PROJECT_PATH
113    ifneq (,$(strip $(wildcard AndroidManifest.xml)))
114        NDK_PROJECT_PATH := .
115    else
116        ifneq (,$(strip $(wildcard jni/Android.mk)))
117            NDK_PROJECT_PATH := .
118        endif
119    endif
120endif
121ifndef NDK_PROJECT_PATH
122    NDK_PROJECT_PATH := $(call find-project-dir,.,jni/Android.mk)
123endif
124ifndef NDK_PROJECT_PATH
125    NDK_PROJECT_PATH := $(call find-project-dir,.,AndroidManifest.xml)
126endif
127ifndef NDK_PROJECT_PATH
128    $(call __ndk_info,Could not find application project directory !)
129    $(call __ndk_info,Please define the NDK_PROJECT_PATH variable to point to it.)
130    $(call __ndk_error,Aborting)
131endif
132
133# Check that there are no spaces in the project path, or bad things will happen
134ifneq ($(words $(NDK_PROJECT_PATH)),1)
135    $(call __ndk_info,Your Android application project path contains spaces: '$(NDK_PROJECT_PATH)')
136    $(call __ndk_info,The Android NDK build cannot work here. Please move your project to a different location.)
137    $(call __ndk_error,Aborting.)
138endif
139
140NDK_APPLICATION_MK := $(strip $(wildcard $(NDK_PROJECT_PATH)/jni/Application.mk))
141ifndef NDK_APPLICATION_MK
142    NDK_APPLICATION_MK := $(NDK_ROOT)/build/core/default-application.mk
143endif
144
145$(call ndk_log,Found project path: $(NDK_PROJECT_PATH))
146
147# Place all generated files here
148NDK_APP_OUT := $(strip $(NDK_OUT))
149ifndef NDK_APP_OUT
150  NDK_APP_OUT := $(NDK_PROJECT_PATH)/obj
151endif
152$(call ndk_log,Ouput path: $(NDK_APP_OUT))
153
154# Fake an application named 'local'
155_app            := local
156_application_mk := $(NDK_APPLICATION_MK)
157NDK_APPS        := $(_app)
158
159# For cygwin, put generated dependency conversion script here
160# Do not define this variable for other host platforms
161#
162ifeq ($(HOST_OS),cygwin)
163NDK_DEPENDENCIES_CONVERTER := $(NDK_APP_OUT)/convert-dependencies.sh
164endif
165
166include $(BUILD_SYSTEM)/add-application.mk
167
168# If a goal is DUMP_xxx then we dump a variable xxx instead
169# of building anything
170#
171DUMP_VAR     := $(patsubst DUMP_%,%,$(filter DUMP_%,$(MAKECMDGOALS)))
172MAKECMDGOALS := $(filter-out DUMP_$(DUMP_VAR),$(MAKECMDGOALS))
173
174include $(BUILD_SYSTEM)/setup-imports.mk
175
176ifneq (,$(DUMP_VAR))
177
178# We only support a single DUMP_XXX goal at a time for now.
179ifneq ($(words $(DUMP_VAR)),1)
180    $(call __ndk_error,!!TOO-MANY-DUMP-VARIABLES!!)
181endif
182
183$(foreach _app,$(NDK_APPS),\
184  $(eval include $(BUILD_SYSTEM)/setup-app.mk)\
185)
186
187DUMP_$(DUMP_VAR):
188	@echo $($(DUMP_VAR))
189else
190    # Build it
191    include $(BUILD_SYSTEM)/build-all.mk
192endif
193