GNU.cmake 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_GNU)
  5. return()
  6. endif()
  7. set(__COMPILER_GNU 1)
  8. include(Compiler/CMakeCommonCompilerMacros)
  9. include(Internal/CMakeCheckCompilerFlag)
  10. set(__pch_header_C "c-header")
  11. set(__pch_header_CXX "c++-header")
  12. set(__pch_header_OBJC "objective-c-header")
  13. set(__pch_header_OBJCXX "objective-c++-header")
  14. macro(__compiler_gnu lang)
  15. # Feature flags.
  16. set(CMAKE_${lang}_VERBOSE_FLAG "-v")
  17. set(CMAKE_${lang}_COMPILE_OPTIONS_PIC "-fPIC")
  18. set (_CMAKE_${lang}_PIE_MAY_BE_SUPPORTED_BY_LINKER NO)
  19. if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 3.4)
  20. set(CMAKE_${lang}_COMPILE_OPTIONS_PIE "-fPIE")
  21. # Support of PIE at link stage depends on various elements : platform, compiler, linker
  22. # so to activate it, module CheckPIESupported must be used.
  23. set (_CMAKE_${lang}_PIE_MAY_BE_SUPPORTED_BY_LINKER YES)
  24. set(CMAKE_${lang}_LINK_OPTIONS_PIE ${CMAKE_${lang}_COMPILE_OPTIONS_PIE} "-pie")
  25. set(CMAKE_${lang}_LINK_OPTIONS_NO_PIE "-no-pie")
  26. endif()
  27. if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 4.0)
  28. set(CMAKE_${lang}_COMPILE_OPTIONS_VISIBILITY "-fvisibility=")
  29. endif()
  30. set(CMAKE_SHARED_LIBRARY_${lang}_FLAGS "-fPIC")
  31. set(CMAKE_SHARED_LIBRARY_CREATE_${lang}_FLAGS "-shared")
  32. set(CMAKE_${lang}_COMPILE_OPTIONS_SYSROOT "--sysroot=")
  33. set(CMAKE_${lang}_LINKER_WRAPPER_FLAG "-Wl,")
  34. set(CMAKE_${lang}_LINKER_WRAPPER_FLAG_SEP ",")
  35. # Older versions of gcc (< 4.5) contain a bug causing them to report a missing
  36. # header file as a warning if depfiles are enabled, causing check_header_file
  37. # tests to always succeed. Work around this by disabling dependency tracking
  38. # in try_compile mode.
  39. get_property(_IN_TC GLOBAL PROPERTY IN_TRY_COMPILE)
  40. if(NOT _IN_TC OR CMAKE_FORCE_DEPFILES)
  41. # distcc does not transform -o to -MT when invoking the preprocessor
  42. # internally, as it ought to. Work around this bug by setting -MT here
  43. # even though it isn't strictly necessary.
  44. set(CMAKE_DEPFILE_FLAGS_${lang} "-MD -MT <OBJECT> -MF <DEPFILE>")
  45. endif()
  46. # Initial configuration flags.
  47. string(APPEND CMAKE_${lang}_FLAGS_INIT " ")
  48. string(APPEND CMAKE_${lang}_FLAGS_DEBUG_INIT " -g")
  49. string(APPEND CMAKE_${lang}_FLAGS_MINSIZEREL_INIT " -Os -DNDEBUG")
  50. string(APPEND CMAKE_${lang}_FLAGS_RELEASE_INIT " -O3 -DNDEBUG")
  51. string(APPEND CMAKE_${lang}_FLAGS_RELWITHDEBINFO_INIT " -O2 -g -DNDEBUG")
  52. set(CMAKE_${lang}_CREATE_PREPROCESSED_SOURCE "<CMAKE_${lang}_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -E <SOURCE> > <PREPROCESSED_SOURCE>")
  53. set(CMAKE_${lang}_CREATE_ASSEMBLY_SOURCE "<CMAKE_${lang}_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -S <SOURCE> -o <ASSEMBLY_SOURCE>")
  54. if(NOT APPLE OR NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 4) # work around #4462
  55. set(CMAKE_INCLUDE_SYSTEM_FLAG_${lang} "-isystem ")
  56. endif()
  57. set(_CMAKE_${lang}_IPO_SUPPORTED_BY_CMAKE YES)
  58. set(_CMAKE_${lang}_IPO_MAY_BE_SUPPORTED_BY_COMPILER NO)
  59. # '-flto' introduced since GCC 4.5:
  60. # * https://gcc.gnu.org/onlinedocs/gcc-4.4.7/gcc/Option-Summary.html (no)
  61. # * https://gcc.gnu.org/onlinedocs/gcc-4.5.4/gcc/Option-Summary.html (yes)
  62. if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 4.5)
  63. set(_CMAKE_${lang}_IPO_MAY_BE_SUPPORTED_BY_COMPILER YES)
  64. set(__lto_flags -flto)
  65. if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 4.7)
  66. # '-ffat-lto-objects' introduced since GCC 4.7:
  67. # * https://gcc.gnu.org/onlinedocs/gcc-4.6.4/gcc/Option-Summary.html (no)
  68. # * https://gcc.gnu.org/onlinedocs/gcc-4.7.4/gcc/Option-Summary.html (yes)
  69. list(APPEND __lto_flags -fno-fat-lto-objects)
  70. endif()
  71. set(CMAKE_${lang}_COMPILE_OPTIONS_IPO ${__lto_flags})
  72. # Need to use version of 'ar'/'ranlib' with plugin support.
  73. # Quote from [documentation][1]:
  74. #
  75. # To create static libraries suitable for LTO,
  76. # use gcc-ar and gcc-ranlib instead of ar and ranlib
  77. #
  78. # [1]: https://gcc.gnu.org/onlinedocs/gcc-4.9.4/gcc/Optimize-Options.html
  79. set(CMAKE_${lang}_ARCHIVE_CREATE_IPO
  80. "\"${CMAKE_${lang}_COMPILER_AR}\" cr <TARGET> <LINK_FLAGS> <OBJECTS>"
  81. )
  82. set(CMAKE_${lang}_ARCHIVE_APPEND_IPO
  83. "\"${CMAKE_${lang}_COMPILER_AR}\" r <TARGET> <LINK_FLAGS> <OBJECTS>"
  84. )
  85. set(CMAKE_${lang}_ARCHIVE_FINISH_IPO
  86. "\"${CMAKE_${lang}_COMPILER_RANLIB}\" <TARGET>"
  87. )
  88. endif()
  89. set(CMAKE_${lang}_COMPILER_PREDEFINES_COMMAND "${CMAKE_${lang}_COMPILER}")
  90. if(CMAKE_${lang}_COMPILER_ARG1)
  91. separate_arguments(_COMPILER_ARGS NATIVE_COMMAND "${CMAKE_${lang}_COMPILER_ARG1}")
  92. list(APPEND CMAKE_${lang}_COMPILER_PREDEFINES_COMMAND ${_COMPILER_ARGS})
  93. unset(_COMPILER_ARGS)
  94. endif()
  95. list(APPEND CMAKE_${lang}_COMPILER_PREDEFINES_COMMAND "-dM" "-E" "-c" "${CMAKE_ROOT}/Modules/CMakeCXXCompilerABI.cpp")
  96. set(CMAKE_PCH_EXTENSION .gch)
  97. if (NOT CMAKE_GENERATOR MATCHES "Xcode")
  98. set(CMAKE_PCH_PROLOGUE "#pragma GCC system_header")
  99. endif()
  100. set(CMAKE_${lang}_COMPILE_OPTIONS_USE_PCH -Winvalid-pch -include <PCH_HEADER>)
  101. set(CMAKE_${lang}_COMPILE_OPTIONS_CREATE_PCH -Winvalid-pch -x ${__pch_header_${lang}} -include <PCH_HEADER>)
  102. endmacro()