Darwin-Initialize.cmake 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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(_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_GENERATOR}" MATCHES Xcode
  45. OR CMAKE_OSX_DEPLOYMENT_TARGET
  46. OR CMAKE_OSX_ARCHITECTURES MATCHES "[^;]"
  47. OR NOT EXISTS "/usr/include/sys/types.h")
  48. # Find installed SDKs in either Xcode-4.3+ or pre-4.3 SDKs directory.
  49. set(_CMAKE_OSX_SDKS_DIR "")
  50. if(OSX_DEVELOPER_ROOT)
  51. foreach(d Platforms/MacOSX.platform/Developer/SDKs SDKs)
  52. file(GLOB _CMAKE_OSX_SDKS ${OSX_DEVELOPER_ROOT}/${d}/*)
  53. if(_CMAKE_OSX_SDKS)
  54. set(_CMAKE_OSX_SDKS_DIR ${OSX_DEVELOPER_ROOT}/${d})
  55. break()
  56. endif()
  57. endforeach()
  58. endif()
  59. if(_CMAKE_OSX_SDKS_DIR)
  60. # Select SDK for current OSX version accounting for the known
  61. # specially named SDKs.
  62. set(_CMAKE_OSX_SDKS_VER_SUFFIX_10.4 "u")
  63. set(_CMAKE_OSX_SDKS_VER_SUFFIX_10.3 ".9")
  64. # find the latest SDK
  65. set(_CMAKE_OSX_LATEST_SDK_VERSION "0.0")
  66. file(GLOB _CMAKE_OSX_SDKS RELATIVE "${_CMAKE_OSX_SDKS_DIR}" "${_CMAKE_OSX_SDKS_DIR}/MacOSX*.sdk")
  67. foreach(_SDK ${_CMAKE_OSX_SDKS})
  68. if(_SDK MATCHES "MacOSX([0-9]+\\.[0-9]+)[^/]*\\.sdk" AND CMAKE_MATCH_1 VERSION_GREATER ${_CMAKE_OSX_LATEST_SDK_VERSION})
  69. set(_CMAKE_OSX_LATEST_SDK_VERSION "${CMAKE_MATCH_1}")
  70. endif()
  71. endforeach()
  72. # pick an SDK that works
  73. set(_CMAKE_OSX_SYSROOT_DEFAULT)
  74. foreach(ver ${CMAKE_OSX_DEPLOYMENT_TARGET}
  75. ${_CURRENT_OSX_VERSION}
  76. ${_CMAKE_OSX_LATEST_SDK_VERSION})
  77. set(_CMAKE_OSX_DEPLOYMENT_TARGET ${ver})
  78. set(_CMAKE_OSX_SDKS_VER ${_CMAKE_OSX_DEPLOYMENT_TARGET}${_CMAKE_OSX_SDKS_VER_SUFFIX_${_CMAKE_OSX_DEPLOYMENT_TARGET}})
  79. set(_CMAKE_OSX_SYSROOT_CHECK "${_CMAKE_OSX_SDKS_DIR}/MacOSX${_CMAKE_OSX_SDKS_VER}.sdk")
  80. if(IS_DIRECTORY "${_CMAKE_OSX_SYSROOT_CHECK}")
  81. set(_CMAKE_OSX_SYSROOT_DEFAULT "${_CMAKE_OSX_SYSROOT_CHECK}")
  82. break()
  83. endif()
  84. endforeach()
  85. if(NOT CMAKE_CROSSCOMPILING AND NOT CMAKE_OSX_DEPLOYMENT_TARGET AND _CURRENT_OSX_VERSION VERSION_LESS _CMAKE_OSX_DEPLOYMENT_TARGET)
  86. set(CMAKE_OSX_DEPLOYMENT_TARGET ${_CURRENT_OSX_VERSION} CACHE STRING
  87. "Minimum OS X version to target for deployment (at runtime); newer APIs weak linked. Set to empty string for default value." FORCE)
  88. endif()
  89. else()
  90. # Assume developer files are in root (such as Xcode 4.5 command-line tools).
  91. set(_CMAKE_OSX_SYSROOT_DEFAULT "")
  92. endif()
  93. endif()
  94. # Set cache variable - end user may change this during ccmake or cmake-gui configure.
  95. # Choose the type based on the current value.
  96. set(_CMAKE_OSX_SYSROOT_TYPE STRING)
  97. foreach(v CMAKE_OSX_SYSROOT _CMAKE_OSX_SYSROOT_DEFAULT)
  98. if("x${${v}}" MATCHES "/")
  99. set(_CMAKE_OSX_SYSROOT_TYPE PATH)
  100. break()
  101. endif()
  102. endforeach()
  103. set(CMAKE_OSX_SYSROOT "${_CMAKE_OSX_SYSROOT_DEFAULT}" CACHE ${_CMAKE_OSX_SYSROOT_TYPE}
  104. "The product will be built against the headers and libraries located inside the indicated SDK.")
  105. # Transform the cached value to something we can use.
  106. set(_CMAKE_OSX_SYSROOT_PATH "")
  107. if(CMAKE_OSX_SYSROOT)
  108. if("x${CMAKE_OSX_SYSROOT}" MATCHES "/")
  109. # This is a path to the SDK. Make sure it exists.
  110. if(NOT IS_DIRECTORY "${CMAKE_OSX_SYSROOT}")
  111. message(WARNING "Ignoring CMAKE_OSX_SYSROOT value:\n ${CMAKE_OSX_SYSROOT}\n"
  112. "because the directory does not exist.")
  113. set(CMAKE_OSX_SYSROOT "")
  114. endif()
  115. set(_CMAKE_OSX_SYSROOT_PATH "${CMAKE_OSX_SYSROOT}")
  116. else()
  117. # Transform the sdk name into a path.
  118. execute_process(
  119. COMMAND xcodebuild -sdk ${CMAKE_OSX_SYSROOT} -version Path
  120. OUTPUT_VARIABLE _stdout
  121. OUTPUT_STRIP_TRAILING_WHITESPACE
  122. ERROR_VARIABLE _stderr
  123. RESULT_VARIABLE _failed
  124. )
  125. if(NOT _failed AND IS_DIRECTORY "${_stdout}")
  126. set(_CMAKE_OSX_SYSROOT_PATH "${_stdout}")
  127. # For non-Xcode generators use the path.
  128. if(NOT "${CMAKE_GENERATOR}" MATCHES "Xcode")
  129. set(CMAKE_OSX_SYSROOT "${_CMAKE_OSX_SYSROOT_PATH}")
  130. endif()
  131. endif()
  132. endif()
  133. endif()