From 6f487086a529999b00dcf63f4667c59e93881e3b Mon Sep 17 00:00:00 2001 From: Yuval Tassa Date: Sun, 19 Jul 2026 23:45:27 -0700 Subject: [PATCH] Add MUJOCO_ENABLE_LTO CMake option to make LTO configurable. Fixes #3374, #2904 PiperOrigin-RevId: 950629900 Change-Id: If4b3755f0e8653efbd9e6a4134140dd96b1f743d --- cmake/MujocoOptions.cmake | 17 ++++++++++------- sample/cmake/SampleOptions.cmake | 17 ++++++++++------- simulate/cmake/SimulateOptions.cmake | 17 ++++++++++------- test/CMakeLists.txt | 2 +- 4 files changed, 31 insertions(+), 22 deletions(-) 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