GNU.cmake 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. macro(__compiler_gnu lang)
  10. # Feature flags.
  11. set(CMAKE_${lang}_VERBOSE_FLAG "-v")
  12. set(CMAKE_${lang}_COMPILE_OPTIONS_PIC "-fPIC")
  13. if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 3.4)
  14. set(CMAKE_${lang}_COMPILE_OPTIONS_PIE "-fPIE")
  15. endif()
  16. if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 4.0)
  17. set(CMAKE_${lang}_COMPILE_OPTIONS_VISIBILITY "-fvisibility=")
  18. endif()
  19. set(CMAKE_SHARED_LIBRARY_${lang}_FLAGS "-fPIC")
  20. set(CMAKE_SHARED_LIBRARY_CREATE_${lang}_FLAGS "-shared")
  21. set(CMAKE_${lang}_COMPILE_OPTIONS_SYSROOT "--sysroot=")
  22. # Older versions of gcc (< 4.5) contain a bug causing them to report a missing
  23. # header file as a warning if depfiles are enabled, causing check_header_file
  24. # tests to always succeed. Work around this by disabling dependency tracking
  25. # in try_compile mode.
  26. get_property(_IN_TC GLOBAL PROPERTY IN_TRY_COMPILE)
  27. if(NOT _IN_TC OR CMAKE_FORCE_DEPFILES)
  28. # distcc does not transform -o to -MT when invoking the preprocessor
  29. # internally, as it ought to. Work around this bug by setting -MT here
  30. # even though it isn't strictly necessary.
  31. set(CMAKE_DEPFILE_FLAGS_${lang} "-MD -MT <OBJECT> -MF <DEPFILE>")
  32. endif()
  33. # Initial configuration flags.
  34. string(APPEND CMAKE_${lang}_FLAGS_INIT " ")
  35. string(APPEND CMAKE_${lang}_FLAGS_DEBUG_INIT " -g")
  36. string(APPEND CMAKE_${lang}_FLAGS_MINSIZEREL_INIT " -Os -DNDEBUG")
  37. string(APPEND CMAKE_${lang}_FLAGS_RELEASE_INIT " -O3 -DNDEBUG")
  38. string(APPEND CMAKE_${lang}_FLAGS_RELWITHDEBINFO_INIT " -O2 -g -DNDEBUG")
  39. set(CMAKE_${lang}_CREATE_PREPROCESSED_SOURCE "<CMAKE_${lang}_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -E <SOURCE> > <PREPROCESSED_SOURCE>")
  40. set(CMAKE_${lang}_CREATE_ASSEMBLY_SOURCE "<CMAKE_${lang}_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -S <SOURCE> -o <ASSEMBLY_SOURCE>")
  41. if(NOT APPLE OR NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 4) # work around #4462
  42. set(CMAKE_INCLUDE_SYSTEM_FLAG_${lang} "-isystem ")
  43. endif()
  44. set(_CMAKE_${lang}_IPO_SUPPORTED_BY_CMAKE YES)
  45. set(_CMAKE_${lang}_IPO_MAY_BE_SUPPORTED_BY_COMPILER NO)
  46. # '-flto' introduced since GCC 4.5:
  47. # * https://gcc.gnu.org/onlinedocs/gcc-4.4.7/gcc/Option-Summary.html (no)
  48. # * https://gcc.gnu.org/onlinedocs/gcc-4.5.4/gcc/Option-Summary.html (yes)
  49. if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 4.5)
  50. set(_CMAKE_${lang}_IPO_MAY_BE_SUPPORTED_BY_COMPILER YES)
  51. set(__lto_flags -flto)
  52. if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 4.7)
  53. # '-ffat-lto-objects' introduced since GCC 4.7:
  54. # * https://gcc.gnu.org/onlinedocs/gcc-4.6.4/gcc/Option-Summary.html (no)
  55. # * https://gcc.gnu.org/onlinedocs/gcc-4.7.4/gcc/Option-Summary.html (yes)
  56. list(APPEND __lto_flags -fno-fat-lto-objects)
  57. endif()
  58. set(CMAKE_${lang}_COMPILE_OPTIONS_IPO ${__lto_flags})
  59. # Need to use version of 'ar'/'ranlib' with plugin support.
  60. # Quote from [documentation][1]:
  61. #
  62. # To create static libraries suitable for LTO,
  63. # use gcc-ar and gcc-ranlib instead of ar and ranlib
  64. #
  65. # [1]: https://gcc.gnu.org/onlinedocs/gcc-4.9.4/gcc/Optimize-Options.html
  66. set(CMAKE_${lang}_ARCHIVE_CREATE_IPO
  67. "\"${CMAKE_${lang}_COMPILER_AR}\" cr <TARGET> <LINK_FLAGS> <OBJECTS>"
  68. )
  69. set(CMAKE_${lang}_ARCHIVE_APPEND_IPO
  70. "\"${CMAKE_${lang}_COMPILER_AR}\" r <TARGET> <LINK_FLAGS> <OBJECTS>"
  71. )
  72. set(CMAKE_${lang}_ARCHIVE_FINISH_IPO
  73. "\"${CMAKE_${lang}_COMPILER_RANLIB}\" <TARGET>"
  74. )
  75. endif()
  76. endmacro()