GNU.cmake 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. # define flags for linker depfile generation
  48. if(NOT DEFINED CMAKE_${lang}_LINKER_DEPFILE_SUPPORTED)
  49. ## Ensure ninja tool is recent enough...
  50. if(CMAKE_GENERATOR MATCHES "^Ninja")
  51. # Ninja 1.10 or upper is required
  52. execute_process(COMMAND "${CMAKE_MAKE_PROGRAM}" --version
  53. OUTPUT_VARIABLE _ninja_version
  54. ERROR_VARIABLE _ninja_version)
  55. if (_ninja_version MATCHES "[0-9]+(\\.[0-9]+)*")
  56. set (_ninja_version "${CMAKE_MATCH_0}")
  57. endif()
  58. if (_ninja_version VERSION_LESS "1.10")
  59. set(CMAKE_${lang}_LINKER_DEPFILE_SUPPORTED FALSE)
  60. endif()
  61. unset(_ninja_version)
  62. endif()
  63. if (NOT DEFINED CMAKE_${lang}_LINKER_DEPFILE_SUPPORTED)
  64. ## check if this feature is supported by the linker
  65. execute_process(COMMAND "${CMAKE_${lang}_COMPILER}" -Wl,--help
  66. OUTPUT_VARIABLE _linker_capabilities
  67. ERROR_VARIABLE _linker_capabilities)
  68. if(_linker_capabilities MATCHES "--dependency-file")
  69. set(CMAKE_${lang}_LINKER_DEPFILE_SUPPORTED TRUE)
  70. else()
  71. set(CMAKE_${lang}_LINKER_DEPFILE_SUPPORTED FALSE)
  72. endif()
  73. unset(_linker_capabilities)
  74. endif()
  75. endif()
  76. if (CMAKE_${lang}_LINKER_DEPFILE_SUPPORTED)
  77. set(CMAKE_${lang}_LINKER_DEPFILE_FLAGS "LINKER:--dependency-file,<DEP_FILE>")
  78. set(CMAKE_${lang}_LINKER_DEPFILE_FORMAT gcc)
  79. set(CMAKE_${lang}_LINK_DEPENDS_USE_LINKER TRUE)
  80. else()
  81. unset(CMAKE_${lang}_LINK_DEPENDS_USE_LINKER)
  82. endif()
  83. # Initial configuration flags.
  84. string(APPEND CMAKE_${lang}_FLAGS_INIT " ")
  85. string(APPEND CMAKE_${lang}_FLAGS_DEBUG_INIT " -g")
  86. string(APPEND CMAKE_${lang}_FLAGS_MINSIZEREL_INIT " -Os -DNDEBUG")
  87. string(APPEND CMAKE_${lang}_FLAGS_RELEASE_INIT " -O3 -DNDEBUG")
  88. string(APPEND CMAKE_${lang}_FLAGS_RELWITHDEBINFO_INIT " -O2 -g -DNDEBUG")
  89. set(CMAKE_${lang}_CREATE_PREPROCESSED_SOURCE "<CMAKE_${lang}_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -E <SOURCE> > <PREPROCESSED_SOURCE>")
  90. set(CMAKE_${lang}_CREATE_ASSEMBLY_SOURCE "<CMAKE_${lang}_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -S <SOURCE> -o <ASSEMBLY_SOURCE>")
  91. if(NOT APPLE OR NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 4) # work around #4462
  92. set(CMAKE_INCLUDE_SYSTEM_FLAG_${lang} "-isystem ")
  93. endif()
  94. set(_CMAKE_${lang}_IPO_SUPPORTED_BY_CMAKE YES)
  95. set(_CMAKE_${lang}_IPO_MAY_BE_SUPPORTED_BY_COMPILER NO)
  96. # '-flto' introduced since GCC 4.5:
  97. # * https://gcc.gnu.org/onlinedocs/gcc-4.4.7/gcc/Option-Summary.html (no)
  98. # * https://gcc.gnu.org/onlinedocs/gcc-4.5.4/gcc/Option-Summary.html (yes)
  99. if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 4.5)
  100. set(_CMAKE_${lang}_IPO_MAY_BE_SUPPORTED_BY_COMPILER YES)
  101. set(__lto_flags "")
  102. # '-flto=auto' introduced since GCC 10.1:
  103. # * https://gcc.gnu.org/onlinedocs/gcc-9.5.0/gcc/Optimize-Options.html#Optimize-Options (no)
  104. # * https://gcc.gnu.org/onlinedocs/gcc-10.1.0/gcc/Optimize-Options.html#Optimize-Options (yes)
  105. # Since GCC 12.1, the abundance of a parameter produces a warning if compiling multiple targets.
  106. # FIXME: What version of GCC for Windows added support for -flto=auto? 10.3 does not have it.
  107. if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 11.0)
  108. list(APPEND __lto_flags -flto=auto)
  109. elseif(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 10.1)
  110. if (CMAKE_HOST_WIN32)
  111. list(APPEND __lto_flags -flto=1)
  112. else()
  113. list(APPEND __lto_flags -flto=auto)
  114. endif()
  115. else()
  116. list(APPEND __lto_flags -flto)
  117. endif()
  118. if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 4.7)
  119. # '-ffat-lto-objects' introduced since GCC 4.7:
  120. # * https://gcc.gnu.org/onlinedocs/gcc-4.6.4/gcc/Option-Summary.html (no)
  121. # * https://gcc.gnu.org/onlinedocs/gcc-4.7.4/gcc/Option-Summary.html (yes)
  122. list(APPEND __lto_flags -fno-fat-lto-objects)
  123. endif()
  124. set(CMAKE_${lang}_COMPILE_OPTIONS_IPO ${__lto_flags})
  125. # Need to use version of 'ar'/'ranlib' with plugin support.
  126. # Quote from [documentation][1]:
  127. #
  128. # To create static libraries suitable for LTO,
  129. # use gcc-ar and gcc-ranlib instead of ar and ranlib
  130. #
  131. # [1]: https://gcc.gnu.org/onlinedocs/gcc-4.9.4/gcc/Optimize-Options.html
  132. set(CMAKE_${lang}_ARCHIVE_CREATE_IPO
  133. "\"${CMAKE_${lang}_COMPILER_AR}\" cr <TARGET> <LINK_FLAGS> <OBJECTS>"
  134. )
  135. set(CMAKE_${lang}_ARCHIVE_APPEND_IPO
  136. "\"${CMAKE_${lang}_COMPILER_AR}\" r <TARGET> <LINK_FLAGS> <OBJECTS>"
  137. )
  138. set(CMAKE_${lang}_ARCHIVE_FINISH_IPO
  139. "\"${CMAKE_${lang}_COMPILER_RANLIB}\" <TARGET>"
  140. )
  141. endif()
  142. if("${lang}" STREQUAL "CXX")
  143. set(CMAKE_${lang}_COMPILER_PREDEFINES_COMMAND "${CMAKE_${lang}_COMPILER}")
  144. if(CMAKE_${lang}_COMPILER_ARG1)
  145. separate_arguments(_COMPILER_ARGS NATIVE_COMMAND "${CMAKE_${lang}_COMPILER_ARG1}")
  146. list(APPEND CMAKE_${lang}_COMPILER_PREDEFINES_COMMAND ${_COMPILER_ARGS})
  147. unset(_COMPILER_ARGS)
  148. endif()
  149. list(APPEND CMAKE_${lang}_COMPILER_PREDEFINES_COMMAND "-dM" "-E" "-c" "${CMAKE_ROOT}/Modules/CMakeCXXCompilerABI.cpp")
  150. endif()
  151. if(NOT "x${lang}" STREQUAL "xFortran")
  152. set(CMAKE_PCH_EXTENSION .gch)
  153. if (NOT CMAKE_GENERATOR MATCHES "Xcode")
  154. set(CMAKE_PCH_PROLOGUE "#pragma GCC system_header")
  155. endif()
  156. set(CMAKE_${lang}_COMPILE_OPTIONS_INVALID_PCH -Winvalid-pch)
  157. set(CMAKE_${lang}_COMPILE_OPTIONS_USE_PCH -include <PCH_HEADER>)
  158. set(CMAKE_${lang}_COMPILE_OPTIONS_CREATE_PCH -x ${__pch_header_${lang}} -include <PCH_HEADER>)
  159. endif()
  160. # '-fdiagnostics-color=always' introduced since GCC 4.9
  161. # https://gcc.gnu.org/gcc-4.9/changes.html
  162. if(CMAKE_${lang}_COMPILER_VERSION VERSION_GREATER_EQUAL 4.9)
  163. set(CMAKE_${lang}_COMPILE_OPTIONS_COLOR_DIAGNOSTICS "-fdiagnostics-color=always")
  164. set(CMAKE_${lang}_COMPILE_OPTIONS_COLOR_DIAGNOSTICS_OFF "-fno-diagnostics-color")
  165. endif()
  166. endmacro()