c++cmakeconanconan-2

Build Conan 2 packages with LTO


I want to build from source Conan 2 packages with LTO using GCC and Unix Makefiles generator. I'm building packages with conan install . --build=missing. LTO (Link Time Optimization) can be enabled in CMake by set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE).

My initial approach was to add compilation flags to the Conan profile:

[conf]
tools.build:cxxflags+=["-flto=auto"]

However, this doesn't work alone. As I understand, CMake will still use ld as the linker, but objects generated with LTO should be linked by g++ in this case. This is probably not the only thing to set up for LTO.

I'm trying to set up multiple LTO-related low-level details by hand, but it would be ideal to just pass the high-level CMAKE_INTERPROCEDURAL_OPTIMIZATION to CMake somehow. Is this feasible?


Solution

  • There are 2 different ways to inject CMake variables into packages, always assuming that those package recipes use the CMakeToolchain: