Skip to content

Commit d528a26

Browse files
committedSep 1, 2022
cmake: Use same default optimization level as Makefiles.
The optimization level might be overridden by default settings of cmake.
1 parent 0053452 commit d528a26

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
 

‎CMakeLists.txt

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

90+
# Set default optimization level
91+
if (NOT MSVC)
92+
# Check if user specified optimization level in COMMON_OPT flags.
93+
string(REGEX MATCH "-O[0-9]|-Ofast|-Og|-Os" OPENBLAS_COMMON_OPT_OFLAGS
94+
"${COMMON_OPT} ${CCOMMON_OPT} ${FCOMMON_OPT}")
95+
96+
if (NOT "${OPENBLAS_COMMON_OPT_OFLAGS}" STREQUAL "")
97+
# Filter out optimization level from cmake's default flags.
98+
# They would override the optimization level set in COMMON_OPTs.
99+
string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE)
100+
set(CompilerLangs
101+
C
102+
CXX
103+
Fortran
104+
ASM
105+
)
106+
foreach(CompilerLang ${CompilerLangs})
107+
string(REGEX REPLACE "-O[0-9]|-Ofast|-Og|-Os" ""
108+
CMAKE_${CompilerLang}_FLAGS_${CMAKE_BUILD_TYPE}
109+
"${CMAKE_${CompilerLang}_FLAGS_${CMAKE_BUILD_TYPE}}")
110+
endforeach()
111+
else()
112+
# Prepend default optimization level to COMMON_OPTs.
113+
# These flags might be overridden by cmake's default flags.
114+
if (CMAKE_BUILD_TYPE EQUAL "DEBUG")
115+
set(COMMON_OPT "-Og ${COMMON_OPT}")
116+
set(CCOMMON_OPT "-Og ${CCOMMON_OPT}")
117+
set(FCOMMON_OPT "-Og ${FCOMMON_OPT}")
118+
else ()
119+
set(COMMON_OPT "-O2 ${COMMON_OPT}")
120+
set(CCOMMON_OPT "-O2 ${CCOMMON_OPT}")
121+
set(FCOMMON_OPT "-O2 ${FCOMMON_OPT}")
122+
endif ()
123+
endif ()
124+
endif ()
125+
90126
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.")
91127

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

0 commit comments

Comments
 (0)
Failed to load comments.