@@ -108,6 +108,51 @@ message(STATUS "C++ Standard: ${CMAKE_CXX_STANDARD}")
108108message (STATUS "C++ Standard Required: ${CMAKE_CXX_STANDARD_REQUIRED} " )
109109message (STATUS "C++ Extensions: ${CMAKE_CXX_EXTENSIONS} " )
110110
111+ # Detect OS distribution and version from /etc/os-release
112+ # LTO is known to cause build errors on Debian 11 and Rocky Linux 8
113+ set (LTO_UNSUPPORTED_OS FALSE )
114+ if (EXISTS "/etc/os-release" )
115+ file (STRINGS "/etc/os-release" OS_RELEASE_CONTENTS)
116+ set (OS_ID "" )
117+ set (OS_VERSION_ID "" )
118+ foreach (line ${OS_RELEASE_CONTENTS} )
119+ if (line MATCHES "^ID=(.*)$" )
120+ string (REGEX REPLACE "^ID=\" ?([^\" ]*)\" ?$" "\\ 1" OS_ID "${line} " )
121+ endif ()
122+ if (line MATCHES "^VERSION_ID=(.*)$" )
123+ string (REGEX REPLACE "^VERSION_ID=\" ?([^\" ]*)\" ?$" "\\ 1" OS_VERSION_ID "${line} " )
124+ endif ()
125+ endforeach ()
126+
127+ # Check for unsupported OS for LTO
128+ if ((OS_ID STREQUAL "debian" AND OS_VERSION_ID STREQUAL "11" ) OR
129+ (OS_ID STREQUAL "rocky" AND OS_VERSION_ID MATCHES "^8" ))
130+ set (LTO_UNSUPPORTED_OS TRUE )
131+ message (STATUS "Detected unsupported OS (${OS_ID} ${OS_VERSION_ID} ): LTO will be disabled due to known build issues" )
132+ endif ()
133+ endif ()
134+
135+ option (LINK_TIME_OPTIMIZATION "Flag to control link time optimization: on by default" ON )
136+ set (LTO_STATUS "disabled" )
137+ if (LINK_TIME_OPTIMIZATION AND NOT LTO_UNSUPPORTED_OS)
138+ include (CheckIPOSupported)
139+ check_ipo_supported(RESULT lto_supported OUTPUT lto_error)
140+ if (lto_supported)
141+ # LTO is enabled globally for Release build
142+ set (CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE )
143+ set (CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO FALSE )
144+ set (CMAKE_INTERPROCEDURAL_OPTIMIZATION_DEBUG FALSE )
145+
146+ string (TOUPPER "${CMAKE_BUILD_TYPE} " UPPER_BUILD_TYPE)
147+ if (UPPER_BUILD_TYPE STREQUAL "RELEASE" )
148+ set (LTO_STATUS "enabled" )
149+ endif ()
150+ else ()
151+ set (LTO_STATUS "not supported: ${lto_error} " )
152+ endif ()
153+ endif ()
154+ message (STATUS "LTO/IPO is ${LTO_STATUS} " )
155+
111156# configure a header file to pass some of the CMake settings
112157configure_file (
113158 ${OPENROAD_HOME} /include /ord/Version .hh.cmake
0 commit comments