Darwin.cmake 9.1 KB

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