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 # 23 NDK_ROOT := $(dir $(lastword $(MAKEFILE_LIST))) 24 NDK_ROOT := $(strip $(NDK_ROOT:%build/core/=%)) 25 NDK_ROOT := $(subst \,/,$(NDK_ROOT)) 26 NDK_ROOT := $(NDK_ROOT:%/=%) 27 ifeq ($(NDK_ROOT),) 28 # for the case when we're invoked from the NDK install path 29 NDK_ROOT := . 30 endif 31 ifeq ($(NDK_LOG),1) 32 $(info Android NDK: NDK installation path auto-detected: '$(NDK_ROOT)') 33 endif 34 ifneq ($(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.) 38 endif 39 40 include $(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 57 ifeq ($(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 # 69 host-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 ) 74 else 75 host-dir-parent = $(patsubst %/,%,$(dir $1)) 76 endif 77 78 find-project-dir = $(strip $(call find-project-dir-inner,$(abspath $1),$2)) 79 80 find-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 87 find-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 101 NDK_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 # 112 ifndef 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 120 endif 121 ifndef NDK_PROJECT_PATH 122 NDK_PROJECT_PATH := $(call find-project-dir,.,jni/Android.mk) 123 endif 124 ifndef NDK_PROJECT_PATH 125 NDK_PROJECT_PATH := $(call find-project-dir,.,AndroidManifest.xml) 126 endif 127 ifndef 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) 131 endif 132 133 # Check that there are no spaces in the project path, or bad things will happen 134 ifneq ($(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.) 138 endif 139 140 NDK_APPLICATION_MK := $(strip $(wildcard $(NDK_PROJECT_PATH)/jni/Application.mk)) 141 ifndef NDK_APPLICATION_MK 142 NDK_APPLICATION_MK := $(NDK_ROOT)/build/core/default-application.mk 143 endif 144 145 $(call ndk_log,Found project path: $(NDK_PROJECT_PATH)) 146 147 # Place all generated intermediate files here 148 NDK_APP_OUT := $(strip $(NDK_OUT)) 149 ifndef NDK_APP_OUT 150 NDK_APP_OUT := $(NDK_PROJECT_PATH)/obj 151 endif 152 $(call ndk_log,Ouput path: $(NDK_APP_OUT)) 153 154 # Place all generated library files here. This is rarely changed since aapt expects the default libs/ 155 NDK_APP_LIBS_OUT := $(strip $(NDK_LIBS_OUT)) 156 ifndef NDK_APP_LIBS_OUT 157 NDK_APP_LIBS_OUT := $(NDK_PROJECT_PATH)/libs 158 endif 159 160 # Fake an application named 'local' 161 _app := local 162 _application_mk := $(NDK_APPLICATION_MK) 163 NDK_APPS := $(_app) 164 165 include $(BUILD_SYSTEM)/add-application.mk 166 167 # For cygwin, put generated dependency conversion script here 168 # Do not define this variable for other host platforms 169 # 170 ifeq ($(HOST_OS),cygwin) 171 NDK_DEPENDENCIES_CONVERTER := $(NDK_APP_OUT)/convert-dependencies.sh 172 endif 173 174 # If a goal is DUMP_xxx then we dump a variable xxx instead 175 # of building anything 176 # 177 DUMP_VAR := $(patsubst DUMP_%,%,$(filter DUMP_%,$(MAKECMDGOALS))) 178 MAKECMDGOALS := $(filter-out DUMP_$(DUMP_VAR),$(MAKECMDGOALS)) 179 180 include $(BUILD_SYSTEM)/setup-imports.mk 181 182 ifneq (,$(DUMP_VAR)) 183 184 # We only support a single DUMP_XXX goal at a time for now. 185 ifneq ($(words $(DUMP_VAR)),1) 186 $(call __ndk_error,!!TOO-MANY-DUMP-VARIABLES!!) 187 endif 188 189 $(foreach _app,$(NDK_APPS),\ 190 $(eval include $(BUILD_SYSTEM)/setup-app.mk)\ 191 ) 192 193 DUMP_$(DUMP_VAR): 194 @echo $($(DUMP_VAR)) 195 else 196 # Build it 197 include $(BUILD_SYSTEM)/build-all.mk 198 endif 199