How to conditionally enable ltcg only if Qt was built with ltcg?

799 Views Asked by At

A qmake project is sometimes built using Qt that was compiled with ltcg. Since the Qt's ltcg build setting doesn't propagate automatically to that of the project that uses given Qt install, the MSVC linker wastes time and issues the following warning:

MSIL .netmodule or module compiled with /GL found; restarting link with /LTCG; add /LTCG to the link command line to improve linker performance

Is there a way to conditionally enable ltcg for the project iff the Qt it was built with also had ltcg enabled?

1

There are 1 best solutions below

0
On

It turns out that the configuration options used to build each Qt module are retained in QT.<module>.module_config, where <module> is the name of the module, such as core.

Thus, given that every Qt project uses the core module, we can forward the option from that module into our project file as follows:

contains(QT.core.module_config, ltcg) {
  CONFIG += ltcg
  msvc: QMAKE_LIBFLAGS *= /ltcg
}

The QMAKE_LIBFLAGS option is missing from Qt's configuration system - the librarian complains about the lack of /ltcg just as the linker does.

The following snippet was using to find those variables:

for(var, $$list($$enumerate_vars())) {
    value = $$eval($$var)
    contains(value, ltcg): message($$var = $$value)
}