Add MUJOCO_ENABLE_LTO CMake option to make LTO configurable. Fixes #3374, #2904

PiperOrigin-RevId: 950629900
Change-Id: If4b3755f0e8653efbd9e6a4134140dd96b1f743d
This commit is contained in:
Yuval Tassa
2026-07-19 23:45:27 -07:00
committed by Copybara-Service
parent 3990305373
commit 6f487086a5
4 changed files with 31 additions and 22 deletions
+10 -7
View File
@@ -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)
+10 -7
View File
@@ -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)
+10 -7
View File
@@ -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)
+1 -1
View File
@@ -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