Skip to content

Commit d43ae5c

Browse files
committed
abstract the configuration of the targets
1 parent 94a49a6 commit d43ae5c

File tree

1 file changed

+64
-155
lines changed

1 file changed

+64
-155
lines changed

src/CMakeLists.txt

Lines changed: 64 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,56 @@
1-
#### Pre-process: .fpp -> .f90 via Fypp
1+
# Helper function to configure stdlib targets
2+
function(configure_stdlib_target target_name regular_sources_var fypp_files_var cpp_files_var)
3+
#### Pre-process: .fpp -> .f90 via Fypp
4+
fypp_f90("${fyppFlags}" "${${fypp_files_var}}" ${target_name}_fypp_outFiles)
5+
#### Pre-process: .fypp -> .F90 via Fypp (for C preprocessor directives)
6+
fypp_f90pp("${fyppFlags}" "${${cpp_files_var}}" ${target_name}_cpp_outFiles)
7+
8+
list(APPEND all_sources ${${target_name}_fypp_outFiles})
9+
list(APPEND all_sources ${${target_name}_cpp_outFiles})
10+
list(APPEND all_sources ${${regular_sources_var}})
11+
12+
add_library(${target_name} ${all_sources})
13+
14+
set_target_properties(
15+
${target_name}
16+
PROPERTIES
17+
POSITION_INDEPENDENT_CODE ON
18+
WINDOWS_EXPORT_ALL_SYMBOLS ON
19+
)
20+
21+
if(CMAKE_Fortran_COMPILER_ID STREQUAL GNU AND CMAKE_Fortran_COMPILER_VERSION VERSION_LESS 10.0)
22+
target_compile_options(
23+
${target_name}
24+
PRIVATE
25+
$<$<COMPILE_LANGUAGE:Fortran>:-fno-range-check>
26+
)
27+
endif()
28+
29+
set(LIB_MOD_DIR ${CMAKE_CURRENT_BINARY_DIR}/mod_files/${target_name}/)
30+
set(INSTALL_MOD_DIR "${CMAKE_INSTALL_MODULEDIR}/${target_name}")
31+
# We need the module directory before we finish the configure stage since the
32+
# build interface might resolve before the module directory is generated by CMake
33+
if(NOT EXISTS "${LIB_MOD_DIR}")
34+
file(MAKE_DIRECTORY "${LIB_MOD_DIR}")
35+
endif()
36+
37+
set_target_properties(${target_name} PROPERTIES
38+
Fortran_MODULE_DIRECTORY ${LIB_MOD_DIR}
39+
)
40+
target_include_directories(${target_name} PUBLIC
41+
$<BUILD_INTERFACE:${LIB_MOD_DIR}>
42+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_MODULEDIR}>
43+
)
44+
45+
install(TARGETS ${target_name}
46+
EXPORT ${PROJECT_NAME}-targets
47+
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
48+
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
49+
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
50+
)
51+
install(DIRECTORY ${LIB_MOD_DIR} DESTINATION "${INSTALL_MOD_DIR}")
52+
endfunction()
253

3-
# Create a list of the files to be preprocessed
454
set(blas_fppFiles
555
stdlib_kinds.fypp
656
blas/stdlib_blas_constants.fypp
@@ -18,57 +68,12 @@ set(blas_fppFiles
1868
)
1969
set(blas_cppFiles stdlib_linalg_constants.fypp stdlib_linalg_blas.fypp)
2070

21-
fypp_f90("${fyppFlags}" "${blas_fppFiles}" blas_outFiles)
22-
fypp_f90pp("${fyppFlags}" "${blas_cppFiles}" blas_outPreprocFiles)
23-
24-
add_library(stdlib_blas ${blas_outFiles} ${blas_outPreprocFiles})
71+
configure_stdlib_target(stdlib_blas "" blas_fppFiles blas_cppFiles)
2572

2673
if(BLAS_FOUND)
2774
target_link_libraries(stdlib_blas PUBLIC "BLAS::BLAS")
2875
endif()
2976

30-
set_target_properties(
31-
stdlib_blas
32-
PROPERTIES POSITION_INDEPENDENT_CODE ON WINDOWS_EXPORT_ALL_SYMBOLS ON
33-
)
34-
35-
if(
36-
CMAKE_Fortran_COMPILER_ID STREQUAL GNU
37-
AND CMAKE_Fortran_COMPILER_VERSION VERSION_LESS 10.0
38-
)
39-
target_compile_options(
40-
stdlib_blas
41-
PRIVATE $<$<COMPILE_LANGUAGE:Fortran>:-fno-range-check>
42-
)
43-
endif()
44-
45-
set(LIB_MOD_DIR ${CMAKE_CURRENT_BINARY_DIR}/mod_files/blas/)
46-
# We need the module directory before we finish the configure stage since the
47-
# build interface might resolve before the module directory is generated by CMake
48-
if(NOT EXISTS "${LIB_MOD_DIR}")
49-
file(MAKE_DIRECTORY "${LIB_MOD_DIR}")
50-
endif()
51-
52-
set_target_properties(
53-
stdlib_blas
54-
PROPERTIES Fortran_MODULE_DIRECTORY ${LIB_MOD_DIR}
55-
)
56-
target_include_directories(
57-
stdlib_blas
58-
PUBLIC
59-
$<BUILD_INTERFACE:${LIB_MOD_DIR}>
60-
$<INSTALL_INTERFACE:${CMAKE_INSTALL_MODULEDIR}>
61-
)
62-
63-
install(
64-
TARGETS stdlib_blas
65-
EXPORT ${PROJECT_NAME}-targets
66-
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
67-
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
68-
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
69-
)
70-
install(DIRECTORY ${LIB_MOD_DIR} DESTINATION "${CMAKE_INSTALL_MODULEDIR}/blas")
71-
7277
set(lapack_fppFiles
7378
stdlib_kinds.fypp
7479
stdlib_linalg_state.fypp
@@ -131,67 +136,21 @@ set(lapack_fppFiles
131136
lapack/stdlib_lapack_svd_comp.fypp
132137
lapack/stdlib_lapack_svd_comp2.fypp
133138
)
134-
set(lapack_cppFiles stdlib_linalg_constants.fypp stdlib_linalg_lapack.fypp)
135-
136-
fypp_f90("${fyppFlags}" "${lapack_fppFiles}" lapack_outFiles)
137-
fypp_f90pp("${fyppFlags}" "${lapack_cppFiles}" lapack_outPreprocFiles)
139+
set(lapack_cppFiles
140+
stdlib_linalg_constants.fypp
141+
stdlib_linalg_lapack.fypp
142+
)
143+
set(lapack_f90Files
144+
$<IF:$<BOOL:${f18errorstop}>,f18estop.f90,f08estop.f90>
145+
)
138146

139-
add_library(stdlib_lapack ${lapack_outFiles} ${lapack_outPreprocFiles})
147+
configure_stdlib_target(stdlib_lapack lapack_f90Files lapack_fppFiles lapack_cppFiles)
140148

141149
if(LAPACK_FOUND)
142150
target_link_libraries(stdlib_lapack PUBLIC "LAPACK::LAPACK")
143151
endif()
144152
target_link_libraries(stdlib_lapack PUBLIC stdlib_blas)
145153

146-
set_target_properties(
147-
stdlib_lapack
148-
PROPERTIES POSITION_INDEPENDENT_CODE ON WINDOWS_EXPORT_ALL_SYMBOLS ON
149-
)
150-
151-
if(
152-
CMAKE_Fortran_COMPILER_ID STREQUAL GNU
153-
AND CMAKE_Fortran_COMPILER_VERSION VERSION_LESS 10.0
154-
)
155-
target_compile_options(
156-
stdlib_lapack
157-
PRIVATE $<$<COMPILE_LANGUAGE:Fortran>:-fno-range-check>
158-
)
159-
endif()
160-
161-
set(LIB_MOD_DIR ${CMAKE_CURRENT_BINARY_DIR}/mod_files/lapack/)
162-
# We need the module directory before we finish the configure stage since the
163-
# build interface might resolve before the module directory is generated by CMake
164-
if(NOT EXISTS "${LIB_MOD_DIR}")
165-
file(MAKE_DIRECTORY "${LIB_MOD_DIR}")
166-
endif()
167-
168-
set_target_properties(
169-
stdlib_lapack
170-
PROPERTIES Fortran_MODULE_DIRECTORY ${LIB_MOD_DIR}
171-
)
172-
173-
if(f18errorstop)
174-
target_sources(stdlib_lapack PRIVATE f18estop.f90)
175-
else()
176-
target_sources(stdlib_lapack PRIVATE f08estop.f90)
177-
endif()
178-
179-
target_include_directories(
180-
stdlib_lapack
181-
PUBLIC
182-
$<BUILD_INTERFACE:${LIB_MOD_DIR}>
183-
$<INSTALL_INTERFACE:${CMAKE_INSTALL_MODULEDIR}>
184-
)
185-
186-
install(
187-
TARGETS stdlib_lapack
188-
EXPORT ${PROJECT_NAME}-targets
189-
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
190-
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
191-
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
192-
)
193-
install(DIRECTORY ${LIB_MOD_DIR} DESTINATION "${CMAKE_INSTALL_MODULEDIR}")
194-
195154
set(fppFiles
196155
stdlib_ascii.fypp
197156
stdlib_bitsets.fypp
@@ -280,14 +239,8 @@ set(fppFiles
280239
stdlib_strings.fypp
281240
stdlib_version.fypp
282241
)
283-
284-
# Preprocessed files to contain preprocessor directives -> .F90
285242
set(cppFiles stdlib_linalg_constants.fypp)
286-
287-
fypp_f90("${fyppFlags}" "${fppFiles}" outFiles)
288-
fypp_f90pp("${fyppFlags}" "${cppFiles}" outPreprocFiles)
289-
290-
set(SRC
243+
set(f90Files
291244
stdlib_ansi.f90
292245
stdlib_ansi_operator.f90
293246
stdlib_ansi_to_string.f90
@@ -308,53 +261,9 @@ set(SRC
308261
stdlib_specialfunctions_legendre.f90
309262
stdlib_quadrature_gauss.f90
310263
stdlib_stringlist_type.f90
311-
${outFiles}
312-
${outPreprocFiles}
264+
$<IF:$<BOOL:${f18errorstop}>,f18estop.f90,f08estop.f90>
313265
)
314266

315-
add_library(${PROJECT_NAME} ${SRC})
267+
configure_stdlib_target(${PROJECT_NAME} f90Files fppFiles cppFiles)
316268

317269
target_link_libraries(${PROJECT_NAME} PUBLIC stdlib_blas stdlib_lapack)
318-
319-
set_target_properties(
320-
${PROJECT_NAME}
321-
PROPERTIES
322-
POSITION_INDEPENDENT_CODE ON
323-
WINDOWS_EXPORT_ALL_SYMBOLS ON
324-
)
325-
326-
if(CMAKE_Fortran_COMPILER_ID STREQUAL GNU AND CMAKE_Fortran_COMPILER_VERSION VERSION_LESS 10.0)
327-
target_compile_options(
328-
${PROJECT_NAME}
329-
PRIVATE
330-
$<$<COMPILE_LANGUAGE:Fortran>:-fno-range-check>
331-
)
332-
endif()
333-
334-
set(LIB_MOD_DIR ${CMAKE_CURRENT_BINARY_DIR}/mod_files/)
335-
# We need the module directory before we finish the configure stage since the
336-
# build interface might resolve before the module directory is generated by CMake
337-
if(NOT EXISTS "${LIB_MOD_DIR}")
338-
file(MAKE_DIRECTORY "${LIB_MOD_DIR}")
339-
endif()
340-
341-
set_target_properties(${PROJECT_NAME} PROPERTIES
342-
Fortran_MODULE_DIRECTORY ${LIB_MOD_DIR})
343-
target_include_directories(${PROJECT_NAME} PUBLIC
344-
$<BUILD_INTERFACE:${LIB_MOD_DIR}>
345-
$<INSTALL_INTERFACE:${CMAKE_INSTALL_MODULEDIR}>
346-
)
347-
348-
if(f18errorstop)
349-
target_sources(${PROJECT_NAME} PRIVATE f18estop.f90)
350-
else()
351-
target_sources(${PROJECT_NAME} PRIVATE f08estop.f90)
352-
endif()
353-
354-
install(TARGETS ${PROJECT_NAME}
355-
EXPORT ${PROJECT_NAME}-targets
356-
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
357-
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
358-
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
359-
)
360-
install(DIRECTORY ${LIB_MOD_DIR} DESTINATION "${CMAKE_INSTALL_MODULEDIR}")

0 commit comments

Comments
 (0)