cmakelto

How do I enable link time optimization (LTO) with CMake?


I have the most recent cmake build and trying all build configurations (Debug, MinSizeRel, RelWithDebugInfo, Release, General) I see nowhere (text search) in generated makefiles the string -lto, so or the functionality is still not present, or it requires manual intervertion (in that case a text search for LTO or Link time optimization) over the documentation gives no result, so I see nowhere in official documentation a way to enable LTO.

Is there any way to enable LTO "globally" (for all compilers) without specifying manually flags that could be at worst supported only by GCC?


Solution

  • Edit: as of 28 October, 2015 on CMake IRC

    jcelerier | I have a question about INTERPROCEDURAL_OPTIMIZATION
    jcelerier | it sounds like it should enable -flto on gcc
    +ngladitz | jcelerier: its only implemented for the intel compiler
    jcelerier | ngladitz: ah, okay
    jcelerier | are there other switches for pgo / lto ?
    jcelerier | or must it be done by hand ?
    +ngladitz | there currently is no first class support otherwise     
    

    Related CMake issue: https://gitlab.kitware.com/cmake/cmake/issues/15245


    CMake has the INTERPROCEDURAL_OPTIMIZATION property on targets, which sounds like it may enable LTO on some platforms.

    Link : http://www.cmake.org/cmake/help/v3.0/prop_tgt/INTERPROCEDURAL_OPTIMIZATION.html

    To enable it on a target MyLib :

    add_library(MyLib ...)
    ...
    set_property(TARGET MyLib PROPERTY INTERPROCEDURAL_OPTIMIZATION True)