-
Notifications
You must be signed in to change notification settings - Fork 24
/
CMakeLists.txt
276 lines (216 loc) · 11.1 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
cmake_minimum_required(VERSION 3.13)
####################################################################################################
# General settings
####################################################################################################
project(ChimesCalc
VERSION 1.0
DESCRIPTION "ChIMES calculator: Utilities to compute ChIMES interactions"
LANGUAGES CXX C)
string(TOLOWER "${PROJECT_NAME}" PROJECT_NAME_LOWER)
include(GNUInstallDirs)
set(INSTALL_INCLUDEDIR "${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME_LOWER}")
set(INSTALL_MODULEDIR "${INSTALL_INCLUDEDIR}/modfiles")
include(cmake/ChimesCalc.cmake)
include(config.cmake)
chimescalc_setup_build_type()
chimescalc_setup_compiler_flags()
# Check whether libm is needed to provide the round() function in C
include(CheckLibraryExists)
CHECK_LIBRARY_EXISTS("m" "round" "" NEEDS_LIB_M)
if(NEEDS_LIB_M)
set(LIB_M "m")
endif()
####################################################################################################
# Static/shared library
####################################################################################################
set(c-sources
"chimesFF/src/chimesFF.cpp"
"chimesFF/api/chimescalc_C.cpp"
"serial_interface/src/serial_chimes_interface.cpp"
"serial_interface/api/chimescalc_serial_C.cpp")
set(c-includes
"chimesFF/src/chimesFF.h"
"chimesFF/api/chimescalc_C.h"
"serial_interface/src/serial_chimes_interface.h"
"serial_interface/api/chimescalc_serial_C.h")
add_library(ChimesCalc "${c-sources}")
set_target_properties(ChimesCalc
PROPERTIES
OUTPUT_NAME "chimescalc"
PUBLIC_HEADER "${c-includes}")
target_include_directories(ChimesCalc PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/chimesFF/src>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/chimesFF/api>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/serial_interface/src>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/serial_interface/api>
$<INSTALL_INTERFACE:${INSTALL_INCLUDEDIR}>)
target_compile_definitions(ChimesCalc PRIVATE "DEBUG=${DEBUG}")
target_compile_features(ChimesCalc PRIVATE cxx_std_11)
install(TARGETS ChimesCalc
EXPORT ${PROJECT_NAME_LOWER}-targets
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
PUBLIC_HEADER DESTINATION "${INSTALL_INCLUDEDIR}")
####################################################################################################
# Executables for C/C++
####################################################################################################
# Main executable installed as chimescalc
add_executable(ChimesCalcExe serial_interface/examples/cpp/main.cpp)
set_target_properties(ChimesCalcExe PROPERTIES OUTPUT_NAME "chimescalc")
target_link_libraries(ChimesCalcExe ChimesCalc)
target_compile_features (ChimesCalcExe PUBLIC cxx_std_11)
target_compile_definitions(ChimesCalcExe PRIVATE "DEBUG=${DEBUG}")
install(TARGETS ChimesCalcExe DESTINATION ${CMAKE_INSTALL_BINDIR})
# Test executables
add_executable(chimescalc-test_direct-C chimesFF/examples/c/main.c)
target_link_libraries (chimescalc-test_direct-C ChimesCalc ${LIB_M})
target_compile_features (chimescalc-test_direct-C PRIVATE cxx_std_11)
target_compile_definitions(chimescalc-test_direct-C PRIVATE "DEBUG=${DEBUG}")
add_executable(chimescalc-test_serial-C serial_interface/examples/c/main.c)
target_link_libraries (chimescalc-test_serial-C ChimesCalc)
target_compile_features (chimescalc-test_serial-C PRIVATE cxx_std_11)
target_compile_definitions(chimescalc-test_serial-C PRIVATE "DEBUG=${DEBUG}")
add_executable(chimescalc-test_serial-C_instance serial_interface/examples/c_instance/main.c)
target_link_libraries (chimescalc-test_serial-C_instance ChimesCalc)
target_compile_features (chimescalc-test_serial-C_instance PRIVATE cxx_std_11)
target_compile_definitions(chimescalc-test_serial-C_instance PRIVATE "DEBUG=${DEBUG}")
####################################################################################################
# Dynamically loadable library (e.g for Python)
####################################################################################################
add_library(ChimesCalc_dynamic MODULE "${c-sources}")
set_target_properties(ChimesCalc_dynamic
PROPERTIES OUTPUT_NAME "chimescalc_dl")
target_include_directories(ChimesCalc_dynamic PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/chimesFF/src>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/chimesFF/api>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/serial_interface/src>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/serial_interface/api>
$<INSTALL_INTERFACE:${INSTALL_INCLUDEDIR}>)
target_compile_definitions(ChimesCalc_dynamic PRIVATE "DEBUG=${DEBUG}")
target_compile_features(ChimesCalc_dynamic PRIVATE cxx_std_11)
install(TARGETS ChimesCalc_dynamic
EXPORT ${PROJECT_NAME_LOWER}-targets
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
PUBLIC_HEADER DESTINATION "${INSTALL_INCLUDEDIR}")
####################################################################################################
# Fortran wrappers (not contained in base library as it requires Fortran runtime library)
####################################################################################################
if(WITH_FORTRAN_API OR WITH_FORTRAN08_API)
set(f90safe-fortran-sources
"chimesFF/api/chimescalc_F.f90"
"serial_interface/api/chimescalc_serial_F.f90")
set(fortran-sources
${f90safe-fortran-sources})
# Optional, as ancient Fortran compilers may have difficulties with it
if(WITH_FORTRAN08_API)
list(APPEND fortran-sources
"serial_interface/api/chimescalc_serial_F08.f90")
endif()
add_library(ChimesCalc_Fortran "${fortran-sources}")
set_target_properties(ChimesCalc_Fortran PROPERTIES OUTPUT_NAME "chimescalc_fortran")
target_link_libraries(ChimesCalc_Fortran PUBLIC ChimesCalc)
set(moddir "${CMAKE_CURRENT_BINARY_DIR}/modfiles")
set_target_properties(ChimesCalc_Fortran PROPERTIES
Fortran_MODULE_DIRECTORY "${moddir}")
target_include_directories(ChimesCalc_Fortran PUBLIC
$<BUILD_INTERFACE:${moddir}>
$<INSTALL_INTERFACE:${INSTALL_MODULEDIR}>)
install(TARGETS ChimesCalc_Fortran
EXPORT ${PROJECT_NAME_LOWER}-targets
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")
install(DIRECTORY "${moddir}/" DESTINATION "${INSTALL_MODULEDIR}")
################################################################################################
# Executables for Fortran (90/08)
################################################################################################
add_executable(chimescalc-test_serial-F serial_interface/examples/fortran/main.F90)
target_link_libraries(chimescalc-test_serial-F ChimesCalc_Fortran)
target_compile_definitions(chimescalc-test_serial-F PRIVATE "DEBUG=${DEBUG}")
add_executable(chimescalc-test_serial-F_instance serial_interface/examples/fortran_instance/main.F90)
target_link_libraries(chimescalc-test_serial-F_instance ChimesCalc_Fortran)
target_compile_definitions(chimescalc-test_serial-F_instance PRIVATE "DEBUG=${DEBUG}")
# Optional, as ancient Fortran compilers may have difficulties with it
if(WITH_FORTRAN08_API)
add_executable(chimescalc-test_serial-F08 serial_interface/examples/fortran08/main.F90)
target_link_libraries(chimescalc-test_serial-F08 ChimesCalc_Fortran)
target_compile_definitions(chimescalc-test_serial-F08 PRIVATE "DEBUG=${DEBUG}")
endif()
endif()
####################################################################################################
# Create CMake export file
####################################################################################################
include(CMakePackageConfigHelpers)
install(
EXPORT ${PROJECT_NAME_LOWER}-targets
FILE "${PROJECT_NAME_LOWER}-targets.cmake"
NAMESPACE "${PROJECT_NAME}::"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME_LOWER}")
configure_package_config_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/export/${PROJECT_NAME_LOWER}-config.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/cmake/${PROJECT_NAME_LOWER}-config.cmake"
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME_LOWER}")
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/cmake/${PROJECT_NAME_LOWER}-config-version.cmake"
VERSION "${PROJECT_VERSION}"
COMPATIBILITY SameMajorVersion)
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/cmake/${PROJECT_NAME_LOWER}-config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/cmake/${PROJECT_NAME_LOWER}-config-version.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME_LOWER}")
####################################################################################################
# Add regression tests for the various APIs
####################################################################################################
enable_testing()
set(apis
"cpp;${CMAKE_CURRENT_BINARY_DIR}/chimescalc"
"c;${CMAKE_CURRENT_BINARY_DIR}/chimescalc-test_serial-C"
"c_instance;${CMAKE_CURRENT_BINARY_DIR}/chimescalc-test_serial-C_instance"
"fortran;${CMAKE_CURRENT_BINARY_DIR}/chimescalc-test_serial-F"
"fortran_instance;${CMAKE_CURRENT_BINARY_DIR}/chimescalc-test_serial-F_instance"
"fortran08;${CMAKE_CURRENT_BINARY_DIR}/chimescalc-test_serial-F08")
set(_testdir "${CMAKE_CURRENT_SOURCE_DIR}/serial_interface/tests")
file(STRINGS ${_testdir}/test_list.dat _testcases)
foreach(_testcase IN LISTS _testcases)
if("${_testcase}" STREQUAL "")
continue()
endif()
list(GET _testcase 0 _paramfile)
string(STRIP "${_paramfile}" _paramfile)
list(GET _testcase 1 _geometry)
string(STRIP "${_geometry}" _geometry)
string(REPLACE "#" "" _geometry_escaped "${_geometry}")
list(GET _testcase 2 _configopt)
string(STRIP "${_configopt}" _configopt)
list(GET _testcase 3 _testlabels)
string(STRIP "${_testlabels}" _testlabels)
set(_add_test False)
foreach(_label IN LISTS TEST_LABELS)
if("${_testlabels}" MATCHES "${_label}")
set(_add_test True)
break()
endif()
endforeach()
if(NOT _add_test)
continue()
endif()
set(_apis "${apis}")
list(LENGTH _apis _apis_length )
while(_apis_length GREATER 0)
list(POP_FRONT _apis _api_abbrev)
list(POP_FRONT _apis _api_executable)
#message(STATUS "Test added: ${_api_abbrev}:${_paramfile}:${_geometry_escaped}")
list(LENGTH _apis _apis_length)
add_test(
NAME "${_api_abbrev}/${_paramfile}:${_geometry_escaped}"
COMMAND
${_testdir}/run_single_test.sh
${_api_executable}
${_paramfile}
${_geometry}
${_configopt}
${CMAKE_CURRENT_BINARY_DIR}/_test/${_api_abbrev}/${_paramfile}:${_geometry_escaped})
if("${_api_abbrev}" STREQUAL "py")
set_tests_properties("${_api_abbrev}/${_paramfile}:${_geometry_escaped}"
PROPERTIES ENVIRONMENT "PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR}"
ENVIRONMENT "LD_LIBRARY_PATH=${CMAKE_CURRENT_BINARY_DIR}")
endif()
endwhile()
endforeach()