Clang.cmake 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. # This module is shared by multiple languages; use include blocker.
  4. if(__COMPILER_CLANG)
  5. return()
  6. endif()
  7. set(__COMPILER_CLANG 1)
  8. include(Compiler/CMakeCommonCompilerMacros)
  9. if("x${CMAKE_C_SIMULATE_ID}" STREQUAL "xMSVC"
  10. OR "x${CMAKE_CXX_SIMULATE_ID}" STREQUAL "xMSVC"
  11. OR "x${CMAKE_Fortran_SIMULATE_ID}" STREQUAL "xMSVC")
  12. macro(__compiler_clang lang)
  13. endmacro()
  14. else()
  15. include(Compiler/GNU)
  16. macro(__compiler_clang lang)
  17. __compiler_gnu(${lang})
  18. set(CMAKE_${lang}_COMPILE_OPTIONS_PIE "-fPIE")
  19. # Link options for PIE are already set in 'Compiler/GNU.cmake'
  20. # but clang may require alternate syntax on some platforms
  21. if (APPLE)
  22. set(CMAKE_${lang}_LINK_OPTIONS_PIE ${CMAKE_${lang}_COMPILE_OPTIONS_PIE} -Xlinker -pie)
  23. set(CMAKE_${lang}_LINK_OPTIONS_NO_PIE -Xlinker -no_pie)
  24. endif()
  25. set(CMAKE_INCLUDE_SYSTEM_FLAG_${lang} "-isystem ")
  26. set(CMAKE_${lang}_COMPILE_OPTIONS_VISIBILITY "-fvisibility=")
  27. if(CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 3.4.0)
  28. set(CMAKE_${lang}_COMPILE_OPTIONS_TARGET "-target ")
  29. set(CMAKE_${lang}_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN "-gcc-toolchain ")
  30. else()
  31. set(CMAKE_${lang}_COMPILE_OPTIONS_TARGET "--target=")
  32. set(CMAKE_${lang}_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN "--gcc-toolchain=")
  33. endif()
  34. set(CMAKE_${lang}_LINKER_WRAPPER_FLAG "-Xlinker" " ")
  35. set(CMAKE_${lang}_LINKER_WRAPPER_FLAG_SEP)
  36. if(CMAKE_${lang}_COMPILER_TARGET)
  37. if(CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 3.4.0)
  38. list(APPEND CMAKE_${lang}_COMPILER_PREDEFINES_COMMAND "-target" "${CMAKE_${lang}_COMPILER_TARGET}")
  39. else()
  40. list(APPEND CMAKE_${lang}_COMPILER_PREDEFINES_COMMAND "--target=${CMAKE_${lang}_COMPILER_TARGET}")
  41. endif()
  42. endif()
  43. set(_CMAKE_${lang}_IPO_SUPPORTED_BY_CMAKE YES)
  44. set(_CMAKE_${lang}_IPO_MAY_BE_SUPPORTED_BY_COMPILER YES)
  45. string(COMPARE EQUAL "${CMAKE_${lang}_COMPILER_ID}" "AppleClang" __is_apple_clang)
  46. # '-flto=thin' available since Clang 3.9 and Xcode 8
  47. # * http://clang.llvm.org/docs/ThinLTO.html#clang-llvm
  48. # * https://trac.macports.org/wiki/XcodeVersionInfo
  49. set(_CMAKE_LTO_THIN TRUE)
  50. if(__is_apple_clang)
  51. if(CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 8.0)
  52. set(_CMAKE_LTO_THIN FALSE)
  53. endif()
  54. else()
  55. if(CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 3.9)
  56. set(_CMAKE_LTO_THIN FALSE)
  57. endif()
  58. endif()
  59. if(_CMAKE_LTO_THIN)
  60. set(CMAKE_${lang}_COMPILE_OPTIONS_IPO "-flto=thin")
  61. else()
  62. set(CMAKE_${lang}_COMPILE_OPTIONS_IPO "-flto")
  63. endif()
  64. if(ANDROID)
  65. # https://github.com/android-ndk/ndk/issues/242
  66. set(CMAKE_${lang}_LINK_OPTIONS_IPO "-fuse-ld=gold")
  67. endif()
  68. if(ANDROID OR __is_apple_clang)
  69. set(__ar "${CMAKE_AR}")
  70. set(__ranlib "${CMAKE_RANLIB}")
  71. else()
  72. set(__ar "${CMAKE_${lang}_COMPILER_AR}")
  73. set(__ranlib "${CMAKE_${lang}_COMPILER_RANLIB}")
  74. endif()
  75. set(CMAKE_${lang}_ARCHIVE_CREATE_IPO
  76. "\"${__ar}\" cr <TARGET> <LINK_FLAGS> <OBJECTS>"
  77. )
  78. set(CMAKE_${lang}_ARCHIVE_APPEND_IPO
  79. "\"${__ar}\" r <TARGET> <LINK_FLAGS> <OBJECTS>"
  80. )
  81. set(CMAKE_${lang}_ARCHIVE_FINISH_IPO
  82. "\"${__ranlib}\" <TARGET>"
  83. )
  84. set(CMAKE_PCH_EXTENSION .pch)
  85. if (NOT CMAKE_GENERATOR MATCHES "Xcode")
  86. set(CMAKE_PCH_PROLOGUE "#pragma clang system_header")
  87. endif()
  88. set(CMAKE_${lang}_COMPILE_OPTIONS_USE_PCH -Xclang -include-pch -Xclang <PCH_FILE>)
  89. set(CMAKE_${lang}_COMPILE_OPTIONS_CREATE_PCH -Xclang -emit-pch -Xclang -include -Xclang <PCH_HEADER>)
  90. endmacro()
  91. endif()