zeromq / libzmq

ZeroMQ core engine in C++, implements ZMTP/3.1
https://www.zeromq.org
Mozilla Public License 2.0
9.64k stars 2.35k forks source link

Do not force LTO on MSVC users in CMake #4482

Open Tradias opened 1 year ago

Tradias commented 1 year ago

Issue description

When building through CMake with MSVC, BUILD_SHARED=on and CMAKE_BUILD_TYPE != Debug then /GL and /LTCG are automatically added to the compilation. This forces consumers to also link their application with /LTCG which is not always desired. Recommendation: Allow users to choose link-time optimization themselves, e.g. by setting CMAKE_INTERPROCEDURAL_OPTIMIZATION.

Environment

Minimal test code / Steps to reproduce the issue

  1. vcpkg install --host-triplet=x64-windows-static --triplet=x64-windows-static zeromq
  2. Link the installed library into a test project and compile a release build

What's the actual result? (include assertion message & call stack if applicable)

libzmq-mt-s-4_3_4.lib(zmq.cpp.obj) : MSIL .netmodule or module compiled with /GL found; restarting link with /LTCG; add /LTCG to the link command line to improve linker performance

What's the expected result?

Nothing

ljluestc commented 2 weeks ago

cmake_minimum_required(VERSION 3.10)
project(libzmq)

# Option to enable link-time optimization (LTCG) and whole program optimization (GL)
option(ZMQ_ENABLE_LTO "Enable link-time optimization (LTCG) and whole program optimization (GL)" OFF)

# Add the necessary flags based on the option
if (BUILD_SHARED)
    if (ZMQ_ENABLE_LTO)
        add_compile_options("/GL")
        add_link_options("/LTCG")
    endif()
endif()

# Your other CMake configuration code...

# Check for build type and set IPO if enabled
if (CMAKE_BUILD_TYPE STREQUAL "Release")
    if (ZMQ_ENABLE_LTO)
        set_target_properties(${TARGET_NAME} PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE)
    else()
        set_target_properties(${TARGET_NAME} PROPERTIES INTERPROCEDURAL_OPTIMIZATION ${CMAKE_INTERPROCEDURAL_OPTIMIZATION})
    endif()
endif()