aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLasse Collin <lasse.collin@tukaani.org>2024-02-17 21:27:48 +0200
committerLasse Collin <lasse.collin@tukaani.org>2024-02-19 16:28:49 +0200
commit4430e075f7ccfc47972d6ca0aa1c3779fc265e10 (patch)
tree0e9be5301abd86522a272f7f6a2ccec1f21320d9
parentCMake: Handle symbol versioning on MicroBlaze specially. (diff)
downloadxz-4430e075f7ccfc47972d6ca0aa1c3779fc265e10.tar.xz
CMake: Use -O2 instead of -O3 in CMAKE_BUILD_TYPE=Release.
-O3 doesn't seem useful for speed but it makes the code bigger. CMake makes is difficult for users to simply override the optimization level: CFLAGS / CMAKE_C_FLAGS aren't helpful because they go before CMAKE_C_FLAGS_RELEASE. Of course, users can override CMAKE_C_FLAGS_RELEASE directly but then they have to remember to add also -DNDEBUG to disable assertions. This commit changes -O3 to -O2 in CMAKE_C_FLAGS_RELEASE if and only if CMAKE_C_FLAGS_RELEASE cache variable doesn't already exist. So if a custom value is passed on the command line (or reconfiguring an already-configured build), the cache variable won't be modified.
-rw-r--r--CMakeLists.txt19
1 files changed, 19 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 19ae4814..9716f350 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -74,9 +74,28 @@ string(REGEX REPLACE
.*$"
"\\1.\\2.\\3" PACKAGE_VERSION "${PACKAGE_VERSION}")
+# With several compilers, CMAKE_BUILD_TYPE=Release uses -O3 optimization
+# which results in bigger code without a clear difference in speed. If
+# no user-defined CMAKE_C_FLAGS_RELEASE is present, override -O3 to -O2
+# to make it possible to recommend CMAKE_BUILD_TYPE=Release.
+if(NOT DEFINED CMAKE_C_FLAGS_RELEASE)
+ set(OVERRIDE_O3_IN_C_FLAGS_RELEASE ON)
+endif()
+
# Among other things, this gives us variables xz_VERSION and xz_VERSION_MAJOR.
project(xz VERSION "${PACKAGE_VERSION}" LANGUAGES C)
+if(OVERRIDE_O3_IN_C_FLAGS_RELEASE)
+ # Looking at CMake's source, there aren't any _FLAGS_RELEASE_INIT
+ # entries where "-O3" would appear as part of some other option,
+ # thus a simple search and replace should be fine.
+ string(REPLACE -O3 -O2 CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
+
+ # Update the cache value while keeping its docstring unchanged.
+ set_property(CACHE CMAKE_C_FLAGS_RELEASE
+ PROPERTY VALUE "${CMAKE_C_FLAGS_RELEASE}")
+endif()
+
# We need a compiler that supports enough C99 or newer (variable-length arrays
# aren't needed, those are optional in C17). Setting CMAKE_C_STANDARD here
# makes it the default for all targets. It doesn't affect the INTERFACE so