Skip to content

Commit a6e2183

Browse files
committed
CMake: Pass OpenMP compiler and linker flags through CMake targets
Using `OpenMP::OpenMP_LANG` targets for CMake is less error-prone than passing the compiler and linker flags manually. Furthermore, it allows the user to customize those flags by setting `OpenMP_LANG_FLAGS`, `OpenMP_LANG_LIB_NAMES`, and `OpenMP_omp_LIBRARY`.
1 parent 37b8547 commit a6e2183

File tree

11 files changed

+46
-33
lines changed

11 files changed

+46
-33
lines changed

CMakeLists.txt

+8-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ endif()
110110
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.")
111111

112112
if (USE_OPENMP)
113-
find_package(OpenMP REQUIRED)
113+
find_package(OpenMP COMPONENTS C REQUIRED)
114+
set(CCOMMON_OPT "${CCOMMON_OPT} -DUSE_OPENMP")
114115
endif ()
115116

116117
include("${PROJECT_SOURCE_DIR}/cmake/utils.cmake")
@@ -230,6 +231,12 @@ endif ()
230231
# add objects to the openblas lib
231232
if(NOT NO_LAPACK)
232233
add_library(LAPACK_OVERRIDES OBJECT ${LA_SOURCES})
234+
if (USE_OPENMP AND (NOT NOFORTRAN))
235+
# Disable OpenMP for LAPACK Fortran codes on Windows.
236+
if(NOT ${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
237+
target_link_libraries(LAPACK_OVERRIDES OpenMP::OpenMP_Fortran)
238+
endif()
239+
endif()
233240
list(APPEND TARGET_OBJS "$<TARGET_OBJECTS:LAPACK_OVERRIDES>")
234241
endif()
235242
if(NOT NO_LAPACKE)

cmake/arch.cmake

-11
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,6 @@ if (${CMAKE_C_COMPILER_ID} STREQUAL "Intel")
3131
set(CCOMMON_OPT "${CCOMMON_OPT} -wd981")
3232
endif ()
3333

34-
if (USE_OPENMP)
35-
# USE_SIMPLE_THREADED_LEVEL3 = 1
36-
# NO_AFFINITY = 1
37-
find_package(OpenMP REQUIRED)
38-
if (OpenMP_FOUND)
39-
set(CCOMMON_OPT "${CCOMMON_OPT} ${OpenMP_C_FLAGS} -DUSE_OPENMP")
40-
set(FCOMMON_OPT "${FCOMMON_OPT} ${OpenMP_Fortran_FLAGS}")
41-
endif()
42-
endif ()
43-
44-
4534
if (DYNAMIC_ARCH)
4635
if (ARM64)
4736
set(DYNAMIC_CORE ARMV8 CORTEXA53 CORTEXA57 THUNDERX THUNDERX2T99 TSV110 EMAG8180 NEOVERSEN1 THUNDERX3T110)

cmake/f_check.cmake

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ include(CheckLanguage)
2424
check_language(Fortran)
2525
if(CMAKE_Fortran_COMPILER)
2626
enable_language(Fortran)
27+
if (USE_OPENMP)
28+
find_package(OpenMP COMPONENTS Fortran REQUIRED)
29+
endif ()
2730
else()
2831
set (NOFORTRAN 1)
2932
if (NOT NO_LAPACK)

cmake/fc.cmake

+12-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ if (${F_COMPILER} STREQUAL "FLANG" AND NOT CMAKE_Fortran_COMPILER_ID STREQUAL "L
77
# This is for classic Flang. LLVM Flang is handled with gfortran below.
88
set(CCOMMON_OPT "${CCOMMON_OPT} -DF_INTERFACE_FLANG")
99
if (USE_OPENMP)
10-
set(FCOMMON_OPT "${FCOMMON_OPT} -fopenmp")
10+
set(OpenMP_Fortran_FLAGS "-fopenmp")
1111
endif ()
1212
set(FCOMMON_OPT "${FCOMMON_OPT} -Mrecursive -Kieee")
1313
endif ()
@@ -117,7 +117,7 @@ if (${F_COMPILER} STREQUAL "GFORTRAN" OR ${F_COMPILER} STREQUAL "F95" OR CMAKE_F
117117
endif ()
118118

119119
if (USE_OPENMP)
120-
set(FCOMMON_OPT "${FCOMMON_OPT} -fopenmp")
120+
set(OpenMP_Fortran_FLAGS "-fopenmp")
121121
endif ()
122122
endif ()
123123

@@ -128,14 +128,14 @@ if (${F_COMPILER} STREQUAL "INTEL" OR CMAKE_Fortran_COMPILER_ID MATCHES "Intel")
128128
endif ()
129129
set(FCOMMON_OPT "${FCOMMON_OPT} -recursive -fp-model=consistent")
130130
if (USE_OPENMP)
131-
set(FCOMMON_OPT "${FCOMMON_OPT} -openmp")
131+
set(OpenMP_Fortran_FLAGS "-openmp")
132132
endif ()
133133
endif ()
134134

135135
if (${F_COMPILER} STREQUAL "FUJITSU")
136136
set(CCOMMON_OPT "${CCOMMON_OPT} -DF_INTERFACE_FUJITSU")
137137
if (USE_OPENMP)
138-
set(FCOMMON_OPT "${FCOMMON_OPT} -openmp")
138+
set(OpenMP_Fortran_FLAGS "-openmp")
139139
endif ()
140140
endif ()
141141

@@ -151,7 +151,7 @@ if (${F_COMPILER} STREQUAL "IBM")
151151
set(FCOMMON_OPT "${FCOMMON_OPT} -q32")
152152
endif ()
153153
if (USE_OPENMP)
154-
set(FCOMMON_OPT "${FCOMMON_OPT} -openmp")
154+
set(OpenMP_Fortran_FLAGS "-openmp")
155155
endif ()
156156
endif ()
157157

@@ -168,7 +168,7 @@ if (${F_COMPILER} STREQUAL "PGI" OR ${F_COMPILER} STREQUAL "PGF95")
168168
endif ()
169169
set(FCOMMON_OPT "${FCOMMON_OPT} -Mrecursive")
170170
if (USE_OPENMP)
171-
set(FCOMMON_OPT "${FCOMMON_OPT} -mp")
171+
set(OpenMP_Fortran_FLAGS "-mp")
172172
endif ()
173173
endif ()
174174

@@ -195,7 +195,7 @@ if (${F_COMPILER} STREQUAL "PATHSCALE")
195195
endif ()
196196

197197
if (USE_OPENMP)
198-
set(FCOMMON_OPT "${FCOMMON_OPT} -mp")
198+
set(OpenMP_Fortran_FLAGS "-mp")
199199
endif ()
200200
endif ()
201201

@@ -233,7 +233,7 @@ if (${F_COMPILER} STREQUAL "OPEN64")
233233

234234
if (USE_OPENMP)
235235
set(FEXTRALIB "${FEXTRALIB} -lstdc++")
236-
set(FCOMMON_OPT "${FCOMMON_OPT} -mp")
236+
set(OpenMP_Fortran_FLAGS "-mp")
237237
endif ()
238238
endif ()
239239

@@ -245,14 +245,14 @@ if (${F_COMPILER} STREQUAL "SUN")
245245
set(FCOMMON_OPT "${FCOMMON_OPT} -m64")
246246
endif ()
247247
if (USE_OPENMP)
248-
set(FCOMMON_OPT "${FCOMMON_OPT} -xopenmp=parallel")
248+
set(OpenMP_Fortran_FLAGS "-xopenmp=parallel")
249249
endif ()
250250
endif ()
251251

252252
if (${F_COMPILER} STREQUAL "COMPAQ")
253253
set(CCOMMON_OPT "${CCOMMON_OPT} -DF_INTERFACE_COMPAQ")
254254
if (USE_OPENMP)
255-
set(FCOMMON_OPT "${FCOMMON_OPT} -openmp")
255+
set(OpenMP_Fortran_FLAGS "-openmp")
256256
endif ()
257257
endif ()
258258

@@ -265,7 +265,7 @@ if (${F_COMPILER} STREQUAL "CRAY")
265265
if (NOT USE_OPENMP)
266266
set(FCOMMON_OPT "${FCOMMON_OPT} -fno-openmp")
267267
else ()
268-
set(FCOMMON_OPT "${FCOMMON_OPT} -fopenmp")
268+
set(OpenMP_Fortran_FLAGS "-fopenmp")
269269
endif ()
270270
endif ()
271271

@@ -290,7 +290,7 @@ if (${F_COMPILER} STREQUAL "NAGFOR")
290290
# -w=unused: Suppress warning messages about unused variables
291291
set(FCOMMON_OPT "${FCOMMON_OPT} -w=x77 -w=ques -w=unused")
292292
if (USE_OPENMP)
293-
set(FCOMMON_OPT "${FCOMMON_OPT} -openmp")
293+
set(OpenMP_Fortran_FLAGS "-openmp")
294294
endif ()
295295
endif ()
296296

cmake/system.cmake

-9
Original file line numberDiff line numberDiff line change
@@ -655,15 +655,6 @@ if (LAPACK_STRLEN)
655655
endif()
656656
set(LAPACK_FPFLAGS "${LAPACK_FPFLAGS} ${FPFLAGS}")
657657

658-
#Disable -fopenmp for LAPACK Fortran codes on Windows.
659-
if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
660-
set(FILTER_FLAGS "-fopenmp;-mp;-openmp;-xopenmp=parallel")
661-
foreach (FILTER_FLAG ${FILTER_FLAGS})
662-
string(REPLACE ${FILTER_FLAG} "" LAPACK_FFLAGS ${LAPACK_FFLAGS})
663-
string(REPLACE ${FILTER_FLAG} "" LAPACK_FPFLAGS ${LAPACK_FPFLAGS})
664-
endforeach ()
665-
endif ()
666-
667658
if (CMAKE_Fortran_COMPILER)
668659
if ("${F_COMPILER}" STREQUAL "NAGFOR" OR "${F_COMPILER}" STREQUAL "CRAY" OR CMAKE_Fortran_COMPILER_ID MATCHES "LLVMFlang.*")
669660
set(FILTER_FLAGS "-msse3;-mssse3;-msse4.1;-mavx;-mavx2,-mskylake-avx512")

driver/level2/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,7 @@ if (USE_THREAD)
223223
endif ()
224224

225225
add_library(driver_level2 OBJECT ${OPENBLAS_SRC})
226+
227+
if (USE_OPENMP)
228+
target_link_libraries(driver_level2 OpenMP::OpenMP_C)
229+
endif()

driver/level3/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,7 @@ endforeach ()
171171
#
172172

173173
add_library(driver_level3 OBJECT ${OPENBLAS_SRC})
174+
175+
if (USE_OPENMP)
176+
target_link_libraries(driver_level3 OpenMP::OpenMP_C)
177+
endif()

driver/others/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,7 @@ endif ()
8888
#endif
8989

9090
add_library(driver_others OBJECT ${OPENBLAS_SRC} ${MEMORY} ${SMP_SOURCES} ${COMMON_SOURCES})
91+
92+
if (USE_OPENMP)
93+
target_link_libraries(driver_others OpenMP::OpenMP_C)
94+
endif()

interface/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,7 @@ if ( BUILD_COMPLEX16 AND NOT BUILD_DOUBLE)
250250
endif ()
251251

252252
add_library(interface OBJECT ${OPENBLAS_SRC})
253+
254+
if (USE_OPENMP)
255+
target_link_libraries(interface OpenMP::OpenMP_C)
256+
endif()

kernel/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,9 @@ endif ()
13641364
if (USE_GEMM3M)
13651365
target_compile_definitions(kernel${TSUFFIX} PRIVATE USE_GEMM3M)
13661366
endif()
1367+
if (USE_OPENMP)
1368+
target_link_libraries(kernel${TSUFFIX} OpenMP::OpenMP_C)
1369+
endif()
13671370
endfunction ()
13681371

13691372

lapack/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,7 @@ GenerateCombinationObjects("${UNIT_SOURCES}" "UNIT" "N" "" 4)
117117
GenerateCombinationObjects("${UNIT_SOURCES2}" "UNIT" "N" "" 0 "" "" 3)
118118

119119
add_library(lapack OBJECT ${OPENBLAS_SRC})
120+
121+
if (USE_OPENMP)
122+
target_link_libraries(lapack OpenMP::OpenMP_C)
123+
endif()

0 commit comments

Comments
 (0)