Darwin.cmake 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. set(APPLE 1)
  2. if(CMAKE_SYSTEM_NAME STREQUAL "iOS" OR CMAKE_SYSTEM_NAME STREQUAL "tvOS" OR CMAKE_SYSTEM_NAME STREQUAL "watchOS")
  3. if(NOT DEFINED CMAKE_MACOSX_BUNDLE)
  4. set(CMAKE_MACOSX_BUNDLE ON)
  5. endif()
  6. list(APPEND CMAKE_FIND_ROOT_PATH "${_CMAKE_OSX_SYSROOT_PATH}")
  7. if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_LIBRARY)
  8. set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
  9. endif()
  10. if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_INCLUDE)
  11. set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
  12. endif()
  13. if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_PACKAGE)
  14. set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
  15. endif()
  16. endif()
  17. # Darwin versions:
  18. # 6.x == Mac OSX 10.2 (Jaguar)
  19. # 7.x == Mac OSX 10.3 (Panther)
  20. # 8.x == Mac OSX 10.4 (Tiger)
  21. # 9.x == Mac OSX 10.5 (Leopard)
  22. # 10.x == Mac OSX 10.6 (Snow Leopard)
  23. # 11.x == Mac OSX 10.7 (Lion)
  24. # 12.x == Mac OSX 10.8 (Mountain Lion)
  25. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+).*$" "\\1" DARWIN_MAJOR_VERSION "${CMAKE_SYSTEM_VERSION}")
  26. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+).*$" "\\2" DARWIN_MINOR_VERSION "${CMAKE_SYSTEM_VERSION}")
  27. # Do not use the "-Wl,-search_paths_first" flag with the OSX 10.2 compiler.
  28. # Done this way because it is too early to do a TRY_COMPILE.
  29. if(NOT DEFINED HAVE_FLAG_SEARCH_PATHS_FIRST)
  30. set(HAVE_FLAG_SEARCH_PATHS_FIRST 0)
  31. if("${DARWIN_MAJOR_VERSION}" GREATER 6)
  32. set(HAVE_FLAG_SEARCH_PATHS_FIRST 1)
  33. endif()
  34. endif()
  35. # More desirable, but does not work:
  36. #include(CheckCXXCompilerFlag)
  37. #CHECK_CXX_COMPILER_FLAG("-Wl,-search_paths_first" HAVE_FLAG_SEARCH_PATHS_FIRST)
  38. set(CMAKE_SHARED_LIBRARY_PREFIX "lib")
  39. set(CMAKE_SHARED_LIBRARY_SUFFIX ".dylib")
  40. set(CMAKE_SHARED_MODULE_PREFIX "lib")
  41. set(CMAKE_SHARED_MODULE_SUFFIX ".so")
  42. set(CMAKE_MODULE_EXISTS 1)
  43. set(CMAKE_DL_LIBS "")
  44. # Enable rpath support for 10.5 and greater where it is known to work.
  45. if("${DARWIN_MAJOR_VERSION}" GREATER 8)
  46. set(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG "-Wl,-rpath,")
  47. endif()
  48. foreach(lang C CXX OBJC OBJCXX)
  49. set(CMAKE_${lang}_OSX_COMPATIBILITY_VERSION_FLAG "-compatibility_version ")
  50. set(CMAKE_${lang}_OSX_CURRENT_VERSION_FLAG "-current_version ")
  51. set(CMAKE_${lang}_LINK_FLAGS "-Wl,-headerpad_max_install_names")
  52. if(HAVE_FLAG_SEARCH_PATHS_FIRST)
  53. set(CMAKE_${lang}_LINK_FLAGS "-Wl,-search_paths_first ${CMAKE_${lang}_LINK_FLAGS}")
  54. endif()
  55. set(CMAKE_SHARED_LIBRARY_CREATE_${lang}_FLAGS "-dynamiclib -Wl,-headerpad_max_install_names")
  56. set(CMAKE_SHARED_MODULE_CREATE_${lang}_FLAGS "-bundle -Wl,-headerpad_max_install_names")
  57. set(CMAKE_SHARED_MODULE_LOADER_${lang}_FLAG "-Wl,-bundle_loader,")
  58. endforeach()
  59. set(CMAKE_PLATFORM_HAS_INSTALLNAME 1)
  60. set(CMAKE_FIND_LIBRARY_SUFFIXES ".tbd" ".dylib" ".so" ".a")
  61. # hack: if a new cmake (which uses CMAKE_INSTALL_NAME_TOOL) runs on an old build tree
  62. # (where install_name_tool was hardcoded) and where CMAKE_INSTALL_NAME_TOOL isn't in the cache
  63. # and still cmake didn't fail in CMakeFindBinUtils.cmake (because it isn't rerun)
  64. # hardcode CMAKE_INSTALL_NAME_TOOL here to install_name_tool, so it behaves as it did before, Alex
  65. if(NOT DEFINED CMAKE_INSTALL_NAME_TOOL)
  66. find_program(CMAKE_INSTALL_NAME_TOOL install_name_tool)
  67. mark_as_advanced(CMAKE_INSTALL_NAME_TOOL)
  68. endif()
  69. # Enable shared library versioning.
  70. set(CMAKE_SHARED_LIBRARY_SONAME_C_FLAG "-install_name")
  71. if("${_CURRENT_OSX_VERSION}" VERSION_LESS "10.5")
  72. # Need to list dependent shared libraries on link line. When building
  73. # with -isysroot (for universal binaries), the linker always looks for
  74. # dependent libraries under the sysroot. Listing them on the link
  75. # line works around the problem.
  76. set(CMAKE_LINK_DEPENDENT_LIBRARY_FILES 1)
  77. endif()
  78. foreach(lang C CXX Fortran OBJC OBJCXX)
  79. # Xcode does not support -isystem yet.
  80. if(XCODE)
  81. set(CMAKE_INCLUDE_SYSTEM_FLAG_${lang})
  82. endif()
  83. set(CMAKE_${lang}_CREATE_SHARED_LIBRARY
  84. "<CMAKE_${lang}_COMPILER> <LANGUAGE_COMPILE_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_${lang}_FLAGS> <LINK_FLAGS> -o <TARGET> <SONAME_FLAG> <TARGET_INSTALLNAME_DIR><TARGET_SONAME> <OBJECTS> <LINK_LIBRARIES>")
  85. set(CMAKE_${lang}_CREATE_SHARED_MODULE
  86. "<CMAKE_${lang}_COMPILER> <LANGUAGE_COMPILE_FLAGS> <CMAKE_SHARED_MODULE_CREATE_${lang}_FLAGS> <LINK_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
  87. set(CMAKE_${lang}_CREATE_MACOSX_FRAMEWORK
  88. "<CMAKE_${lang}_COMPILER> <LANGUAGE_COMPILE_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_${lang}_FLAGS> <LINK_FLAGS> -o <TARGET> <SONAME_FLAG> <TARGET_INSTALLNAME_DIR><TARGET_SONAME> <OBJECTS> <LINK_LIBRARIES>")
  89. # Set default framework search path flag for languages known to use a
  90. # preprocessor that may find headers in frameworks.
  91. set(CMAKE_${lang}_FRAMEWORK_SEARCH_FLAG -F)
  92. endforeach()
  93. # default to searching for frameworks first
  94. if(NOT DEFINED CMAKE_FIND_FRAMEWORK)
  95. set(CMAKE_FIND_FRAMEWORK FIRST)
  96. endif()
  97. # Older OS X linkers do not report their framework search path
  98. # with -v but "man ld" documents the following locations.
  99. set(CMAKE_PLATFORM_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES
  100. ${_CMAKE_OSX_SYSROOT_PATH}/Library/Frameworks
  101. ${_CMAKE_OSX_SYSROOT_PATH}/System/Library/Frameworks
  102. )
  103. if(_CMAKE_OSX_SYSROOT_PATH)
  104. # Treat some paths as implicit so we do not override the SDK versions.
  105. list(APPEND CMAKE_PLATFORM_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES
  106. /System/Library/Frameworks)
  107. endif()
  108. if("${_CURRENT_OSX_VERSION}" VERSION_LESS "10.5")
  109. # Older OS X tools had more implicit paths.
  110. list(APPEND CMAKE_PLATFORM_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES
  111. ${_CMAKE_OSX_SYSROOT_PATH}/Network/Library/Frameworks)
  112. endif()
  113. # set up the default search directories for frameworks
  114. set(CMAKE_SYSTEM_FRAMEWORK_PATH
  115. ~/Library/Frameworks
  116. )
  117. if(_CMAKE_OSX_SYSROOT_PATH)
  118. list(APPEND CMAKE_SYSTEM_FRAMEWORK_PATH
  119. ${_CMAKE_OSX_SYSROOT_PATH}/Library/Frameworks
  120. ${_CMAKE_OSX_SYSROOT_PATH}/Network/Library/Frameworks
  121. ${_CMAKE_OSX_SYSROOT_PATH}/System/Library/Frameworks
  122. )
  123. # add platform developer framework path if exists
  124. foreach(_path
  125. # Xcode 6
  126. ${_CMAKE_OSX_SYSROOT_PATH}/../../Library/Frameworks
  127. # Xcode 5 iOS
  128. ${_CMAKE_OSX_SYSROOT_PATH}/Developer/Library/Frameworks
  129. # Xcode 5 OSX
  130. ${_CMAKE_OSX_SYSROOT_PATH}/../../../../../Library/Frameworks
  131. )
  132. get_filename_component(_abolute_path "${_path}" ABSOLUTE)
  133. if(EXISTS "${_abolute_path}")
  134. list(APPEND CMAKE_SYSTEM_FRAMEWORK_PATH "${_abolute_path}")
  135. break()
  136. endif()
  137. endforeach()
  138. endif()
  139. if (OSX_DEVELOPER_ROOT AND EXISTS "${OSX_DEVELOPER_ROOT}/Library/Frameworks")
  140. list(APPEND CMAKE_SYSTEM_FRAMEWORK_PATH
  141. ${OSX_DEVELOPER_ROOT}/Library/Frameworks)
  142. endif()
  143. list(APPEND CMAKE_SYSTEM_FRAMEWORK_PATH
  144. /Library/Frameworks
  145. /Network/Library/Frameworks
  146. /System/Library/Frameworks)
  147. # Warn about known system mis-configuration case.
  148. if(CMAKE_OSX_SYSROOT)
  149. get_property(_IN_TC GLOBAL PROPERTY IN_TRY_COMPILE)
  150. if(NOT _IN_TC AND
  151. NOT IS_SYMLINK "${CMAKE_OSX_SYSROOT}/Library/Frameworks"
  152. AND IS_SYMLINK "${CMAKE_OSX_SYSROOT}/Library/Frameworks/Frameworks")
  153. message(WARNING "The SDK Library/Frameworks path\n"
  154. " ${CMAKE_OSX_SYSROOT}/Library/Frameworks\n"
  155. "is not set up correctly on this system. "
  156. "This is known to occur when installing Xcode 3.2.6:\n"
  157. " http://bugs.python.org/issue14018\n"
  158. "The problem may cause build errors that report missing system frameworks. "
  159. "Fix your SDK symlinks to resolve this issue and avoid this warning."
  160. )
  161. endif()
  162. endif()
  163. # default to searching for application bundles first
  164. if(NOT DEFINED CMAKE_FIND_APPBUNDLE)
  165. set(CMAKE_FIND_APPBUNDLE FIRST)
  166. endif()
  167. # set up the default search directories for application bundles
  168. set(_apps_paths)
  169. foreach(_path
  170. "~/Applications"
  171. "/Applications"
  172. "${OSX_DEVELOPER_ROOT}/../Applications" # Xcode 4.3+
  173. "${OSX_DEVELOPER_ROOT}/Applications" # pre-4.3
  174. )
  175. get_filename_component(_apps "${_path}" ABSOLUTE)
  176. if(EXISTS "${_apps}")
  177. list(APPEND _apps_paths "${_apps}")
  178. endif()
  179. endforeach()
  180. if(_apps_paths)
  181. list(REMOVE_DUPLICATES _apps_paths)
  182. endif()
  183. set(CMAKE_SYSTEM_APPBUNDLE_PATH
  184. ${_apps_paths})
  185. unset(_apps_paths)
  186. include(Platform/UnixPaths)
  187. if(_CMAKE_OSX_SYSROOT_PATH AND EXISTS ${_CMAKE_OSX_SYSROOT_PATH}/usr/include)
  188. list(APPEND CMAKE_SYSTEM_PREFIX_PATH ${_CMAKE_OSX_SYSROOT_PATH}/usr)
  189. foreach(lang C CXX OBJC OBJCXX)
  190. list(APPEND _CMAKE_${lang}_IMPLICIT_INCLUDE_DIRECTORIES_INIT ${_CMAKE_OSX_SYSROOT_PATH}/usr/include)
  191. endforeach()
  192. endif()
  193. list(APPEND CMAKE_SYSTEM_PREFIX_PATH
  194. /sw # Fink
  195. /opt/local # MacPorts
  196. )