xref: /OK3568_Linux_fs/external/mpp/build/android/android.toolchain.cmake (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun# Copyright (c) 2010-2011, Ethan Rublee
2*4882a593Smuzhiyun# Copyright (c) 2011-2014, Andrey Kamaev
3*4882a593Smuzhiyun# All rights reserved.
4*4882a593Smuzhiyun#
5*4882a593Smuzhiyun# Redistribution and use in source and binary forms, with or without
6*4882a593Smuzhiyun# modification, are permitted provided that the following conditions are met:
7*4882a593Smuzhiyun#
8*4882a593Smuzhiyun# 1.  Redistributions of source code must retain the above copyright notice,
9*4882a593Smuzhiyun#     this list of conditions and the following disclaimer.
10*4882a593Smuzhiyun#
11*4882a593Smuzhiyun# 2.  Redistributions in binary form must reproduce the above copyright notice,
12*4882a593Smuzhiyun#     this list of conditions and the following disclaimer in the documentation
13*4882a593Smuzhiyun#     and/or other materials provided with the distribution.
14*4882a593Smuzhiyun#
15*4882a593Smuzhiyun# 3.  Neither the name of the copyright holder nor the names of its
16*4882a593Smuzhiyun#     contributors may be used to endorse or promote products derived from this
17*4882a593Smuzhiyun#     software without specific prior written permission.
18*4882a593Smuzhiyun#
19*4882a593Smuzhiyun# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20*4882a593Smuzhiyun# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*4882a593Smuzhiyun# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*4882a593Smuzhiyun# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23*4882a593Smuzhiyun# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*4882a593Smuzhiyun# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*4882a593Smuzhiyun# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*4882a593Smuzhiyun# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*4882a593Smuzhiyun# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*4882a593Smuzhiyun# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*4882a593Smuzhiyun# POSSIBILITY OF SUCH DAMAGE.
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun# ------------------------------------------------------------------------------
32*4882a593Smuzhiyun#  Android CMake toolchain file, for use with the Android NDK r5-r10d
33*4882a593Smuzhiyun#  Requires cmake 2.6.3 or newer (2.8.9 or newer is recommended).
34*4882a593Smuzhiyun#  See home page: https://github.com/taka-no-me/android-cmake
35*4882a593Smuzhiyun#
36*4882a593Smuzhiyun#  Usage Linux:
37*4882a593Smuzhiyun#   $ export ANDROID_NDK=/absolute/path/to/the/android-ndk
38*4882a593Smuzhiyun#   $ mkdir build && cd build
39*4882a593Smuzhiyun#   $ cmake -DCMAKE_TOOLCHAIN_FILE=path/to/the/android.toolchain.cmake ..
40*4882a593Smuzhiyun#   $ make -j8
41*4882a593Smuzhiyun#
42*4882a593Smuzhiyun#  Usage Windows:
43*4882a593Smuzhiyun#     You need native port of make to build your project.
44*4882a593Smuzhiyun#     Android NDK r7 (and newer) already has make.exe on board.
45*4882a593Smuzhiyun#     For older NDK you have to install it separately.
46*4882a593Smuzhiyun#     For example, this one: http://gnuwin32.sourceforge.net/packages/make.htm
47*4882a593Smuzhiyun#
48*4882a593Smuzhiyun#   $ SET ANDROID_NDK=C:\absolute\path\to\the\android-ndk
49*4882a593Smuzhiyun#   $ mkdir build && cd build
50*4882a593Smuzhiyun#   $ cmake.exe -G"MinGW Makefiles"
51*4882a593Smuzhiyun#       -DCMAKE_TOOLCHAIN_FILE=path\to\the\android.toolchain.cmake
52*4882a593Smuzhiyun#       -DCMAKE_MAKE_PROGRAM="%ANDROID_NDK%\prebuilt\windows\bin\make.exe" ..
53*4882a593Smuzhiyun#   $ cmake.exe --build .
54*4882a593Smuzhiyun#
55*4882a593Smuzhiyun#
56*4882a593Smuzhiyun#  Options (can be set as cmake parameters: -D<option_name>=<value>):
57*4882a593Smuzhiyun#    ANDROID_NDK=/opt/android-ndk - path to the NDK root.
58*4882a593Smuzhiyun#      Can be set as environment variable. Can be set only at first cmake run.
59*4882a593Smuzhiyun#
60*4882a593Smuzhiyun#    ANDROID_ABI=armeabi-v7a - specifies the target Application Binary
61*4882a593Smuzhiyun#      Interface (ABI). This option nearly matches to the APP_ABI variable
62*4882a593Smuzhiyun#      used by ndk-build tool from Android NDK.
63*4882a593Smuzhiyun#
64*4882a593Smuzhiyun#      Possible targets are:
65*4882a593Smuzhiyun#        "armeabi" - ARMv5TE based CPU with software floating point operations
66*4882a593Smuzhiyun#        "armeabi-v7a" - ARMv7 based devices with hardware FPU instructions
67*4882a593Smuzhiyun#            this ABI target is used by default
68*4882a593Smuzhiyun#        "armeabi-v7a with NEON" - same as armeabi-v7a, but
69*4882a593Smuzhiyun#            sets NEON as floating-point unit
70*4882a593Smuzhiyun#        "armeabi-v7a with VFPV3" - same as armeabi-v7a, but
71*4882a593Smuzhiyun#            sets VFPV3 as floating-point unit (has 32 registers instead of 16)
72*4882a593Smuzhiyun#        "armeabi-v6 with VFP" - tuned for ARMv6 processors having VFP
73*4882a593Smuzhiyun#        "x86" - IA-32 instruction set
74*4882a593Smuzhiyun#        "mips" - MIPS32 instruction set
75*4882a593Smuzhiyun#
76*4882a593Smuzhiyun#      64-bit ABIs for NDK r10 and newer:
77*4882a593Smuzhiyun#        "arm64-v8a" - ARMv8 AArch64 instruction set
78*4882a593Smuzhiyun#        "x86_64" - Intel64 instruction set (r1)
79*4882a593Smuzhiyun#        "mips64" - MIPS64 instruction set (r6)
80*4882a593Smuzhiyun#
81*4882a593Smuzhiyun#    ANDROID_NATIVE_API_LEVEL=android-8 - level of Android API compile for.
82*4882a593Smuzhiyun#      Option is read-only when standalone toolchain is used.
83*4882a593Smuzhiyun#      Note: building for "android-L" requires explicit configuration.
84*4882a593Smuzhiyun#
85*4882a593Smuzhiyun#    ANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.9 - the name of compiler
86*4882a593Smuzhiyun#      toolchain to be used. The list of possible values depends on the NDK
87*4882a593Smuzhiyun#      version. For NDK r10c the possible values are:
88*4882a593Smuzhiyun#
89*4882a593Smuzhiyun#        * aarch64-linux-android-4.9
90*4882a593Smuzhiyun#        * aarch64-linux-android-clang3.4
91*4882a593Smuzhiyun#        * aarch64-linux-android-clang3.5
92*4882a593Smuzhiyun#        * arm-linux-androideabi-4.6
93*4882a593Smuzhiyun#        * arm-linux-androideabi-4.8
94*4882a593Smuzhiyun#        * arm-linux-androideabi-4.9 (default)
95*4882a593Smuzhiyun#        * arm-linux-androideabi-clang3.4
96*4882a593Smuzhiyun#        * arm-linux-androideabi-clang3.5
97*4882a593Smuzhiyun#        * mips64el-linux-android-4.9
98*4882a593Smuzhiyun#        * mips64el-linux-android-clang3.4
99*4882a593Smuzhiyun#        * mips64el-linux-android-clang3.5
100*4882a593Smuzhiyun#        * mipsel-linux-android-4.6
101*4882a593Smuzhiyun#        * mipsel-linux-android-4.8
102*4882a593Smuzhiyun#        * mipsel-linux-android-4.9
103*4882a593Smuzhiyun#        * mipsel-linux-android-clang3.4
104*4882a593Smuzhiyun#        * mipsel-linux-android-clang3.5
105*4882a593Smuzhiyun#        * x86-4.6
106*4882a593Smuzhiyun#        * x86-4.8
107*4882a593Smuzhiyun#        * x86-4.9
108*4882a593Smuzhiyun#        * x86-clang3.4
109*4882a593Smuzhiyun#        * x86-clang3.5
110*4882a593Smuzhiyun#        * x86_64-4.9
111*4882a593Smuzhiyun#        * x86_64-clang3.4
112*4882a593Smuzhiyun#        * x86_64-clang3.5
113*4882a593Smuzhiyun#
114*4882a593Smuzhiyun#    ANDROID_FORCE_ARM_BUILD=OFF - set ON to generate 32-bit ARM instructions
115*4882a593Smuzhiyun#      instead of Thumb. Is not available for "armeabi-v6 with VFP"
116*4882a593Smuzhiyun#      (is forced to be ON) ABI.
117*4882a593Smuzhiyun#
118*4882a593Smuzhiyun#    ANDROID_NO_UNDEFINED=ON - set ON to show all undefined symbols as linker
119*4882a593Smuzhiyun#      errors even if they are not used.
120*4882a593Smuzhiyun#
121*4882a593Smuzhiyun#    ANDROID_SO_UNDEFINED=OFF - set ON to allow undefined symbols in shared
122*4882a593Smuzhiyun#      libraries. Automatically turned for NDK r5x and r6x due to GLESv2
123*4882a593Smuzhiyun#      problems.
124*4882a593Smuzhiyun#
125*4882a593Smuzhiyun#    ANDROID_STL=gnustl_static - specify the runtime to use.
126*4882a593Smuzhiyun#
127*4882a593Smuzhiyun#      Possible values are:
128*4882a593Smuzhiyun#        none           -> Do not configure the runtime.
129*4882a593Smuzhiyun#        system         -> Use the default minimal system C++ runtime library.
130*4882a593Smuzhiyun#                          Implies -fno-rtti -fno-exceptions.
131*4882a593Smuzhiyun#                          Is not available for standalone toolchain.
132*4882a593Smuzhiyun#        system_re      -> Use the default minimal system C++ runtime library.
133*4882a593Smuzhiyun#                          Implies -frtti -fexceptions.
134*4882a593Smuzhiyun#                          Is not available for standalone toolchain.
135*4882a593Smuzhiyun#        gabi++_static  -> Use the GAbi++ runtime as a static library.
136*4882a593Smuzhiyun#                          Implies -frtti -fno-exceptions.
137*4882a593Smuzhiyun#                          Available for NDK r7 and newer.
138*4882a593Smuzhiyun#                          Is not available for standalone toolchain.
139*4882a593Smuzhiyun#        gabi++_shared  -> Use the GAbi++ runtime as a shared library.
140*4882a593Smuzhiyun#                          Implies -frtti -fno-exceptions.
141*4882a593Smuzhiyun#                          Available for NDK r7 and newer.
142*4882a593Smuzhiyun#                          Is not available for standalone toolchain.
143*4882a593Smuzhiyun#        stlport_static -> Use the STLport runtime as a static library.
144*4882a593Smuzhiyun#                          Implies -fno-rtti -fno-exceptions for NDK before r7.
145*4882a593Smuzhiyun#                          Implies -frtti -fno-exceptions for NDK r7 and newer.
146*4882a593Smuzhiyun#                          Is not available for standalone toolchain.
147*4882a593Smuzhiyun#        stlport_shared -> Use the STLport runtime as a shared library.
148*4882a593Smuzhiyun#                          Implies -fno-rtti -fno-exceptions for NDK before r7.
149*4882a593Smuzhiyun#                          Implies -frtti -fno-exceptions for NDK r7 and newer.
150*4882a593Smuzhiyun#                          Is not available for standalone toolchain.
151*4882a593Smuzhiyun#        gnustl_static  -> Use the GNU STL as a static library.
152*4882a593Smuzhiyun#                          Implies -frtti -fexceptions.
153*4882a593Smuzhiyun#        gnustl_shared  -> Use the GNU STL as a shared library.
154*4882a593Smuzhiyun#                          Implies -frtti -fno-exceptions.
155*4882a593Smuzhiyun#                          Available for NDK r7b and newer.
156*4882a593Smuzhiyun#                          Silently degrades to gnustl_static if not available.
157*4882a593Smuzhiyun#
158*4882a593Smuzhiyun#    ANDROID_STL_FORCE_FEATURES=ON - turn rtti and exceptions support based on
159*4882a593Smuzhiyun#      chosen runtime. If disabled, then the user is responsible for settings
160*4882a593Smuzhiyun#      these options.
161*4882a593Smuzhiyun#
162*4882a593Smuzhiyun#  What?:
163*4882a593Smuzhiyun#    android-cmake toolchain searches for NDK/toolchain in the following order:
164*4882a593Smuzhiyun#      ANDROID_NDK - cmake parameter
165*4882a593Smuzhiyun#      ANDROID_NDK - environment variable
166*4882a593Smuzhiyun#      ANDROID_STANDALONE_TOOLCHAIN - cmake parameter
167*4882a593Smuzhiyun#      ANDROID_STANDALONE_TOOLCHAIN - environment variable
168*4882a593Smuzhiyun#      ANDROID_NDK - default locations
169*4882a593Smuzhiyun#      ANDROID_STANDALONE_TOOLCHAIN - default locations
170*4882a593Smuzhiyun#
171*4882a593Smuzhiyun#    Make sure to do the following in your scripts:
172*4882a593Smuzhiyun#      SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${my_cxx_flags}" )
173*4882a593Smuzhiyun#      SET( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${my_cxx_flags}" )
174*4882a593Smuzhiyun#    The flags will be prepopulated with critical flags, so don't loose them.
175*4882a593Smuzhiyun#    Also be aware that toolchain also sets configuration-specific compiler
176*4882a593Smuzhiyun#    flags and linker flags.
177*4882a593Smuzhiyun#
178*4882a593Smuzhiyun#    ANDROID and BUILD_ANDROID will be set to true, you may test any of these
179*4882a593Smuzhiyun#    variables to make necessary Android-specific configuration changes.
180*4882a593Smuzhiyun#
181*4882a593Smuzhiyun#    Also ARMEABI or ARMEABI_V7A or X86 or MIPS or ARM64_V8A or X86_64 or MIPS64
182*4882a593Smuzhiyun#    will be set true, mutually exclusive. NEON option will be set true
183*4882a593Smuzhiyun#    if VFP is set to NEON.
184*4882a593Smuzhiyun#
185*4882a593Smuzhiyun# ------------------------------------------------------------------------------
186*4882a593Smuzhiyun
187*4882a593Smuzhiyuncmake_minimum_required( VERSION 2.6.3 )
188*4882a593Smuzhiyun
189*4882a593Smuzhiyunset(MPP_PROJECT_NAME mpp)
190*4882a593Smuzhiyunset(VPU_PROJECT_NAME vpu)
191*4882a593Smuzhiyun
192*4882a593Smuzhiyunif( DEFINED CMAKE_CROSSCOMPILING )
193*4882a593Smuzhiyun # subsequent toolchain loading is not really needed
194*4882a593Smuzhiyun return()
195*4882a593Smuzhiyunendif()
196*4882a593Smuzhiyun
197*4882a593Smuzhiyunif( CMAKE_TOOLCHAIN_FILE )
198*4882a593Smuzhiyun # touch toolchain variable to suppress "unused variable" warning
199*4882a593Smuzhiyunendif()
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun# inherit settings in recursive loads
202*4882a593Smuzhiyunget_property( _CMAKE_IN_TRY_COMPILE GLOBAL PROPERTY IN_TRY_COMPILE )
203*4882a593Smuzhiyunif( _CMAKE_IN_TRY_COMPILE )
204*4882a593Smuzhiyun include( "${CMAKE_CURRENT_SOURCE_DIR}/../android.toolchain.config.cmake" OPTIONAL )
205*4882a593Smuzhiyunendif()
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun# this one is important
208*4882a593Smuzhiyunif( CMAKE_VERSION VERSION_GREATER "3.0.99" )
209*4882a593Smuzhiyun set( CMAKE_SYSTEM_NAME Android )
210*4882a593Smuzhiyunelse()
211*4882a593Smuzhiyun set( CMAKE_SYSTEM_NAME Linux )
212*4882a593Smuzhiyunendif()
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun# this one not so much
215*4882a593Smuzhiyunset( CMAKE_SYSTEM_VERSION 1 )
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun# rpath makes low sense for Android
218*4882a593Smuzhiyunset( CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG "" )
219*4882a593Smuzhiyunset( CMAKE_SKIP_RPATH TRUE CACHE BOOL "If set, runtime paths are not added when using shared libraries." )
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun# NDK search paths
222*4882a593Smuzhiyunset( ANDROID_SUPPORTED_NDK_VERSIONS ${ANDROID_EXTRA_NDK_VERSIONS} -r10d -r10c -r10b -r10 -r9d -r9c -r9b -r9 -r8e -r8d -r8c -r8b -r8 -r7c -r7b -r7 -r6b -r6 -r5c -r5b -r5 "" )
223*4882a593Smuzhiyunif( NOT DEFINED ANDROID_NDK_SEARCH_PATHS )
224*4882a593Smuzhiyun if( CMAKE_HOST_WIN32 )
225*4882a593Smuzhiyun  file( TO_CMAKE_PATH "$ENV{PROGRAMFILES}" ANDROID_NDK_SEARCH_PATHS )
226*4882a593Smuzhiyun  set( ANDROID_NDK_SEARCH_PATHS "${ANDROID_NDK_SEARCH_PATHS}" "$ENV{SystemDrive}/NVPACK" )
227*4882a593Smuzhiyun else()
228*4882a593Smuzhiyun  file( TO_CMAKE_PATH "$ENV{HOME}" ANDROID_NDK_SEARCH_PATHS )
229*4882a593Smuzhiyun  set( ANDROID_NDK_SEARCH_PATHS /opt "${ANDROID_NDK_SEARCH_PATHS}/NVPACK" )
230*4882a593Smuzhiyun endif()
231*4882a593Smuzhiyunendif()
232*4882a593Smuzhiyunif( NOT DEFINED ANDROID_STANDALONE_TOOLCHAIN_SEARCH_PATH )
233*4882a593Smuzhiyun set( ANDROID_STANDALONE_TOOLCHAIN_SEARCH_PATH /opt/android-toolchain )
234*4882a593Smuzhiyunendif()
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun# known ABIs
237*4882a593Smuzhiyunset( ANDROID_SUPPORTED_ABIS_arm "armeabi-v7a;armeabi;armeabi-v7a with NEON;armeabi-v7a with VFPV3;armeabi-v6 with VFP" )
238*4882a593Smuzhiyunset( ANDROID_SUPPORTED_ABIS_arm64 "arm64-v8a" )
239*4882a593Smuzhiyunset( ANDROID_SUPPORTED_ABIS_x86 "x86" )
240*4882a593Smuzhiyunset( ANDROID_SUPPORTED_ABIS_x86_64 "x86_64" )
241*4882a593Smuzhiyunset( ANDROID_SUPPORTED_ABIS_mips "mips" )
242*4882a593Smuzhiyunset( ANDROID_SUPPORTED_ABIS_mips64 "mips64" )
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun# API level defaults
245*4882a593Smuzhiyunset( ANDROID_DEFAULT_NDK_API_LEVEL 8 )
246*4882a593Smuzhiyunset( ANDROID_DEFAULT_NDK_API_LEVEL_arm64 21 )
247*4882a593Smuzhiyunset( ANDROID_DEFAULT_NDK_API_LEVEL_x86 9 )
248*4882a593Smuzhiyunset( ANDROID_DEFAULT_NDK_API_LEVEL_x86_64 21 )
249*4882a593Smuzhiyunset( ANDROID_DEFAULT_NDK_API_LEVEL_mips 9 )
250*4882a593Smuzhiyunset( ANDROID_DEFAULT_NDK_API_LEVEL_mips64 21 )
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun
253*4882a593Smuzhiyunmacro( __LIST_FILTER listvar regex )
254*4882a593Smuzhiyun  if( ${listvar} )
255*4882a593Smuzhiyun    foreach( __val ${${listvar}} )
256*4882a593Smuzhiyun      if( __val MATCHES "${regex}" )
257*4882a593Smuzhiyun        list( REMOVE_ITEM ${listvar} "${__val}" )
258*4882a593Smuzhiyun      endif()
259*4882a593Smuzhiyun    endforeach()
260*4882a593Smuzhiyun  endif()
261*4882a593Smuzhiyunendmacro()
262*4882a593Smuzhiyun
263*4882a593Smuzhiyunmacro( __INIT_VARIABLE var_name )
264*4882a593Smuzhiyun  set( __test_path 0 )
265*4882a593Smuzhiyun  foreach( __var ${ARGN} )
266*4882a593Smuzhiyun    if( __var STREQUAL "PATH" )
267*4882a593Smuzhiyun      set( __test_path 1 )
268*4882a593Smuzhiyun      break()
269*4882a593Smuzhiyun    endif()
270*4882a593Smuzhiyun  endforeach()
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun  if( __test_path AND NOT EXISTS "${${var_name}}" )
273*4882a593Smuzhiyun    unset( ${var_name} CACHE )
274*4882a593Smuzhiyun  endif()
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun  if( " ${${var_name}}" STREQUAL " " )
277*4882a593Smuzhiyun    set( __values 0 )
278*4882a593Smuzhiyun    foreach( __var ${ARGN} )
279*4882a593Smuzhiyun      if( __var STREQUAL "VALUES" )
280*4882a593Smuzhiyun        set( __values 1 )
281*4882a593Smuzhiyun      elseif( NOT __var STREQUAL "PATH" )
282*4882a593Smuzhiyun        if( __var MATCHES "^ENV_.*$" )
283*4882a593Smuzhiyun          string( REPLACE "ENV_" "" __var "${__var}" )
284*4882a593Smuzhiyun          set( __value "$ENV{${__var}}" )
285*4882a593Smuzhiyun        elseif( DEFINED ${__var} )
286*4882a593Smuzhiyun          set( __value "${${__var}}" )
287*4882a593Smuzhiyun        elseif( __values )
288*4882a593Smuzhiyun          set( __value "${__var}" )
289*4882a593Smuzhiyun        else()
290*4882a593Smuzhiyun          set( __value "" )
291*4882a593Smuzhiyun        endif()
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun        if( NOT " ${__value}" STREQUAL " " AND (NOT __test_path OR EXISTS "${__value}") )
294*4882a593Smuzhiyun          set( ${var_name} "${__value}" )
295*4882a593Smuzhiyun          break()
296*4882a593Smuzhiyun        endif()
297*4882a593Smuzhiyun      endif()
298*4882a593Smuzhiyun    endforeach()
299*4882a593Smuzhiyun    unset( __value )
300*4882a593Smuzhiyun    unset( __values )
301*4882a593Smuzhiyun  endif()
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun  if( __test_path )
304*4882a593Smuzhiyun    file( TO_CMAKE_PATH "${${var_name}}" ${var_name} )
305*4882a593Smuzhiyun  endif()
306*4882a593Smuzhiyun  unset( __test_path )
307*4882a593Smuzhiyunendmacro()
308*4882a593Smuzhiyun
309*4882a593Smuzhiyunmacro( __DETECT_NATIVE_API_LEVEL _var _path )
310*4882a593Smuzhiyun  set( __ndkApiLevelRegex "^[\t ]*#define[\t ]+__ANDROID_API__[\t ]+([0-9]+)[\t ]*.*$" )
311*4882a593Smuzhiyun  file( STRINGS ${_path} __apiFileContent REGEX "${__ndkApiLevelRegex}" )
312*4882a593Smuzhiyun  if( NOT __apiFileContent )
313*4882a593Smuzhiyun    message( SEND_ERROR "Could not get Android native API level. Probably you have specified invalid level value, or your copy of NDK/toolchain is broken." )
314*4882a593Smuzhiyun  endif()
315*4882a593Smuzhiyun  string( REGEX REPLACE "${__ndkApiLevelRegex}" "\\1" ${_var} "${__apiFileContent}" )
316*4882a593Smuzhiyun  unset( __apiFileContent )
317*4882a593Smuzhiyun  unset( __ndkApiLevelRegex )
318*4882a593Smuzhiyunendmacro()
319*4882a593Smuzhiyun
320*4882a593Smuzhiyunmacro( __DETECT_TOOLCHAIN_MACHINE_NAME _var _root )
321*4882a593Smuzhiyun if( EXISTS "${_root}" )
322*4882a593Smuzhiyun    file( GLOB __gccExePath RELATIVE "${_root}/bin/" "${_root}/bin/*-gcc${TOOL_OS_SUFFIX}" )
323*4882a593Smuzhiyun    __LIST_FILTER( __gccExePath "^[.].*" )
324*4882a593Smuzhiyun    list( LENGTH __gccExePath __gccExePathsCount )
325*4882a593Smuzhiyun    if( NOT __gccExePathsCount EQUAL 1  AND NOT _CMAKE_IN_TRY_COMPILE )
326*4882a593Smuzhiyun      message( WARNING "Could not determine machine name for compiler from ${_root}" )
327*4882a593Smuzhiyun      set( ${_var} "" )
328*4882a593Smuzhiyun    else()
329*4882a593Smuzhiyun      get_filename_component( __gccExeName "${__gccExePath}" NAME_WE )
330*4882a593Smuzhiyun      string( REPLACE "-gcc" "" ${_var} "${__gccExeName}" )
331*4882a593Smuzhiyun    endif()
332*4882a593Smuzhiyun    unset( __gccExePath )
333*4882a593Smuzhiyun    unset( __gccExePathsCount )
334*4882a593Smuzhiyun    unset( __gccExeName )
335*4882a593Smuzhiyun  else()
336*4882a593Smuzhiyun    set( ${_var} "" )
337*4882a593Smuzhiyun  endif()
338*4882a593Smuzhiyunendmacro()
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun# fight against cygwin
342*4882a593Smuzhiyunset( ANDROID_FORBID_SYGWIN TRUE CACHE BOOL "Prevent cmake from working under cygwin and using cygwin tools")
343*4882a593Smuzhiyunmark_as_advanced( ANDROID_FORBID_SYGWIN )
344*4882a593Smuzhiyunif( ANDROID_FORBID_SYGWIN )
345*4882a593Smuzhiyun if( CYGWIN )
346*4882a593Smuzhiyun  message( FATAL_ERROR "Android NDK and android-cmake toolchain are not welcome Cygwin. It is unlikely that this cmake toolchain will work under cygwin. But if you want to try then you can set cmake variable ANDROID_FORBID_SYGWIN to FALSE and rerun cmake." )
347*4882a593Smuzhiyun endif()
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun if( CMAKE_HOST_WIN32 )
350*4882a593Smuzhiyun  # remove cygwin from PATH
351*4882a593Smuzhiyun  set( __new_path "$ENV{PATH}")
352*4882a593Smuzhiyun  __LIST_FILTER( __new_path "cygwin" )
353*4882a593Smuzhiyun  set(ENV{PATH} "${__new_path}")
354*4882a593Smuzhiyun  unset(__new_path)
355*4882a593Smuzhiyun endif()
356*4882a593Smuzhiyunendif()
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun# detect current host platform
360*4882a593Smuzhiyunif( NOT DEFINED ANDROID_NDK_HOST_X64 AND (CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "amd64|x86_64|AMD64" OR CMAKE_HOST_APPLE) )
361*4882a593Smuzhiyun set( ANDROID_NDK_HOST_X64 1 CACHE BOOL "Try to use 64-bit compiler toolchain" )
362*4882a593Smuzhiyun mark_as_advanced( ANDROID_NDK_HOST_X64 )
363*4882a593Smuzhiyunendif()
364*4882a593Smuzhiyun
365*4882a593Smuzhiyunset( TOOL_OS_SUFFIX "" )
366*4882a593Smuzhiyunif( CMAKE_HOST_APPLE )
367*4882a593Smuzhiyun set( ANDROID_NDK_HOST_SYSTEM_NAME "darwin-x86_64" )
368*4882a593Smuzhiyun set( ANDROID_NDK_HOST_SYSTEM_NAME2 "darwin-x86" )
369*4882a593Smuzhiyunelseif( CMAKE_HOST_WIN32 )
370*4882a593Smuzhiyun set( ANDROID_NDK_HOST_SYSTEM_NAME "windows-x86_64" )
371*4882a593Smuzhiyun set( ANDROID_NDK_HOST_SYSTEM_NAME2 "windows" )
372*4882a593Smuzhiyun set( TOOL_OS_SUFFIX ".exe" )
373*4882a593Smuzhiyunelseif( CMAKE_HOST_UNIX )
374*4882a593Smuzhiyun set( ANDROID_NDK_HOST_SYSTEM_NAME "linux-x86_64" )
375*4882a593Smuzhiyun set( ANDROID_NDK_HOST_SYSTEM_NAME2 "linux-x86" )
376*4882a593Smuzhiyunelse()
377*4882a593Smuzhiyun message( FATAL_ERROR "Cross-compilation on your platform is not supported by this cmake toolchain" )
378*4882a593Smuzhiyunendif()
379*4882a593Smuzhiyun
380*4882a593Smuzhiyunif( NOT ANDROID_NDK_HOST_X64 )
381*4882a593Smuzhiyun set( ANDROID_NDK_HOST_SYSTEM_NAME ${ANDROID_NDK_HOST_SYSTEM_NAME2} )
382*4882a593Smuzhiyunendif()
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun# see if we have path to Android NDK
385*4882a593Smuzhiyunif( NOT ANDROID_NDK AND NOT ANDROID_STANDALONE_TOOLCHAIN )
386*4882a593Smuzhiyun  __INIT_VARIABLE( ANDROID_NDK PATH ENV_ANDROID_NDK )
387*4882a593Smuzhiyunendif()
388*4882a593Smuzhiyunif( NOT ANDROID_NDK )
389*4882a593Smuzhiyun # see if we have path to Android standalone toolchain
390*4882a593Smuzhiyun __INIT_VARIABLE( ANDROID_STANDALONE_TOOLCHAIN PATH ENV_ANDROID_STANDALONE_TOOLCHAIN )
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun if( NOT ANDROID_STANDALONE_TOOLCHAIN )
393*4882a593Smuzhiyun  #try to find Android NDK in one of the the default locations
394*4882a593Smuzhiyun  set( __ndkSearchPaths )
395*4882a593Smuzhiyun  foreach( __ndkSearchPath ${ANDROID_NDK_SEARCH_PATHS} )
396*4882a593Smuzhiyun   foreach( suffix ${ANDROID_SUPPORTED_NDK_VERSIONS} )
397*4882a593Smuzhiyun    list( APPEND __ndkSearchPaths "${__ndkSearchPath}/android-ndk${suffix}" )
398*4882a593Smuzhiyun   endforeach()
399*4882a593Smuzhiyun  endforeach()
400*4882a593Smuzhiyun  __INIT_VARIABLE( ANDROID_NDK PATH VALUES ${__ndkSearchPaths} )
401*4882a593Smuzhiyun  unset( __ndkSearchPaths )
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun  if( ANDROID_NDK )
404*4882a593Smuzhiyun   message( STATUS "Using default path for Android NDK: ${ANDROID_NDK}" )
405*4882a593Smuzhiyun   message( STATUS "  If you prefer to use a different location, please define a cmake or environment variable: ANDROID_NDK" )
406*4882a593Smuzhiyun  else()
407*4882a593Smuzhiyun   #try to find Android standalone toolchain in one of the the default locations
408*4882a593Smuzhiyun   __INIT_VARIABLE( ANDROID_STANDALONE_TOOLCHAIN PATH ANDROID_STANDALONE_TOOLCHAIN_SEARCH_PATH )
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun   if( ANDROID_STANDALONE_TOOLCHAIN )
411*4882a593Smuzhiyun    message( STATUS "Using default path for standalone toolchain ${ANDROID_STANDALONE_TOOLCHAIN}" )
412*4882a593Smuzhiyun    message( STATUS "  If you prefer to use a different location, please define the variable: ANDROID_STANDALONE_TOOLCHAIN" )
413*4882a593Smuzhiyun   endif( ANDROID_STANDALONE_TOOLCHAIN )
414*4882a593Smuzhiyun  endif( ANDROID_NDK )
415*4882a593Smuzhiyun endif( NOT ANDROID_STANDALONE_TOOLCHAIN )
416*4882a593Smuzhiyunendif( NOT ANDROID_NDK )
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun# remember found paths
419*4882a593Smuzhiyunif( ANDROID_NDK )
420*4882a593Smuzhiyun get_filename_component( ANDROID_NDK "${ANDROID_NDK}" ABSOLUTE )
421*4882a593Smuzhiyun set( ANDROID_NDK "${ANDROID_NDK}" CACHE INTERNAL "Path of the Android NDK" FORCE )
422*4882a593Smuzhiyun set( BUILD_WITH_ANDROID_NDK True )
423*4882a593Smuzhiyun if( EXISTS "${ANDROID_NDK}/RELEASE.TXT" )
424*4882a593Smuzhiyun  file( STRINGS "${ANDROID_NDK}/RELEASE.TXT" ANDROID_NDK_RELEASE_FULL LIMIT_COUNT 1 REGEX "r[0-9]+[a-z]?" )
425*4882a593Smuzhiyun  string( REGEX MATCH "r([0-9]+)([a-z]?)" ANDROID_NDK_RELEASE "${ANDROID_NDK_RELEASE_FULL}" )
426*4882a593Smuzhiyun else()
427*4882a593Smuzhiyun  set( ANDROID_NDK_RELEASE "r1x" )
428*4882a593Smuzhiyun  set( ANDROID_NDK_RELEASE_FULL "unreleased" )
429*4882a593Smuzhiyun endif()
430*4882a593Smuzhiyun string( REGEX REPLACE "r([0-9]+)([a-z]?)" "\\1*1000" ANDROID_NDK_RELEASE_NUM "${ANDROID_NDK_RELEASE}" )
431*4882a593Smuzhiyun string( FIND " abcdefghijklmnopqastuvwxyz" "${CMAKE_MATCH_2}" __ndkReleaseLetterNum )
432*4882a593Smuzhiyun math( EXPR ANDROID_NDK_RELEASE_NUM "${ANDROID_NDK_RELEASE_NUM}+${__ndkReleaseLetterNum}" )
433*4882a593Smuzhiyunelseif( ANDROID_STANDALONE_TOOLCHAIN )
434*4882a593Smuzhiyun get_filename_component( ANDROID_STANDALONE_TOOLCHAIN "${ANDROID_STANDALONE_TOOLCHAIN}" ABSOLUTE )
435*4882a593Smuzhiyun # try to detect change
436*4882a593Smuzhiyun if( CMAKE_AR )
437*4882a593Smuzhiyun  string( LENGTH "${ANDROID_STANDALONE_TOOLCHAIN}" __length )
438*4882a593Smuzhiyun  string( SUBSTRING "${CMAKE_AR}" 0 ${__length} __androidStandaloneToolchainPreviousPath )
439*4882a593Smuzhiyun  if( NOT __androidStandaloneToolchainPreviousPath STREQUAL ANDROID_STANDALONE_TOOLCHAIN )
440*4882a593Smuzhiyun   message( FATAL_ERROR "It is not possible to change path to the Android standalone toolchain on subsequent run." )
441*4882a593Smuzhiyun  endif()
442*4882a593Smuzhiyun  unset( __androidStandaloneToolchainPreviousPath )
443*4882a593Smuzhiyun  unset( __length )
444*4882a593Smuzhiyun endif()
445*4882a593Smuzhiyun set( ANDROID_STANDALONE_TOOLCHAIN "${ANDROID_STANDALONE_TOOLCHAIN}" CACHE INTERNAL "Path of the Android standalone toolchain" FORCE )
446*4882a593Smuzhiyun set( BUILD_WITH_STANDALONE_TOOLCHAIN True )
447*4882a593Smuzhiyunelse()
448*4882a593Smuzhiyun list(GET ANDROID_NDK_SEARCH_PATHS 0 ANDROID_NDK_SEARCH_PATH)
449*4882a593Smuzhiyun message( FATAL_ERROR "Could not find neither Android NDK nor Android standalone toolchain.
450*4882a593Smuzhiyun    You should either set an environment variable:
451*4882a593Smuzhiyun      export ANDROID_NDK=~/my-android-ndk
452*4882a593Smuzhiyun    or
453*4882a593Smuzhiyun      export ANDROID_STANDALONE_TOOLCHAIN=~/my-android-toolchain
454*4882a593Smuzhiyun    or put the toolchain or NDK in the default path:
455*4882a593Smuzhiyun      sudo ln -s ~/my-android-ndk ${ANDROID_NDK_SEARCH_PATH}/android-ndk
456*4882a593Smuzhiyun      sudo ln -s ~/my-android-toolchain ${ANDROID_STANDALONE_TOOLCHAIN_SEARCH_PATH}" )
457*4882a593Smuzhiyunendif()
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun# android NDK layout
460*4882a593Smuzhiyunif( BUILD_WITH_ANDROID_NDK )
461*4882a593Smuzhiyun if( NOT DEFINED ANDROID_NDK_LAYOUT )
462*4882a593Smuzhiyun  # try to automatically detect the layout
463*4882a593Smuzhiyun  if( EXISTS "${ANDROID_NDK}/RELEASE.TXT")
464*4882a593Smuzhiyun   set( ANDROID_NDK_LAYOUT "RELEASE" )
465*4882a593Smuzhiyun  elseif( EXISTS "${ANDROID_NDK}/../../linux-x86/toolchain/" )
466*4882a593Smuzhiyun   set( ANDROID_NDK_LAYOUT "LINARO" )
467*4882a593Smuzhiyun  elseif( EXISTS "${ANDROID_NDK}/../../gcc/" )
468*4882a593Smuzhiyun   set( ANDROID_NDK_LAYOUT "ANDROID" )
469*4882a593Smuzhiyun  endif()
470*4882a593Smuzhiyun endif()
471*4882a593Smuzhiyun set( ANDROID_NDK_LAYOUT "${ANDROID_NDK_LAYOUT}" CACHE STRING "The inner layout of NDK" )
472*4882a593Smuzhiyun mark_as_advanced( ANDROID_NDK_LAYOUT )
473*4882a593Smuzhiyun if( ANDROID_NDK_LAYOUT STREQUAL "LINARO" )
474*4882a593Smuzhiyun  set( ANDROID_NDK_HOST_SYSTEM_NAME ${ANDROID_NDK_HOST_SYSTEM_NAME2} ) # only 32-bit at the moment
475*4882a593Smuzhiyun  set( ANDROID_NDK_TOOLCHAINS_PATH "${ANDROID_NDK}/../../${ANDROID_NDK_HOST_SYSTEM_NAME}/toolchain" )
476*4882a593Smuzhiyun  set( ANDROID_NDK_TOOLCHAINS_SUBPATH  "" )
477*4882a593Smuzhiyun  set( ANDROID_NDK_TOOLCHAINS_SUBPATH2 "" )
478*4882a593Smuzhiyun elseif( ANDROID_NDK_LAYOUT STREQUAL "ANDROID" )
479*4882a593Smuzhiyun  set( ANDROID_NDK_HOST_SYSTEM_NAME ${ANDROID_NDK_HOST_SYSTEM_NAME2} ) # only 32-bit at the moment
480*4882a593Smuzhiyun  set( ANDROID_NDK_TOOLCHAINS_PATH "${ANDROID_NDK}/../../gcc/${ANDROID_NDK_HOST_SYSTEM_NAME}/arm" )
481*4882a593Smuzhiyun  set( ANDROID_NDK_TOOLCHAINS_SUBPATH  "" )
482*4882a593Smuzhiyun  set( ANDROID_NDK_TOOLCHAINS_SUBPATH2 "" )
483*4882a593Smuzhiyun else() # ANDROID_NDK_LAYOUT STREQUAL "RELEASE"
484*4882a593Smuzhiyun  set( ANDROID_NDK_TOOLCHAINS_PATH "${ANDROID_NDK}/toolchains" )
485*4882a593Smuzhiyun  set( ANDROID_NDK_TOOLCHAINS_SUBPATH  "/prebuilt/${ANDROID_NDK_HOST_SYSTEM_NAME}" )
486*4882a593Smuzhiyun  set( ANDROID_NDK_TOOLCHAINS_SUBPATH2 "/prebuilt/${ANDROID_NDK_HOST_SYSTEM_NAME2}" )
487*4882a593Smuzhiyun endif()
488*4882a593Smuzhiyun get_filename_component( ANDROID_NDK_TOOLCHAINS_PATH "${ANDROID_NDK_TOOLCHAINS_PATH}" ABSOLUTE )
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun # try to detect change of NDK
491*4882a593Smuzhiyun if( CMAKE_AR )
492*4882a593Smuzhiyun  string( LENGTH "${ANDROID_NDK_TOOLCHAINS_PATH}" __length )
493*4882a593Smuzhiyun  string( SUBSTRING "${CMAKE_AR}" 0 ${__length} __androidNdkPreviousPath )
494*4882a593Smuzhiyun  if( NOT __androidNdkPreviousPath STREQUAL ANDROID_NDK_TOOLCHAINS_PATH )
495*4882a593Smuzhiyun   message( FATAL_ERROR "It is not possible to change the path to the NDK on subsequent CMake run. You must remove all generated files from your build folder first.
496*4882a593Smuzhiyun   " )
497*4882a593Smuzhiyun  endif()
498*4882a593Smuzhiyun  unset( __androidNdkPreviousPath )
499*4882a593Smuzhiyun  unset( __length )
500*4882a593Smuzhiyun endif()
501*4882a593Smuzhiyunendif()
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun# get all the details about standalone toolchain
505*4882a593Smuzhiyunif( BUILD_WITH_STANDALONE_TOOLCHAIN )
506*4882a593Smuzhiyun __DETECT_NATIVE_API_LEVEL( ANDROID_SUPPORTED_NATIVE_API_LEVELS "${ANDROID_STANDALONE_TOOLCHAIN}/sysroot/usr/include/android/api-level.h" )
507*4882a593Smuzhiyun set( ANDROID_STANDALONE_TOOLCHAIN_API_LEVEL ${ANDROID_SUPPORTED_NATIVE_API_LEVELS} )
508*4882a593Smuzhiyun set( __availableToolchains "standalone" )
509*4882a593Smuzhiyun __DETECT_TOOLCHAIN_MACHINE_NAME( __availableToolchainMachines "${ANDROID_STANDALONE_TOOLCHAIN}" )
510*4882a593Smuzhiyun if( NOT __availableToolchainMachines )
511*4882a593Smuzhiyun  message( FATAL_ERROR "Could not determine machine name of your toolchain. Probably your Android standalone toolchain is broken." )
512*4882a593Smuzhiyun endif()
513*4882a593Smuzhiyun if( __availableToolchainMachines MATCHES x86_64 )
514*4882a593Smuzhiyun  set( __availableToolchainArchs "x86_64" )
515*4882a593Smuzhiyun elseif( __availableToolchainMachines MATCHES i686 )
516*4882a593Smuzhiyun  set( __availableToolchainArchs "x86" )
517*4882a593Smuzhiyun elseif( __availableToolchainMachines MATCHES aarch64 )
518*4882a593Smuzhiyun  set( __availableToolchainArchs "arm64" )
519*4882a593Smuzhiyun elseif( __availableToolchainMachines MATCHES arm )
520*4882a593Smuzhiyun  set( __availableToolchainArchs "arm" )
521*4882a593Smuzhiyun elseif( __availableToolchainMachines MATCHES mips64el )
522*4882a593Smuzhiyun  set( __availableToolchainArchs "mips64" )
523*4882a593Smuzhiyun elseif( __availableToolchainMachines MATCHES mipsel )
524*4882a593Smuzhiyun  set( __availableToolchainArchs "mips" )
525*4882a593Smuzhiyun endif()
526*4882a593Smuzhiyun execute_process( COMMAND "${ANDROID_STANDALONE_TOOLCHAIN}/bin/${__availableToolchainMachines}-gcc${TOOL_OS_SUFFIX}" -dumpversion
527*4882a593Smuzhiyun                  OUTPUT_VARIABLE __availableToolchainCompilerVersions OUTPUT_STRIP_TRAILING_WHITESPACE )
528*4882a593Smuzhiyun string( REGEX MATCH "[0-9]+[.][0-9]+([.][0-9]+)?" __availableToolchainCompilerVersions "${__availableToolchainCompilerVersions}" )
529*4882a593Smuzhiyun if( EXISTS "${ANDROID_STANDALONE_TOOLCHAIN}/bin/clang${TOOL_OS_SUFFIX}" )
530*4882a593Smuzhiyun  list( APPEND __availableToolchains "standalone-clang" )
531*4882a593Smuzhiyun  list( APPEND __availableToolchainMachines ${__availableToolchainMachines} )
532*4882a593Smuzhiyun  list( APPEND __availableToolchainArchs ${__availableToolchainArchs} )
533*4882a593Smuzhiyun  list( APPEND __availableToolchainCompilerVersions ${__availableToolchainCompilerVersions} )
534*4882a593Smuzhiyun endif()
535*4882a593Smuzhiyunendif()
536*4882a593Smuzhiyun
537*4882a593Smuzhiyunmacro( __GLOB_NDK_TOOLCHAINS __availableToolchainsVar __availableToolchainsLst __toolchain_subpath )
538*4882a593Smuzhiyun foreach( __toolchain ${${__availableToolchainsLst}} )
539*4882a593Smuzhiyun  if( "${__toolchain}" MATCHES "-clang3[.][0-9]$" AND NOT EXISTS "${ANDROID_NDK_TOOLCHAINS_PATH}/${__toolchain}${__toolchain_subpath}" )
540*4882a593Smuzhiyun   SET( __toolchainVersionRegex "^TOOLCHAIN_VERSION[\t ]+:=[\t ]+(.*)$" )
541*4882a593Smuzhiyun   FILE( STRINGS "${ANDROID_NDK_TOOLCHAINS_PATH}/${__toolchain}/setup.mk" __toolchainVersionStr REGEX "${__toolchainVersionRegex}" )
542*4882a593Smuzhiyun   if( __toolchainVersionStr )
543*4882a593Smuzhiyun    string( REGEX REPLACE "${__toolchainVersionRegex}" "\\1" __toolchainVersionStr "${__toolchainVersionStr}" )
544*4882a593Smuzhiyun    string( REGEX REPLACE "-clang3[.][0-9]$" "-${__toolchainVersionStr}" __gcc_toolchain "${__toolchain}" )
545*4882a593Smuzhiyun   else()
546*4882a593Smuzhiyun    string( REGEX REPLACE "-clang3[.][0-9]$" "-4.6" __gcc_toolchain "${__toolchain}" )
547*4882a593Smuzhiyun   endif()
548*4882a593Smuzhiyun   unset( __toolchainVersionStr )
549*4882a593Smuzhiyun   unset( __toolchainVersionRegex )
550*4882a593Smuzhiyun  else()
551*4882a593Smuzhiyun   set( __gcc_toolchain "${__toolchain}" )
552*4882a593Smuzhiyun  endif()
553*4882a593Smuzhiyun  __DETECT_TOOLCHAIN_MACHINE_NAME( __machine "${ANDROID_NDK_TOOLCHAINS_PATH}/${__gcc_toolchain}${__toolchain_subpath}" )
554*4882a593Smuzhiyun  if( __machine )
555*4882a593Smuzhiyun   string( REGEX MATCH "[0-9]+[.][0-9]+([.][0-9x]+)?$" __version "${__gcc_toolchain}" )
556*4882a593Smuzhiyun   if( __machine MATCHES x86_64 )
557*4882a593Smuzhiyun    set( __arch "x86_64" )
558*4882a593Smuzhiyun   elseif( __machine MATCHES i686 )
559*4882a593Smuzhiyun    set( __arch "x86" )
560*4882a593Smuzhiyun   elseif( __machine MATCHES aarch64 )
561*4882a593Smuzhiyun    set( __arch "arm64" )
562*4882a593Smuzhiyun   elseif( __machine MATCHES arm )
563*4882a593Smuzhiyun    set( __arch "arm" )
564*4882a593Smuzhiyun   elseif( __machine MATCHES mips64el )
565*4882a593Smuzhiyun    set( __arch "mips64" )
566*4882a593Smuzhiyun   elseif( __machine MATCHES mipsel )
567*4882a593Smuzhiyun    set( __arch "mips" )
568*4882a593Smuzhiyun   else()
569*4882a593Smuzhiyun    set( __arch "" )
570*4882a593Smuzhiyun   endif()
571*4882a593Smuzhiyun   #message("machine: !${__machine}!\narch: !${__arch}!\nversion: !${__version}!\ntoolchain: !${__toolchain}!\n")
572*4882a593Smuzhiyun   if (__arch)
573*4882a593Smuzhiyun    list( APPEND __availableToolchainMachines "${__machine}" )
574*4882a593Smuzhiyun    list( APPEND __availableToolchainArchs "${__arch}" )
575*4882a593Smuzhiyun    list( APPEND __availableToolchainCompilerVersions "${__version}" )
576*4882a593Smuzhiyun    list( APPEND ${__availableToolchainsVar} "${__toolchain}" )
577*4882a593Smuzhiyun   endif()
578*4882a593Smuzhiyun  endif()
579*4882a593Smuzhiyun  unset( __gcc_toolchain )
580*4882a593Smuzhiyun endforeach()
581*4882a593Smuzhiyunendmacro()
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun# get all the details about NDK
584*4882a593Smuzhiyunif( BUILD_WITH_ANDROID_NDK )
585*4882a593Smuzhiyun file( GLOB ANDROID_SUPPORTED_NATIVE_API_LEVELS RELATIVE "${ANDROID_NDK}/platforms" "${ANDROID_NDK}/platforms/android-*" )
586*4882a593Smuzhiyun string( REPLACE "android-" "" ANDROID_SUPPORTED_NATIVE_API_LEVELS "${ANDROID_SUPPORTED_NATIVE_API_LEVELS}" )
587*4882a593Smuzhiyun set( __availableToolchains "" )
588*4882a593Smuzhiyun set( __availableToolchainMachines "" )
589*4882a593Smuzhiyun set( __availableToolchainArchs "" )
590*4882a593Smuzhiyun set( __availableToolchainCompilerVersions "" )
591*4882a593Smuzhiyun if( ANDROID_TOOLCHAIN_NAME AND EXISTS "${ANDROID_NDK_TOOLCHAINS_PATH}/${ANDROID_TOOLCHAIN_NAME}/" )
592*4882a593Smuzhiyun  # do not go through all toolchains if we know the name
593*4882a593Smuzhiyun  set( __availableToolchainsLst "${ANDROID_TOOLCHAIN_NAME}" )
594*4882a593Smuzhiyun  __GLOB_NDK_TOOLCHAINS( __availableToolchains __availableToolchainsLst "${ANDROID_NDK_TOOLCHAINS_SUBPATH}" )
595*4882a593Smuzhiyun  if( NOT __availableToolchains AND NOT ANDROID_NDK_TOOLCHAINS_SUBPATH STREQUAL ANDROID_NDK_TOOLCHAINS_SUBPATH2 )
596*4882a593Smuzhiyun   __GLOB_NDK_TOOLCHAINS( __availableToolchains __availableToolchainsLst "${ANDROID_NDK_TOOLCHAINS_SUBPATH2}" )
597*4882a593Smuzhiyun   if( __availableToolchains )
598*4882a593Smuzhiyun    set( ANDROID_NDK_TOOLCHAINS_SUBPATH ${ANDROID_NDK_TOOLCHAINS_SUBPATH2} )
599*4882a593Smuzhiyun   endif()
600*4882a593Smuzhiyun  endif()
601*4882a593Smuzhiyun endif()
602*4882a593Smuzhiyun if( NOT __availableToolchains )
603*4882a593Smuzhiyun  file( GLOB __availableToolchainsLst RELATIVE "${ANDROID_NDK_TOOLCHAINS_PATH}" "${ANDROID_NDK_TOOLCHAINS_PATH}/*" )
604*4882a593Smuzhiyun  if( __availableToolchainsLst )
605*4882a593Smuzhiyun   list(SORT __availableToolchainsLst) # we need clang to go after gcc
606*4882a593Smuzhiyun  endif()
607*4882a593Smuzhiyun  __LIST_FILTER( __availableToolchainsLst "^[.]" )
608*4882a593Smuzhiyun  __LIST_FILTER( __availableToolchainsLst "llvm" )
609*4882a593Smuzhiyun  __LIST_FILTER( __availableToolchainsLst "renderscript" )
610*4882a593Smuzhiyun  __GLOB_NDK_TOOLCHAINS( __availableToolchains __availableToolchainsLst "${ANDROID_NDK_TOOLCHAINS_SUBPATH}" )
611*4882a593Smuzhiyun  if( NOT __availableToolchains AND NOT ANDROID_NDK_TOOLCHAINS_SUBPATH STREQUAL ANDROID_NDK_TOOLCHAINS_SUBPATH2 )
612*4882a593Smuzhiyun   __GLOB_NDK_TOOLCHAINS( __availableToolchains __availableToolchainsLst "${ANDROID_NDK_TOOLCHAINS_SUBPATH2}" )
613*4882a593Smuzhiyun   if( __availableToolchains )
614*4882a593Smuzhiyun    set( ANDROID_NDK_TOOLCHAINS_SUBPATH ${ANDROID_NDK_TOOLCHAINS_SUBPATH2} )
615*4882a593Smuzhiyun   endif()
616*4882a593Smuzhiyun  endif()
617*4882a593Smuzhiyun endif()
618*4882a593Smuzhiyun if( NOT __availableToolchains )
619*4882a593Smuzhiyun  message( FATAL_ERROR "Could not find any working toolchain in the NDK. Probably your Android NDK is broken." )
620*4882a593Smuzhiyun endif()
621*4882a593Smuzhiyunendif()
622*4882a593Smuzhiyun
623*4882a593Smuzhiyun# build list of available ABIs
624*4882a593Smuzhiyunset( ANDROID_SUPPORTED_ABIS "" )
625*4882a593Smuzhiyunset( __uniqToolchainArchNames ${__availableToolchainArchs} )
626*4882a593Smuzhiyunlist( REMOVE_DUPLICATES __uniqToolchainArchNames )
627*4882a593Smuzhiyunlist( SORT __uniqToolchainArchNames )
628*4882a593Smuzhiyunforeach( __arch ${__uniqToolchainArchNames} )
629*4882a593Smuzhiyun list( APPEND ANDROID_SUPPORTED_ABIS ${ANDROID_SUPPORTED_ABIS_${__arch}} )
630*4882a593Smuzhiyunendforeach()
631*4882a593Smuzhiyununset( __uniqToolchainArchNames )
632*4882a593Smuzhiyunif( NOT ANDROID_SUPPORTED_ABIS )
633*4882a593Smuzhiyun message( FATAL_ERROR "No one of known Android ABIs is supported by this cmake toolchain." )
634*4882a593Smuzhiyunendif()
635*4882a593Smuzhiyun
636*4882a593Smuzhiyun# choose target ABI
637*4882a593Smuzhiyun__INIT_VARIABLE( ANDROID_ABI VALUES ${ANDROID_SUPPORTED_ABIS} )
638*4882a593Smuzhiyun# verify that target ABI is supported
639*4882a593Smuzhiyunlist( FIND ANDROID_SUPPORTED_ABIS "${ANDROID_ABI}" __androidAbiIdx )
640*4882a593Smuzhiyunif( __androidAbiIdx EQUAL -1 )
641*4882a593Smuzhiyun string( REPLACE ";" "\", \"" PRINTABLE_ANDROID_SUPPORTED_ABIS  "${ANDROID_SUPPORTED_ABIS}" )
642*4882a593Smuzhiyun message( FATAL_ERROR "Specified ANDROID_ABI = \"${ANDROID_ABI}\" is not supported by this cmake toolchain or your NDK/toolchain.
643*4882a593Smuzhiyun   Supported values are: \"${PRINTABLE_ANDROID_SUPPORTED_ABIS}\"
644*4882a593Smuzhiyun   " )
645*4882a593Smuzhiyunendif()
646*4882a593Smuzhiyununset( __androidAbiIdx )
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun# set target ABI options
649*4882a593Smuzhiyunif( ANDROID_ABI STREQUAL "x86" )
650*4882a593Smuzhiyun set( X86 true )
651*4882a593Smuzhiyun set( ANDROID_NDK_ABI_NAME "x86" )
652*4882a593Smuzhiyun set( ANDROID_ARCH_NAME "x86" )
653*4882a593Smuzhiyun set( ANDROID_LLVM_TRIPLE "i686-none-linux-android" )
654*4882a593Smuzhiyun set( CMAKE_SYSTEM_PROCESSOR "i686" )
655*4882a593Smuzhiyunelseif( ANDROID_ABI STREQUAL "x86_64" )
656*4882a593Smuzhiyun set( X86 true )
657*4882a593Smuzhiyun set( X86_64 true )
658*4882a593Smuzhiyun set( ANDROID_NDK_ABI_NAME "x86_64" )
659*4882a593Smuzhiyun set( ANDROID_ARCH_NAME "x86_64" )
660*4882a593Smuzhiyun set( CMAKE_SYSTEM_PROCESSOR "x86_64" )
661*4882a593Smuzhiyun set( ANDROID_LLVM_TRIPLE "x86_64-none-linux-android" )
662*4882a593Smuzhiyunelseif( ANDROID_ABI STREQUAL "mips64" )
663*4882a593Smuzhiyun set( MIPS64 true )
664*4882a593Smuzhiyun set( ANDROID_NDK_ABI_NAME "mips64" )
665*4882a593Smuzhiyun set( ANDROID_ARCH_NAME "mips64" )
666*4882a593Smuzhiyun set( ANDROID_LLVM_TRIPLE "mips64el-none-linux-android" )
667*4882a593Smuzhiyun set( CMAKE_SYSTEM_PROCESSOR "mips64" )
668*4882a593Smuzhiyunelseif( ANDROID_ABI STREQUAL "mips" )
669*4882a593Smuzhiyun set( MIPS true )
670*4882a593Smuzhiyun set( ANDROID_NDK_ABI_NAME "mips" )
671*4882a593Smuzhiyun set( ANDROID_ARCH_NAME "mips" )
672*4882a593Smuzhiyun set( ANDROID_LLVM_TRIPLE "mipsel-none-linux-android" )
673*4882a593Smuzhiyun set( CMAKE_SYSTEM_PROCESSOR "mips" )
674*4882a593Smuzhiyunelseif( ANDROID_ABI STREQUAL "arm64-v8a" )
675*4882a593Smuzhiyun set( ARM64_V8A true )
676*4882a593Smuzhiyun set( ANDROID_NDK_ABI_NAME "arm64-v8a" )
677*4882a593Smuzhiyun set( ANDROID_ARCH_NAME "arm64" )
678*4882a593Smuzhiyun set( ANDROID_LLVM_TRIPLE "aarch64-none-linux-android" )
679*4882a593Smuzhiyun set( CMAKE_SYSTEM_PROCESSOR "aarch64" )
680*4882a593Smuzhiyun set( VFPV3 true )
681*4882a593Smuzhiyun set( NEON true )
682*4882a593Smuzhiyunelseif( ANDROID_ABI STREQUAL "armeabi" )
683*4882a593Smuzhiyun set( ARMEABI true )
684*4882a593Smuzhiyun set( ANDROID_NDK_ABI_NAME "armeabi" )
685*4882a593Smuzhiyun set( ANDROID_ARCH_NAME "arm" )
686*4882a593Smuzhiyun set( ANDROID_LLVM_TRIPLE "armv5te-none-linux-androideabi" )
687*4882a593Smuzhiyun set( CMAKE_SYSTEM_PROCESSOR "armv5te" )
688*4882a593Smuzhiyunelseif( ANDROID_ABI STREQUAL "armeabi-v6 with VFP" )
689*4882a593Smuzhiyun set( ARMEABI_V6 true )
690*4882a593Smuzhiyun set( ANDROID_NDK_ABI_NAME "armeabi" )
691*4882a593Smuzhiyun set( ANDROID_ARCH_NAME "arm" )
692*4882a593Smuzhiyun set( ANDROID_LLVM_TRIPLE "armv5te-none-linux-androideabi" )
693*4882a593Smuzhiyun set( CMAKE_SYSTEM_PROCESSOR "armv6" )
694*4882a593Smuzhiyun # need always fallback to older platform
695*4882a593Smuzhiyun set( ARMEABI true )
696*4882a593Smuzhiyunelseif( ANDROID_ABI STREQUAL "armeabi-v7a")
697*4882a593Smuzhiyun set( ARMEABI_V7A true )
698*4882a593Smuzhiyun set( ANDROID_NDK_ABI_NAME "armeabi-v7a" )
699*4882a593Smuzhiyun set( ANDROID_ARCH_NAME "arm" )
700*4882a593Smuzhiyun set( ANDROID_LLVM_TRIPLE "armv7-none-linux-androideabi" )
701*4882a593Smuzhiyun set( CMAKE_SYSTEM_PROCESSOR "armv7-a" )
702*4882a593Smuzhiyunelseif( ANDROID_ABI STREQUAL "armeabi-v7a with VFPV3" )
703*4882a593Smuzhiyun set( ARMEABI_V7A true )
704*4882a593Smuzhiyun set( ANDROID_NDK_ABI_NAME "armeabi-v7a" )
705*4882a593Smuzhiyun set( ANDROID_ARCH_NAME "arm" )
706*4882a593Smuzhiyun set( ANDROID_LLVM_TRIPLE "armv7-none-linux-androideabi" )
707*4882a593Smuzhiyun set( CMAKE_SYSTEM_PROCESSOR "armv7-a" )
708*4882a593Smuzhiyun set( VFPV3 true )
709*4882a593Smuzhiyunelseif( ANDROID_ABI STREQUAL "armeabi-v7a with NEON" )
710*4882a593Smuzhiyun set( ARMEABI_V7A true )
711*4882a593Smuzhiyun set( ANDROID_NDK_ABI_NAME "armeabi-v7a" )
712*4882a593Smuzhiyun set( ANDROID_ARCH_NAME "arm" )
713*4882a593Smuzhiyun set( ANDROID_LLVM_TRIPLE "armv7-none-linux-androideabi" )
714*4882a593Smuzhiyun set( CMAKE_SYSTEM_PROCESSOR "armv7-a" )
715*4882a593Smuzhiyun set( VFPV3 true )
716*4882a593Smuzhiyun set( NEON true )
717*4882a593Smuzhiyunelse()
718*4882a593Smuzhiyun message( SEND_ERROR "Unknown ANDROID_ABI=\"${ANDROID_ABI}\" is specified." )
719*4882a593Smuzhiyunendif()
720*4882a593Smuzhiyun
721*4882a593Smuzhiyunif( CMAKE_BINARY_DIR AND EXISTS "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeSystem.cmake" )
722*4882a593Smuzhiyun # really dirty hack
723*4882a593Smuzhiyun # it is not possible to change CMAKE_SYSTEM_PROCESSOR after the first run...
724*4882a593Smuzhiyun file( APPEND "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeSystem.cmake" "SET(CMAKE_SYSTEM_PROCESSOR \"${CMAKE_SYSTEM_PROCESSOR}\")\n" )
725*4882a593Smuzhiyunendif()
726*4882a593Smuzhiyun
727*4882a593Smuzhiyunif( ANDROID_ARCH_NAME STREQUAL "arm" AND NOT ARMEABI_V6 )
728*4882a593Smuzhiyun __INIT_VARIABLE( ANDROID_FORCE_ARM_BUILD VALUES OFF )
729*4882a593Smuzhiyun set( ANDROID_FORCE_ARM_BUILD ${ANDROID_FORCE_ARM_BUILD} CACHE BOOL "Use 32-bit ARM instructions instead of Thumb-1" FORCE )
730*4882a593Smuzhiyun mark_as_advanced( ANDROID_FORCE_ARM_BUILD )
731*4882a593Smuzhiyunelse()
732*4882a593Smuzhiyun unset( ANDROID_FORCE_ARM_BUILD CACHE )
733*4882a593Smuzhiyunendif()
734*4882a593Smuzhiyun
735*4882a593Smuzhiyun# choose toolchain
736*4882a593Smuzhiyunif( ANDROID_TOOLCHAIN_NAME )
737*4882a593Smuzhiyun list( FIND __availableToolchains "${ANDROID_TOOLCHAIN_NAME}" __toolchainIdx )
738*4882a593Smuzhiyun if( __toolchainIdx EQUAL -1 )
739*4882a593Smuzhiyun  list( SORT __availableToolchains )
740*4882a593Smuzhiyun  string( REPLACE ";" "\n  * " toolchains_list "${__availableToolchains}" )
741*4882a593Smuzhiyun  set( toolchains_list "  * ${toolchains_list}")
742*4882a593Smuzhiyun  message( FATAL_ERROR "Specified toolchain \"${ANDROID_TOOLCHAIN_NAME}\" is missing in your NDK or broken. Please verify that your NDK is working or select another compiler toolchain.
743*4882a593SmuzhiyunTo configure the toolchain set CMake variable ANDROID_TOOLCHAIN_NAME to one of the following values:\n${toolchains_list}\n" )
744*4882a593Smuzhiyun endif()
745*4882a593Smuzhiyun list( GET __availableToolchainArchs ${__toolchainIdx} __toolchainArch )
746*4882a593Smuzhiyun if( NOT __toolchainArch STREQUAL ANDROID_ARCH_NAME )
747*4882a593Smuzhiyun  message( SEND_ERROR "Selected toolchain \"${ANDROID_TOOLCHAIN_NAME}\" is not able to compile binaries for the \"${ANDROID_ARCH_NAME}\" platform." )
748*4882a593Smuzhiyun endif()
749*4882a593Smuzhiyunelse()
750*4882a593Smuzhiyun set( __toolchainIdx -1 )
751*4882a593Smuzhiyun set( __applicableToolchains "" )
752*4882a593Smuzhiyun set( __toolchainMaxVersion "0.0.0" )
753*4882a593Smuzhiyun list( LENGTH __availableToolchains __availableToolchainsCount )
754*4882a593Smuzhiyun math( EXPR __availableToolchainsCount "${__availableToolchainsCount}-1" )
755*4882a593Smuzhiyun foreach( __idx RANGE ${__availableToolchainsCount} )
756*4882a593Smuzhiyun  list( GET __availableToolchainArchs ${__idx} __toolchainArch )
757*4882a593Smuzhiyun  if( __toolchainArch STREQUAL ANDROID_ARCH_NAME )
758*4882a593Smuzhiyun   list( GET __availableToolchainCompilerVersions ${__idx} __toolchainVersion )
759*4882a593Smuzhiyun   string( REPLACE "x" "99" __toolchainVersion "${__toolchainVersion}")
760*4882a593Smuzhiyun   if( __toolchainVersion VERSION_GREATER __toolchainMaxVersion )
761*4882a593Smuzhiyun    set( __toolchainMaxVersion "${__toolchainVersion}" )
762*4882a593Smuzhiyun    set( __toolchainIdx ${__idx} )
763*4882a593Smuzhiyun   endif()
764*4882a593Smuzhiyun  endif()
765*4882a593Smuzhiyun endforeach()
766*4882a593Smuzhiyun unset( __availableToolchainsCount )
767*4882a593Smuzhiyun unset( __toolchainMaxVersion )
768*4882a593Smuzhiyun unset( __toolchainVersion )
769*4882a593Smuzhiyunendif()
770*4882a593Smuzhiyununset( __toolchainArch )
771*4882a593Smuzhiyunif( __toolchainIdx EQUAL -1 )
772*4882a593Smuzhiyun message( FATAL_ERROR "No one of available compiler toolchains is able to compile for ${ANDROID_ARCH_NAME} platform." )
773*4882a593Smuzhiyunendif()
774*4882a593Smuzhiyunlist( GET __availableToolchains ${__toolchainIdx} ANDROID_TOOLCHAIN_NAME )
775*4882a593Smuzhiyunlist( GET __availableToolchainMachines ${__toolchainIdx} ANDROID_TOOLCHAIN_MACHINE_NAME )
776*4882a593Smuzhiyunlist( GET __availableToolchainCompilerVersions ${__toolchainIdx} ANDROID_COMPILER_VERSION )
777*4882a593Smuzhiyun
778*4882a593Smuzhiyununset( __toolchainIdx )
779*4882a593Smuzhiyununset( __availableToolchains )
780*4882a593Smuzhiyununset( __availableToolchainMachines )
781*4882a593Smuzhiyununset( __availableToolchainArchs )
782*4882a593Smuzhiyununset( __availableToolchainCompilerVersions )
783*4882a593Smuzhiyun
784*4882a593Smuzhiyun# choose native API level
785*4882a593Smuzhiyun__INIT_VARIABLE( ANDROID_NATIVE_API_LEVEL ENV_ANDROID_NATIVE_API_LEVEL ANDROID_API_LEVEL ENV_ANDROID_API_LEVEL ANDROID_STANDALONE_TOOLCHAIN_API_LEVEL ANDROID_DEFAULT_NDK_API_LEVEL_${ANDROID_ARCH_NAME} ANDROID_DEFAULT_NDK_API_LEVEL )
786*4882a593Smuzhiyunstring( REPLACE "android-" "" ANDROID_NATIVE_API_LEVEL "${ANDROID_NATIVE_API_LEVEL}" )
787*4882a593Smuzhiyunstring( STRIP "${ANDROID_NATIVE_API_LEVEL}" ANDROID_NATIVE_API_LEVEL )
788*4882a593Smuzhiyun# adjust API level
789*4882a593Smuzhiyunset( __real_api_level ${ANDROID_DEFAULT_NDK_API_LEVEL_${ANDROID_ARCH_NAME}} )
790*4882a593Smuzhiyunforeach( __level ${ANDROID_SUPPORTED_NATIVE_API_LEVELS} )
791*4882a593Smuzhiyun if( (__level LESS ANDROID_NATIVE_API_LEVEL OR __level STREQUAL ANDROID_NATIVE_API_LEVEL) AND NOT __level LESS __real_api_level )
792*4882a593Smuzhiyun  set( __real_api_level ${__level} )
793*4882a593Smuzhiyun endif()
794*4882a593Smuzhiyunendforeach()
795*4882a593Smuzhiyunif( __real_api_level AND NOT ANDROID_NATIVE_API_LEVEL STREQUAL __real_api_level )
796*4882a593Smuzhiyun message( STATUS "Adjusting Android API level 'android-${ANDROID_NATIVE_API_LEVEL}' to 'android-${__real_api_level}'")
797*4882a593Smuzhiyun set( ANDROID_NATIVE_API_LEVEL ${__real_api_level} )
798*4882a593Smuzhiyunendif()
799*4882a593Smuzhiyununset(__real_api_level)
800*4882a593Smuzhiyun# validate
801*4882a593Smuzhiyunlist( FIND ANDROID_SUPPORTED_NATIVE_API_LEVELS "${ANDROID_NATIVE_API_LEVEL}" __levelIdx )
802*4882a593Smuzhiyunif( __levelIdx EQUAL -1 )
803*4882a593Smuzhiyun message( SEND_ERROR "Specified Android native API level 'android-${ANDROID_NATIVE_API_LEVEL}' is not supported by your NDK/toolchain." )
804*4882a593Smuzhiyunelse()
805*4882a593Smuzhiyun if( BUILD_WITH_ANDROID_NDK )
806*4882a593Smuzhiyun  __DETECT_NATIVE_API_LEVEL( __realApiLevel "${ANDROID_NDK}/platforms/android-${ANDROID_NATIVE_API_LEVEL}/arch-${ANDROID_ARCH_NAME}/usr/include/android/api-level.h" )
807*4882a593Smuzhiyun  if( NOT __realApiLevel EQUAL ANDROID_NATIVE_API_LEVEL AND NOT __realApiLevel GREATER 9000 )
808*4882a593Smuzhiyun   message( SEND_ERROR "Specified Android API level (${ANDROID_NATIVE_API_LEVEL}) does not match to the level found (${__realApiLevel}). Probably your copy of NDK is broken." )
809*4882a593Smuzhiyun  endif()
810*4882a593Smuzhiyun  unset( __realApiLevel )
811*4882a593Smuzhiyun endif()
812*4882a593Smuzhiyun set( ANDROID_NATIVE_API_LEVEL "${ANDROID_NATIVE_API_LEVEL}" CACHE STRING "Android API level for native code" FORCE )
813*4882a593Smuzhiyun set( CMAKE_ANDROID_API ${ANDROID_NATIVE_API_LEVEL} )
814*4882a593Smuzhiyun if( CMAKE_VERSION VERSION_GREATER "2.8" )
815*4882a593Smuzhiyun  list( SORT ANDROID_SUPPORTED_NATIVE_API_LEVELS )
816*4882a593Smuzhiyun  set_property( CACHE ANDROID_NATIVE_API_LEVEL PROPERTY STRINGS ${ANDROID_SUPPORTED_NATIVE_API_LEVELS} )
817*4882a593Smuzhiyun endif()
818*4882a593Smuzhiyunendif()
819*4882a593Smuzhiyununset( __levelIdx )
820*4882a593Smuzhiyun
821*4882a593Smuzhiyun
822*4882a593Smuzhiyun# remember target ABI
823*4882a593Smuzhiyunset( ANDROID_ABI "${ANDROID_ABI}" CACHE STRING "The target ABI for Android. If arm, then armeabi-v7a is recommended for hardware floating point." FORCE )
824*4882a593Smuzhiyunif( CMAKE_VERSION VERSION_GREATER "2.8" )
825*4882a593Smuzhiyun list( SORT ANDROID_SUPPORTED_ABIS_${ANDROID_ARCH_NAME} )
826*4882a593Smuzhiyun set_property( CACHE ANDROID_ABI PROPERTY STRINGS ${ANDROID_SUPPORTED_ABIS_${ANDROID_ARCH_NAME}} )
827*4882a593Smuzhiyunendif()
828*4882a593Smuzhiyun
829*4882a593Smuzhiyun
830*4882a593Smuzhiyun# runtime choice (STL, rtti, exceptions)
831*4882a593Smuzhiyunif( NOT ANDROID_STL )
832*4882a593Smuzhiyun  set( ANDROID_STL gnustl_static )
833*4882a593Smuzhiyunendif()
834*4882a593Smuzhiyunset( ANDROID_STL "${ANDROID_STL}" CACHE STRING "C++ runtime" )
835*4882a593Smuzhiyunset( ANDROID_STL_FORCE_FEATURES ON CACHE BOOL "automatically configure rtti and exceptions support based on C++ runtime" )
836*4882a593Smuzhiyunmark_as_advanced( ANDROID_STL ANDROID_STL_FORCE_FEATURES )
837*4882a593Smuzhiyun
838*4882a593Smuzhiyunif( BUILD_WITH_ANDROID_NDK )
839*4882a593Smuzhiyun if( NOT "${ANDROID_STL}" MATCHES "^(none|system|system_re|gabi\\+\\+_static|gabi\\+\\+_shared|stlport_static|stlport_shared|gnustl_static|gnustl_shared)$")
840*4882a593Smuzhiyun  message( FATAL_ERROR "ANDROID_STL is set to invalid value \"${ANDROID_STL}\".
841*4882a593SmuzhiyunThe possible values are:
842*4882a593Smuzhiyun  none           -> Do not configure the runtime.
843*4882a593Smuzhiyun  system         -> Use the default minimal system C++ runtime library.
844*4882a593Smuzhiyun  system_re      -> Same as system but with rtti and exceptions.
845*4882a593Smuzhiyun  gabi++_static  -> Use the GAbi++ runtime as a static library.
846*4882a593Smuzhiyun  gabi++_shared  -> Use the GAbi++ runtime as a shared library.
847*4882a593Smuzhiyun  stlport_static -> Use the STLport runtime as a static library.
848*4882a593Smuzhiyun  stlport_shared -> Use the STLport runtime as a shared library.
849*4882a593Smuzhiyun  gnustl_static  -> (default) Use the GNU STL as a static library.
850*4882a593Smuzhiyun  gnustl_shared  -> Use the GNU STL as a shared library.
851*4882a593Smuzhiyun" )
852*4882a593Smuzhiyun endif()
853*4882a593Smuzhiyunelseif( BUILD_WITH_STANDALONE_TOOLCHAIN )
854*4882a593Smuzhiyun if( NOT "${ANDROID_STL}" MATCHES "^(none|gnustl_static|gnustl_shared)$")
855*4882a593Smuzhiyun  message( FATAL_ERROR "ANDROID_STL is set to invalid value \"${ANDROID_STL}\".
856*4882a593SmuzhiyunThe possible values are:
857*4882a593Smuzhiyun  none           -> Do not configure the runtime.
858*4882a593Smuzhiyun  gnustl_static  -> (default) Use the GNU STL as a static library.
859*4882a593Smuzhiyun  gnustl_shared  -> Use the GNU STL as a shared library.
860*4882a593Smuzhiyun" )
861*4882a593Smuzhiyun endif()
862*4882a593Smuzhiyunendif()
863*4882a593Smuzhiyun
864*4882a593Smuzhiyununset( ANDROID_RTTI )
865*4882a593Smuzhiyununset( ANDROID_EXCEPTIONS )
866*4882a593Smuzhiyununset( ANDROID_STL_INCLUDE_DIRS )
867*4882a593Smuzhiyununset( __libstl )
868*4882a593Smuzhiyununset( __libsupcxx )
869*4882a593Smuzhiyun
870*4882a593Smuzhiyunif( NOT _CMAKE_IN_TRY_COMPILE AND ANDROID_NDK_RELEASE STREQUAL "r7b" AND ARMEABI_V7A AND NOT VFPV3 AND ANDROID_STL MATCHES "gnustl" )
871*4882a593Smuzhiyun message( WARNING  "The GNU STL armeabi-v7a binaries from NDK r7b can crash non-NEON devices. The files provided with NDK r7b were not configured properly, resulting in crashes on Tegra2-based devices and others when trying to use certain floating-point functions (e.g., cosf, sinf, expf).
872*4882a593SmuzhiyunYou are strongly recommended to switch to another NDK release.
873*4882a593Smuzhiyun" )
874*4882a593Smuzhiyunendif()
875*4882a593Smuzhiyun
876*4882a593Smuzhiyunif( NOT _CMAKE_IN_TRY_COMPILE AND X86 AND ANDROID_STL MATCHES "gnustl" AND ANDROID_NDK_RELEASE STREQUAL "r6" )
877*4882a593Smuzhiyun  message( WARNING  "The x86 system header file from NDK r6 has incorrect definition for ptrdiff_t. You are recommended to upgrade to a newer NDK release or manually patch the header:
878*4882a593SmuzhiyunSee https://android.googlesource.com/platform/development.git f907f4f9d4e56ccc8093df6fee54454b8bcab6c2
879*4882a593Smuzhiyun  diff --git a/ndk/platforms/android-9/arch-x86/include/machine/_types.h b/ndk/platforms/android-9/arch-x86/include/machine/_types.h
880*4882a593Smuzhiyun  index 5e28c64..65892a1 100644
881*4882a593Smuzhiyun  --- a/ndk/platforms/android-9/arch-x86/include/machine/_types.h
882*4882a593Smuzhiyun  +++ b/ndk/platforms/android-9/arch-x86/include/machine/_types.h
883*4882a593Smuzhiyun  @@ -51,7 +51,11 @@ typedef long int       ssize_t;
884*4882a593Smuzhiyun   #endif
885*4882a593Smuzhiyun   #ifndef _PTRDIFF_T
886*4882a593Smuzhiyun   #define _PTRDIFF_T
887*4882a593Smuzhiyun  -typedef long           ptrdiff_t;
888*4882a593Smuzhiyun  +#  ifdef __ANDROID__
889*4882a593Smuzhiyun  +     typedef int            ptrdiff_t;
890*4882a593Smuzhiyun  +#  else
891*4882a593Smuzhiyun  +     typedef long           ptrdiff_t;
892*4882a593Smuzhiyun  +#  endif
893*4882a593Smuzhiyun   #endif
894*4882a593Smuzhiyun" )
895*4882a593Smuzhiyunendif()
896*4882a593Smuzhiyun
897*4882a593Smuzhiyun
898*4882a593Smuzhiyun# setup paths and STL for standalone toolchain
899*4882a593Smuzhiyunif( BUILD_WITH_STANDALONE_TOOLCHAIN )
900*4882a593Smuzhiyun set( ANDROID_TOOLCHAIN_ROOT "${ANDROID_STANDALONE_TOOLCHAIN}" )
901*4882a593Smuzhiyun set( ANDROID_CLANG_TOOLCHAIN_ROOT "${ANDROID_STANDALONE_TOOLCHAIN}" )
902*4882a593Smuzhiyun set( ANDROID_SYSROOT "${ANDROID_STANDALONE_TOOLCHAIN}/sysroot" )
903*4882a593Smuzhiyun
904*4882a593Smuzhiyun if( NOT ANDROID_STL STREQUAL "none" )
905*4882a593Smuzhiyun  set( ANDROID_STL_INCLUDE_DIRS "${ANDROID_STANDALONE_TOOLCHAIN}/include/c++/${ANDROID_COMPILER_VERSION}" )
906*4882a593Smuzhiyun  if( NOT EXISTS "${ANDROID_STL_INCLUDE_DIRS}" )
907*4882a593Smuzhiyun   # old location ( pre r8c )
908*4882a593Smuzhiyun   set( ANDROID_STL_INCLUDE_DIRS "${ANDROID_STANDALONE_TOOLCHAIN}/${ANDROID_TOOLCHAIN_MACHINE_NAME}/include/c++/${ANDROID_COMPILER_VERSION}" )
909*4882a593Smuzhiyun  endif()
910*4882a593Smuzhiyun  if( ARMEABI_V7A AND EXISTS "${ANDROID_STL_INCLUDE_DIRS}/${ANDROID_TOOLCHAIN_MACHINE_NAME}/${CMAKE_SYSTEM_PROCESSOR}/bits" )
911*4882a593Smuzhiyun   list( APPEND ANDROID_STL_INCLUDE_DIRS "${ANDROID_STL_INCLUDE_DIRS}/${ANDROID_TOOLCHAIN_MACHINE_NAME}/${CMAKE_SYSTEM_PROCESSOR}" )
912*4882a593Smuzhiyun  elseif( ARMEABI AND NOT ANDROID_FORCE_ARM_BUILD AND EXISTS "${ANDROID_STL_INCLUDE_DIRS}/${ANDROID_TOOLCHAIN_MACHINE_NAME}/thumb/bits" )
913*4882a593Smuzhiyun   list( APPEND ANDROID_STL_INCLUDE_DIRS "${ANDROID_STL_INCLUDE_DIRS}/${ANDROID_TOOLCHAIN_MACHINE_NAME}/thumb" )
914*4882a593Smuzhiyun  else()
915*4882a593Smuzhiyun   list( APPEND ANDROID_STL_INCLUDE_DIRS "${ANDROID_STL_INCLUDE_DIRS}/${ANDROID_TOOLCHAIN_MACHINE_NAME}" )
916*4882a593Smuzhiyun  endif()
917*4882a593Smuzhiyun  # always search static GNU STL to get the location of libsupc++.a
918*4882a593Smuzhiyun  if( ARMEABI_V7A AND NOT ANDROID_FORCE_ARM_BUILD AND EXISTS "${ANDROID_STANDALONE_TOOLCHAIN}/${ANDROID_TOOLCHAIN_MACHINE_NAME}/lib/${CMAKE_SYSTEM_PROCESSOR}/thumb/libstdc++.a" )
919*4882a593Smuzhiyun   set( __libstl "${ANDROID_STANDALONE_TOOLCHAIN}/${ANDROID_TOOLCHAIN_MACHINE_NAME}/lib/${CMAKE_SYSTEM_PROCESSOR}/thumb" )
920*4882a593Smuzhiyun  elseif( ARMEABI_V7A AND EXISTS "${ANDROID_STANDALONE_TOOLCHAIN}/${ANDROID_TOOLCHAIN_MACHINE_NAME}/lib/${CMAKE_SYSTEM_PROCESSOR}/libstdc++.a" )
921*4882a593Smuzhiyun   set( __libstl "${ANDROID_STANDALONE_TOOLCHAIN}/${ANDROID_TOOLCHAIN_MACHINE_NAME}/lib/${CMAKE_SYSTEM_PROCESSOR}" )
922*4882a593Smuzhiyun  elseif( ARMEABI AND NOT ANDROID_FORCE_ARM_BUILD AND EXISTS "${ANDROID_STANDALONE_TOOLCHAIN}/${ANDROID_TOOLCHAIN_MACHINE_NAME}/lib/thumb/libstdc++.a" )
923*4882a593Smuzhiyun   set( __libstl "${ANDROID_STANDALONE_TOOLCHAIN}/${ANDROID_TOOLCHAIN_MACHINE_NAME}/lib/thumb" )
924*4882a593Smuzhiyun  elseif( EXISTS "${ANDROID_STANDALONE_TOOLCHAIN}/${ANDROID_TOOLCHAIN_MACHINE_NAME}/lib/libstdc++.a" )
925*4882a593Smuzhiyun   set( __libstl "${ANDROID_STANDALONE_TOOLCHAIN}/${ANDROID_TOOLCHAIN_MACHINE_NAME}/lib" )
926*4882a593Smuzhiyun  endif()
927*4882a593Smuzhiyun  if( __libstl )
928*4882a593Smuzhiyun   set( __libsupcxx "${__libstl}/libsupc++.a" )
929*4882a593Smuzhiyun   set( __libstl    "${__libstl}/libstdc++.a" )
930*4882a593Smuzhiyun  endif()
931*4882a593Smuzhiyun  if( NOT EXISTS "${__libsupcxx}" )
932*4882a593Smuzhiyun   message( FATAL_ERROR "The required libstdsupc++.a is missing in your standalone toolchain.
933*4882a593Smuzhiyun Usually it happens because of bug in make-standalone-toolchain.sh script from NDK r7, r7b and r7c.
934*4882a593Smuzhiyun You need to either upgrade to newer NDK or manually copy
935*4882a593Smuzhiyun     $ANDROID_NDK/sources/cxx-stl/gnu-libstdc++/libs/${ANDROID_NDK_ABI_NAME}/libsupc++.a
936*4882a593Smuzhiyun to
937*4882a593Smuzhiyun     ${__libsupcxx}
938*4882a593Smuzhiyun   " )
939*4882a593Smuzhiyun  endif()
940*4882a593Smuzhiyun  if( ANDROID_STL STREQUAL "gnustl_shared" )
941*4882a593Smuzhiyun   if( ARMEABI_V7A AND EXISTS "${ANDROID_STANDALONE_TOOLCHAIN}/${ANDROID_TOOLCHAIN_MACHINE_NAME}/lib/${CMAKE_SYSTEM_PROCESSOR}/libgnustl_shared.so" )
942*4882a593Smuzhiyun    set( __libstl "${ANDROID_STANDALONE_TOOLCHAIN}/${ANDROID_TOOLCHAIN_MACHINE_NAME}/lib/${CMAKE_SYSTEM_PROCESSOR}/libgnustl_shared.so" )
943*4882a593Smuzhiyun   elseif( ARMEABI AND NOT ANDROID_FORCE_ARM_BUILD AND EXISTS "${ANDROID_STANDALONE_TOOLCHAIN}/${ANDROID_TOOLCHAIN_MACHINE_NAME}/lib/thumb/libgnustl_shared.so" )
944*4882a593Smuzhiyun    set( __libstl "${ANDROID_STANDALONE_TOOLCHAIN}/${ANDROID_TOOLCHAIN_MACHINE_NAME}/lib/thumb/libgnustl_shared.so" )
945*4882a593Smuzhiyun   elseif( EXISTS "${ANDROID_STANDALONE_TOOLCHAIN}/${ANDROID_TOOLCHAIN_MACHINE_NAME}/lib/libgnustl_shared.so" )
946*4882a593Smuzhiyun    set( __libstl "${ANDROID_STANDALONE_TOOLCHAIN}/${ANDROID_TOOLCHAIN_MACHINE_NAME}/lib/libgnustl_shared.so" )
947*4882a593Smuzhiyun   endif()
948*4882a593Smuzhiyun  endif()
949*4882a593Smuzhiyun endif()
950*4882a593Smuzhiyunendif()
951*4882a593Smuzhiyun
952*4882a593Smuzhiyun# clang
953*4882a593Smuzhiyunif( "${ANDROID_TOOLCHAIN_NAME}" STREQUAL "standalone-clang" )
954*4882a593Smuzhiyun set( ANDROID_COMPILER_IS_CLANG 1 )
955*4882a593Smuzhiyun execute_process( COMMAND "${ANDROID_CLANG_TOOLCHAIN_ROOT}/bin/clang${TOOL_OS_SUFFIX}" --version OUTPUT_VARIABLE ANDROID_CLANG_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE )
956*4882a593Smuzhiyun string( REGEX MATCH "[0-9]+[.][0-9]+" ANDROID_CLANG_VERSION "${ANDROID_CLANG_VERSION}")
957*4882a593Smuzhiyunelseif( "${ANDROID_TOOLCHAIN_NAME}" MATCHES "-clang3[.][0-9]?$" )
958*4882a593Smuzhiyun string( REGEX MATCH "3[.][0-9]$" ANDROID_CLANG_VERSION "${ANDROID_TOOLCHAIN_NAME}")
959*4882a593Smuzhiyun string( REGEX REPLACE "-clang${ANDROID_CLANG_VERSION}$" "-${ANDROID_COMPILER_VERSION}" ANDROID_GCC_TOOLCHAIN_NAME "${ANDROID_TOOLCHAIN_NAME}" )
960*4882a593Smuzhiyun if( NOT EXISTS "${ANDROID_NDK_TOOLCHAINS_PATH}/llvm-${ANDROID_CLANG_VERSION}${ANDROID_NDK_TOOLCHAINS_SUBPATH}/bin/clang${TOOL_OS_SUFFIX}" )
961*4882a593Smuzhiyun  message( FATAL_ERROR "Could not find the Clang compiler driver" )
962*4882a593Smuzhiyun endif()
963*4882a593Smuzhiyun set( ANDROID_COMPILER_IS_CLANG 1 )
964*4882a593Smuzhiyun set( ANDROID_CLANG_TOOLCHAIN_ROOT "${ANDROID_NDK_TOOLCHAINS_PATH}/llvm-${ANDROID_CLANG_VERSION}${ANDROID_NDK_TOOLCHAINS_SUBPATH}" )
965*4882a593Smuzhiyunelse()
966*4882a593Smuzhiyun set( ANDROID_GCC_TOOLCHAIN_NAME "${ANDROID_TOOLCHAIN_NAME}" )
967*4882a593Smuzhiyun unset( ANDROID_COMPILER_IS_CLANG CACHE )
968*4882a593Smuzhiyunendif()
969*4882a593Smuzhiyun
970*4882a593Smuzhiyunstring( REPLACE "." "" _clang_name "clang${ANDROID_CLANG_VERSION}" )
971*4882a593Smuzhiyunif( NOT EXISTS "${ANDROID_CLANG_TOOLCHAIN_ROOT}/bin/${_clang_name}${TOOL_OS_SUFFIX}" )
972*4882a593Smuzhiyun set( _clang_name "clang" )
973*4882a593Smuzhiyunendif()
974*4882a593Smuzhiyun
975*4882a593Smuzhiyun
976*4882a593Smuzhiyun# setup paths and STL for NDK
977*4882a593Smuzhiyunif( BUILD_WITH_ANDROID_NDK )
978*4882a593Smuzhiyun set( ANDROID_TOOLCHAIN_ROOT "${ANDROID_NDK_TOOLCHAINS_PATH}/${ANDROID_GCC_TOOLCHAIN_NAME}${ANDROID_NDK_TOOLCHAINS_SUBPATH}" )
979*4882a593Smuzhiyun set( ANDROID_SYSROOT "${ANDROID_NDK}/platforms/android-${ANDROID_NATIVE_API_LEVEL}/arch-${ANDROID_ARCH_NAME}" )
980*4882a593Smuzhiyun
981*4882a593Smuzhiyun if( ANDROID_STL STREQUAL "none" )
982*4882a593Smuzhiyun  # do nothing
983*4882a593Smuzhiyun elseif( ANDROID_STL STREQUAL "system" )
984*4882a593Smuzhiyun  set( ANDROID_RTTI             OFF )
985*4882a593Smuzhiyun  set( ANDROID_EXCEPTIONS       OFF )
986*4882a593Smuzhiyun  set( ANDROID_STL_INCLUDE_DIRS "${ANDROID_NDK}/sources/cxx-stl/system/include" )
987*4882a593Smuzhiyun elseif( ANDROID_STL STREQUAL "system_re" )
988*4882a593Smuzhiyun  set( ANDROID_RTTI             ON )
989*4882a593Smuzhiyun  set( ANDROID_EXCEPTIONS       ON )
990*4882a593Smuzhiyun  set( ANDROID_STL_INCLUDE_DIRS "${ANDROID_NDK}/sources/cxx-stl/system/include" )
991*4882a593Smuzhiyun elseif( ANDROID_STL MATCHES "gabi" )
992*4882a593Smuzhiyun  if( ANDROID_NDK_RELEASE_NUM LESS 7000 ) # before r7
993*4882a593Smuzhiyun   message( FATAL_ERROR "gabi++ is not available in your NDK. You have to upgrade to NDK r7 or newer to use gabi++.")
994*4882a593Smuzhiyun  endif()
995*4882a593Smuzhiyun  set( ANDROID_RTTI             ON )
996*4882a593Smuzhiyun  set( ANDROID_EXCEPTIONS       OFF )
997*4882a593Smuzhiyun  set( ANDROID_STL_INCLUDE_DIRS "${ANDROID_NDK}/sources/cxx-stl/gabi++/include" )
998*4882a593Smuzhiyun  set( __libstl                 "${ANDROID_NDK}/sources/cxx-stl/gabi++/libs/${ANDROID_NDK_ABI_NAME}/libgabi++_static.a" )
999*4882a593Smuzhiyun elseif( ANDROID_STL MATCHES "stlport" )
1000*4882a593Smuzhiyun  if( NOT ANDROID_NDK_RELEASE_NUM LESS 8004 ) # before r8d
1001*4882a593Smuzhiyun   set( ANDROID_EXCEPTIONS       ON )
1002*4882a593Smuzhiyun  else()
1003*4882a593Smuzhiyun   set( ANDROID_EXCEPTIONS       OFF )
1004*4882a593Smuzhiyun  endif()
1005*4882a593Smuzhiyun  if( ANDROID_NDK_RELEASE_NUM LESS 7000 ) # before r7
1006*4882a593Smuzhiyun   set( ANDROID_RTTI            OFF )
1007*4882a593Smuzhiyun  else()
1008*4882a593Smuzhiyun   set( ANDROID_RTTI            ON )
1009*4882a593Smuzhiyun  endif()
1010*4882a593Smuzhiyun  set( ANDROID_STL_INCLUDE_DIRS "${ANDROID_NDK}/sources/cxx-stl/stlport/stlport" )
1011*4882a593Smuzhiyun  set( __libstl                 "${ANDROID_NDK}/sources/cxx-stl/stlport/libs/${ANDROID_NDK_ABI_NAME}/libstlport_static.a" )
1012*4882a593Smuzhiyun elseif( ANDROID_STL MATCHES "gnustl" )
1013*4882a593Smuzhiyun  set( ANDROID_EXCEPTIONS       ON )
1014*4882a593Smuzhiyun  set( ANDROID_RTTI             ON )
1015*4882a593Smuzhiyun  if( EXISTS "${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/${ANDROID_COMPILER_VERSION}" )
1016*4882a593Smuzhiyun   if( ARMEABI_V7A AND ANDROID_COMPILER_VERSION VERSION_EQUAL "4.7" AND ANDROID_NDK_RELEASE STREQUAL "r8d" )
1017*4882a593Smuzhiyun    # gnustl binary for 4.7 compiler is buggy :(
1018*4882a593Smuzhiyun    # TODO: look for right fix
1019*4882a593Smuzhiyun    set( __libstl                "${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/4.6" )
1020*4882a593Smuzhiyun   else()
1021*4882a593Smuzhiyun    set( __libstl                "${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/${ANDROID_COMPILER_VERSION}" )
1022*4882a593Smuzhiyun   endif()
1023*4882a593Smuzhiyun  else()
1024*4882a593Smuzhiyun   set( __libstl                "${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++" )
1025*4882a593Smuzhiyun  endif()
1026*4882a593Smuzhiyun  set( ANDROID_STL_INCLUDE_DIRS "${__libstl}/include" "${__libstl}/libs/${ANDROID_NDK_ABI_NAME}/include" "${__libstl}/include/backward" )
1027*4882a593Smuzhiyun  if( EXISTS "${__libstl}/libs/${ANDROID_NDK_ABI_NAME}/libgnustl_static.a" )
1028*4882a593Smuzhiyun   set( __libstl                "${__libstl}/libs/${ANDROID_NDK_ABI_NAME}/libgnustl_static.a" )
1029*4882a593Smuzhiyun  else()
1030*4882a593Smuzhiyun   set( __libstl                "${__libstl}/libs/${ANDROID_NDK_ABI_NAME}/libstdc++.a" )
1031*4882a593Smuzhiyun  endif()
1032*4882a593Smuzhiyun else()
1033*4882a593Smuzhiyun  message( FATAL_ERROR "Unknown runtime: ${ANDROID_STL}" )
1034*4882a593Smuzhiyun endif()
1035*4882a593Smuzhiyun # find libsupc++.a - rtti & exceptions
1036*4882a593Smuzhiyun if( ANDROID_STL STREQUAL "system_re" OR ANDROID_STL MATCHES "gnustl" )
1037*4882a593Smuzhiyun  set( __libsupcxx "${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/${ANDROID_COMPILER_VERSION}/libs/${ANDROID_NDK_ABI_NAME}/libsupc++.a" ) # r8b or newer
1038*4882a593Smuzhiyun  if( NOT EXISTS "${__libsupcxx}" )
1039*4882a593Smuzhiyun   set( __libsupcxx "${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/libs/${ANDROID_NDK_ABI_NAME}/libsupc++.a" ) # r7-r8
1040*4882a593Smuzhiyun  endif()
1041*4882a593Smuzhiyun  if( NOT EXISTS "${__libsupcxx}" ) # before r7
1042*4882a593Smuzhiyun   if( ARMEABI_V7A )
1043*4882a593Smuzhiyun    if( ANDROID_FORCE_ARM_BUILD )
1044*4882a593Smuzhiyun     set( __libsupcxx "${ANDROID_TOOLCHAIN_ROOT}/${ANDROID_TOOLCHAIN_MACHINE_NAME}/lib/${CMAKE_SYSTEM_PROCESSOR}/libsupc++.a" )
1045*4882a593Smuzhiyun    else()
1046*4882a593Smuzhiyun     set( __libsupcxx "${ANDROID_TOOLCHAIN_ROOT}/${ANDROID_TOOLCHAIN_MACHINE_NAME}/lib/${CMAKE_SYSTEM_PROCESSOR}/thumb/libsupc++.a" )
1047*4882a593Smuzhiyun    endif()
1048*4882a593Smuzhiyun   elseif( ARMEABI AND NOT ANDROID_FORCE_ARM_BUILD )
1049*4882a593Smuzhiyun    set( __libsupcxx "${ANDROID_TOOLCHAIN_ROOT}/${ANDROID_TOOLCHAIN_MACHINE_NAME}/lib/thumb/libsupc++.a" )
1050*4882a593Smuzhiyun   else()
1051*4882a593Smuzhiyun    set( __libsupcxx "${ANDROID_TOOLCHAIN_ROOT}/${ANDROID_TOOLCHAIN_MACHINE_NAME}/lib/libsupc++.a" )
1052*4882a593Smuzhiyun   endif()
1053*4882a593Smuzhiyun  endif()
1054*4882a593Smuzhiyun  if( NOT EXISTS "${__libsupcxx}")
1055*4882a593Smuzhiyun   message( ERROR "Could not find libsupc++.a for a chosen platform. Either your NDK is not supported or is broken.")
1056*4882a593Smuzhiyun  endif()
1057*4882a593Smuzhiyun endif()
1058*4882a593Smuzhiyunendif()
1059*4882a593Smuzhiyun
1060*4882a593Smuzhiyun
1061*4882a593Smuzhiyun# case of shared STL linkage
1062*4882a593Smuzhiyunif( ANDROID_STL MATCHES "shared" AND DEFINED __libstl )
1063*4882a593Smuzhiyun string( REPLACE "_static.a" "_shared.so" __libstl "${__libstl}" )
1064*4882a593Smuzhiyun # TODO: check if .so file exists before the renaming
1065*4882a593Smuzhiyunendif()
1066*4882a593Smuzhiyun
1067*4882a593Smuzhiyun
1068*4882a593Smuzhiyun# ccache support
1069*4882a593Smuzhiyun__INIT_VARIABLE( _ndk_ccache NDK_CCACHE ENV_NDK_CCACHE )
1070*4882a593Smuzhiyunif( _ndk_ccache )
1071*4882a593Smuzhiyun if( DEFINED NDK_CCACHE AND NOT EXISTS NDK_CCACHE )
1072*4882a593Smuzhiyun  unset( NDK_CCACHE CACHE )
1073*4882a593Smuzhiyun endif()
1074*4882a593Smuzhiyun find_program( NDK_CCACHE "${_ndk_ccache}" DOC "The path to ccache binary")
1075*4882a593Smuzhiyunelse()
1076*4882a593Smuzhiyun unset( NDK_CCACHE CACHE )
1077*4882a593Smuzhiyunendif()
1078*4882a593Smuzhiyununset( _ndk_ccache )
1079*4882a593Smuzhiyun
1080*4882a593Smuzhiyun
1081*4882a593Smuzhiyun# setup the cross-compiler
1082*4882a593Smuzhiyunif( NOT CMAKE_C_COMPILER )
1083*4882a593Smuzhiyun if( NDK_CCACHE AND NOT ANDROID_SYSROOT MATCHES "[ ;\"]" )
1084*4882a593Smuzhiyun  set( CMAKE_C_COMPILER   "${NDK_CCACHE}" CACHE PATH "ccache as C compiler" )
1085*4882a593Smuzhiyun  set( CMAKE_CXX_COMPILER "${NDK_CCACHE}" CACHE PATH "ccache as C++ compiler" )
1086*4882a593Smuzhiyun  if( ANDROID_COMPILER_IS_CLANG )
1087*4882a593Smuzhiyun   set( CMAKE_C_COMPILER_ARG1   "${ANDROID_CLANG_TOOLCHAIN_ROOT}/bin/${_clang_name}${TOOL_OS_SUFFIX}"   CACHE PATH "C compiler")
1088*4882a593Smuzhiyun   set( CMAKE_CXX_COMPILER_ARG1 "${ANDROID_CLANG_TOOLCHAIN_ROOT}/bin/${_clang_name}++${TOOL_OS_SUFFIX}" CACHE PATH "C++ compiler")
1089*4882a593Smuzhiyun  else()
1090*4882a593Smuzhiyun   set( CMAKE_C_COMPILER_ARG1   "${ANDROID_TOOLCHAIN_ROOT}/bin/${ANDROID_TOOLCHAIN_MACHINE_NAME}-gcc${TOOL_OS_SUFFIX}" CACHE PATH "C compiler")
1091*4882a593Smuzhiyun   set( CMAKE_CXX_COMPILER_ARG1 "${ANDROID_TOOLCHAIN_ROOT}/bin/${ANDROID_TOOLCHAIN_MACHINE_NAME}-g++${TOOL_OS_SUFFIX}" CACHE PATH "C++ compiler")
1092*4882a593Smuzhiyun  endif()
1093*4882a593Smuzhiyun else()
1094*4882a593Smuzhiyun  if( ANDROID_COMPILER_IS_CLANG )
1095*4882a593Smuzhiyun   set( CMAKE_C_COMPILER   "${ANDROID_CLANG_TOOLCHAIN_ROOT}/bin/${_clang_name}${TOOL_OS_SUFFIX}"   CACHE PATH "C compiler")
1096*4882a593Smuzhiyun   set( CMAKE_CXX_COMPILER "${ANDROID_CLANG_TOOLCHAIN_ROOT}/bin/${_clang_name}++${TOOL_OS_SUFFIX}" CACHE PATH "C++ compiler")
1097*4882a593Smuzhiyun  else()
1098*4882a593Smuzhiyun   set( CMAKE_C_COMPILER   "${ANDROID_TOOLCHAIN_ROOT}/bin/${ANDROID_TOOLCHAIN_MACHINE_NAME}-gcc${TOOL_OS_SUFFIX}"    CACHE PATH "C compiler" )
1099*4882a593Smuzhiyun   set( CMAKE_CXX_COMPILER "${ANDROID_TOOLCHAIN_ROOT}/bin/${ANDROID_TOOLCHAIN_MACHINE_NAME}-g++${TOOL_OS_SUFFIX}"    CACHE PATH "C++ compiler" )
1100*4882a593Smuzhiyun  endif()
1101*4882a593Smuzhiyun endif()
1102*4882a593Smuzhiyun set( CMAKE_ASM_COMPILER "${ANDROID_TOOLCHAIN_ROOT}/bin/${ANDROID_TOOLCHAIN_MACHINE_NAME}-gcc${TOOL_OS_SUFFIX}"     CACHE PATH "assembler" )
1103*4882a593Smuzhiyun set( CMAKE_STRIP        "${ANDROID_TOOLCHAIN_ROOT}/bin/${ANDROID_TOOLCHAIN_MACHINE_NAME}-strip${TOOL_OS_SUFFIX}"   CACHE PATH "strip" )
1104*4882a593Smuzhiyun if( EXISTS "${ANDROID_TOOLCHAIN_ROOT}/bin/${ANDROID_TOOLCHAIN_MACHINE_NAME}-gcc-ar${TOOL_OS_SUFFIX}" )
1105*4882a593Smuzhiyun  # Use gcc-ar if we have it for better LTO support.
1106*4882a593Smuzhiyun  set( CMAKE_AR           "${ANDROID_TOOLCHAIN_ROOT}/bin/${ANDROID_TOOLCHAIN_MACHINE_NAME}-gcc-ar${TOOL_OS_SUFFIX}"      CACHE PATH "archive" )
1107*4882a593Smuzhiyun else()
1108*4882a593Smuzhiyun  set( CMAKE_AR           "${ANDROID_TOOLCHAIN_ROOT}/bin/${ANDROID_TOOLCHAIN_MACHINE_NAME}-ar${TOOL_OS_SUFFIX}"      CACHE PATH "archive" )
1109*4882a593Smuzhiyun endif()
1110*4882a593Smuzhiyun set( CMAKE_LINKER       "${ANDROID_TOOLCHAIN_ROOT}/bin/${ANDROID_TOOLCHAIN_MACHINE_NAME}-ld${TOOL_OS_SUFFIX}"      CACHE PATH "linker" )
1111*4882a593Smuzhiyun set( CMAKE_NM           "${ANDROID_TOOLCHAIN_ROOT}/bin/${ANDROID_TOOLCHAIN_MACHINE_NAME}-nm${TOOL_OS_SUFFIX}"      CACHE PATH "nm" )
1112*4882a593Smuzhiyun set( CMAKE_OBJCOPY      "${ANDROID_TOOLCHAIN_ROOT}/bin/${ANDROID_TOOLCHAIN_MACHINE_NAME}-objcopy${TOOL_OS_SUFFIX}" CACHE PATH "objcopy" )
1113*4882a593Smuzhiyun set( CMAKE_OBJDUMP      "${ANDROID_TOOLCHAIN_ROOT}/bin/${ANDROID_TOOLCHAIN_MACHINE_NAME}-objdump${TOOL_OS_SUFFIX}" CACHE PATH "objdump" )
1114*4882a593Smuzhiyun set( CMAKE_RANLIB       "${ANDROID_TOOLCHAIN_ROOT}/bin/${ANDROID_TOOLCHAIN_MACHINE_NAME}-ranlib${TOOL_OS_SUFFIX}"  CACHE PATH "ranlib" )
1115*4882a593Smuzhiyunendif()
1116*4882a593Smuzhiyun
1117*4882a593Smuzhiyunset( _CMAKE_TOOLCHAIN_PREFIX "${ANDROID_TOOLCHAIN_MACHINE_NAME}-" )
1118*4882a593Smuzhiyunif( CMAKE_VERSION VERSION_LESS 2.8.5 )
1119*4882a593Smuzhiyun set( CMAKE_ASM_COMPILER_ARG1 "-c" )
1120*4882a593Smuzhiyunendif()
1121*4882a593Smuzhiyunif( APPLE )
1122*4882a593Smuzhiyun find_program( CMAKE_INSTALL_NAME_TOOL NAMES install_name_tool )
1123*4882a593Smuzhiyun if( NOT CMAKE_INSTALL_NAME_TOOL )
1124*4882a593Smuzhiyun  message( FATAL_ERROR "Could not find install_name_tool, please check your installation." )
1125*4882a593Smuzhiyun endif()
1126*4882a593Smuzhiyun mark_as_advanced( CMAKE_INSTALL_NAME_TOOL )
1127*4882a593Smuzhiyunendif()
1128*4882a593Smuzhiyun
1129*4882a593Smuzhiyun# Force set compilers because standard identification works badly for us
1130*4882a593Smuzhiyun#include( CMakeForceCompiler )
1131*4882a593Smuzhiyun#CMAKE_FORCE_C_COMPILER( "${CMAKE_C_COMPILER}" GNU )
1132*4882a593Smuzhiyunif( ANDROID_COMPILER_IS_CLANG )
1133*4882a593Smuzhiyun set( CMAKE_C_COMPILER_ID Clang )
1134*4882a593Smuzhiyunendif()
1135*4882a593Smuzhiyunset( CMAKE_C_PLATFORM_ID Linux )
1136*4882a593Smuzhiyunif( X86_64 OR MIPS64 OR ARM64_V8A )
1137*4882a593Smuzhiyun set( CMAKE_C_SIZEOF_DATA_PTR 8 )
1138*4882a593Smuzhiyunelse()
1139*4882a593Smuzhiyun set( CMAKE_C_SIZEOF_DATA_PTR 4 )
1140*4882a593Smuzhiyunendif()
1141*4882a593Smuzhiyunset( CMAKE_C_HAS_ISYSROOT 1 )
1142*4882a593Smuzhiyunset( CMAKE_C_COMPILER_ABI ELF )
1143*4882a593Smuzhiyun#CMAKE_FORCE_CXX_COMPILER( "${CMAKE_CXX_COMPILER}" GNU )
1144*4882a593Smuzhiyunif( ANDROID_COMPILER_IS_CLANG )
1145*4882a593Smuzhiyun set( CMAKE_CXX_COMPILER_ID Clang)
1146*4882a593Smuzhiyunendif()
1147*4882a593Smuzhiyunset( CMAKE_CXX_PLATFORM_ID Linux )
1148*4882a593Smuzhiyunset( CMAKE_CXX_SIZEOF_DATA_PTR ${CMAKE_C_SIZEOF_DATA_PTR} )
1149*4882a593Smuzhiyunset( CMAKE_CXX_HAS_ISYSROOT 1 )
1150*4882a593Smuzhiyunset( CMAKE_CXX_COMPILER_ABI ELF )
1151*4882a593Smuzhiyunset( CMAKE_CXX_SOURCE_FILE_EXTENSIONS cc cp cxx cpp CPP c++ C )
1152*4882a593Smuzhiyun# force ASM compiler (required for CMake < 2.8.5)
1153*4882a593Smuzhiyunset( CMAKE_ASM_COMPILER_ID_RUN TRUE )
1154*4882a593Smuzhiyunset( CMAKE_ASM_COMPILER_ID GNU )
1155*4882a593Smuzhiyunset( CMAKE_ASM_COMPILER_WORKS TRUE )
1156*4882a593Smuzhiyunset( CMAKE_ASM_COMPILER_FORCED TRUE )
1157*4882a593Smuzhiyunset( CMAKE_COMPILER_IS_GNUASM 1)
1158*4882a593Smuzhiyunset( CMAKE_ASM_SOURCE_FILE_EXTENSIONS s S asm )
1159*4882a593Smuzhiyun
1160*4882a593Smuzhiyunforeach( lang C CXX ASM )
1161*4882a593Smuzhiyun if( ANDROID_COMPILER_IS_CLANG )
1162*4882a593Smuzhiyun  set( CMAKE_${lang}_COMPILER_VERSION ${ANDROID_CLANG_VERSION} )
1163*4882a593Smuzhiyun else()
1164*4882a593Smuzhiyun  set( CMAKE_${lang}_COMPILER_VERSION ${ANDROID_COMPILER_VERSION} )
1165*4882a593Smuzhiyun endif()
1166*4882a593Smuzhiyunendforeach()
1167*4882a593Smuzhiyun
1168*4882a593Smuzhiyun# flags and definitions
1169*4882a593Smuzhiyunremove_definitions( -DANDROID )
1170*4882a593Smuzhiyunadd_definitions( -DANDROID )
1171*4882a593Smuzhiyun
1172*4882a593Smuzhiyunif( ANDROID_SYSROOT MATCHES "[ ;\"]" )
1173*4882a593Smuzhiyun if( CMAKE_HOST_WIN32 )
1174*4882a593Smuzhiyun  # try to convert path to 8.3 form
1175*4882a593Smuzhiyun  file( WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/cvt83.cmd" "@echo %~s1" )
1176*4882a593Smuzhiyun  execute_process( COMMAND "$ENV{ComSpec}" /c "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/cvt83.cmd" "${ANDROID_SYSROOT}"
1177*4882a593Smuzhiyun                   OUTPUT_VARIABLE __path OUTPUT_STRIP_TRAILING_WHITESPACE
1178*4882a593Smuzhiyun                   RESULT_VARIABLE __result ERROR_QUIET )
1179*4882a593Smuzhiyun  if( __result EQUAL 0 )
1180*4882a593Smuzhiyun   file( TO_CMAKE_PATH "${__path}" ANDROID_SYSROOT )
1181*4882a593Smuzhiyun   set( ANDROID_CXX_FLAGS "--sysroot=${ANDROID_SYSROOT}" )
1182*4882a593Smuzhiyun  else()
1183*4882a593Smuzhiyun   set( ANDROID_CXX_FLAGS "--sysroot=\"${ANDROID_SYSROOT}\"" )
1184*4882a593Smuzhiyun  endif()
1185*4882a593Smuzhiyun else()
1186*4882a593Smuzhiyun  set( ANDROID_CXX_FLAGS "'--sysroot=${ANDROID_SYSROOT}'" )
1187*4882a593Smuzhiyun endif()
1188*4882a593Smuzhiyun if( NOT _CMAKE_IN_TRY_COMPILE )
1189*4882a593Smuzhiyun  # quotes can break try_compile and compiler identification
1190*4882a593Smuzhiyun  message(WARNING "Path to your Android NDK (or toolchain) has non-alphanumeric symbols.\nThe build might be broken.\n")
1191*4882a593Smuzhiyun endif()
1192*4882a593Smuzhiyunelse()
1193*4882a593Smuzhiyun set( ANDROID_CXX_FLAGS "--sysroot=${ANDROID_SYSROOT}" )
1194*4882a593Smuzhiyunendif()
1195*4882a593Smuzhiyun
1196*4882a593Smuzhiyun# NDK flags
1197*4882a593Smuzhiyunif (ARM64_V8A )
1198*4882a593Smuzhiyun set( ANDROID_CXX_FLAGS         "${ANDROID_CXX_FLAGS} -funwind-tables" )
1199*4882a593Smuzhiyun set( ANDROID_CXX_FLAGS_RELEASE "-fomit-frame-pointer -fstrict-aliasing" )
1200*4882a593Smuzhiyun set( ANDROID_CXX_FLAGS_DEBUG   "-fno-omit-frame-pointer -fno-strict-aliasing" )
1201*4882a593Smuzhiyun if( NOT ANDROID_COMPILER_IS_CLANG )
1202*4882a593Smuzhiyun  set( ANDROID_CXX_FLAGS_RELEASE "${ANDROID_CXX_FLAGS_RELEASE} -funswitch-loops -finline-limit=300" )
1203*4882a593Smuzhiyun endif()
1204*4882a593Smuzhiyunelseif( ARMEABI OR ARMEABI_V7A)
1205*4882a593Smuzhiyun set( ANDROID_CXX_FLAGS "${ANDROID_CXX_FLAGS} -funwind-tables" )
1206*4882a593Smuzhiyun if( NOT ANDROID_FORCE_ARM_BUILD AND NOT ARMEABI_V6 )
1207*4882a593Smuzhiyun  set( ANDROID_CXX_FLAGS_RELEASE "-mthumb -fomit-frame-pointer -fno-strict-aliasing" )
1208*4882a593Smuzhiyun  set( ANDROID_CXX_FLAGS_DEBUG   "-marm -fno-omit-frame-pointer -fno-strict-aliasing" )
1209*4882a593Smuzhiyun  if( NOT ANDROID_COMPILER_IS_CLANG )
1210*4882a593Smuzhiyun   set( ANDROID_CXX_FLAGS "${ANDROID_CXX_FLAGS} -finline-limit=64" )
1211*4882a593Smuzhiyun  endif()
1212*4882a593Smuzhiyun else()
1213*4882a593Smuzhiyun  # always compile ARMEABI_V6 in arm mode; otherwise there is no difference from ARMEABI
1214*4882a593Smuzhiyun  set( ANDROID_CXX_FLAGS_RELEASE "-marm -fomit-frame-pointer -fstrict-aliasing" )
1215*4882a593Smuzhiyun  set( ANDROID_CXX_FLAGS_DEBUG   "-marm -fno-omit-frame-pointer -fno-strict-aliasing" )
1216*4882a593Smuzhiyun  if( NOT ANDROID_COMPILER_IS_CLANG )
1217*4882a593Smuzhiyun   set( ANDROID_CXX_FLAGS "${ANDROID_CXX_FLAGS} -funswitch-loops -finline-limit=300" )
1218*4882a593Smuzhiyun  endif()
1219*4882a593Smuzhiyun endif()
1220*4882a593Smuzhiyunelseif( X86 OR X86_64 )
1221*4882a593Smuzhiyun set( ANDROID_CXX_FLAGS "${ANDROID_CXX_FLAGS} -funwind-tables" )
1222*4882a593Smuzhiyun if( NOT ANDROID_COMPILER_IS_CLANG )
1223*4882a593Smuzhiyun  set( ANDROID_CXX_FLAGS "${ANDROID_CXX_FLAGS} -funswitch-loops -finline-limit=300" )
1224*4882a593Smuzhiyun endif()
1225*4882a593Smuzhiyun set( ANDROID_CXX_FLAGS_RELEASE "-fomit-frame-pointer -fstrict-aliasing" )
1226*4882a593Smuzhiyun set( ANDROID_CXX_FLAGS_DEBUG   "-fno-omit-frame-pointer -fno-strict-aliasing" )
1227*4882a593Smuzhiyunelseif( MIPS OR MIPS64 )
1228*4882a593Smuzhiyun set( ANDROID_CXX_FLAGS         "${ANDROID_CXX_FLAGS} -fno-strict-aliasing -finline-functions -funwind-tables -fmessage-length=0" )
1229*4882a593Smuzhiyun set( ANDROID_CXX_FLAGS_RELEASE "-fomit-frame-pointer" )
1230*4882a593Smuzhiyun set( ANDROID_CXX_FLAGS_DEBUG   "-fno-omit-frame-pointer" )
1231*4882a593Smuzhiyun if( NOT ANDROID_COMPILER_IS_CLANG )
1232*4882a593Smuzhiyun  set( ANDROID_CXX_FLAGS "${ANDROID_CXX_FLAGS} -fno-inline-functions-called-once -fgcse-after-reload -frerun-cse-after-loop -frename-registers" )
1233*4882a593Smuzhiyun  set( ANDROID_CXX_FLAGS_RELEASE "${ANDROID_CXX_FLAGS_RELEASE} -funswitch-loops -finline-limit=300" )
1234*4882a593Smuzhiyun endif()
1235*4882a593Smuzhiyunelseif()
1236*4882a593Smuzhiyun set( ANDROID_CXX_FLAGS_RELEASE "" )
1237*4882a593Smuzhiyun set( ANDROID_CXX_FLAGS_DEBUG   "" )
1238*4882a593Smuzhiyunendif()
1239*4882a593Smuzhiyun
1240*4882a593Smuzhiyunset( ANDROID_CXX_FLAGS "${ANDROID_CXX_FLAGS} -fsigned-char" ) # good/necessary when porting desktop libraries
1241*4882a593Smuzhiyun
1242*4882a593Smuzhiyunif( NOT X86 AND NOT ANDROID_COMPILER_IS_CLANG )
1243*4882a593Smuzhiyun set( ANDROID_CXX_FLAGS "-Wno-psabi ${ANDROID_CXX_FLAGS}" )
1244*4882a593Smuzhiyunendif()
1245*4882a593Smuzhiyun
1246*4882a593Smuzhiyunif( NOT ANDROID_COMPILER_VERSION VERSION_LESS "4.6" )
1247*4882a593Smuzhiyun set( ANDROID_CXX_FLAGS "${ANDROID_CXX_FLAGS} -no-canonical-prefixes" ) # see https://android-review.googlesource.com/#/c/47564/
1248*4882a593Smuzhiyunendif()
1249*4882a593Smuzhiyun
1250*4882a593Smuzhiyun# ABI-specific flags
1251*4882a593Smuzhiyunif( ARMEABI_V7A )
1252*4882a593Smuzhiyun set( ANDROID_CXX_FLAGS "${ANDROID_CXX_FLAGS} -march=armv7-a -mfloat-abi=softfp" )
1253*4882a593Smuzhiyun if( NEON )
1254*4882a593Smuzhiyun  set( ANDROID_CXX_FLAGS "${ANDROID_CXX_FLAGS} -mfpu=neon" )
1255*4882a593Smuzhiyun elseif( VFPV3 )
1256*4882a593Smuzhiyun  set( ANDROID_CXX_FLAGS "${ANDROID_CXX_FLAGS} -mfpu=vfpv3" )
1257*4882a593Smuzhiyun else()
1258*4882a593Smuzhiyun  set( ANDROID_CXX_FLAGS "${ANDROID_CXX_FLAGS} -mfpu=vfpv3-d16" )
1259*4882a593Smuzhiyun endif()
1260*4882a593Smuzhiyunelseif( ARMEABI_V6 )
1261*4882a593Smuzhiyun set( ANDROID_CXX_FLAGS "${ANDROID_CXX_FLAGS} -march=armv6 -mfloat-abi=softfp -mfpu=vfp" ) # vfp == vfpv2
1262*4882a593Smuzhiyunelseif( ARMEABI )
1263*4882a593Smuzhiyun set( ANDROID_CXX_FLAGS "${ANDROID_CXX_FLAGS} -march=armv5te -mtune=xscale -msoft-float" )
1264*4882a593Smuzhiyunendif()
1265*4882a593Smuzhiyun
1266*4882a593Smuzhiyun# setup default C link flag
1267*4882a593Smuzhiyunset( CMAKE_C_LINK_EXECUTABLE "<CMAKE_C_COMPILER> <FLAGS> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>" )
1268*4882a593Smuzhiyun
1269*4882a593Smuzhiyunif( ANDROID_STL MATCHES "gnustl" AND (EXISTS "${__libstl}" OR EXISTS "${__libsupcxx}") )
1270*4882a593Smuzhiyun set( CMAKE_CXX_CREATE_SHARED_LIBRARY "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LANGUAGE_COMPILE_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> <CMAKE_SHARED_LIBRARY_SONAME_CXX_FLAG><TARGET_SONAME> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>" )
1271*4882a593Smuzhiyun set( CMAKE_CXX_CREATE_SHARED_MODULE  "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LANGUAGE_COMPILE_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> <CMAKE_SHARED_LIBRARY_SONAME_CXX_FLAG><TARGET_SONAME> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>" )
1272*4882a593Smuzhiyun set( CMAKE_CXX_LINK_EXECUTABLE       "<CMAKE_C_COMPILER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>" )
1273*4882a593Smuzhiyunelse()
1274*4882a593Smuzhiyun set( CMAKE_CXX_CREATE_SHARED_LIBRARY "<CMAKE_CXX_COMPILER> <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LANGUAGE_COMPILE_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> <CMAKE_SHARED_LIBRARY_SONAME_CXX_FLAG><TARGET_SONAME> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>" )
1275*4882a593Smuzhiyun set( CMAKE_CXX_CREATE_SHARED_MODULE  "<CMAKE_CXX_COMPILER> <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LANGUAGE_COMPILE_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> <CMAKE_SHARED_LIBRARY_SONAME_CXX_FLAG><TARGET_SONAME> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>" )
1276*4882a593Smuzhiyun set( CMAKE_CXX_LINK_EXECUTABLE       "<CMAKE_CXX_COMPILER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>" )
1277*4882a593Smuzhiyunendif()
1278*4882a593Smuzhiyun
1279*4882a593Smuzhiyun# STL
1280*4882a593Smuzhiyunif( EXISTS "${__libstl}" OR EXISTS "${__libsupcxx}" )
1281*4882a593Smuzhiyun if( EXISTS "${__libstl}" )
1282*4882a593Smuzhiyun  set( CMAKE_CXX_CREATE_SHARED_LIBRARY "${CMAKE_CXX_CREATE_SHARED_LIBRARY} \"${__libstl}\"" )
1283*4882a593Smuzhiyun  set( CMAKE_CXX_CREATE_SHARED_MODULE  "${CMAKE_CXX_CREATE_SHARED_MODULE} \"${__libstl}\"" )
1284*4882a593Smuzhiyun  set( CMAKE_CXX_LINK_EXECUTABLE       "${CMAKE_CXX_LINK_EXECUTABLE} \"${__libstl}\"" )
1285*4882a593Smuzhiyun endif()
1286*4882a593Smuzhiyun if( EXISTS "${__libsupcxx}" )
1287*4882a593Smuzhiyun  set( CMAKE_CXX_CREATE_SHARED_LIBRARY "${CMAKE_CXX_CREATE_SHARED_LIBRARY} \"${__libsupcxx}\"" )
1288*4882a593Smuzhiyun  set( CMAKE_CXX_CREATE_SHARED_MODULE  "${CMAKE_CXX_CREATE_SHARED_MODULE} \"${__libsupcxx}\"" )
1289*4882a593Smuzhiyun  set( CMAKE_CXX_LINK_EXECUTABLE       "${CMAKE_CXX_LINK_EXECUTABLE} \"${__libsupcxx}\"" )
1290*4882a593Smuzhiyun  # C objects:
1291*4882a593Smuzhiyun  set( CMAKE_C_CREATE_SHARED_LIBRARY "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LANGUAGE_COMPILE_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> <CMAKE_SHARED_LIBRARY_SONAME_C_FLAG><TARGET_SONAME> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>" )
1292*4882a593Smuzhiyun  set( CMAKE_C_CREATE_SHARED_MODULE  "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LANGUAGE_COMPILE_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> <CMAKE_SHARED_LIBRARY_SONAME_C_FLAG><TARGET_SONAME> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>" )
1293*4882a593Smuzhiyun  set( CMAKE_C_LINK_EXECUTABLE       "<CMAKE_C_COMPILER> <FLAGS> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>" )
1294*4882a593Smuzhiyun  set( CMAKE_C_CREATE_SHARED_LIBRARY "${CMAKE_C_CREATE_SHARED_LIBRARY} \"${__libsupcxx}\"" )
1295*4882a593Smuzhiyun  set( CMAKE_C_CREATE_SHARED_MODULE  "${CMAKE_C_CREATE_SHARED_MODULE} \"${__libsupcxx}\"" )
1296*4882a593Smuzhiyun  set( CMAKE_C_LINK_EXECUTABLE       "${CMAKE_C_LINK_EXECUTABLE} \"${__libsupcxx}\"" )
1297*4882a593Smuzhiyun endif()
1298*4882a593Smuzhiyun if( ANDROID_STL MATCHES "gnustl" )
1299*4882a593Smuzhiyun  if( NOT EXISTS "${ANDROID_LIBM_PATH}" )
1300*4882a593Smuzhiyun   set( ANDROID_LIBM_PATH -lm )
1301*4882a593Smuzhiyun  endif()
1302*4882a593Smuzhiyun  set( CMAKE_CXX_CREATE_SHARED_LIBRARY "${CMAKE_CXX_CREATE_SHARED_LIBRARY} ${ANDROID_LIBM_PATH}" )
1303*4882a593Smuzhiyun  set( CMAKE_CXX_CREATE_SHARED_MODULE  "${CMAKE_CXX_CREATE_SHARED_MODULE} ${ANDROID_LIBM_PATH}" )
1304*4882a593Smuzhiyun  set( CMAKE_CXX_LINK_EXECUTABLE       "${CMAKE_CXX_LINK_EXECUTABLE} ${ANDROID_LIBM_PATH}" )
1305*4882a593Smuzhiyun endif()
1306*4882a593Smuzhiyunendif()
1307*4882a593Smuzhiyun
1308*4882a593Smuzhiyun# add crt object to executable link list for Android SDK build
1309*4882a593Smuzhiyun# ----------------------------------------------------------------------------
1310*4882a593Smuzhiyunset( ANDROID_CRT_BEGIN "${ANDROID_SYSROOT}/usr/lib/crtbegin_dynamic.o")
1311*4882a593Smuzhiyunset( ANDROID_CRT_END   "${ANDROID_SYSROOT}/usr/lib/crtend_android.o")
1312*4882a593Smuzhiyunset( CMAKE_C_LINK_EXECUTABLE         "${CMAKE_C_LINK_EXECUTABLE} ${ANDROID_CRT_BEGIN} ${ANDROID_CRT_END}" )
1313*4882a593Smuzhiyunset( CMAKE_CXX_LINK_EXECUTABLE       "${CMAKE_CXX_LINK_EXECUTABLE} ${ANDROID_CRT_BEGIN} ${ANDROID_CRT_END}" )
1314*4882a593Smuzhiyun
1315*4882a593Smuzhiyun# ----------------------------------------------------------------------------
1316*4882a593Smuzhiyun# ----------------------------------------------------------------------------
1317*4882a593Smuzhiyun# add libgcc.a to executable link list
1318*4882a593Smuzhiyun# ----------------------------------------------------------------------------
1319*4882a593Smuzhiyunset( ANDROID_LIB_GCC "${ANDROID_TOOLCHAIN_ROOT}/lib/gcc/${ANDROID_TOOLCHAIN_MACHINE_NAME}/${ANDROID_COMPILER_VERSION}/libgcc.a")
1320*4882a593Smuzhiyunset( CMAKE_C_LINK_EXECUTABLE         "${CMAKE_C_LINK_EXECUTABLE} ${ANDROID_LIB_GCC}" )
1321*4882a593Smuzhiyunset( CMAKE_CXX_LINK_EXECUTABLE       "${CMAKE_CXX_LINK_EXECUTABLE} ${ANDROID_LIB_GCC}" )
1322*4882a593Smuzhiyun
1323*4882a593Smuzhiyun# variables controlling optional build flags
1324*4882a593Smuzhiyunif( ANDROID_NDK_RELEASE_NUM LESS 7000 ) # before r7
1325*4882a593Smuzhiyun # libGLESv2.so in NDK's prior to r7 refers to missing external symbols.
1326*4882a593Smuzhiyun # So this flag option is required for all projects using OpenGL from native.
1327*4882a593Smuzhiyun __INIT_VARIABLE( ANDROID_SO_UNDEFINED                      VALUES ON )
1328*4882a593Smuzhiyunelse()
1329*4882a593Smuzhiyun __INIT_VARIABLE( ANDROID_SO_UNDEFINED                      VALUES OFF )
1330*4882a593Smuzhiyunendif()
1331*4882a593Smuzhiyun__INIT_VARIABLE( ANDROID_NO_UNDEFINED                       VALUES ON )
1332*4882a593Smuzhiyun__INIT_VARIABLE( ANDROID_FUNCTION_LEVEL_LINKING             VALUES ON )
1333*4882a593Smuzhiyun__INIT_VARIABLE( ANDROID_GOLD_LINKER                        VALUES ON )
1334*4882a593Smuzhiyun__INIT_VARIABLE( ANDROID_NOEXECSTACK                        VALUES ON )
1335*4882a593Smuzhiyun__INIT_VARIABLE( ANDROID_RELRO                              VALUES ON )
1336*4882a593Smuzhiyun
1337*4882a593Smuzhiyunset( ANDROID_NO_UNDEFINED           ${ANDROID_NO_UNDEFINED}           CACHE BOOL "Show all undefined symbols as linker errors" )
1338*4882a593Smuzhiyunset( ANDROID_SO_UNDEFINED           ${ANDROID_SO_UNDEFINED}           CACHE BOOL "Allows or disallows undefined symbols in shared libraries" )
1339*4882a593Smuzhiyunset( ANDROID_FUNCTION_LEVEL_LINKING ${ANDROID_FUNCTION_LEVEL_LINKING} CACHE BOOL "Put each function in separate section and enable garbage collection of unused input sections at link time" )
1340*4882a593Smuzhiyunset( ANDROID_GOLD_LINKER            ${ANDROID_GOLD_LINKER}            CACHE BOOL "Enables gold linker" )
1341*4882a593Smuzhiyunset( ANDROID_NOEXECSTACK            ${ANDROID_NOEXECSTACK}            CACHE BOOL "Allows or disallows undefined symbols in shared libraries" )
1342*4882a593Smuzhiyunset( ANDROID_RELRO                  ${ANDROID_RELRO}                  CACHE BOOL "Enables RELRO - a memory corruption mitigation technique" )
1343*4882a593Smuzhiyunmark_as_advanced( ANDROID_NO_UNDEFINED ANDROID_SO_UNDEFINED ANDROID_FUNCTION_LEVEL_LINKING ANDROID_GOLD_LINKER ANDROID_NOEXECSTACK ANDROID_RELRO )
1344*4882a593Smuzhiyun
1345*4882a593Smuzhiyun# linker flags
1346*4882a593Smuzhiyunset( ANDROID_LINKER_FLAGS "-Wl,--allow-multiple-definition" )
1347*4882a593Smuzhiyun
1348*4882a593Smuzhiyunif( ARMEABI_V7A )
1349*4882a593Smuzhiyun # this is *required* to use the following linker flags that routes around
1350*4882a593Smuzhiyun # a CPU bug in some Cortex-A8 implementations:
1351*4882a593Smuzhiyun set( ANDROID_LINKER_FLAGS "${ANDROID_LINKER_FLAGS} -Wl,--fix-cortex-a8" )
1352*4882a593Smuzhiyunendif()
1353*4882a593Smuzhiyun
1354*4882a593Smuzhiyunif( ANDROID_NO_UNDEFINED )
1355*4882a593Smuzhiyun if( MIPS )
1356*4882a593Smuzhiyun  # there is some sysroot-related problem in mips linker...
1357*4882a593Smuzhiyun  if( NOT ANDROID_SYSROOT MATCHES "[ ;\"]" )
1358*4882a593Smuzhiyun   set( ANDROID_LINKER_FLAGS "${ANDROID_LINKER_FLAGS} -Wl,--no-undefined -Wl,-rpath-link,${ANDROID_SYSROOT}/usr/lib" )
1359*4882a593Smuzhiyun  endif()
1360*4882a593Smuzhiyun else()
1361*4882a593Smuzhiyun  set( ANDROID_LINKER_FLAGS "${ANDROID_LINKER_FLAGS} -Wl,--no-undefined" )
1362*4882a593Smuzhiyun endif()
1363*4882a593Smuzhiyunendif()
1364*4882a593Smuzhiyun
1365*4882a593Smuzhiyunif( ANDROID_SO_UNDEFINED )
1366*4882a593Smuzhiyun set( ANDROID_LINKER_FLAGS "${ANDROID_LINKER_FLAGS} -Wl,-allow-shlib-undefined" )
1367*4882a593Smuzhiyunendif()
1368*4882a593Smuzhiyun
1369*4882a593Smuzhiyunif( ANDROID_FUNCTION_LEVEL_LINKING )
1370*4882a593Smuzhiyun set( ANDROID_CXX_FLAGS    "${ANDROID_CXX_FLAGS} -fdata-sections -ffunction-sections" )
1371*4882a593Smuzhiyun set( ANDROID_LINKER_FLAGS "${ANDROID_LINKER_FLAGS} -Wl,--gc-sections" )
1372*4882a593Smuzhiyunendif()
1373*4882a593Smuzhiyun
1374*4882a593Smuzhiyunif( ANDROID_COMPILER_VERSION VERSION_EQUAL "4.6" )
1375*4882a593Smuzhiyun if( ANDROID_GOLD_LINKER AND (CMAKE_HOST_UNIX OR ANDROID_NDK_RELEASE_NUM GREATER 8002) AND (ARMEABI OR ARMEABI_V7A OR X86) )
1376*4882a593Smuzhiyun  set( ANDROID_LINKER_FLAGS "${ANDROID_LINKER_FLAGS} -fuse-ld=gold" )
1377*4882a593Smuzhiyun elseif( ANDROID_NDK_RELEASE_NUM GREATER 8002 ) # after r8b
1378*4882a593Smuzhiyun  set( ANDROID_LINKER_FLAGS "${ANDROID_LINKER_FLAGS} -fuse-ld=bfd" )
1379*4882a593Smuzhiyun elseif( ANDROID_NDK_RELEASE STREQUAL "r8b" AND ARMEABI AND NOT _CMAKE_IN_TRY_COMPILE )
1380*4882a593Smuzhiyun  message( WARNING "The default bfd linker from arm GCC 4.6 toolchain can fail with 'unresolvable R_ARM_THM_CALL relocation' error message. See https://code.google.com/p/android/issues/detail?id=35342
1381*4882a593Smuzhiyun  On Linux and OS X host platform you can workaround this problem using gold linker (default).
1382*4882a593Smuzhiyun  Rerun cmake with -DANDROID_GOLD_LINKER=ON option in case of problems.
1383*4882a593Smuzhiyun" )
1384*4882a593Smuzhiyun endif()
1385*4882a593Smuzhiyunendif() # version 4.6
1386*4882a593Smuzhiyun
1387*4882a593Smuzhiyunif( ANDROID_NOEXECSTACK )
1388*4882a593Smuzhiyun if( ANDROID_COMPILER_IS_CLANG )
1389*4882a593Smuzhiyun  set( ANDROID_CXX_FLAGS    "${ANDROID_CXX_FLAGS} -Xclang -mnoexecstack" )
1390*4882a593Smuzhiyun else()
1391*4882a593Smuzhiyun  set( ANDROID_CXX_FLAGS    "${ANDROID_CXX_FLAGS} -Wa,--noexecstack" )
1392*4882a593Smuzhiyun endif()
1393*4882a593Smuzhiyun set( ANDROID_LINKER_FLAGS "${ANDROID_LINKER_FLAGS} -Wl,-z,noexecstack" )
1394*4882a593Smuzhiyunendif()
1395*4882a593Smuzhiyun
1396*4882a593Smuzhiyunif( ANDROID_RELRO )
1397*4882a593Smuzhiyun set( ANDROID_LINKER_FLAGS "${ANDROID_LINKER_FLAGS} -Wl,-z,relro -Wl,-z,now" )
1398*4882a593Smuzhiyunendif()
1399*4882a593Smuzhiyun
1400*4882a593Smuzhiyun# ----------------------------------------------------------------------------
1401*4882a593Smuzhiyun# add linker definition for Android SDK build
1402*4882a593Smuzhiyun# ----------------------------------------------------------------------------
1403*4882a593Smuzhiyunif( X86_64 OR MIPS64 OR ARM64_V8A )
1404*4882a593Smuzhiyunset( ANDROID_LINKER_FLAGS "${ANDROID_LINKER_FLAGS} -Wl,-dynamic-linker,/system/bin/linker64 " )
1405*4882a593Smuzhiyunelse()
1406*4882a593Smuzhiyunset( ANDROID_LINKER_FLAGS "${ANDROID_LINKER_FLAGS} -Wl,-dynamic-linker,/system/bin/linker " )
1407*4882a593Smuzhiyunendif()
1408*4882a593Smuzhiyun
1409*4882a593Smuzhiyunif( ANDROID_COMPILER_IS_CLANG )
1410*4882a593Smuzhiyun set( ANDROID_CXX_FLAGS "-target ${ANDROID_LLVM_TRIPLE} -Qunused-arguments ${ANDROID_CXX_FLAGS}" )
1411*4882a593Smuzhiyun if( BUILD_WITH_ANDROID_NDK )
1412*4882a593Smuzhiyun  set( ANDROID_CXX_FLAGS "-gcc-toolchain ${ANDROID_TOOLCHAIN_ROOT} ${ANDROID_CXX_FLAGS}" )
1413*4882a593Smuzhiyun endif()
1414*4882a593Smuzhiyunendif()
1415*4882a593Smuzhiyun
1416*4882a593Smuzhiyun# ----------------------------------------------------------------------------
1417*4882a593Smuzhiyun# add nostdlib for Android SDK build
1418*4882a593Smuzhiyun# ----------------------------------------------------------------------------
1419*4882a593Smuzhiyunset( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -nostdlib -ldl -lc" )
1420*4882a593Smuzhiyun
1421*4882a593Smuzhiyun# cache flags
1422*4882a593Smuzhiyunset( CMAKE_CXX_FLAGS           ""                        CACHE STRING "c++ flags" )
1423*4882a593Smuzhiyunset( CMAKE_C_FLAGS             ""                        CACHE STRING "c flags" )
1424*4882a593Smuzhiyunset( CMAKE_CXX_FLAGS_RELEASE   "-O3 -DNDEBUG"            CACHE STRING "c++ Release flags" )
1425*4882a593Smuzhiyunset( CMAKE_C_FLAGS_RELEASE     "-O3 -DNDEBUG"            CACHE STRING "c Release flags" )
1426*4882a593Smuzhiyunset( CMAKE_CXX_FLAGS_DEBUG     "-O0 -g -DDEBUG -D_DEBUG" CACHE STRING "c++ Debug flags" )
1427*4882a593Smuzhiyunset( CMAKE_C_FLAGS_DEBUG       "-O0 -g -DDEBUG -D_DEBUG" CACHE STRING "c Debug flags" )
1428*4882a593Smuzhiyunset( CMAKE_SHARED_LINKER_FLAGS ""                        CACHE STRING "shared linker flags" )
1429*4882a593Smuzhiyunset( CMAKE_MODULE_LINKER_FLAGS ""                        CACHE STRING "module linker flags" )
1430*4882a593Smuzhiyunset( CMAKE_EXE_LINKER_FLAGS    "-Wl,-z,nocopyreloc"      CACHE STRING "executable linker flags" )
1431*4882a593Smuzhiyun
1432*4882a593Smuzhiyun# put flags to cache (for debug purpose only)
1433*4882a593Smuzhiyunset( ANDROID_CXX_FLAGS         "${ANDROID_CXX_FLAGS}"         CACHE INTERNAL "Android specific c/c++ flags" )
1434*4882a593Smuzhiyunset( ANDROID_CXX_FLAGS_RELEASE "${ANDROID_CXX_FLAGS_RELEASE}" CACHE INTERNAL "Android specific c/c++ Release flags" )
1435*4882a593Smuzhiyunset( ANDROID_CXX_FLAGS_DEBUG   "${ANDROID_CXX_FLAGS_DEBUG}"   CACHE INTERNAL "Android specific c/c++ Debug flags" )
1436*4882a593Smuzhiyunset( ANDROID_LINKER_FLAGS      "${ANDROID_LINKER_FLAGS}"      CACHE INTERNAL "Android specific c/c++ linker flags" )
1437*4882a593Smuzhiyun
1438*4882a593Smuzhiyun# finish flags
1439*4882a593Smuzhiyunset( CMAKE_CXX_FLAGS           "${ANDROID_CXX_FLAGS} ${CMAKE_CXX_FLAGS}" )
1440*4882a593Smuzhiyunset( CMAKE_C_FLAGS             "${ANDROID_CXX_FLAGS} ${CMAKE_C_FLAGS}" )
1441*4882a593Smuzhiyunset( CMAKE_CXX_FLAGS_RELEASE   "${ANDROID_CXX_FLAGS_RELEASE} ${CMAKE_CXX_FLAGS_RELEASE}" )
1442*4882a593Smuzhiyunset( CMAKE_C_FLAGS_RELEASE     "${ANDROID_CXX_FLAGS_RELEASE} ${CMAKE_C_FLAGS_RELEASE}" )
1443*4882a593Smuzhiyunset( CMAKE_CXX_FLAGS_DEBUG     "${ANDROID_CXX_FLAGS_DEBUG} ${CMAKE_CXX_FLAGS_DEBUG}" )
1444*4882a593Smuzhiyunset( CMAKE_C_FLAGS_DEBUG       "${ANDROID_CXX_FLAGS_DEBUG} ${CMAKE_C_FLAGS_DEBUG}" )
1445*4882a593Smuzhiyunset( CMAKE_SHARED_LINKER_FLAGS "${ANDROID_LINKER_FLAGS} ${CMAKE_SHARED_LINKER_FLAGS}" )
1446*4882a593Smuzhiyunset( CMAKE_MODULE_LINKER_FLAGS "${ANDROID_LINKER_FLAGS} ${CMAKE_MODULE_LINKER_FLAGS}" )
1447*4882a593Smuzhiyunset( CMAKE_EXE_LINKER_FLAGS    "${ANDROID_LINKER_FLAGS} ${CMAKE_EXE_LINKER_FLAGS}" )
1448*4882a593Smuzhiyun
1449*4882a593Smuzhiyunif( MIPS AND BUILD_WITH_ANDROID_NDK AND ANDROID_NDK_RELEASE STREQUAL "r8" )
1450*4882a593Smuzhiyun set( CMAKE_SHARED_LINKER_FLAGS "-Wl,-T,${ANDROID_NDK_TOOLCHAINS_PATH}/${ANDROID_GCC_TOOLCHAIN_NAME}/mipself.xsc ${CMAKE_SHARED_LINKER_FLAGS}" )
1451*4882a593Smuzhiyun set( CMAKE_MODULE_LINKER_FLAGS "-Wl,-T,${ANDROID_NDK_TOOLCHAINS_PATH}/${ANDROID_GCC_TOOLCHAIN_NAME}/mipself.xsc ${CMAKE_MODULE_LINKER_FLAGS}" )
1452*4882a593Smuzhiyun set( CMAKE_EXE_LINKER_FLAGS    "-Wl,-T,${ANDROID_NDK_TOOLCHAINS_PATH}/${ANDROID_GCC_TOOLCHAIN_NAME}/mipself.x ${CMAKE_EXE_LINKER_FLAGS}" )
1453*4882a593Smuzhiyunendif()
1454*4882a593Smuzhiyun
1455*4882a593Smuzhiyun# pie/pic
1456*4882a593Smuzhiyunif( NOT (ANDROID_NATIVE_API_LEVEL LESS 16) AND (NOT DEFINED ANDROID_APP_PIE OR ANDROID_APP_PIE) AND (CMAKE_VERSION VERSION_GREATER 2.8.8) )
1457*4882a593Smuzhiyun set( CMAKE_POSITION_INDEPENDENT_CODE TRUE )
1458*4882a593Smuzhiyun set( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fPIE -pie")
1459*4882a593Smuzhiyunelse()
1460*4882a593Smuzhiyun set( CMAKE_POSITION_INDEPENDENT_CODE FALSE )
1461*4882a593Smuzhiyun set( CMAKE_CXX_FLAGS "-fpic ${CMAKE_CXX_FLAGS}" )
1462*4882a593Smuzhiyun set( CMAKE_C_FLAGS   "-fpic ${CMAKE_C_FLAGS}" )
1463*4882a593Smuzhiyunendif()
1464*4882a593Smuzhiyun
1465*4882a593Smuzhiyun# configure rtti
1466*4882a593Smuzhiyunif( DEFINED ANDROID_RTTI AND ANDROID_STL_FORCE_FEATURES )
1467*4882a593Smuzhiyun if( ANDROID_RTTI )
1468*4882a593Smuzhiyun  set( CMAKE_CXX_FLAGS "-frtti ${CMAKE_CXX_FLAGS}" )
1469*4882a593Smuzhiyun else()
1470*4882a593Smuzhiyun  set( CMAKE_CXX_FLAGS "-fno-rtti ${CMAKE_CXX_FLAGS}" )
1471*4882a593Smuzhiyun endif()
1472*4882a593Smuzhiyunendif()
1473*4882a593Smuzhiyun
1474*4882a593Smuzhiyun# configure exceptios
1475*4882a593Smuzhiyunif( DEFINED ANDROID_EXCEPTIONS AND ANDROID_STL_FORCE_FEATURES )
1476*4882a593Smuzhiyun if( ANDROID_EXCEPTIONS )
1477*4882a593Smuzhiyun  set( CMAKE_CXX_FLAGS "-fexceptions ${CMAKE_CXX_FLAGS}" )
1478*4882a593Smuzhiyun  set( CMAKE_C_FLAGS "-fexceptions ${CMAKE_C_FLAGS}" )
1479*4882a593Smuzhiyun else()
1480*4882a593Smuzhiyun  set( CMAKE_CXX_FLAGS "-fno-exceptions ${CMAKE_CXX_FLAGS}" )
1481*4882a593Smuzhiyun  set( CMAKE_C_FLAGS "-fno-exceptions ${CMAKE_C_FLAGS}" )
1482*4882a593Smuzhiyun endif()
1483*4882a593Smuzhiyunendif()
1484*4882a593Smuzhiyun
1485*4882a593Smuzhiyun# global includes and link directories
1486*4882a593Smuzhiyuninclude_directories( SYSTEM "${ANDROID_SYSROOT}/usr/include" ${ANDROID_STL_INCLUDE_DIRS} )
1487*4882a593Smuzhiyun
1488*4882a593Smuzhiyun# ----------------------------------------------------------------------------
1489*4882a593Smuzhiyun# comment unused directory detection
1490*4882a593Smuzhiyun# ----------------------------------------------------------------------------
1491*4882a593Smuzhiyunif( DEFINED CMAKE_INSTALL_PREFIX )
1492*4882a593Smuzhiyun get_filename_component(__android_install_path "${CMAKE_INSTALL_PREFIX}/libs/${ANDROID_NDK_ABI_NAME}" ABSOLUTE) # avoid CMP0015 policy warning
1493*4882a593Smuzhiyun link_directories( "${__android_install_path}" )
1494*4882a593Smuzhiyunendif()
1495*4882a593Smuzhiyun
1496*4882a593Smuzhiyun# ----------------------------------------------------------------------------
1497*4882a593Smuzhiyun# add sysroot library
1498*4882a593Smuzhiyun# ----------------------------------------------------------------------------
1499*4882a593Smuzhiyunlink_directories( "${ANDROID_SYSROOT}/usr/lib" )
1500*4882a593Smuzhiyun
1501*4882a593Smuzhiyun# detect if need link crtbegin_so.o explicitly
1502*4882a593Smuzhiyunif( NOT DEFINED ANDROID_EXPLICIT_CRT_LINK )
1503*4882a593Smuzhiyun set( __cmd "${CMAKE_CXX_CREATE_SHARED_LIBRARY}" )
1504*4882a593Smuzhiyun string( REPLACE "<CMAKE_CXX_COMPILER>" "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}" __cmd "${__cmd}" )
1505*4882a593Smuzhiyun string( REPLACE "<CMAKE_C_COMPILER>"   "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}"   __cmd "${__cmd}" )
1506*4882a593Smuzhiyun string( REPLACE "<CMAKE_SHARED_LIBRARY_CXX_FLAGS>" "${CMAKE_CXX_FLAGS}" __cmd "${__cmd}" )
1507*4882a593Smuzhiyun string( REPLACE "<LANGUAGE_COMPILE_FLAGS>" "" __cmd "${__cmd}" )
1508*4882a593Smuzhiyun string( REPLACE "<LINK_FLAGS>" "${CMAKE_SHARED_LINKER_FLAGS}" __cmd "${__cmd}" )
1509*4882a593Smuzhiyun string( REPLACE "<CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS>" "-shared" __cmd "${__cmd}" )
1510*4882a593Smuzhiyun string( REPLACE "<CMAKE_SHARED_LIBRARY_SONAME_CXX_FLAG>" "" __cmd "${__cmd}" )
1511*4882a593Smuzhiyun string( REPLACE "<TARGET_SONAME>" "" __cmd "${__cmd}" )
1512*4882a593Smuzhiyun string( REPLACE "<TARGET>" "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/toolchain_crtlink_test.so" __cmd "${__cmd}" )
1513*4882a593Smuzhiyun string( REPLACE "<OBJECTS>" "\"${ANDROID_SYSROOT}/usr/lib/crtbegin_so.o\"" __cmd "${__cmd}" )
1514*4882a593Smuzhiyun string( REPLACE "<LINK_LIBRARIES>" "" __cmd "${__cmd}" )
1515*4882a593Smuzhiyun separate_arguments( __cmd )
1516*4882a593Smuzhiyun foreach( __var ANDROID_NDK ANDROID_NDK_TOOLCHAINS_PATH ANDROID_STANDALONE_TOOLCHAIN )
1517*4882a593Smuzhiyun  if( ${__var} )
1518*4882a593Smuzhiyun   set( __tmp "${${__var}}" )
1519*4882a593Smuzhiyun   separate_arguments( __tmp )
1520*4882a593Smuzhiyun   string( REPLACE "${__tmp}" "${${__var}}" __cmd "${__cmd}")
1521*4882a593Smuzhiyun  endif()
1522*4882a593Smuzhiyun endforeach()
1523*4882a593Smuzhiyun string( REPLACE "'" "" __cmd "${__cmd}" )
1524*4882a593Smuzhiyun string( REPLACE "\"" "" __cmd "${__cmd}" )
1525*4882a593Smuzhiyun execute_process( COMMAND ${__cmd} RESULT_VARIABLE __cmd_result OUTPUT_QUIET ERROR_QUIET )
1526*4882a593Smuzhiyun if( __cmd_result EQUAL 0 )
1527*4882a593Smuzhiyun  set( ANDROID_EXPLICIT_CRT_LINK ON )
1528*4882a593Smuzhiyun else()
1529*4882a593Smuzhiyun  set( ANDROID_EXPLICIT_CRT_LINK OFF )
1530*4882a593Smuzhiyun endif()
1531*4882a593Smuzhiyunendif()
1532*4882a593Smuzhiyun
1533*4882a593Smuzhiyunif( ANDROID_EXPLICIT_CRT_LINK )
1534*4882a593Smuzhiyun set( CMAKE_CXX_CREATE_SHARED_LIBRARY "${CMAKE_CXX_CREATE_SHARED_LIBRARY} \"${ANDROID_SYSROOT}/usr/lib/crtbegin_so.o\"" )
1535*4882a593Smuzhiyun set( CMAKE_CXX_CREATE_SHARED_MODULE  "${CMAKE_CXX_CREATE_SHARED_MODULE} \"${ANDROID_SYSROOT}/usr/lib/crtbegin_so.o\"" )
1536*4882a593Smuzhiyunendif()
1537*4882a593Smuzhiyun
1538*4882a593Smuzhiyun# setup output directories
1539*4882a593Smuzhiyunset( CMAKE_INSTALL_PREFIX "${ANDROID_TOOLCHAIN_ROOT}/user" CACHE STRING "path for installing" )
1540*4882a593Smuzhiyun
1541*4882a593Smuzhiyunif( DEFINED LIBRARY_OUTPUT_PATH_ROOT
1542*4882a593Smuzhiyun      OR EXISTS "${CMAKE_SOURCE_DIR}/AndroidManifest.xml"
1543*4882a593Smuzhiyun      OR (EXISTS "${CMAKE_SOURCE_DIR}/../AndroidManifest.xml" AND EXISTS "${CMAKE_SOURCE_DIR}/../jni/") )
1544*4882a593Smuzhiyun  set( LIBRARY_OUTPUT_PATH_ROOT ${CMAKE_SOURCE_DIR} CACHE PATH "Root for binaries output, set this to change where Android libs are installed to" )
1545*4882a593Smuzhiyun  if( NOT _CMAKE_IN_TRY_COMPILE )
1546*4882a593Smuzhiyun    if( EXISTS "${CMAKE_SOURCE_DIR}/jni/CMakeLists.txt" )
1547*4882a593Smuzhiyun      set( EXECUTABLE_OUTPUT_PATH "${LIBRARY_OUTPUT_PATH_ROOT}/bin/${ANDROID_NDK_ABI_NAME}" CACHE PATH "Output directory for applications" )
1548*4882a593Smuzhiyun    else()
1549*4882a593Smuzhiyun      set( EXECUTABLE_OUTPUT_PATH "${LIBRARY_OUTPUT_PATH_ROOT}/bin" CACHE PATH "Output directory for applications" )
1550*4882a593Smuzhiyun    endif()
1551*4882a593Smuzhiyun    set( LIBRARY_OUTPUT_PATH "${LIBRARY_OUTPUT_PATH_ROOT}/libs/${ANDROID_NDK_ABI_NAME}" CACHE PATH "Output directory for Android libs" )
1552*4882a593Smuzhiyun  endif()
1553*4882a593Smuzhiyunendif()
1554*4882a593Smuzhiyun
1555*4882a593Smuzhiyun# copy shaed stl library to build directory
1556*4882a593Smuzhiyunif( NOT _CMAKE_IN_TRY_COMPILE AND __libstl MATCHES "[.]so$" AND DEFINED LIBRARY_OUTPUT_PATH )
1557*4882a593Smuzhiyun  get_filename_component( __libstlname "${__libstl}" NAME )
1558*4882a593Smuzhiyun  execute_process( COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${__libstl}" "${LIBRARY_OUTPUT_PATH}/${__libstlname}" RESULT_VARIABLE __fileCopyProcess )
1559*4882a593Smuzhiyun  if( NOT __fileCopyProcess EQUAL 0 OR NOT EXISTS "${LIBRARY_OUTPUT_PATH}/${__libstlname}")
1560*4882a593Smuzhiyun    message( SEND_ERROR "Failed copying of ${__libstl} to the ${LIBRARY_OUTPUT_PATH}/${__libstlname}" )
1561*4882a593Smuzhiyun  endif()
1562*4882a593Smuzhiyun  unset( __fileCopyProcess )
1563*4882a593Smuzhiyun  unset( __libstlname )
1564*4882a593Smuzhiyunendif()
1565*4882a593Smuzhiyun
1566*4882a593Smuzhiyun
1567*4882a593Smuzhiyun# set these global flags for cmake client scripts to change behavior
1568*4882a593Smuzhiyunset( ANDROID True )
1569*4882a593Smuzhiyunset( BUILD_ANDROID True )
1570*4882a593Smuzhiyun
1571*4882a593Smuzhiyun# where is the target environment
1572*4882a593Smuzhiyunset( CMAKE_FIND_ROOT_PATH "${ANDROID_TOOLCHAIN_ROOT}/bin" "${ANDROID_TOOLCHAIN_ROOT}/${ANDROID_TOOLCHAIN_MACHINE_NAME}" "${ANDROID_SYSROOT}" "${CMAKE_INSTALL_PREFIX}" "${CMAKE_INSTALL_PREFIX}/share" )
1573*4882a593Smuzhiyun
1574*4882a593Smuzhiyun# only search for libraries and includes in the ndk toolchain
1575*4882a593Smuzhiyunset( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ONLY )
1576*4882a593Smuzhiyunset( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY )
1577*4882a593Smuzhiyunset( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY )
1578*4882a593Smuzhiyun
1579*4882a593Smuzhiyun
1580*4882a593Smuzhiyun# macro to find packages on the host OS
1581*4882a593Smuzhiyunmacro( find_host_package )
1582*4882a593Smuzhiyun set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER )
1583*4882a593Smuzhiyun set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY NEVER )
1584*4882a593Smuzhiyun set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE NEVER )
1585*4882a593Smuzhiyun if( CMAKE_HOST_WIN32 )
1586*4882a593Smuzhiyun  SET( WIN32 1 )
1587*4882a593Smuzhiyun  SET( UNIX )
1588*4882a593Smuzhiyun elseif( CMAKE_HOST_APPLE )
1589*4882a593Smuzhiyun  SET( APPLE 1 )
1590*4882a593Smuzhiyun  SET( UNIX )
1591*4882a593Smuzhiyun endif()
1592*4882a593Smuzhiyun find_package( ${ARGN} )
1593*4882a593Smuzhiyun SET( WIN32 )
1594*4882a593Smuzhiyun SET( APPLE )
1595*4882a593Smuzhiyun SET( UNIX 1 )
1596*4882a593Smuzhiyun set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ONLY )
1597*4882a593Smuzhiyun set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY )
1598*4882a593Smuzhiyun set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY )
1599*4882a593Smuzhiyunendmacro()
1600*4882a593Smuzhiyun
1601*4882a593Smuzhiyun
1602*4882a593Smuzhiyun# macro to find programs on the host OS
1603*4882a593Smuzhiyunmacro( find_host_program )
1604*4882a593Smuzhiyun set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER )
1605*4882a593Smuzhiyun set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY NEVER )
1606*4882a593Smuzhiyun set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE NEVER )
1607*4882a593Smuzhiyun if( CMAKE_HOST_WIN32 )
1608*4882a593Smuzhiyun  SET( WIN32 1 )
1609*4882a593Smuzhiyun  SET( UNIX )
1610*4882a593Smuzhiyun elseif( CMAKE_HOST_APPLE )
1611*4882a593Smuzhiyun  SET( APPLE 1 )
1612*4882a593Smuzhiyun  SET( UNIX )
1613*4882a593Smuzhiyun endif()
1614*4882a593Smuzhiyun find_program( ${ARGN} )
1615*4882a593Smuzhiyun SET( WIN32 )
1616*4882a593Smuzhiyun SET( APPLE )
1617*4882a593Smuzhiyun SET( UNIX 1 )
1618*4882a593Smuzhiyun set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ONLY )
1619*4882a593Smuzhiyun set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY )
1620*4882a593Smuzhiyun set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY )
1621*4882a593Smuzhiyunendmacro()
1622*4882a593Smuzhiyun
1623*4882a593Smuzhiyun
1624*4882a593Smuzhiyun# export toolchain settings for the try_compile() command
1625*4882a593Smuzhiyunif( NOT _CMAKE_IN_TRY_COMPILE )
1626*4882a593Smuzhiyun set( __toolchain_config "")
1627*4882a593Smuzhiyun foreach( __var NDK_CCACHE  LIBRARY_OUTPUT_PATH_ROOT  ANDROID_FORBID_SYGWIN
1628*4882a593Smuzhiyun                ANDROID_NDK_HOST_X64
1629*4882a593Smuzhiyun                ANDROID_NDK
1630*4882a593Smuzhiyun                ANDROID_NDK_LAYOUT
1631*4882a593Smuzhiyun                ANDROID_STANDALONE_TOOLCHAIN
1632*4882a593Smuzhiyun                ANDROID_TOOLCHAIN_NAME
1633*4882a593Smuzhiyun                ANDROID_ABI
1634*4882a593Smuzhiyun                ANDROID_NATIVE_API_LEVEL
1635*4882a593Smuzhiyun                ANDROID_STL
1636*4882a593Smuzhiyun                ANDROID_STL_FORCE_FEATURES
1637*4882a593Smuzhiyun                ANDROID_FORCE_ARM_BUILD
1638*4882a593Smuzhiyun                ANDROID_NO_UNDEFINED
1639*4882a593Smuzhiyun                ANDROID_SO_UNDEFINED
1640*4882a593Smuzhiyun                ANDROID_FUNCTION_LEVEL_LINKING
1641*4882a593Smuzhiyun                ANDROID_GOLD_LINKER
1642*4882a593Smuzhiyun                ANDROID_NOEXECSTACK
1643*4882a593Smuzhiyun                ANDROID_RELRO
1644*4882a593Smuzhiyun                ANDROID_LIBM_PATH
1645*4882a593Smuzhiyun                ANDROID_EXPLICIT_CRT_LINK
1646*4882a593Smuzhiyun                ANDROID_APP_PIE
1647*4882a593Smuzhiyun                )
1648*4882a593Smuzhiyun  if( DEFINED ${__var} )
1649*4882a593Smuzhiyun   if( ${__var} MATCHES " ")
1650*4882a593Smuzhiyun    set( __toolchain_config "${__toolchain_config}set( ${__var} \"${${__var}}\" CACHE INTERNAL \"\" )\n" )
1651*4882a593Smuzhiyun   else()
1652*4882a593Smuzhiyun    set( __toolchain_config "${__toolchain_config}set( ${__var} ${${__var}} CACHE INTERNAL \"\" )\n" )
1653*4882a593Smuzhiyun   endif()
1654*4882a593Smuzhiyun  endif()
1655*4882a593Smuzhiyun endforeach()
1656*4882a593Smuzhiyun file( WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/android.toolchain.config.cmake" "${__toolchain_config}" )
1657*4882a593Smuzhiyun unset( __toolchain_config )
1658*4882a593Smuzhiyunendif()
1659*4882a593Smuzhiyun
1660*4882a593Smuzhiyun
1661*4882a593Smuzhiyun# force cmake to produce / instead of \ in build commands for Ninja generator
1662*4882a593Smuzhiyunif( CMAKE_GENERATOR MATCHES "Ninja" AND CMAKE_HOST_WIN32 )
1663*4882a593Smuzhiyun # it is a bad hack after all
1664*4882a593Smuzhiyun # CMake generates Ninja makefiles with UNIX paths only if it thinks that we are going to build with MinGW
1665*4882a593Smuzhiyun set( CMAKE_COMPILER_IS_MINGW TRUE ) # tell CMake that we are MinGW
1666*4882a593Smuzhiyun set( CMAKE_CROSSCOMPILING TRUE )    # stop recursion
1667*4882a593Smuzhiyun enable_language( C )
1668*4882a593Smuzhiyun enable_language( CXX )
1669*4882a593Smuzhiyun # unset( CMAKE_COMPILER_IS_MINGW ) # can't unset because CMake does not convert back-slashes in response files without it
1670*4882a593Smuzhiyun unset( MINGW )
1671*4882a593Smuzhiyunendif()
1672*4882a593Smuzhiyun
1673*4882a593Smuzhiyun
1674*4882a593Smuzhiyun# Variables controlling behavior or set by cmake toolchain:
1675*4882a593Smuzhiyun#   ANDROID_ABI : "armeabi-v7a" (default), "armeabi", "armeabi-v7a with NEON", "armeabi-v7a with VFPV3", "armeabi-v6 with VFP", "x86", "mips", "arm64-v8a", "x86_64", "mips64"
1676*4882a593Smuzhiyun#   ANDROID_NATIVE_API_LEVEL : 3,4,5,8,9,14,15,16,17,18,19,21 (depends on NDK version)
1677*4882a593Smuzhiyun#   ANDROID_STL : gnustl_static/gnustl_shared/stlport_static/stlport_shared/gabi++_static/gabi++_shared/system_re/system/none
1678*4882a593Smuzhiyun#   ANDROID_FORBID_SYGWIN : ON/OFF
1679*4882a593Smuzhiyun#   ANDROID_NO_UNDEFINED : ON/OFF
1680*4882a593Smuzhiyun#   ANDROID_SO_UNDEFINED : OFF/ON  (default depends on NDK version)
1681*4882a593Smuzhiyun#   ANDROID_FUNCTION_LEVEL_LINKING : ON/OFF
1682*4882a593Smuzhiyun#   ANDROID_GOLD_LINKER : ON/OFF
1683*4882a593Smuzhiyun#   ANDROID_NOEXECSTACK : ON/OFF
1684*4882a593Smuzhiyun#   ANDROID_RELRO : ON/OFF
1685*4882a593Smuzhiyun#   ANDROID_FORCE_ARM_BUILD : ON/OFF
1686*4882a593Smuzhiyun#   ANDROID_STL_FORCE_FEATURES : ON/OFF
1687*4882a593Smuzhiyun#   ANDROID_LIBM_PATH : path to libm.so (set to something like $(TOP)/out/target/product/<product_name>/obj/lib/libm.so) to workaround unresolved `sincos`
1688*4882a593Smuzhiyun# Can be set only at the first run:
1689*4882a593Smuzhiyun#   ANDROID_NDK : path to your NDK install
1690*4882a593Smuzhiyun#   NDK_CCACHE : path to your ccache executable
1691*4882a593Smuzhiyun#   ANDROID_TOOLCHAIN_NAME : the NDK name of compiler toolchain
1692*4882a593Smuzhiyun#   ANDROID_NDK_HOST_X64 : try to use x86_64 toolchain (default for x64 host systems)
1693*4882a593Smuzhiyun#   ANDROID_NDK_LAYOUT : the inner NDK structure (RELEASE, LINARO, ANDROID)
1694*4882a593Smuzhiyun#   LIBRARY_OUTPUT_PATH_ROOT : <any valid path>
1695*4882a593Smuzhiyun#   ANDROID_STANDALONE_TOOLCHAIN
1696*4882a593Smuzhiyun#
1697*4882a593Smuzhiyun# Primary read-only variables:
1698*4882a593Smuzhiyun#   ANDROID : always TRUE
1699*4882a593Smuzhiyun#   ARMEABI : TRUE for arm v6 and older devices
1700*4882a593Smuzhiyun#   ARMEABI_V6 : TRUE for arm v6
1701*4882a593Smuzhiyun#   ARMEABI_V7A : TRUE for arm v7a
1702*4882a593Smuzhiyun#   ARM64_V8A : TRUE for arm64-v8a
1703*4882a593Smuzhiyun#   NEON : TRUE if NEON unit is enabled
1704*4882a593Smuzhiyun#   VFPV3 : TRUE if VFP version 3 is enabled
1705*4882a593Smuzhiyun#   X86 : TRUE if configured for x86
1706*4882a593Smuzhiyun#   X86_64 : TRUE if configured for x86_64
1707*4882a593Smuzhiyun#   MIPS : TRUE if configured for mips
1708*4882a593Smuzhiyun#   MIPS64 : TRUE if configured for mips64
1709*4882a593Smuzhiyun#   BUILD_WITH_ANDROID_NDK : TRUE if NDK is used
1710*4882a593Smuzhiyun#   BUILD_WITH_STANDALONE_TOOLCHAIN : TRUE if standalone toolchain is used
1711*4882a593Smuzhiyun#   ANDROID_NDK_HOST_SYSTEM_NAME : "windows", "linux-x86" or "darwin-x86" depending on host platform
1712*4882a593Smuzhiyun#   ANDROID_NDK_ABI_NAME : "armeabi", "armeabi-v7a", "x86", "mips", "arm64-v8a", "x86_64", "mips64" depending on ANDROID_ABI
1713*4882a593Smuzhiyun#   ANDROID_NDK_RELEASE : from r5 to r10d; set only for NDK
1714*4882a593Smuzhiyun#   ANDROID_NDK_RELEASE_NUM : numeric ANDROID_NDK_RELEASE version (1000*major+minor)
1715*4882a593Smuzhiyun#   ANDROID_ARCH_NAME : "arm", "x86", "mips", "arm64", "x86_64", "mips64" depending on ANDROID_ABI
1716*4882a593Smuzhiyun#   ANDROID_SYSROOT : path to the compiler sysroot
1717*4882a593Smuzhiyun#   TOOL_OS_SUFFIX : "" or ".exe" depending on host platform
1718*4882a593Smuzhiyun#   ANDROID_COMPILER_IS_CLANG : TRUE if clang compiler is used
1719*4882a593Smuzhiyun#
1720*4882a593Smuzhiyun# Secondary (less stable) read-only variables:
1721*4882a593Smuzhiyun#   ANDROID_COMPILER_VERSION : GCC version used (not Clang version)
1722*4882a593Smuzhiyun#   ANDROID_CLANG_VERSION : version of clang compiler if clang is used
1723*4882a593Smuzhiyun#   ANDROID_CXX_FLAGS : C/C++ compiler flags required by Android platform
1724*4882a593Smuzhiyun#   ANDROID_SUPPORTED_ABIS : list of currently allowed values for ANDROID_ABI
1725*4882a593Smuzhiyun#   ANDROID_TOOLCHAIN_MACHINE_NAME : "arm-linux-androideabi", "arm-eabi" or "i686-android-linux"
1726*4882a593Smuzhiyun#   ANDROID_TOOLCHAIN_ROOT : path to the top level of toolchain (standalone or placed inside NDK)
1727*4882a593Smuzhiyun#   ANDROID_CLANG_TOOLCHAIN_ROOT : path to clang tools
1728*4882a593Smuzhiyun#   ANDROID_SUPPORTED_NATIVE_API_LEVELS : list of native API levels found inside NDK
1729*4882a593Smuzhiyun#   ANDROID_STL_INCLUDE_DIRS : stl include paths
1730*4882a593Smuzhiyun#   ANDROID_RTTI : if rtti is enabled by the runtime
1731*4882a593Smuzhiyun#   ANDROID_EXCEPTIONS : if exceptions are enabled by the runtime
1732*4882a593Smuzhiyun#   ANDROID_GCC_TOOLCHAIN_NAME : read-only, differs from ANDROID_TOOLCHAIN_NAME only if clang is used
1733*4882a593Smuzhiyun#
1734*4882a593Smuzhiyun# Defaults:
1735*4882a593Smuzhiyun#   ANDROID_DEFAULT_NDK_API_LEVEL
1736*4882a593Smuzhiyun#   ANDROID_DEFAULT_NDK_API_LEVEL_${ARCH}
1737*4882a593Smuzhiyun#   ANDROID_NDK_SEARCH_PATHS
1738*4882a593Smuzhiyun#   ANDROID_SUPPORTED_ABIS_${ARCH}
1739*4882a593Smuzhiyun#   ANDROID_SUPPORTED_NDK_VERSIONS
1740