Home | History | Annotate | Download | only in core
      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 
     23 LLVM_VERSION_LIST := 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5
     24 NDK_64BIT_TOOLCHAIN_LIST := 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 #
     29 ifndef 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
    104 else # 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)
    114 endif # NDK_TOOLCHAIN is not empty
    115 
    116 TARGET_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 #
    124 SYSROOT_INC := $(NDK_PLATFORMS_ROOT)/$(TARGET_PLATFORM)/arch-$(TARGET_ARCH)
    125 SYSROOT_LINK := $(SYSROOT_INC)
    126 
    127 TARGET_PREBUILT_SHARED_LIBRARIES :=
    128 
    129 # Define default values for TOOLCHAIN_NAME, this can be overriden in
    130 # the setup file.
    131 TOOLCHAIN_NAME   := $(TARGET_TOOLCHAIN)
    132 TOOLCHAIN_VERSION := $(call last,$(subst -,$(space),$(TARGET_TOOLCHAIN)))
    133 
    134 # Define the root path of the toolchain in the NDK tree.
    135 TOOLCHAIN_ROOT   := $(NDK_ROOT)/toolchains/$(TOOLCHAIN_NAME)
    136 
    137 # Define the root path where toolchain prebuilts are stored
    138 TOOLCHAIN_PREBUILT_ROOT := $(call host-prebuilt-tag,$(TOOLCHAIN_ROOT))
    139 
    140 # Do the same for TOOLCHAIN_PREFIX. Note that we must chop the version
    141 # number from the toolchain name, e.g. arm-eabi-4.4.0 -> path/bin/arm-eabi-
    142 # to do that, we split at dashes, remove the last element, then merge the
    143 # result. Finally, add the complete path prefix.
    144 #
    145 TOOLCHAIN_PREFIX := $(call merge,-,$(call chop,$(call split,-,$(TOOLCHAIN_NAME))))-
    146 TOOLCHAIN_PREFIX := $(TOOLCHAIN_PREBUILT_ROOT)/bin/$(TOOLCHAIN_PREFIX)
    147 
    148 # We expect the gdbserver binary for this toolchain to be located at its root.
    149 TARGET_GDBSERVER := $(NDK_ROOT)/prebuilt/android-$(TARGET_ARCH)/gdbserver/gdbserver
    150 
    151 # compute NDK_APP_DST_DIR as the destination directory for the generated files
    152 NDK_APP_DST_DIR := $(NDK_APP_LIBS_OUT)/$(TARGET_ARCH_ABI)
    153 # install armeabi-v7a-hard to lib/armeabi-v7a, unless under testing where env. var. _NDK_TESTING_ALL_
    154 # is set to one of yes, all, all32, or all64
    155 ifeq (,$(filter yes all all32 all64,$(_NDK_TESTING_ALL_)))
    156 ifeq ($(TARGET_ARCH_ABI),armeabi-v7a-hard)
    157 NDK_APP_DST_DIR := $(NDK_APP_LIBS_OUT)/armeabi-v7a
    158 endif
    159 endif
    160 
    161 # Default build commands, can be overriden by the toolchain's setup script
    162 include $(BUILD_SYSTEM)/default-build-commands.mk
    163 
    164 # now call the toolchain-specific setup script
    165 include $(NDK_TOOLCHAIN.$(TARGET_TOOLCHAIN).setup)
    166 
    167 clean-installed-binaries::
    168 
    169 # Ensure that for debuggable applications, gdbserver will be copied to
    170 # the proper location
    171 
    172 NDK_APP_GDBSERVER := $(NDK_APP_DST_DIR)/gdbserver
    173 NDK_APP_GDBSETUP := $(NDK_APP_DST_DIR)/gdb.setup
    174 
    175 ifeq ($(NDK_APP_DEBUGGABLE),true)
    176 ifeq ($(TARGET_SONAME_EXTENSION),.so)
    177 
    178 installed_modules: $(NDK_APP_GDBSERVER)
    179 
    180 $(NDK_APP_GDBSERVER): PRIVATE_ABI     := $(TARGET_ARCH_ABI)
    181 $(NDK_APP_GDBSERVER): PRIVATE_NAME    := $(TOOLCHAIN_NAME)
    182 $(NDK_APP_GDBSERVER): PRIVATE_SRC     := $(TARGET_GDBSERVER)
    183 $(NDK_APP_GDBSERVER): PRIVATE_DST     := $(NDK_APP_GDBSERVER)
    184 
    185 $(call generate-file-dir,$(NDK_APP_GDBSERVER))
    186 
    187 $(NDK_APP_GDBSERVER): clean-installed-binaries
    188 	$(call host-echo-build-step,$(PRIVATE_ABI),Gdbserver) "[$(PRIVATE_NAME)] $(call pretty-dir,$(PRIVATE_DST))"
    189 	$(hide) $(call host-install,$(PRIVATE_SRC),$(PRIVATE_DST))
    190 endif
    191 
    192 # Install gdb.setup for both .so and .bc projects
    193 ifneq (,$(filter $(TARGET_SONAME_EXTENSION),.so .bc))
    194 installed_modules: $(NDK_APP_GDBSETUP)
    195 
    196 $(NDK_APP_GDBSETUP): PRIVATE_ABI := $(TARGET_ARCH_ABI)
    197 $(NDK_APP_GDBSETUP): PRIVATE_DST := $(NDK_APP_GDBSETUP)
    198 $(NDK_APP_GDBSETUP): PRIVATE_SOLIB_PATH := $(TARGET_OUT)
    199 $(NDK_APP_GDBSETUP): PRIVATE_SRC_DIRS := $(SYSROOT_INC)/usr/include
    200 
    201 $(NDK_APP_GDBSETUP):
    202 	$(call host-echo-build-step,$(PRIVATE_ABI),Gdbsetup) "$(call pretty-dir,$(PRIVATE_DST))"
    203 	$(hide) $(HOST_ECHO) "set solib-search-path $(call host-path,$(PRIVATE_SOLIB_PATH))" > $(PRIVATE_DST)
    204 	$(hide) $(HOST_ECHO) "directory $(call host-path,$(call remove-duplicates,$(PRIVATE_SRC_DIRS)))" >> $(PRIVATE_DST)
    205 
    206 $(call generate-file-dir,$(NDK_APP_GDBSETUP))
    207 
    208 # This prevents parallel execution to clear gdb.setup after it has been written to
    209 $(NDK_APP_GDBSETUP): clean-installed-binaries
    210 endif
    211 endif
    212 
    213 # free the dictionary of LOCAL_MODULE definitions
    214 $(call modules-clear)
    215 
    216 $(call ndk-stl-select,$(NDK_APP_STL))
    217 
    218 # now parse the Android.mk for the application, this records all
    219 # module declarations, but does not populate the dependency graph yet.
    220 include $(NDK_APP_BUILD_SCRIPT)
    221 
    222 $(call ndk-stl-add-dependencies,$(NDK_APP_STL))
    223 
    224 # recompute all dependencies between modules
    225 $(call modules-compute-dependencies)
    226 
    227 # for debugging purpose
    228 ifdef NDK_DEBUG_MODULES
    229 $(call modules-dump-database)
    230 endif
    231 
    232 # now, really build the modules, the second pass allows one to deal
    233 # with exported values
    234 $(foreach __pass2_module,$(__ndk_modules),\
    235     $(eval LOCAL_MODULE := $(__pass2_module))\
    236     $(eval include $(BUILD_SYSTEM)/build-binary.mk)\
    237 )
    238 
    239 # Now compute the closure of all module dependencies.
    240 #
    241 # If APP_MODULES is not defined in the Application.mk, we
    242 # will build all modules that were listed from the top-level Android.mk
    243 # and the installable imported ones they depend on
    244 #
    245 ifeq ($(strip $(NDK_APP_MODULES)),)
    246     WANTED_MODULES := $(call modules-get-all-installable,$(modules-get-top-list))
    247     ifeq (,$(strip $(WANTED_MODULES)))
    248         WANTED_MODULES := $(modules-get-top-list)
    249         $(call ndk_log,[$(TARGET_ARCH_ABI)] No installable modules in project - forcing static library build)
    250     endif
    251 else
    252     WANTED_MODULES := $(call module-get-all-dependencies,$(NDK_APP_MODULES))
    253 endif
    254 
    255 $(call ndk_log,[$(TARGET_ARCH_ABI)] Modules to build: $(WANTED_MODULES))
    256 
    257 WANTED_INSTALLED_MODULES += $(call map,module-get-installed,$(WANTED_MODULES))
    258