为什么 CMake 在启用 LTO/IPO 时会设置 -no-fat-lto-objects?

Why does CMake set -no-fat-lto-objects when I enable LTO/IPO?

提问人:einpoklum 提问时间:1/22/2022 更新时间:6/12/2023 访问量:1440

问:

我正在使用 CMake 为我的 C 编译启用 IPO(过程间优化):

set_property(TARGET foo PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)

正如预期的那样,这会导致添加编译器标志。但是,它还添加了:这意味着生成的目标文件将只有中间代码,而不是正确编译的中间代码;这意味着链接器必须支持我的系统编译器的中间表示形式,并且能够识别 IPO/LTO。-flto-fno-fat-lto-objects

我没有要求,也不想要它。我可以让 CMake 添加此选项吗?-fno-fat-lto-objects

C CMake 编译 中级语言 LTO

评论


答:

2赞 KamilCuk 1/22/2022 #1
if(CMAKE_C_COMPILER MATCHES "GNU")
   set(CMAKE_C_COMPILE_OPTIONS_IPO "-flto")
endif()

如何找到它:

  1. 导航到 CMake 安装目录和 ,大部分内容都在那里。Modules

    • 它在我的 Linux 系统上/usr/share/cmake/Modules
  2. 找到您感兴趣的字符串或类似字符串

    • 在我的系统上,我这样做:

      $ grep fno-fat-lto-objects -r .
      ./Compiler/GNU.cmake:      list(APPEND __lto_flags -fno-fat-lto-objects)
      
  3. 导航并检查生成的文件,即使用字符串的上下文:

       # '-flto' introduced since GCC 4.5:
       # * https://gcc.gnu.org/onlinedocs/gcc-4.4.7/gcc/Option-Summary.html (no)
       # * https://gcc.gnu.org/onlinedocs/gcc-4.5.4/gcc/Option-Summary.html (yes)
       if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 4.5)
         set(_CMAKE_${lang}_IPO_MAY_BE_SUPPORTED_BY_COMPILER YES)
         set(__lto_flags -flto)
    
         if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 4.7)
           # '-ffat-lto-objects' introduced since GCC 4.7:
           # * https://gcc.gnu.org/onlinedocs/gcc-4.6.4/gcc/Option-Summary.html (no)
           # * https://gcc.gnu.org/onlinedocs/gcc-4.7.4/gcc/Option-Summary.html (yes)
           list(APPEND __lto_flags -fno-fat-lto-objects)
         endif()
    
         set(CMAKE_${lang}_COMPILE_OPTIONS_IPO ${__lto_flags})
    
  4. 想出一种解决方法来实现此类 coe 的自定义行为。

评论

0赞 einpoklum 1/22/2022
此外,对于后来的叮当声和早期的叮当声。-flto=full-flto
0赞 KamilCuk 1/22/2022
GNU.cmake 也由 clang 执行。也许更改为类似 then regex remove from it 或类似的东西。if(CMAKE_C_COMPILER MATCHES "GNU")if(CMAKE_C_COMPILE_OPTIONS_IPO MATCHES "-fno-fat-lto-objects")-fno-fat-lto-objects
6赞 einpoklum 1/22/2022 #2

我相信这是一个 CMake 错误......我现在已经提交了

开发人员只是做出了错误的假设,即这就是人们想要的。