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