android.toolchain.cmake 85 KB

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