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?
There are 2 different ways to inject CMake variables into packages, always assuming that those package recipes use the CMakeToolchain
:
Injecting a full custom CMake toolchain file, using the tools.cmake.cmaketoolchain:user_toolchain
configuration that accepts a list of user toolchains. These toolchains can also be distributed with the profile that declares the conf, making it relative with {{profile_dir}}
. There is an example in this section of the docs
Directly passing CMake variables with the tools.cmake.cmaketoolchain:extra_variables
, see this section of the CMakeToolchain docs