Darwin.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. SET(APPLE 1)
  2. # Darwin versions:
  3. # 6.x == Mac OSX 10.2 (Jaguar)
  4. # 7.x == Mac OSX 10.3 (Panther)
  5. # 8.x == Mac OSX 10.4 (Tiger)
  6. # 9.x == Mac OSX 10.5 (Leopard)
  7. # 10.x == Mac OSX 10.6 (Snow Leopard)
  8. STRING(REGEX REPLACE "^([0-9]+)\\.([0-9]+).*$" "\\1" DARWIN_MAJOR_VERSION "${CMAKE_SYSTEM_VERSION}")
  9. STRING(REGEX REPLACE "^([0-9]+)\\.([0-9]+).*$" "\\2" DARWIN_MINOR_VERSION "${CMAKE_SYSTEM_VERSION}")
  10. # Do not use the "-Wl,-search_paths_first" flag with the OSX 10.2 compiler.
  11. # Done this way because it is too early to do a TRY_COMPILE.
  12. IF(NOT DEFINED HAVE_FLAG_SEARCH_PATHS_FIRST)
  13. SET(HAVE_FLAG_SEARCH_PATHS_FIRST 0)
  14. IF("${DARWIN_MAJOR_VERSION}" GREATER 6)
  15. SET(HAVE_FLAG_SEARCH_PATHS_FIRST 1)
  16. ENDIF("${DARWIN_MAJOR_VERSION}" GREATER 6)
  17. ENDIF(NOT DEFINED HAVE_FLAG_SEARCH_PATHS_FIRST)
  18. # More desirable, but does not work:
  19. #INCLUDE(CheckCXXCompilerFlag)
  20. #CHECK_CXX_COMPILER_FLAG("-Wl,-search_paths_first" HAVE_FLAG_SEARCH_PATHS_FIRST)
  21. SET(CMAKE_SHARED_LIBRARY_PREFIX "lib")
  22. SET(CMAKE_SHARED_LIBRARY_SUFFIX ".dylib")
  23. SET(CMAKE_SHARED_MODULE_PREFIX "lib")
  24. SET(CMAKE_SHARED_MODULE_SUFFIX ".so")
  25. SET(CMAKE_MODULE_EXISTS 1)
  26. SET(CMAKE_DL_LIBS "")
  27. SET(CMAKE_C_OSX_COMPATIBILITY_VERSION_FLAG "-compatibility_version ")
  28. SET(CMAKE_C_OSX_CURRENT_VERSION_FLAG "-current_version ")
  29. SET(CMAKE_CXX_OSX_COMPATIBILITY_VERSION_FLAG "${CMAKE_C_OSX_COMPATIBILITY_VERSION_FLAG}")
  30. SET(CMAKE_CXX_OSX_CURRENT_VERSION_FLAG "${CMAKE_C_OSX_CURRENT_VERSION_FLAG}")
  31. SET(CMAKE_C_LINK_FLAGS "-headerpad_max_install_names")
  32. SET(CMAKE_CXX_LINK_FLAGS "-headerpad_max_install_names")
  33. IF(HAVE_FLAG_SEARCH_PATHS_FIRST)
  34. SET(CMAKE_C_LINK_FLAGS "-Wl,-search_paths_first ${CMAKE_C_LINK_FLAGS}")
  35. SET(CMAKE_CXX_LINK_FLAGS "-Wl,-search_paths_first ${CMAKE_CXX_LINK_FLAGS}")
  36. ENDIF(HAVE_FLAG_SEARCH_PATHS_FIRST)
  37. SET(CMAKE_PLATFORM_HAS_INSTALLNAME 1)
  38. SET(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-dynamiclib -headerpad_max_install_names")
  39. SET(CMAKE_SHARED_MODULE_CREATE_C_FLAGS "-bundle -headerpad_max_install_names")
  40. SET(CMAKE_SHARED_MODULE_LOADER_C_FLAG "-Wl,-bundle_loader,")
  41. SET(CMAKE_SHARED_MODULE_LOADER_CXX_FLAG "-Wl,-bundle_loader,")
  42. SET(CMAKE_FIND_LIBRARY_SUFFIXES ".dylib" ".so" ".a")
  43. # hack: if a new cmake (which uses CMAKE_INSTALL_NAME_TOOL) runs on an old build tree
  44. # (where install_name_tool was hardcoded) and where CMAKE_INSTALL_NAME_TOOL isn't in the cache
  45. # and still cmake didn't fail in CMakeFindBinUtils.cmake (because it isn't rerun)
  46. # hardcode CMAKE_INSTALL_NAME_TOOL here to install_name_tool, so it behaves as it did before, Alex
  47. IF(NOT DEFINED CMAKE_INSTALL_NAME_TOOL)
  48. FIND_PROGRAM(CMAKE_INSTALL_NAME_TOOL install_name_tool)
  49. ENDIF(NOT DEFINED CMAKE_INSTALL_NAME_TOOL)
  50. # Set the assumed (Pre 10.5 or Default) location of the developer tools
  51. SET(OSX_DEVELOPER_ROOT "/Developer")
  52. # Find installed SDKs
  53. FILE(GLOB _CMAKE_OSX_SDKS "${OSX_DEVELOPER_ROOT}/SDKs/*")
  54. # If nothing is found there, then try locating the dev tools based on the xcode-select tool
  55. # (available in Xcode >= 3.0 installations)
  56. IF(NOT _CMAKE_OSX_SDKS)
  57. FIND_PROGRAM(CMAKE_XCODE_SELECT xcode-select)
  58. IF(CMAKE_XCODE_SELECT)
  59. EXECUTE_PROCESS(COMMAND ${CMAKE_XCODE_SELECT} "-print-path"
  60. OUTPUT_VARIABLE OSX_DEVELOPER_ROOT)
  61. FILE(GLOB _CMAKE_OSX_SDKS "${OSX_DEVELOPER_ROOT}/SDKs/*")
  62. ENDIF(CMAKE_XCODE_SELECT)
  63. ENDIF(NOT _CMAKE_OSX_SDKS)
  64. EXECUTE_PROCESS(COMMAND sw_vers -productVersion
  65. OUTPUT_VARIABLE CURRENT_OSX_VERSION
  66. OUTPUT_STRIP_TRAILING_WHITESPACE)
  67. #----------------------------------------------------------------------------
  68. # CMAKE_OSX_DEPLOYMENT_TARGET
  69. # Environment variable set by the user overrides our default.
  70. # Use the same environment variable that Xcode uses.
  71. SET(ENV_MACOSX_DEPLOYMENT_TARGET "$ENV{MACOSX_DEPLOYMENT_TARGET}")
  72. # Set CMAKE_OSX_DEPLOYMENT_TARGET_DEFAULT - if user has already specified an SDK
  73. # with CMAKE_OSX_SYSROOT, then deployment target should default to "", otherwise,
  74. # default it to the current OSX version.
  75. IF(CMAKE_OSX_SYSROOT)
  76. SET(CMAKE_OSX_DEPLOYMENT_TARGET_DEFAULT "")
  77. ELSE(CMAKE_OSX_SYSROOT)
  78. STRING(REGEX REPLACE "^.*(10)\\.([0-9]+)\\.*([0-9]+)*.*$" "\\1.\\2"
  79. CMAKE_OSX_DEPLOYMENT_TARGET_DEFAULT ${CURRENT_OSX_VERSION})
  80. ENDIF(CMAKE_OSX_SYSROOT)
  81. # Use environment or default as initial cache value:
  82. IF(NOT ENV_MACOSX_DEPLOYMENT_TARGET STREQUAL "")
  83. SET(CMAKE_OSX_DEPLOYMENT_TARGET_VALUE ${ENV_MACOSX_DEPLOYMENT_TARGET})
  84. ELSE(NOT ENV_MACOSX_DEPLOYMENT_TARGET STREQUAL "")
  85. SET(CMAKE_OSX_DEPLOYMENT_TARGET_VALUE ${CMAKE_OSX_DEPLOYMENT_TARGET_DEFAULT})
  86. ENDIF(NOT ENV_MACOSX_DEPLOYMENT_TARGET STREQUAL "")
  87. # Set cache variable - end user may change this during ccmake or cmake-gui configure.
  88. IF(CURRENT_OSX_VERSION GREATER 10.3)
  89. SET(CMAKE_OSX_DEPLOYMENT_TARGET ${CMAKE_OSX_DEPLOYMENT_TARGET_VALUE} CACHE STRING
  90. "Minimum OS X version to target for deployment (at runtime); newer APIs weak linked. Set to empty string for default value.")
  91. ENDIF(CURRENT_OSX_VERSION GREATER 10.3)
  92. #----------------------------------------------------------------------------
  93. # CMAKE_OSX_SYSROOT
  94. # Environment variable set by the user overrides our default.
  95. # Use the same environment variable that Xcode uses.
  96. SET(ENV_SDKROOT "$ENV{SDKROOT}")
  97. # Set CMAKE_OSX_SYSROOT_DEFAULT based on CMAKE_OSX_DEPLOYMENT_TARGET,
  98. # accounting for the known specially named SDKs.
  99. SET(CMAKE_OSX_SYSROOT_DEFAULT
  100. "${OSX_DEVELOPER_ROOT}/SDKs/MacOSX${CMAKE_OSX_DEPLOYMENT_TARGET}.sdk")
  101. IF(CMAKE_OSX_DEPLOYMENT_TARGET EQUAL "10.4")
  102. SET(CMAKE_OSX_SYSROOT_DEFAULT
  103. "${OSX_DEVELOPER_ROOT}/SDKs/MacOSX10.4u.sdk")
  104. ENDIF(CMAKE_OSX_DEPLOYMENT_TARGET EQUAL "10.4")
  105. IF(CMAKE_OSX_DEPLOYMENT_TARGET EQUAL "10.3")
  106. SET(CMAKE_OSX_SYSROOT_DEFAULT
  107. "${OSX_DEVELOPER_ROOT}/SDKs/MacOSX10.3.9.sdk")
  108. ENDIF(CMAKE_OSX_DEPLOYMENT_TARGET EQUAL "10.3")
  109. # Use environment or default as initial cache value:
  110. IF(NOT ENV_SDKROOT STREQUAL "")
  111. SET(CMAKE_OSX_SYSROOT_VALUE ${ENV_SDKROOT})
  112. ELSE(NOT ENV_SDKROOT STREQUAL "")
  113. SET(CMAKE_OSX_SYSROOT_VALUE ${CMAKE_OSX_SYSROOT_DEFAULT})
  114. ENDIF(NOT ENV_SDKROOT STREQUAL "")
  115. # Set cache variable - end user may change this during ccmake or cmake-gui configure.
  116. SET(CMAKE_OSX_SYSROOT ${CMAKE_OSX_SYSROOT_VALUE} CACHE PATH
  117. "The product will be built against the headers and libraries located inside the indicated SDK.")
  118. #----------------------------------------------------------------------------
  119. function(SanityCheckSDKAndDeployTarget _sdk_path _deploy)
  120. if(_deploy STREQUAL "")
  121. return()
  122. endif()
  123. if(_sdk_path STREQUAL "")
  124. message(FATAL_ERROR "CMAKE_OSX_DEPLOYMENT_TARGET='${_deploy}' but CMAKE_OSX_SYSROOT is empty... - either set CMAKE_OSX_SYSROOT to a valid SDK or set CMAKE_OSX_DEPLOYMENT_TARGET to empty")
  125. endif()
  126. string(REGEX REPLACE "(.*MacOSX*)(....)(.*\\.sdk)" "\\2" SDK "${_sdk_path}")
  127. if(_deploy GREATER "${SDK}")
  128. message(FATAL_ERROR "CMAKE_OSX_DEPLOYMENT_TARGET (${_deploy}) is greater than CMAKE_OSX_SYSROOT SDK (${_sdk_path}). Please set CMAKE_OSX_DEPLOYMENT_TARGET to ${SDK}")
  129. endif()
  130. endfunction(SanityCheckSDKAndDeployTarget)
  131. #----------------------------------------------------------------------------
  132. # Make sure the combination of SDK and Deployment Target are allowed
  133. SanityCheckSDKAndDeployTarget("${CMAKE_OSX_SYSROOT}" "${CMAKE_OSX_DEPLOYMENT_TARGET}")
  134. # set _CMAKE_OSX_MACHINE to uname -m
  135. EXECUTE_PROCESS(COMMAND uname -m
  136. OUTPUT_STRIP_TRAILING_WHITESPACE
  137. OUTPUT_VARIABLE _CMAKE_OSX_MACHINE)
  138. # check for Power PC and change to ppc
  139. IF(_CMAKE_OSX_MACHINE MATCHES "Power")
  140. SET(_CMAKE_OSX_MACHINE ppc)
  141. ENDIF(_CMAKE_OSX_MACHINE MATCHES "Power")
  142. # check for environment variable CMAKE_OSX_ARCHITECTURES
  143. # if it is set.
  144. IF(NOT "$ENV{CMAKE_OSX_ARCHITECTURES}" STREQUAL "")
  145. SET(CMAKE_OSX_ARCHITECTURES_VALUE "$ENV{CMAKE_OSX_ARCHITECTURES}")
  146. ELSE(NOT "$ENV{CMAKE_OSX_ARCHITECTURES}" STREQUAL "")
  147. SET(CMAKE_OSX_ARCHITECTURES_VALUE "")
  148. ENDIF(NOT "$ENV{CMAKE_OSX_ARCHITECTURES}" STREQUAL "")
  149. # now put _CMAKE_OSX_MACHINE into the cache
  150. SET(CMAKE_OSX_ARCHITECTURES ${CMAKE_OSX_ARCHITECTURES_VALUE} CACHE STRING
  151. "Build architectures for OSX")
  152. IF("${CMAKE_BACKWARDS_COMPATIBILITY}" MATCHES "^1\\.[0-6]$")
  153. SET(CMAKE_SHARED_MODULE_CREATE_C_FLAGS
  154. "${CMAKE_SHARED_MODULE_CREATE_C_FLAGS} -flat_namespace -undefined suppress")
  155. ENDIF("${CMAKE_BACKWARDS_COMPATIBILITY}" MATCHES "^1\\.[0-6]$")
  156. IF(NOT XCODE)
  157. # Enable shared library versioning. This flag is not actually referenced
  158. # but the fact that the setting exists will cause the generators to support
  159. # soname computation.
  160. SET(CMAKE_SHARED_LIBRARY_SONAME_C_FLAG "-install_name")
  161. ENDIF(NOT XCODE)
  162. # Xcode does not support -isystem yet.
  163. IF(XCODE)
  164. SET(CMAKE_INCLUDE_SYSTEM_FLAG_C)
  165. SET(CMAKE_INCLUDE_SYSTEM_FLAG_CXX)
  166. ENDIF(XCODE)
  167. # Need to list dependent shared libraries on link line. When building
  168. # with -isysroot (for universal binaries), the linker always looks for
  169. # dependent libraries under the sysroot. Listing them on the link
  170. # line works around the problem.
  171. SET(CMAKE_LINK_DEPENDENT_LIBRARY_FILES 1)
  172. SET(CMAKE_C_CREATE_SHARED_LIBRARY_FORBIDDEN_FLAGS -w)
  173. SET(CMAKE_CXX_CREATE_SHARED_LIBRARY_FORBIDDEN_FLAGS -w)
  174. SET(CMAKE_C_CREATE_SHARED_LIBRARY
  175. "<CMAKE_C_COMPILER> <LANGUAGE_COMPILE_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> <LINK_FLAGS> -o <TARGET> -install_name <TARGET_INSTALLNAME_DIR><TARGET_SONAME> <OBJECTS> <LINK_LIBRARIES>")
  176. SET(CMAKE_CXX_CREATE_SHARED_LIBRARY
  177. "<CMAKE_CXX_COMPILER> <LANGUAGE_COMPILE_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> <LINK_FLAGS> -o <TARGET> -install_name <TARGET_INSTALLNAME_DIR><TARGET_SONAME> <OBJECTS> <LINK_LIBRARIES>")
  178. SET(CMAKE_Fortran_CREATE_SHARED_LIBRARY
  179. "<CMAKE_Fortran_COMPILER> <LANGUAGE_COMPILE_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_Fortran_FLAGS> <LINK_FLAGS> -o <TARGET> -install_name <TARGET_INSTALLNAME_DIR><TARGET_SONAME> <OBJECTS> <LINK_LIBRARIES>")
  180. SET(CMAKE_CXX_CREATE_SHARED_MODULE
  181. "<CMAKE_CXX_COMPILER> <LANGUAGE_COMPILE_FLAGS> <CMAKE_SHARED_MODULE_CREATE_CXX_FLAGS> <LINK_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
  182. SET(CMAKE_C_CREATE_SHARED_MODULE
  183. "<CMAKE_C_COMPILER> <LANGUAGE_COMPILE_FLAGS> <CMAKE_SHARED_MODULE_CREATE_C_FLAGS> <LINK_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
  184. SET(CMAKE_Fortran_CREATE_SHARED_MODULE
  185. "<CMAKE_Fortran_COMPILER> <LANGUAGE_COMPILE_FLAGS> <CMAKE_SHARED_MODULE_CREATE_Fortran_FLAGS> <LINK_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
  186. SET(CMAKE_C_CREATE_MACOSX_FRAMEWORK
  187. "<CMAKE_C_COMPILER> <LANGUAGE_COMPILE_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> <LINK_FLAGS> -o <TARGET> -install_name <TARGET_INSTALLNAME_DIR><TARGET_SONAME> <OBJECTS> <LINK_LIBRARIES>")
  188. SET(CMAKE_CXX_CREATE_MACOSX_FRAMEWORK
  189. "<CMAKE_CXX_COMPILER> <LANGUAGE_COMPILE_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> <LINK_FLAGS> -o <TARGET> -install_name <TARGET_INSTALLNAME_DIR><TARGET_SONAME> <OBJECTS> <LINK_LIBRARIES>")
  190. # default to searching for frameworks first
  191. SET(CMAKE_FIND_FRAMEWORK FIRST)
  192. # set up the default search directories for frameworks
  193. SET(CMAKE_SYSTEM_FRAMEWORK_PATH
  194. ~/Library/Frameworks
  195. /Library/Frameworks
  196. /Network/Library/Frameworks
  197. /System/Library/Frameworks)
  198. # default to searching for application bundles first
  199. SET(CMAKE_FIND_APPBUNDLE FIRST)
  200. # set up the default search directories for application bundles
  201. SET(CMAKE_SYSTEM_APPBUNDLE_PATH
  202. ~/Applications
  203. /Applications
  204. /Developer/Applications)
  205. INCLUDE(Platform/UnixPaths)
  206. LIST(APPEND CMAKE_SYSTEM_PREFIX_PATH /sw)