diff --git a/cmake/MujocoOptions.cmake b/cmake/MujocoOptions.cmake index 952163a1..74bf25bb 100644 --- a/cmake/MujocoOptions.cmake +++ b/cmake/MujocoOptions.cmake @@ -108,13 +108,16 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang endif() endif() -# Enable interprocedural optimization (LTO) by default for non-Debug builds, but -# only when the caller has not made an explicit choice. Checking the value (rather -# than whether it is DEFINED) meant an explicit `-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=OFF` -# was silently overridden back to ON, e.g. in CI where LTO is disabled to cut -# build time. -if(NOT DEFINED CMAKE_INTERPROCEDURAL_OPTIMIZATION AND (CMAKE_BUILD_TYPE AND NOT CMAKE_BUILD_TYPE STREQUAL "Debug")) - set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON) +# Provide a user-facing option to control Link-Time Optimization (LTO/IPO). +# When ON (the default), LTO is enabled for non-Debug builds unless the caller +# has already set CMAKE_INTERPROCEDURAL_OPTIMIZATION explicitly. +# Users can disable LTO entirely with -DMUJOCO_ENABLE_LTO=OFF. +option(MUJOCO_ENABLE_LTO "Enable Link-Time Optimization (LTO) for non-Debug builds." ON) + +if(MUJOCO_ENABLE_LTO) + if(NOT DEFINED CMAKE_INTERPROCEDURAL_OPTIMIZATION AND (CMAKE_BUILD_TYPE AND NOT CMAKE_BUILD_TYPE STREQUAL "Debug")) + set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON) + endif() endif() include(MujocoHarden) diff --git a/sample/cmake/SampleOptions.cmake b/sample/cmake/SampleOptions.cmake index 952163a1..74bf25bb 100644 --- a/sample/cmake/SampleOptions.cmake +++ b/sample/cmake/SampleOptions.cmake @@ -108,13 +108,16 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang endif() endif() -# Enable interprocedural optimization (LTO) by default for non-Debug builds, but -# only when the caller has not made an explicit choice. Checking the value (rather -# than whether it is DEFINED) meant an explicit `-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=OFF` -# was silently overridden back to ON, e.g. in CI where LTO is disabled to cut -# build time. -if(NOT DEFINED CMAKE_INTERPROCEDURAL_OPTIMIZATION AND (CMAKE_BUILD_TYPE AND NOT CMAKE_BUILD_TYPE STREQUAL "Debug")) - set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON) +# Provide a user-facing option to control Link-Time Optimization (LTO/IPO). +# When ON (the default), LTO is enabled for non-Debug builds unless the caller +# has already set CMAKE_INTERPROCEDURAL_OPTIMIZATION explicitly. +# Users can disable LTO entirely with -DMUJOCO_ENABLE_LTO=OFF. +option(MUJOCO_ENABLE_LTO "Enable Link-Time Optimization (LTO) for non-Debug builds." ON) + +if(MUJOCO_ENABLE_LTO) + if(NOT DEFINED CMAKE_INTERPROCEDURAL_OPTIMIZATION AND (CMAKE_BUILD_TYPE AND NOT CMAKE_BUILD_TYPE STREQUAL "Debug")) + set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON) + endif() endif() include(MujocoHarden) diff --git a/simulate/cmake/SimulateOptions.cmake b/simulate/cmake/SimulateOptions.cmake index 952163a1..74bf25bb 100644 --- a/simulate/cmake/SimulateOptions.cmake +++ b/simulate/cmake/SimulateOptions.cmake @@ -108,13 +108,16 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang endif() endif() -# Enable interprocedural optimization (LTO) by default for non-Debug builds, but -# only when the caller has not made an explicit choice. Checking the value (rather -# than whether it is DEFINED) meant an explicit `-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=OFF` -# was silently overridden back to ON, e.g. in CI where LTO is disabled to cut -# build time. -if(NOT DEFINED CMAKE_INTERPROCEDURAL_OPTIMIZATION AND (CMAKE_BUILD_TYPE AND NOT CMAKE_BUILD_TYPE STREQUAL "Debug")) - set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON) +# Provide a user-facing option to control Link-Time Optimization (LTO/IPO). +# When ON (the default), LTO is enabled for non-Debug builds unless the caller +# has already set CMAKE_INTERPROCEDURAL_OPTIMIZATION explicitly. +# Users can disable LTO entirely with -DMUJOCO_ENABLE_LTO=OFF. +option(MUJOCO_ENABLE_LTO "Enable Link-Time Optimization (LTO) for non-Debug builds." ON) + +if(MUJOCO_ENABLE_LTO) + if(NOT DEFINED CMAKE_INTERPROCEDURAL_OPTIMIZATION AND (CMAKE_BUILD_TYPE AND NOT CMAKE_BUILD_TYPE STREQUAL "Debug")) + set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON) + endif() endif() include(MujocoHarden) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 9d136432..13ba059c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -56,7 +56,7 @@ macro(mujoco_test name) set_target_properties(${name} PROPERTIES BUILD_RPATH ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) # gtest_discover_tests is recommended over gtest_add_tests, but has issues on Windows. # It also requires a GoogleTest binary, so we fall back for custom MAIN_TARGET (e.g. benchmarks). - if(WIN32 OR _ARGS_MAIN_TARGET) + if(WIN32 OR APPLE OR _ARGS_MAIN_TARGET) gtest_add_tests( TARGET ${name} SOURCES ${name}.cc