• Jason Rhinelander's avatar
    Overhaul LTO flag detection · 1bee6e7d
    Jason Rhinelander authored
    Clang on linux currently fails to run cmake:
    
        $ CC=clang CXX=clang++ cmake ..
        ...
        -- Configuring done
        CMake Error at tools/pybind11Tools.cmake:135 (target_compile_options):
          Error evaluating generator expression:
    
            $<:-flto>
    
          Expression did not evaluate to a known generator expression
        Call Stack (most recent call first):
          tests/CMakeLists.txt:68 (pybind11_add_module)
    
    But investigating this led to various other -flto detection problems;
    this commit thus overhauls LTO flag detection:
    
    - -flto needs to be passed to the linker as well
    - Also compile with -fno-fat-lto-objects under GCC
    - Pass the equivalent flags to MSVC
    - Enable LTO flags for via generator expressions (for non-debug builds
      only), so that multi-config builds (like on Windows) still work
      properly.  This seems reasonable, however, even on single-config
      builds (and simplifies the cmake code a bit).
    - clang's lto linker plugins don't accept '-Os', so replace it with
      '-O3' when doing a MINSIZEREL build
    - Enable trying ThinLTO by default for test suite (only affects clang)
    - Match Clang$ rather than ^Clang$ because, for cmake with 3.0+
      policies in effect, the compiler ID will be AppleClang on macOS.
    1bee6e7d
pybind11Tools.cmake 7.75 KB