iOS.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. # This file is part of the ios-cmake project. It was retrieved from
  2. # https://github.com/cristeab/ios-cmake.git, which is a fork of
  3. # https://code.google.com/p/ios-cmake/. Which in turn is based off of
  4. # the Platform/Darwin.cmake and Platform/UnixPaths.cmake files which
  5. # are included with CMake 2.8.4
  6. #
  7. # The ios-cmake project is licensed under the new BSD license.
  8. #
  9. # Copyright (c) 2014, Bogdan Cristea and LTE Engineering Software,
  10. # Kitware, Inc., Insight Software Consortium. All rights reserved.
  11. # Redistribution and use in source and binary forms, with or without
  12. # modification, are permitted provided that the following conditions
  13. # are met:
  14. # 1. Redistributions of source code must retain the above copyright
  15. # notice, this list of conditions and the following disclaimer.
  16. #
  17. # 2. Redistributions in binary form must reproduce the above copyright
  18. # notice, this list of conditions and the following disclaimer in the
  19. # documentation and/or other materials provided with the distribution.
  20. #
  21. # 3. Neither the name of the copyright holder nor the names of its
  22. # contributors may be used to endorse or promote products derived from
  23. # this software without specific prior written permission.
  24. #
  25. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  28. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  29. # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  30. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  32. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  35. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. # POSSIBILITY OF SUCH DAMAGE.
  37. #
  38. # This file is based off of the Platform/Darwin.cmake and
  39. # Platform/UnixPaths.cmake files which are included with CMake 2.8.4
  40. # It has been altered for iOS development.
  41. #
  42. # Updated by Alex Stewart (alexs.mac@gmail.com).
  43. # The following variables control the behaviour of this toolchain:
  44. #
  45. # IOS_PLATFORM: OS (default) or SIMULATOR or SIMULATOR64
  46. # OS = Build for iPhoneOS.
  47. # SIMULATOR = Build for x86 i386 iPhone Simulator.
  48. # SIMULATOR64 = Build for x86 x86_64 iPhone Simulator.
  49. # CMAKE_OSX_SYSROOT: Path to the iOS SDK to use. By default this is
  50. # automatically determined from IOS_PLATFORM and xcodebuild, but
  51. # can also be manually specified (although this should not be required).
  52. # CMAKE_IOS_DEVELOPER_ROOT: Path to the Developer directory for the iOS platform
  53. # being compiled for. By default this is automatically determined from
  54. # CMAKE_OSX_SYSROOT, but can also be manually specified (although this should
  55. # not be required).
  56. #
  57. # This toolchain defines the following variables for use externally:
  58. #
  59. # XCODE_VERSION: Version number (not including Build version) of Xcode detected.
  60. # IOS_SDK_VERSION: Version of iOS SDK being used.
  61. # CMAKE_OSX_ARCHITECTURES: Architectures being compiled for (generated from
  62. # IOS_PLATFORM).
  63. #
  64. # This toolchain defines the following macros for use externally:
  65. #
  66. # set_xcode_property (TARGET XCODE_PROPERTY XCODE_VALUE)
  67. # A convenience macro for setting xcode specific properties on targets
  68. # example: set_xcode_property (myioslib IPHONEOS_DEPLOYMENT_TARGET "3.1").
  69. #
  70. # find_host_package (PROGRAM ARGS)
  71. # A macro used to find executable programs on the host system, not within the
  72. # iOS environment. Thanks to the android-cmake project for providing the
  73. # command.
  74. # Get the Xcode version being used.
  75. execute_process(COMMAND xcodebuild -version
  76. OUTPUT_VARIABLE XCODE_VERSION
  77. ERROR_QUIET
  78. OUTPUT_STRIP_TRAILING_WHITESPACE)
  79. string(REGEX MATCH "Xcode [0-9\\.]+" XCODE_VERSION "${XCODE_VERSION}")
  80. string(REGEX REPLACE "Xcode ([0-9\\.]+)" "\\1" XCODE_VERSION "${XCODE_VERSION}")
  81. message(STATUS "Building with Xcode version: ${XCODE_VERSION}")
  82. # Default to building for iPhoneOS if not specified otherwise, and we cannot
  83. # determine the platform from the CMAKE_OSX_ARCHITECTURES variable. The use
  84. # of CMAKE_OSX_ARCHITECTURES is such that try_compile() projects can correctly
  85. # determine the value of IOS_PLATFORM from the root project, as
  86. # CMAKE_OSX_ARCHITECTURES is propagated to them by CMake.
  87. if (NOT DEFINED IOS_PLATFORM)
  88. if (CMAKE_OSX_ARCHITECTURES)
  89. if (CMAKE_OSX_ARCHITECTURES MATCHES ".*arm.*")
  90. set(IOS_PLATFORM "OS")
  91. elseif (CMAKE_OSX_ARCHITECTURES MATCHES "i386")
  92. set(IOS_PLATFORM "SIMULATOR")
  93. elseif (CMAKE_OSX_ARCHITECTURES MATCHES "x86_64")
  94. set(IOS_PLATFORM "SIMULATOR64")
  95. endif()
  96. endif()
  97. if (NOT IOS_PLATFORM)
  98. set(IOS_PLATFORM "OS")
  99. endif()
  100. endif()
  101. set(IOS_PLATFORM ${IOS_PLATFORM} CACHE STRING
  102. "Type of iOS platform for which to build.")
  103. # Determine the platform name and architectures for use in xcodebuild commands
  104. # from the specified IOS_PLATFORM name.
  105. if (IOS_PLATFORM STREQUAL "OS")
  106. set(XCODE_IOS_PLATFORM iphoneos)
  107. set(IOS_ARCH armv7 armv7s arm64)
  108. elseif (IOS_PLATFORM STREQUAL "SIMULATOR")
  109. set(XCODE_IOS_PLATFORM iphonesimulator)
  110. set(IOS_ARCH i386)
  111. elseif(IOS_PLATFORM STREQUAL "SIMULATOR64")
  112. set(XCODE_IOS_PLATFORM iphonesimulator)
  113. set(IOS_ARCH x86_64)
  114. else()
  115. message(FATAL_ERROR "Invalid IOS_PLATFORM: ${IOS_PLATFORM}")
  116. endif()
  117. # If user did not specify the SDK root to use, then query xcodebuild for it.
  118. if (NOT CMAKE_OSX_SYSROOT)
  119. execute_process(COMMAND xcodebuild -version -sdk ${XCODE_IOS_PLATFORM} Path
  120. OUTPUT_VARIABLE CMAKE_OSX_SYSROOT
  121. ERROR_QUIET
  122. OUTPUT_STRIP_TRAILING_WHITESPACE)
  123. message(STATUS "Using SDK: ${CMAKE_OSX_SYSROOT} for platform: ${IOS_PLATFORM}")
  124. endif()
  125. if (NOT EXISTS ${CMAKE_OSX_SYSROOT})
  126. message(FATAL_ERROR "Invalid CMAKE_OSX_SYSROOT: ${CMAKE_OSX_SYSROOT} "
  127. "does not exist.")
  128. endif()
  129. # Get the SDK version information.
  130. execute_process(COMMAND xcodebuild -sdk ${CMAKE_OSX_SYSROOT} -version SDKVersion
  131. OUTPUT_VARIABLE IOS_SDK_VERSION
  132. ERROR_QUIET
  133. OUTPUT_STRIP_TRAILING_WHITESPACE)
  134. # Find the Developer root for the specific iOS platform being compiled for
  135. # from CMAKE_OSX_SYSROOT. Should be ../../ from SDK specified in
  136. # CMAKE_OSX_SYSROOT. There does not appear to be a direct way to obtain
  137. # this information from xcrun or xcodebuild.
  138. if (NOT CMAKE_IOS_DEVELOPER_ROOT)
  139. get_filename_component(IOS_PLATFORM_SDK_DIR ${CMAKE_OSX_SYSROOT} PATH)
  140. get_filename_component(CMAKE_IOS_DEVELOPER_ROOT ${IOS_PLATFORM_SDK_DIR} PATH)
  141. endif()
  142. if (NOT EXISTS ${CMAKE_IOS_DEVELOPER_ROOT})
  143. message(FATAL_ERROR "Invalid CMAKE_IOS_DEVELOPER_ROOT: "
  144. "${CMAKE_IOS_DEVELOPER_ROOT} does not exist.")
  145. endif()
  146. # Find the C & C++ compilers for the specified SDK.
  147. if (NOT CMAKE_C_COMPILER)
  148. execute_process(COMMAND xcrun -sdk ${CMAKE_OSX_SYSROOT} -find clang
  149. OUTPUT_VARIABLE CMAKE_C_COMPILER
  150. ERROR_QUIET
  151. OUTPUT_STRIP_TRAILING_WHITESPACE)
  152. message(STATUS "Using C compiler: ${CMAKE_C_COMPILER}")
  153. endif()
  154. if (NOT CMAKE_CXX_COMPILER)
  155. execute_process(COMMAND xcrun -sdk ${CMAKE_OSX_SYSROOT} -find clang++
  156. OUTPUT_VARIABLE CMAKE_CXX_COMPILER
  157. ERROR_QUIET
  158. OUTPUT_STRIP_TRAILING_WHITESPACE)
  159. message(STATUS "Using CXX compiler: ${CMAKE_CXX_COMPILER}")
  160. endif()
  161. # Find (Apple's) libtool.
  162. execute_process(COMMAND xcrun -sdk ${CMAKE_OSX_SYSROOT} -find libtool
  163. OUTPUT_VARIABLE IOS_LIBTOOL
  164. ERROR_QUIET
  165. OUTPUT_STRIP_TRAILING_WHITESPACE)
  166. message(STATUS "Using libtool: ${IOS_LIBTOOL}")
  167. # Configure libtool to be used instead of ar + ranlib to build static libraries.
  168. # This is required on Xcode 7+, but should also work on previous versions of
  169. # Xcode.
  170. set(CMAKE_C_CREATE_STATIC_LIBRARY
  171. "${IOS_LIBTOOL} -static -o <TARGET> <LINK_FLAGS> <OBJECTS> ")
  172. set(CMAKE_CXX_CREATE_STATIC_LIBRARY
  173. "${IOS_LIBTOOL} -static -o <TARGET> <LINK_FLAGS> <OBJECTS> ")
  174. # Get the version of Darwin (OS X) of the host.
  175. execute_process(COMMAND uname -r
  176. OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_VERSION
  177. ERROR_QUIET
  178. OUTPUT_STRIP_TRAILING_WHITESPACE)
  179. # Specify minimum version of deployment target.
  180. # Unless specified, the latest SDK version is used by default.
  181. set(IOS_DEPLOYMENT_TARGET "${IOS_SDK_VERSION}"
  182. CACHE STRING "Minimum iOS version to build for." )
  183. message(STATUS "Building for minimum iOS version: ${IOS_DEPLOYMENT_TARGET}"
  184. " (SDK version: ${IOS_SDK_VERSION})")
  185. if (NOT IOS_DEPLOYMENT_TARGET VERSION_LESS 11.0)
  186. # iOS 11+ does not support 32-bit architectures (armv7).
  187. foreach(ARCH ${IOS_ARCH})
  188. if (ARCH MATCHES "armv7*")
  189. message(STATUS "Removing iOS architecture: ${ARCH} from build as it is "
  190. "not supported by the minimum iOS version to build for: "
  191. "${IOS_DEPLOYMENT_TARGET} (iOS >= 11 requires 64-bit).")
  192. else()
  193. list(APPEND VALID_IOS_ARCH_FOR_SDK_VERSION ${ARCH})
  194. endif()
  195. endforeach()
  196. set(IOS_ARCH ${VALID_IOS_ARCH_FOR_SDK_VERSION})
  197. endif()
  198. message(STATUS "Configuring iOS build for platform: ${IOS_PLATFORM}, "
  199. "architecture(s): ${IOS_ARCH}")
  200. # Standard settings.
  201. set(CMAKE_SYSTEM_NAME Darwin)
  202. set(CMAKE_SYSTEM_VERSION ${IOS_SDK_VERSION})
  203. set(UNIX TRUE)
  204. set(APPLE TRUE)
  205. set(IOS TRUE)
  206. # Force unset of OS X-specific deployment target (otherwise autopopulated),
  207. # required as of cmake 2.8.10.
  208. set(CMAKE_OSX_DEPLOYMENT_TARGET "" CACHE STRING
  209. "Must be empty for iOS builds." FORCE)
  210. # Set the architectures for which to build.
  211. set(CMAKE_OSX_ARCHITECTURES ${IOS_ARCH} CACHE STRING "Build architecture for iOS")
  212. # All iOS/Darwin specific settings - some may be redundant.
  213. set(CMAKE_SHARED_LIBRARY_PREFIX "lib")
  214. set(CMAKE_SHARED_LIBRARY_SUFFIX ".dylib")
  215. set(CMAKE_SHARED_MODULE_PREFIX "lib")
  216. set(CMAKE_SHARED_MODULE_SUFFIX ".so")
  217. set(CMAKE_MODULE_EXISTS 1)
  218. set(CMAKE_DL_LIBS "")
  219. set(CMAKE_C_OSX_COMPATIBILITY_VERSION_FLAG "-compatibility_version ")
  220. set(CMAKE_C_OSX_CURRENT_VERSION_FLAG "-current_version ")
  221. set(CMAKE_CXX_OSX_COMPATIBILITY_VERSION_FLAG "${CMAKE_C_OSX_COMPATIBILITY_VERSION_FLAG}")
  222. set(CMAKE_CXX_OSX_CURRENT_VERSION_FLAG "${CMAKE_C_OSX_CURRENT_VERSION_FLAG}")
  223. # Note that only Xcode 7+ supports the newer more specific:
  224. # -m${XCODE_IOS_PLATFORM}-version-min flags, older versions of Xcode use:
  225. # -m(ios/ios-simulator)-version-min instead.
  226. if (XCODE_VERSION VERSION_LESS 7.0)
  227. if (IOS_PLATFORM STREQUAL "OS")
  228. set(XCODE_IOS_PLATFORM_VERSION_FLAGS
  229. "-mios-version-min=${IOS_DEPLOYMENT_TARGET}")
  230. else()
  231. # SIMULATOR or SIMULATOR64 both use -mios-simulator-version-min.
  232. set(XCODE_IOS_PLATFORM_VERSION_FLAGS
  233. "-mios-simulator-version-min=${IOS_DEPLOYMENT_TARGET}")
  234. endif()
  235. else()
  236. # Xcode 7.0+ uses flags we can build directly from XCODE_IOS_PLATFORM.
  237. set(XCODE_IOS_PLATFORM_VERSION_FLAGS
  238. "-m${XCODE_IOS_PLATFORM}-version-min=${IOS_DEPLOYMENT_TARGET}")
  239. endif()
  240. set(CMAKE_C_FLAGS
  241. "${XCODE_IOS_PLATFORM_VERSION_FLAGS} -fobjc-abi-version=2 -fobjc-arc ${CMAKE_C_FLAGS}")
  242. # Hidden visibility is required for C++ on iOS.
  243. set(CMAKE_CXX_FLAGS
  244. "${XCODE_IOS_PLATFORM_VERSION_FLAGS} -fvisibility=hidden -fvisibility-inlines-hidden -fobjc-abi-version=2 -fobjc-arc ${CMAKE_CXX_FLAGS}")
  245. set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG -O3 -fomit-frame-pointer -ffast-math ${CMAKE_CXX_FLAGS_RELEASE}")
  246. set(CMAKE_C_LINK_FLAGS "${XCODE_IOS_PLATFORM_VERSION_FLAGS} -Wl,-search_paths_first ${CMAKE_C_LINK_FLAGS}")
  247. set(CMAKE_CXX_LINK_FLAGS "${XCODE_IOS_PLATFORM_VERSION_FLAGS} -Wl,-search_paths_first ${CMAKE_CXX_LINK_FLAGS}")
  248. # In order to ensure that the updated compiler flags are used in try_compile()
  249. # tests, we have to forcibly set them in the CMake cache, not merely set them
  250. # in the local scope.
  251. list(APPEND VARS_TO_FORCE_IN_CACHE
  252. CMAKE_C_FLAGS
  253. CMAKE_CXX_FLAGS
  254. CMAKE_CXX_RELEASE
  255. CMAKE_C_LINK_FLAGS
  256. CMAKE_CXX_LINK_FLAGS)
  257. foreach(VAR_TO_FORCE ${VARS_TO_FORCE_IN_CACHE})
  258. set(${VAR_TO_FORCE} "${${VAR_TO_FORCE}}" CACHE STRING "" FORCE)
  259. endforeach()
  260. set(CMAKE_PLATFORM_HAS_INSTALLNAME 1)
  261. set(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-dynamiclib -headerpad_max_install_names")
  262. set(CMAKE_SHARED_MODULE_CREATE_C_FLAGS "-bundle -headerpad_max_install_names")
  263. set(CMAKE_SHARED_MODULE_LOADER_C_FLAG "-Wl,-bundle_loader,")
  264. set(CMAKE_SHARED_MODULE_LOADER_CXX_FLAG "-Wl,-bundle_loader,")
  265. set(CMAKE_FIND_LIBRARY_SUFFIXES ".dylib" ".so" ".a")
  266. # Hack: if a new cmake (which uses CMAKE_INSTALL_NAME_TOOL) runs on an old
  267. # build tree (where install_name_tool was hardcoded) and where
  268. # CMAKE_INSTALL_NAME_TOOL isn't in the cache and still cmake didn't fail in
  269. # CMakeFindBinUtils.cmake (because it isn't rerun) hardcode
  270. # CMAKE_INSTALL_NAME_TOOL here to install_name_tool, so it behaves as it did
  271. # before, Alex.
  272. if (NOT DEFINED CMAKE_INSTALL_NAME_TOOL)
  273. find_program(CMAKE_INSTALL_NAME_TOOL install_name_tool)
  274. endif (NOT DEFINED CMAKE_INSTALL_NAME_TOOL)
  275. # Set the find root to the iOS developer roots and to user defined paths.
  276. set(CMAKE_FIND_ROOT_PATH ${CMAKE_IOS_DEVELOPER_ROOT} ${CMAKE_OSX_SYSROOT}
  277. ${CMAKE_PREFIX_PATH} CACHE STRING "iOS find search path root" FORCE)
  278. # Default to searching for frameworks first.
  279. set(CMAKE_FIND_FRAMEWORK FIRST)
  280. # Set up the default search directories for frameworks.
  281. set(CMAKE_SYSTEM_FRAMEWORK_PATH
  282. ${CMAKE_OSX_SYSROOT}/System/Library/Frameworks
  283. ${CMAKE_OSX_SYSROOT}/System/Library/PrivateFrameworks
  284. ${CMAKE_OSX_SYSROOT}/Developer/Library/Frameworks)
  285. # Only search the specified iOS SDK, not the remainder of the host filesystem.
  286. set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
  287. set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
  288. set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
  289. set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
  290. # This little macro lets you set any XCode specific property.
  291. macro(set_xcode_property TARGET XCODE_PROPERTY XCODE_VALUE)
  292. set_property(TARGET ${TARGET} PROPERTY
  293. XCODE_ATTRIBUTE_${XCODE_PROPERTY} ${XCODE_VALUE})
  294. endmacro(set_xcode_property)
  295. # This macro lets you find executable programs on the host system.
  296. macro(find_host_package)
  297. set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY NEVER)
  298. set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE NEVER)
  299. set(IOS FALSE)
  300. find_package(${ARGN})
  301. set(IOS TRUE)
  302. set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
  303. set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
  304. endmacro(find_host_package)