Darwin-Initialize.cmake 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. # Transform the cached value to something we can use.
  112. set(_CMAKE_OSX_SYSROOT_PATH "")
  113. if(CMAKE_OSX_SYSROOT)
  114. if("x${CMAKE_OSX_SYSROOT}" MATCHES "/")
  115. # This is a path to the SDK. Make sure it exists.
  116. if(NOT IS_DIRECTORY "${CMAKE_OSX_SYSROOT}")
  117. message(WARNING "Ignoring CMAKE_OSX_SYSROOT value:\n ${CMAKE_OSX_SYSROOT}\n"
  118. "because the directory does not exist.")
  119. set(CMAKE_OSX_SYSROOT "")
  120. endif()
  121. set(_CMAKE_OSX_SYSROOT_PATH "${CMAKE_OSX_SYSROOT}")
  122. else()
  123. # Transform the sdk name into a path.
  124. execute_process(
  125. COMMAND xcodebuild -sdk ${CMAKE_OSX_SYSROOT} -version Path
  126. OUTPUT_VARIABLE _stdout
  127. OUTPUT_STRIP_TRAILING_WHITESPACE
  128. ERROR_VARIABLE _stderr
  129. RESULT_VARIABLE _failed
  130. )
  131. if(NOT _failed AND IS_DIRECTORY "${_stdout}")
  132. set(_CMAKE_OSX_SYSROOT_PATH "${_stdout}")
  133. # For non-Xcode generators use the path.
  134. if(NOT "${CMAKE_GENERATOR}" MATCHES "Xcode")
  135. set(CMAKE_OSX_SYSROOT "${_CMAKE_OSX_SYSROOT_PATH}")
  136. endif()
  137. endif()
  138. endif()
  139. endif()