GNU.cmake 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. set(__pch_header_C "c-header")
  10. set(__pch_header_CXX "c++-header")
  11. set(__pch_header_OBJC "objective-c-header")
  12. set(__pch_header_OBJCXX "objective-c++-header")
  13. macro(__compiler_gnu lang)
  14. # Feature flags.
  15. set(CMAKE_${lang}_VERBOSE_FLAG "-v")
  16. set(CMAKE_${lang}_COMPILE_OPTIONS_WARNING_AS_ERROR "-Werror")
  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(CMAKE_${lang}_COMPILER_ID STREQUAL "GNU" AND _IN_TC AND NOT CMAKE_FORCE_DEPFILES)
  41. else()
  42. # distcc does not transform -o to -MT when invoking the preprocessor
  43. # internally, as it ought to. Work around this bug by setting -MT here
  44. # even though it isn't strictly necessary.
  45. set(CMAKE_DEPFILE_FLAGS_${lang} "-MD -MT <DEP_TARGET> -MF <DEP_FILE>")
  46. endif()
  47. # Initial configuration flags.
  48. string(APPEND CMAKE_${lang}_FLAGS_INIT " ")
  49. string(APPEND CMAKE_${lang}_FLAGS_DEBUG_INIT " -g")
  50. string(APPEND CMAKE_${lang}_FLAGS_MINSIZEREL_INIT " -Os -DNDEBUG")
  51. string(APPEND CMAKE_${lang}_FLAGS_RELEASE_INIT " -O3 -DNDEBUG")
  52. string(APPEND CMAKE_${lang}_FLAGS_RELWITHDEBINFO_INIT " -O2 -g -DNDEBUG")
  53. set(CMAKE_${lang}_CREATE_PREPROCESSED_SOURCE "<CMAKE_${lang}_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -E <SOURCE> > <PREPROCESSED_SOURCE>")
  54. set(CMAKE_${lang}_CREATE_ASSEMBLY_SOURCE "<CMAKE_${lang}_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -S <SOURCE> -o <ASSEMBLY_SOURCE>")
  55. if(NOT APPLE OR NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 4) # work around #4462
  56. set(CMAKE_INCLUDE_SYSTEM_FLAG_${lang} "-isystem ")
  57. endif()
  58. set(_CMAKE_${lang}_IPO_SUPPORTED_BY_CMAKE YES)
  59. set(_CMAKE_${lang}_IPO_MAY_BE_SUPPORTED_BY_COMPILER NO)
  60. # '-flto' introduced since GCC 4.5:
  61. # * https://gcc.gnu.org/onlinedocs/gcc-4.4.7/gcc/Option-Summary.html (no)
  62. # * https://gcc.gnu.org/onlinedocs/gcc-4.5.4/gcc/Option-Summary.html (yes)
  63. if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 4.5)
  64. set(_CMAKE_${lang}_IPO_MAY_BE_SUPPORTED_BY_COMPILER YES)
  65. set(__lto_flags -flto)
  66. if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 4.7)
  67. # '-ffat-lto-objects' introduced since GCC 4.7:
  68. # * https://gcc.gnu.org/onlinedocs/gcc-4.6.4/gcc/Option-Summary.html (no)
  69. # * https://gcc.gnu.org/onlinedocs/gcc-4.7.4/gcc/Option-Summary.html (yes)
  70. list(APPEND __lto_flags -fno-fat-lto-objects)
  71. endif()
  72. set(CMAKE_${lang}_COMPILE_OPTIONS_IPO ${__lto_flags})
  73. # Need to use version of 'ar'/'ranlib' with plugin support.
  74. # Quote from [documentation][1]:
  75. #
  76. # To create static libraries suitable for LTO,
  77. # use gcc-ar and gcc-ranlib instead of ar and ranlib
  78. #
  79. # [1]: https://gcc.gnu.org/onlinedocs/gcc-4.9.4/gcc/Optimize-Options.html
  80. set(CMAKE_${lang}_ARCHIVE_CREATE_IPO
  81. "\"${CMAKE_${lang}_COMPILER_AR}\" cr <TARGET> <LINK_FLAGS> <OBJECTS>"
  82. )
  83. set(CMAKE_${lang}_ARCHIVE_APPEND_IPO
  84. "\"${CMAKE_${lang}_COMPILER_AR}\" r <TARGET> <LINK_FLAGS> <OBJECTS>"
  85. )
  86. set(CMAKE_${lang}_ARCHIVE_FINISH_IPO
  87. "\"${CMAKE_${lang}_COMPILER_RANLIB}\" <TARGET>"
  88. )
  89. endif()
  90. set(CMAKE_${lang}_COMPILER_PREDEFINES_COMMAND "${CMAKE_${lang}_COMPILER}")
  91. if(CMAKE_${lang}_COMPILER_ARG1)
  92. separate_arguments(_COMPILER_ARGS NATIVE_COMMAND "${CMAKE_${lang}_COMPILER_ARG1}")
  93. list(APPEND CMAKE_${lang}_COMPILER_PREDEFINES_COMMAND ${_COMPILER_ARGS})
  94. unset(_COMPILER_ARGS)
  95. endif()
  96. list(APPEND CMAKE_${lang}_COMPILER_PREDEFINES_COMMAND "-dM" "-E" "-c" "${CMAKE_ROOT}/Modules/CMakeCXXCompilerABI.cpp")
  97. if(NOT "x${lang}" STREQUAL "xFortran")
  98. set(CMAKE_PCH_EXTENSION .gch)
  99. if (NOT CMAKE_GENERATOR MATCHES "Xcode")
  100. set(CMAKE_PCH_PROLOGUE "#pragma GCC system_header")
  101. endif()
  102. set(CMAKE_${lang}_COMPILE_OPTIONS_INVALID_PCH -Winvalid-pch)
  103. set(CMAKE_${lang}_COMPILE_OPTIONS_USE_PCH -include <PCH_HEADER>)
  104. set(CMAKE_${lang}_COMPILE_OPTIONS_CREATE_PCH -x ${__pch_header_${lang}} -include <PCH_HEADER>)
  105. endif()
  106. # '-fdiagnostics-color=always' introduced since GCC 4.9
  107. # https://gcc.gnu.org/gcc-4.9/changes.html
  108. if(CMAKE_${lang}_COMPILER_VERSION VERSION_GREATER_EQUAL 4.9)
  109. set(CMAKE_${lang}_COMPILE_OPTIONS_COLOR_DIAGNOSTICS "-fdiagnostics-color=always")
  110. set(CMAKE_${lang}_COMPILE_OPTIONS_COLOR_DIAGNOSTICS_OFF "-fno-diagnostics-color")
  111. endif()
  112. endmacro()