Darwin-Initialize.cmake 12 KB

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