Darwin-Initialize.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. # Ask xcode-select where to find /Developer or fall back to ancient location.
  2. execute_process(COMMAND xcode-select -print-path
  3. OUTPUT_VARIABLE _stdout
  4. OUTPUT_STRIP_TRAILING_WHITESPACE
  5. ERROR_VARIABLE _stderr
  6. RESULT_VARIABLE _failed)
  7. if(NOT _failed AND IS_DIRECTORY ${_stdout})
  8. set(OSX_DEVELOPER_ROOT ${_stdout})
  9. elseif(IS_DIRECTORY "/Developer")
  10. set(OSX_DEVELOPER_ROOT "/Developer")
  11. else()
  12. set(OSX_DEVELOPER_ROOT "")
  13. endif()
  14. execute_process(COMMAND sw_vers -productVersion
  15. OUTPUT_VARIABLE CURRENT_OSX_VERSION
  16. OUTPUT_STRIP_TRAILING_WHITESPACE)
  17. # Save CMAKE_OSX_ARCHITECTURES from the environment.
  18. set(CMAKE_OSX_ARCHITECTURES "$ENV{CMAKE_OSX_ARCHITECTURES}" CACHE STRING
  19. "Build architectures for OSX")
  20. if(NOT CMAKE_CROSSCOMPILING AND
  21. CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND
  22. CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "^(arm64|x86_64)$")
  23. execute_process(COMMAND sysctl -q hw.optional.arm64
  24. OUTPUT_VARIABLE _sysctl_stdout
  25. ERROR_VARIABLE _sysctl_stderr
  26. RESULT_VARIABLE _sysctl_result
  27. )
  28. # When building on an Apple Silicon host, we need to explicitly specify
  29. # the architecture to the toolchain since it will otherwise guess the
  30. # architecture based on that of the build system tool.
  31. # Set an *internal variable* to tell the generators to do this.
  32. if(_sysctl_result EQUAL 0 AND _sysctl_stdout MATCHES "hw.optional.arm64: 1")
  33. set(_CMAKE_APPLE_ARCHS_DEFAULT "${CMAKE_HOST_SYSTEM_PROCESSOR}")
  34. endif()
  35. unset(_sysctl_result)
  36. unset(_sysctl_stderr)
  37. unset(_sysctl_stdout)
  38. endif()
  39. # macOS, iOS, tvOS, and watchOS should lookup compilers from
  40. # Platform/Apple-${CMAKE_CXX_COMPILER_ID}-<LANG>
  41. set(CMAKE_EFFECTIVE_SYSTEM_NAME "Apple")
  42. #----------------------------------------------------------------------------
  43. # _CURRENT_OSX_VERSION - as a two-component string: 10.5, 10.6, ...
  44. #
  45. string(REGEX REPLACE "^([0-9]+\\.[0-9]+).*$" "\\1"
  46. _CURRENT_OSX_VERSION "${CURRENT_OSX_VERSION}")
  47. #----------------------------------------------------------------------------
  48. # CMAKE_OSX_DEPLOYMENT_TARGET
  49. # Set cache variable - end user may change this during ccmake or cmake-gui configure.
  50. if(CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND _CURRENT_OSX_VERSION VERSION_GREATER 10.3)
  51. set(CMAKE_OSX_DEPLOYMENT_TARGET "$ENV{MACOSX_DEPLOYMENT_TARGET}" CACHE STRING
  52. "Minimum OS X version to target for deployment (at runtime); newer APIs weak linked. Set to empty string for default value.")
  53. endif()
  54. #----------------------------------------------------------------------------
  55. # CMAKE_OSX_SYSROOT
  56. if(CMAKE_OSX_SYSROOT)
  57. # Use the existing value without further computation to choose a default.
  58. set(_CMAKE_OSX_SYSROOT_DEFAULT "${CMAKE_OSX_SYSROOT}")
  59. elseif(NOT "x$ENV{SDKROOT}" STREQUAL "x" AND
  60. (NOT "x$ENV{SDKROOT}" MATCHES "/" OR IS_DIRECTORY "$ENV{SDKROOT}"))
  61. # Use the value of SDKROOT from the environment.
  62. set(_CMAKE_OSX_SYSROOT_DEFAULT "$ENV{SDKROOT}")
  63. elseif(CMAKE_SYSTEM_NAME STREQUAL iOS)
  64. set(_CMAKE_OSX_SYSROOT_DEFAULT "iphoneos")
  65. elseif(CMAKE_SYSTEM_NAME STREQUAL tvOS)
  66. set(_CMAKE_OSX_SYSROOT_DEFAULT "appletvos")
  67. elseif(CMAKE_SYSTEM_NAME STREQUAL watchOS)
  68. set(_CMAKE_OSX_SYSROOT_DEFAULT "watchos")
  69. elseif("${CMAKE_GENERATOR}" MATCHES Xcode
  70. OR CMAKE_OSX_DEPLOYMENT_TARGET
  71. OR CMAKE_OSX_ARCHITECTURES MATCHES "[^;]"
  72. OR NOT EXISTS "/usr/include/sys/types.h")
  73. # Find installed SDKs in either Xcode-4.3+ or pre-4.3 SDKs directory.
  74. set(_CMAKE_OSX_SDKS_DIR "")
  75. if(OSX_DEVELOPER_ROOT)
  76. foreach(_d Platforms/MacOSX.platform/Developer/SDKs SDKs)
  77. file(GLOB _CMAKE_OSX_SDKS ${OSX_DEVELOPER_ROOT}/${_d}/*)
  78. if(_CMAKE_OSX_SDKS)
  79. set(_CMAKE_OSX_SDKS_DIR ${OSX_DEVELOPER_ROOT}/${_d})
  80. break()
  81. endif()
  82. endforeach()
  83. endif()
  84. if(_CMAKE_OSX_SDKS_DIR)
  85. # Find the latest SDK as recommended by Apple (Technical Q&A QA1806)
  86. set(_CMAKE_OSX_LATEST_SDK_VERSION "0.0")
  87. file(GLOB _CMAKE_OSX_SDKS RELATIVE "${_CMAKE_OSX_SDKS_DIR}" "${_CMAKE_OSX_SDKS_DIR}/MacOSX*.sdk")
  88. foreach(_SDK ${_CMAKE_OSX_SDKS})
  89. if(IS_DIRECTORY "${_CMAKE_OSX_SDKS_DIR}/${_SDK}"
  90. AND _SDK MATCHES "MacOSX([0-9]+\\.[0-9]+)[^/]*\\.sdk"
  91. AND CMAKE_MATCH_1 VERSION_GREATER ${_CMAKE_OSX_LATEST_SDK_VERSION})
  92. set(_CMAKE_OSX_LATEST_SDK_VERSION "${CMAKE_MATCH_1}")
  93. endif()
  94. endforeach()
  95. if(NOT _CMAKE_OSX_LATEST_SDK_VERSION STREQUAL "0.0")
  96. set(_CMAKE_OSX_SYSROOT_DEFAULT "${_CMAKE_OSX_SDKS_DIR}/MacOSX${_CMAKE_OSX_LATEST_SDK_VERSION}.sdk")
  97. else()
  98. message(WARNING "Could not find any valid SDKs in ${_CMAKE_OSX_SDKS_DIR}")
  99. endif()
  100. if(NOT CMAKE_CROSSCOMPILING AND NOT CMAKE_OSX_DEPLOYMENT_TARGET
  101. AND (_CURRENT_OSX_VERSION VERSION_LESS _CMAKE_OSX_LATEST_SDK_VERSION
  102. OR _CMAKE_OSX_LATEST_SDK_VERSION STREQUAL "0.0"))
  103. set(CMAKE_OSX_DEPLOYMENT_TARGET ${_CURRENT_OSX_VERSION} CACHE STRING
  104. "Minimum OS X version to target for deployment (at runtime); newer APIs weak linked. Set to empty string for default value." FORCE)
  105. endif()
  106. else()
  107. # Assume developer files are in root (such as Xcode 4.5 command-line tools).
  108. set(_CMAKE_OSX_SYSROOT_DEFAULT "")
  109. endif()
  110. endif()
  111. # Set cache variable - end user may change this during ccmake or cmake-gui configure.
  112. # Choose the type based on the current value.
  113. set(_CMAKE_OSX_SYSROOT_TYPE STRING)
  114. foreach(_v CMAKE_OSX_SYSROOT _CMAKE_OSX_SYSROOT_DEFAULT)
  115. if("x${${_v}}" MATCHES "/")
  116. set(_CMAKE_OSX_SYSROOT_TYPE PATH)
  117. break()
  118. endif()
  119. endforeach()
  120. set(CMAKE_OSX_SYSROOT "${_CMAKE_OSX_SYSROOT_DEFAULT}" CACHE ${_CMAKE_OSX_SYSROOT_TYPE}
  121. "The product will be built against the headers and libraries located inside the indicated SDK.")
  122. # Resolves the SDK name into a path
  123. function(_apple_resolve_sdk_path sdk_name ret)
  124. execute_process(
  125. COMMAND xcrun -sdk ${sdk_name} --show-sdk-path
  126. OUTPUT_VARIABLE _stdout
  127. OUTPUT_STRIP_TRAILING_WHITESPACE
  128. ERROR_VARIABLE _stderr
  129. RESULT_VARIABLE _failed
  130. )
  131. set(${ret} "${_stdout}" PARENT_SCOPE)
  132. endfunction()
  133. function(_apple_resolve_supported_archs_for_sdk_from_system_lib sdk_path ret ret_failed)
  134. # Detect the supported SDK architectures by inspecting the main libSystem library.
  135. set(common_lib_prefix "${sdk_path}/usr/lib/libSystem")
  136. set(system_lib_dylib_path "${common_lib_prefix}.dylib")
  137. set(system_lib_tbd_path "${common_lib_prefix}.tbd")
  138. # Newer SDKs ship text based dylib stub files which contain the architectures supported by the
  139. # library in text form.
  140. if(EXISTS "${system_lib_tbd_path}")
  141. file(STRINGS "${system_lib_tbd_path}" tbd_lines REGEX "^(archs|targets): +\\[.+\\]")
  142. if(NOT tbd_lines)
  143. set(${ret_failed} TRUE PARENT_SCOPE)
  144. return()
  145. endif()
  146. # The tbd architectures line looks like the following:
  147. # archs: [ armv7, armv7s, arm64, arm64e ]
  148. # or for version 4 TBD files:
  149. # targets: [ armv7-ios, armv7s-ios, arm64-ios, arm64e-ios ]
  150. list(GET tbd_lines 0 first_arch_line)
  151. string(REGEX REPLACE
  152. "(archs|targets): +\\[ (.+) \\]" "\\2" arches_comma_separated "${first_arch_line}")
  153. string(STRIP "${arches_comma_separated}" arches_comma_separated)
  154. string(REPLACE "," ";" arch_list "${arches_comma_separated}")
  155. string(REPLACE " " "" arch_list "${arch_list}")
  156. # Remove -platform suffix from target (version 4 only)
  157. string(REGEX REPLACE "-[a-z-]+" "" arch_list "${arch_list}")
  158. if(NOT arch_list)
  159. set(${ret_failed} TRUE PARENT_SCOPE)
  160. return()
  161. endif()
  162. set(${ret} "${arch_list}" PARENT_SCOPE)
  163. elseif(EXISTS "${system_lib_dylib_path}")
  164. # Old SDKs (Xcode < 7) ship dylib files, use lipo to inspect the supported architectures.
  165. # Can't use -archs because the option is not available in older Xcode versions.
  166. execute_process(
  167. COMMAND lipo -info ${system_lib_dylib_path}
  168. OUTPUT_VARIABLE lipo_output
  169. OUTPUT_STRIP_TRAILING_WHITESPACE
  170. ERROR_VARIABLE _stderr
  171. RESULT_VARIABLE _failed
  172. )
  173. if(_failed OR NOT lipo_output OR NOT lipo_output MATCHES "(Non-fat file:|Architectures in the fat file:)")
  174. set(${ret_failed} TRUE PARENT_SCOPE)
  175. return()
  176. endif()
  177. # The lipo output looks like the following:
  178. # Non-fat file: <path> is architecture: i386
  179. # Architectures in the fat file: <path> are: i386 x86_64
  180. string(REGEX REPLACE
  181. "^(.+)is architecture:(.+)" "\\2" arches_space_separated "${lipo_output}")
  182. string(REGEX REPLACE
  183. "^(.+)are:(.+)" "\\2" arches_space_separated "${arches_space_separated}")
  184. # Need to clean up the arches, with Xcode 4.6.3 the output of lipo -info contains some
  185. # additional info, e.g.
  186. # Architectures in the fat file: <path> are: armv7 (cputype (12) cpusubtype (11))
  187. string(REGEX REPLACE
  188. "\\(.+\\)" "" arches_space_separated "${arches_space_separated}")
  189. # The output is space separated.
  190. string(STRIP "${arches_space_separated}" arches_space_separated)
  191. string(REPLACE " " ";" arch_list "${arches_space_separated}")
  192. if(NOT arch_list)
  193. set(${ret_failed} TRUE PARENT_SCOPE)
  194. return()
  195. endif()
  196. set(${ret} "${arch_list}" PARENT_SCOPE)
  197. else()
  198. # This shouldn't happen, but keep it for safety.
  199. message(WARNING "No way to find architectures for given sdk_path '${sdk_path}'")
  200. set(${ret_failed} TRUE PARENT_SCOPE)
  201. endif()
  202. endfunction()
  203. # Handle multi-arch sysroots. Do this before CMAKE_OSX_SYSROOT is
  204. # transformed into a path, so that we know the sysroot name.
  205. function(_apple_resolve_multi_arch_sysroots)
  206. if(DEFINED CMAKE_APPLE_ARCH_SYSROOTS)
  207. return() # Already cached
  208. endif()
  209. list(LENGTH CMAKE_OSX_ARCHITECTURES _num_archs)
  210. if(NOT (_num_archs GREATER 1))
  211. return() # Only apply to multi-arch
  212. endif()
  213. if(CMAKE_OSX_SYSROOT STREQUAL "macosx")
  214. # macOS doesn't have a simulator sdk / sysroot, so there is no need to handle per-sdk arches.
  215. return()
  216. endif()
  217. if(IS_DIRECTORY "${CMAKE_OSX_SYSROOT}")
  218. if(NOT CMAKE_OSX_SYSROOT STREQUAL _CMAKE_OSX_SYSROOT_DEFAULT)
  219. message(WARNING "Can not resolve multi-arch sysroots with CMAKE_OSX_SYSROOT set to path (${CMAKE_OSX_SYSROOT})")
  220. endif()
  221. return()
  222. endif()
  223. string(REPLACE "os" "simulator" _simulator_sdk ${CMAKE_OSX_SYSROOT})
  224. set(_sdks "${CMAKE_OSX_SYSROOT};${_simulator_sdk}")
  225. foreach(sdk ${_sdks})
  226. _apple_resolve_sdk_path(${sdk} _sdk_path)
  227. if(NOT IS_DIRECTORY "${_sdk_path}")
  228. message(WARNING "Failed to resolve SDK path for '${sdk}'")
  229. continue()
  230. endif()
  231. _apple_resolve_supported_archs_for_sdk_from_system_lib(${_sdk_path} _sdk_archs _failed)
  232. if(_failed)
  233. # Failure to extract supported architectures for an SDK means that the installed SDK is old
  234. # and does not provide such information (SDKs that come with Xcode >= 10.x started providing
  235. # the information). In such a case, return early, and handle multi-arch builds the old way
  236. # (no per-sdk arches).
  237. return()
  238. endif()
  239. set(_sdk_archs_${sdk} ${_sdk_archs})
  240. set(_sdk_path_${sdk} ${_sdk_path})
  241. endforeach()
  242. foreach(arch ${CMAKE_OSX_ARCHITECTURES})
  243. set(_arch_sysroot "")
  244. foreach(sdk ${_sdks})
  245. list(FIND _sdk_archs_${sdk} ${arch} arch_index)
  246. if(NOT arch_index EQUAL -1)
  247. set(_arch_sysroot ${_sdk_path_${sdk}})
  248. break()
  249. endif()
  250. endforeach()
  251. if(_arch_sysroot)
  252. list(APPEND _arch_sysroots ${_arch_sysroot})
  253. else()
  254. message(WARNING "No SDK found for architecture '${arch}'")
  255. list(APPEND _arch_sysroots "${arch}-SDK-NOTFOUND")
  256. endif()
  257. endforeach()
  258. set(CMAKE_APPLE_ARCH_SYSROOTS "${_arch_sysroots}" CACHE INTERNAL
  259. "Architecture dependent sysroots, one per CMAKE_OSX_ARCHITECTURES")
  260. endfunction()
  261. _apple_resolve_multi_arch_sysroots()
  262. # Transform CMAKE_OSX_SYSROOT to absolute path
  263. set(_CMAKE_OSX_SYSROOT_PATH "")
  264. if(CMAKE_OSX_SYSROOT)
  265. if("x${CMAKE_OSX_SYSROOT}" MATCHES "/")
  266. # This is a path to the SDK. Make sure it exists.
  267. if(NOT IS_DIRECTORY "${CMAKE_OSX_SYSROOT}")
  268. message(WARNING "Ignoring CMAKE_OSX_SYSROOT value:\n ${CMAKE_OSX_SYSROOT}\n"
  269. "because the directory does not exist.")
  270. set(CMAKE_OSX_SYSROOT "")
  271. endif()
  272. set(_CMAKE_OSX_SYSROOT_PATH "${CMAKE_OSX_SYSROOT}")
  273. else()
  274. _apple_resolve_sdk_path(${CMAKE_OSX_SYSROOT} _sdk_path)
  275. if(IS_DIRECTORY "${_sdk_path}")
  276. set(_CMAKE_OSX_SYSROOT_PATH "${_sdk_path}")
  277. # For non-Xcode generators use the path.
  278. if(NOT "${CMAKE_GENERATOR}" MATCHES "Xcode")
  279. set(CMAKE_OSX_SYSROOT "${_CMAKE_OSX_SYSROOT_PATH}")
  280. endif()
  281. endif()
  282. endif()
  283. endif()