Darwin-Initialize.cmake 5.7 KB

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