WholeProgramOptimization in CMake

There are 3 places in a Visual Studio project where Whole Program Optimization settings come into picture -

1) At Project level (Project → General tab)

WholeProgramOptimization

This is a convenience meta-setting to enable Whole Program Optimization.

In the project XML it's located at<PropertyGroup>/<WholeProgramOptimization>

2) At compiler level (C/C++ → Optimization tab)

WholeProgramOptimization /GL

This is the actual /GL setting, it defaults to the project-level setting.

In the project XML it's located at<ItemDefinitionGroup>/<ClCompile>/<WholeProgramOptimization>

3) At linker level (Linker → Optimization tab)

Link-Time Code Generation /LTGC

This is the actual /LTCG setting, it defaults to the project-level setting.

In the project XML it's located at <ItemDefinitionGroup>/<Link>/<LinkTimeCodeGeneration>

The following CMake commands won't set WholeProgramOptimization at project level, but at compiler and linker level. That's why the "convenience" setting in the General tab is blank. The net effect, however, is the same. WholeProgramOptimization is on.

set_target_properties(Wpo PROPERTIES COMPILE_FLAGS "$<$<CONFIG:Release>:/GL>")
set_target_properties(Wpo PROPERTIES LINK_FLAGS "$<$<CONFIG:Release>:/LTCG>")

To use this feature you need to add compiler option /GL(whole program optimization) and linker option /LTCG (Link-time Code Generation).

SET_TARGET_PROPERTIES(Wpo PROPERTIES COMPILE_FLAGS "/GL")
SET_TARGET_PROPERTIES(Wpo PROPERTIES LINK_FLAGS "/LTCG")