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 file is included repeatedly from build/core/setup-abi.mk and is used
17# to setup the target toolchain for a given platform/abi combination.
18#
19
20$(call assert-defined,TARGET_PLATFORM TARGET_ARCH TARGET_ARCH_ABI)
21$(call assert-defined,NDK_APPS NDK_APP_STL)
22
23LLVM_VERSION_LIST := 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6
24NDK_64BIT_TOOLCHAIN_LIST := clang3.6 clang3.5 clang3.4 4.9
25
26# Check that we have a toolchain that supports the current ABI.
27# NOTE: If NDK_TOOLCHAIN is defined, we're going to use it.
28#
29ifndef NDK_TOOLCHAIN
30    TARGET_TOOLCHAIN_LIST := $(strip $(sort $(NDK_ABI.$(TARGET_ARCH_ABI).toolchains)))
31
32    # Filter out the Clang toolchain, so that we can keep GCC as the default
33    # toolchain.
34    $(foreach _ver,$(LLVM_VERSION_LIST), \
35        $(eval TARGET_TOOLCHAIN_LIST := \
36            $(filter-out %-clang$(_ver),$(TARGET_TOOLCHAIN_LIST))))
37
38    ifeq (,$(findstring 64,$(TARGET_ARCH_ABI)))
39      # Filter out 4.6 and 4.7 which are deprecated
40      __filtered_toolchain_list := $(filter-out %4.6 %4.7,$(TARGET_TOOLCHAIN_LIST))
41      ifdef __filtered_toolchain_list
42          TARGET_TOOLCHAIN_LIST := $(__filtered_toolchain_list)
43      endif
44    else
45      # Filter out 4.6, 4.7 and 4.8 which don't have good 64-bit support in all supported arch
46      TARGET_TOOLCHAIN_LIST := $(filter-out %4.6 %4.7 %4.8 %4.8l,$(TARGET_TOOLCHAIN_LIST))
47    endif
48
49    ifndef TARGET_TOOLCHAIN_LIST
50        $(call __ndk_info,There is no toolchain that supports the $(TARGET_ARCH_ABI) ABI.)
51        $(call __ndk_info,Please modify the APP_ABI definition in $(NDK_APP_APPLICATION_MK) to use)
52        $(call __ndk_info,a set of the following values: $(NDK_ALL_ABIS))
53        $(call __ndk_error,Aborting)
54    endif
55    # Select the last toolchain from the sorted list.
56    # For now, this is enough to select by default gcc4.8 for 32-bit, and 4.9 for 64-bit, the the
57    # latest llvm if no gcc
58    ifneq (,$(filter-out llvm-%,$(TARGET_TOOLCHAIN_LIST)))
59        TARGET_TOOLCHAIN := $(firstword $(TARGET_TOOLCHAIN_LIST))
60    else
61        TARGET_TOOLCHAIN := $(lastword $(TARGET_TOOLCHAIN_LIST))
62    endif
63    # If NDK_TOOLCHAIN_VERSION is defined, we replace the toolchain version
64    # suffix with it.
65    #
66    ifdef NDK_TOOLCHAIN_VERSION
67        # Replace "clang" with the most recent verion
68        ifeq ($(NDK_TOOLCHAIN_VERSION),clang)
69            override NDK_TOOLCHAIN_VERSION := clang$(lastword $(LLVM_VERSION_LIST))
70        endif
71        __use_ndk_toolchain_version := true
72        ifneq (,$(findstring 64,$(TARGET_ARCH_ABI)))
73            # don't allow NDK_TOOLCHAIN_VERSION to change if it doesn't support 64-bit
74            ifeq (,$(filter $(NDK_64BIT_TOOLCHAIN_LIST),$(NDK_TOOLCHAIN_VERSION)))
75                $(call ndk_log,Specified NDK_TOOLCHAIN_VERSION $(NDK_TOOLCHAIN_VERSION) does not support 64-bit)
76                $(call ndk_log,Using default target toolchain '$(TARGET_TOOLCHAIN)' for '$(TARGET_ARCH_ABI)' ABI)
77                __use_ndk_toolchain_version := false;
78            endif
79        endif
80        ifeq ($(__use_ndk_toolchain_version),true)
81            # We assume the toolchain name uses dashes (-) as separators and doesn't
82            # contain any space. The following is a bit subtle, but essentially
83            # does the following:
84            #
85            #   1/ Use 'subst' to convert dashes into spaces, this generates a list
86            #   2/ Use 'chop' to remove the last element of the list
87            #   3/ Use 'subst' again to convert the spaces back into dashes
88            #
89            # So it TARGET_TOOLCHAIN is 'foo-bar-zoo-xxx', then
90            # TARGET_TOOLCHAIN_BASE will be 'foo-bar-zoo'
91            #
92            TARGET_TOOLCHAIN_BASE := $(subst $(space),-,$(call chop,$(subst -,$(space),$(TARGET_TOOLCHAIN))))
93            # if TARGET_TOOLCHAIN_BASE is llvm, remove clang from NDK_TOOLCHAIN_VERSION
94            VERSION := $(NDK_TOOLCHAIN_VERSION)
95            ifeq ($(TARGET_TOOLCHAIN_BASE),llvm)
96                VERSION := $(subst clang,,$(NDK_TOOLCHAIN_VERSION))
97            endif
98            TARGET_TOOLCHAIN := $(TARGET_TOOLCHAIN_BASE)-$(VERSION)
99            $(call ndk_log,Using target toolchain '$(TARGET_TOOLCHAIN)' for '$(TARGET_ARCH_ABI)' ABI (through NDK_TOOLCHAIN_VERSION))
100        endif
101    else
102        $(call ndk_log,Using target toolchain '$(TARGET_TOOLCHAIN)' for '$(TARGET_ARCH_ABI)' ABI)
103    endif
104else # NDK_TOOLCHAIN is not empty
105    TARGET_TOOLCHAIN_LIST := $(strip $(filter $(NDK_TOOLCHAIN),$(NDK_ABI.$(TARGET_ARCH_ABI).toolchains)))
106    ifndef TARGET_TOOLCHAIN_LIST
107        $(call __ndk_info,The selected toolchain ($(NDK_TOOLCHAIN)) does not support the $(TARGET_ARCH_ABI) ABI.)
108        $(call __ndk_info,Please modify the APP_ABI definition in $(NDK_APP_APPLICATION_MK) to use)
109        $(call __ndk_info,a set of the following values: $(NDK_TOOLCHAIN.$(NDK_TOOLCHAIN).abis))
110        $(call __ndk_info,Or change your NDK_TOOLCHAIN definition.)
111        $(call __ndk_error,Aborting)
112    endif
113    TARGET_TOOLCHAIN := $(NDK_TOOLCHAIN)
114endif # NDK_TOOLCHAIN is not empty
115
116TARGET_ABI := $(TARGET_PLATFORM)-$(TARGET_ARCH_ABI)
117
118# setup sysroot variable.
119# SYSROOT_INC points to a directory that contains all public header
120# files for a given platform, and
121# SYSROOT_LIB points to libraries and object files used for linking
122# the generated target files properly.
123#
124SYSROOT_INC := $(NDK_PLATFORMS_ROOT)/$(TARGET_PLATFORM)/arch-$(TARGET_ARCH)
125SYSROOT_LINK := $(SYSROOT_INC)
126
127TARGET_PREBUILT_SHARED_LIBRARIES :=
128
129# Define default values for TOOLCHAIN_NAME, this can be overriden in
130# the setup file.
131TOOLCHAIN_NAME   := $(TARGET_TOOLCHAIN)
132TOOLCHAIN_VERSION := $(call last,$(subst -,$(space),$(TARGET_TOOLCHAIN)))
133
134# Define the root path where toolchain prebuilts are stored
135TOOLCHAIN_PREBUILT_ROOT := $(call get-toolchain-root,$(TOOLCHAIN_NAME))
136
137# Do the same for TOOLCHAIN_PREFIX. Note that we must chop the version
138# number from the toolchain name, e.g. arm-eabi-4.4.0 -> path/bin/arm-eabi-
139# to do that, we split at dashes, remove the last element, then merge the
140# result. Finally, add the complete path prefix.
141#
142TOOLCHAIN_PREFIX := $(call merge,-,$(call chop,$(call split,-,$(TOOLCHAIN_NAME))))-
143TOOLCHAIN_PREFIX := $(TOOLCHAIN_PREBUILT_ROOT)/bin/$(TOOLCHAIN_PREFIX)
144
145# We expect the gdbserver binary for this toolchain to be located at its root.
146TARGET_GDBSERVER := $(NDK_ROOT)/prebuilt/android-$(TARGET_ARCH)/gdbserver/gdbserver
147
148# compute NDK_APP_DST_DIR as the destination directory for the generated files
149NDK_APP_DST_DIR := $(NDK_APP_LIBS_OUT)/$(TARGET_ARCH_ABI)
150# install armeabi-v7a-hard to lib/armeabi-v7a, unless under testing where env. var. _NDK_TESTING_ALL_
151# is set to one of yes, all, all32, or all64
152ifeq (,$(filter yes all all32 all64,$(_NDK_TESTING_ALL_)))
153ifeq ($(TARGET_ARCH_ABI),armeabi-v7a-hard)
154NDK_APP_DST_DIR := $(NDK_APP_LIBS_OUT)/armeabi-v7a
155endif
156endif
157
158# Default build commands, can be overriden by the toolchain's setup script
159include $(BUILD_SYSTEM)/default-build-commands.mk
160
161# now call the toolchain-specific setup script
162include $(NDK_TOOLCHAIN.$(TARGET_TOOLCHAIN).setup)
163
164clean-installed-binaries::
165
166# Ensure that for debuggable applications, gdbserver will be copied to
167# the proper location
168
169NDK_APP_GDBSERVER := $(NDK_APP_DST_DIR)/gdbserver
170NDK_APP_GDBSETUP := $(NDK_APP_DST_DIR)/gdb.setup
171
172ifeq ($(NDK_APP_DEBUGGABLE),true)
173ifeq ($(TARGET_SONAME_EXTENSION),.so)
174
175installed_modules: $(NDK_APP_GDBSERVER)
176
177$(NDK_APP_GDBSERVER): PRIVATE_ABI     := $(TARGET_ARCH_ABI)
178$(NDK_APP_GDBSERVER): PRIVATE_NAME    := $(TOOLCHAIN_NAME)
179$(NDK_APP_GDBSERVER): PRIVATE_SRC     := $(TARGET_GDBSERVER)
180$(NDK_APP_GDBSERVER): PRIVATE_DST     := $(NDK_APP_GDBSERVER)
181
182$(call generate-file-dir,$(NDK_APP_GDBSERVER))
183
184$(NDK_APP_GDBSERVER): clean-installed-binaries
185	$(call host-echo-build-step,$(PRIVATE_ABI),Gdbserver) "[$(PRIVATE_NAME)] $(call pretty-dir,$(PRIVATE_DST))"
186	$(hide) $(call host-install,$(PRIVATE_SRC),$(PRIVATE_DST))
187endif
188
189# Install gdb.setup for both .so and .bc projects
190ifneq (,$(filter $(TARGET_SONAME_EXTENSION),.so .bc))
191installed_modules: $(NDK_APP_GDBSETUP)
192
193$(NDK_APP_GDBSETUP): PRIVATE_ABI := $(TARGET_ARCH_ABI)
194$(NDK_APP_GDBSETUP): PRIVATE_DST := $(NDK_APP_GDBSETUP)
195$(NDK_APP_GDBSETUP): PRIVATE_SOLIB_PATH := $(TARGET_OUT)
196$(NDK_APP_GDBSETUP): PRIVATE_SRC_DIRS := $(SYSROOT_INC)/usr/include
197
198$(NDK_APP_GDBSETUP):
199	$(call host-echo-build-step,$(PRIVATE_ABI),Gdbsetup) "$(call pretty-dir,$(PRIVATE_DST))"
200	$(hide) $(HOST_ECHO) "set solib-search-path $(call host-path,$(PRIVATE_SOLIB_PATH))" > $(PRIVATE_DST)
201	$(hide) $(HOST_ECHO) "directory $(call host-path,$(call remove-duplicates,$(PRIVATE_SRC_DIRS)))" >> $(PRIVATE_DST)
202
203$(call generate-file-dir,$(NDK_APP_GDBSETUP))
204
205# This prevents parallel execution to clear gdb.setup after it has been written to
206$(NDK_APP_GDBSETUP): clean-installed-binaries
207endif
208endif
209
210# free the dictionary of LOCAL_MODULE definitions
211$(call modules-clear)
212
213$(call ndk-stl-select,$(NDK_APP_STL))
214
215# now parse the Android.mk for the application, this records all
216# module declarations, but does not populate the dependency graph yet.
217include $(NDK_APP_BUILD_SCRIPT)
218
219$(call ndk-stl-add-dependencies,$(NDK_APP_STL))
220
221# recompute all dependencies between modules
222$(call modules-compute-dependencies)
223
224# for debugging purpose
225ifdef NDK_DEBUG_MODULES
226$(call modules-dump-database)
227endif
228
229# now, really build the modules, the second pass allows one to deal
230# with exported values
231$(foreach __pass2_module,$(__ndk_modules),\
232    $(eval LOCAL_MODULE := $(__pass2_module))\
233    $(eval include $(BUILD_SYSTEM)/build-binary.mk)\
234)
235
236# Now compute the closure of all module dependencies.
237#
238# If APP_MODULES is not defined in the Application.mk, we
239# will build all modules that were listed from the top-level Android.mk
240# and the installable imported ones they depend on
241#
242ifeq ($(strip $(NDK_APP_MODULES)),)
243    WANTED_MODULES := $(call modules-get-all-installable,$(modules-get-top-list))
244    ifeq (,$(strip $(WANTED_MODULES)))
245        WANTED_MODULES := $(modules-get-top-list)
246        $(call ndk_log,[$(TARGET_ARCH_ABI)] No installable modules in project - forcing static library build)
247    endif
248else
249    WANTED_MODULES := $(call module-get-all-dependencies,$(NDK_APP_MODULES))
250endif
251
252$(call ndk_log,[$(TARGET_ARCH_ABI)] Modules to build: $(WANTED_MODULES))
253
254WANTED_INSTALLED_MODULES += $(call map,module-get-installed,$(WANTED_MODULES))
255