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