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

3

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

set_property(TARGET foo PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)

如预期的那样,这会添加一个-flto编译器标志。但是,它还会添加-fno-fat-lto-objects:这意味着生成的目标文件仅包含中间代码,而不是经过正确编译和中间代码的两者;这也意味着链接器必须支持我的系统编译器的中间表示,并且要具备IPO/LTO感知能力。
我没有要求添加-fno-fat-lto-objects,也不想要它。我能否让CMake不添加此选项?
2个回答

6
我相信这是一个CMake的bug... 我已经现在提交了
开发者们只是错误地假设这就是人们想要的。

2
if(CMAKE_C_COMPILER MATCHES "GNU")
   set(CMAKE_C_COMPILE_OPTIONS_IPO "-flto")
endif()

如何找到它:

步骤:

  1. Navigate to your CMake installation directory and to Modules, most of the stuff is there.

    • It's /usr/share/cmake/Modules on my Linux system
  2. Find the string or similar string that you are interested in

    • on my system, I do:

      $ grep fno-fat-lto-objects -r .
      ./Compiler/GNU.cmake:      list(APPEND __lto_flags -fno-fat-lto-objects)
      
  3. Navigate and inspect the resulting files, the context where the string is used:

       # '-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. Come up with a workaround to implement custom behavior of such coe.


此外,对于较新的clang编译器,使用-flto=full选项,而对于较早的clang编译器,则使用-flto选项。 - einpoklum
GNU.cmake 也被 clang 执行。或许可以将 if(CMAKE_C_COMPILER MATCHES "GNU") 更改为类似于 if(CMAKE_C_COMPILE_OPTIONS_IPO MATCHES "-fno-fat-lto-objects") 的内容,然后从中使用正则表达式删除 -fno-fat-lto-objects 或类似的选项。 - KamilCuk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接