Skip to content

Commit 7db2983

Browse files
committedAug 30, 2022
cmake: Use same default optimization level as Makefiles.
Support overriding optimization level in COMMON_OPT, CCOMMON_OPT, and FCOMMON_OPT.
1 parent 0053452 commit 7db2983

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
 

‎CMakeLists.txt

+30
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,36 @@ if(MSVC AND MSVC_STATIC_CRT)
8787
endforeach()
8888
endif()
8989

90+
# Set default optimization level
91+
if (NOT MSVC)
92+
# Filter out cmake's default optimization level from release and debug flags.
93+
# They would override any optimization level set in COMMON_OPTs
94+
set(CompilerLangs
95+
CXX
96+
C
97+
Fortran
98+
ASM
99+
)
100+
foreach(CompilerLang ${CompilerLangs})
101+
string(REGEX REPLACE "-O[0-9]|-Ofast|-Og|-Os"
102+
"" CMAKE_${CompilerLang}_FLAGS_RELEASE
103+
"${CMAKE_${CompilerLang}_FLAGS_RELEASE}")
104+
string(REGEX REPLACE "-O[0-9]|-Ofast|-Og|-Os"
105+
"" CMAKE_${CompilerLang}_FLAGS_DEDUG
106+
"${CMAKE_${CompilerLang}_FLAGS_DEDUG}")
107+
endforeach()
108+
# Prepend default optimization level to COMMON_OPTs
109+
if (CMAKE_BUILD_TYPE EQUAL "DEBUG")
110+
set(COMMON_OPT "-Og ${COMMON_OPT}")
111+
set(CCOMMON_OPT "-Og ${CCOMMON_OPT}")
112+
set(FCOMMON_OPT "-Og ${FCOMMON_OPT}")
113+
else ()
114+
set(COMMON_OPT "-O2 ${COMMON_OPT}")
115+
set(CCOMMON_OPT "-O2 ${CCOMMON_OPT}")
116+
set(FCOMMON_OPT "-O2 ${FCOMMON_OPT}")
117+
endif ()
118+
endif ()
119+
90120
message(WARNING "CMake support is experimental. It does not yet support all build options and may not produce the same Makefiles that OpenBLAS ships with.")
91121

92122
include("${PROJECT_SOURCE_DIR}/cmake/utils.cmake")

0 commit comments

Comments
 (0)
Failed to load comments.