forked from crest-lab/crest
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
364 lines (317 loc) · 10.7 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# This file is part of crest.
# SPDX-Identifier: LGPL-3.0-or-later
#
# crest is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# crest is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with crest. If not, see <https://www.gnu.org/licenses/>.
cmake_minimum_required(VERSION 3.17)
option(WITH_OBJECT "To build using object library" TRUE)
option(INSTALL_MODULES "Install Fortran module files to include directory." FALSE)
# Buggy CMake versions
if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.27.0 AND CMAKE_VERSION VERSION_LESS 3.28.0)
set(WITH_OBJECT FALSE)
endif()
# Setup the crest Project
project(
crest
LANGUAGES "C" "Fortran"
VERSION 3.0.2
DESCRIPTION "A tool for the exploration of low-energy chemical space"
)
# Follow GNU conventions for installing directories
include(GNUInstallDirs)
# Include further configurations
add_subdirectory("config")
###############################################################################
####################### SUBPROJECTS & DEPENDENCIES ############################
###############################################################################
# Check a specific CMake targets and execute the
# corresponding Find scripts (located in config/modules/)
# LAPACK, BLAS, OpenMP (usually via shared dependencies)
if(STATICBUILD)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
set(CMAKE_EXE_LINKER_FLAGS "-static")
set(CMAKE_LINK_SEARCH_START_STATIC TRUE)
set(CMAKE_LINK_SEARCH_END_STATIC TRUE)
set(BLA_STATIC ON)
endif()
find_package(LAPACK REQUIRED)
find_package(BLAS REQUIRED)
if(NOT TARGET "OpenMP::OpenMP_Fortran" AND WITH_OpenMP)
find_package("OpenMP" REQUIRED)
get_target_property(OpenMP_Fortran_LIBRARIES OpenMP::OpenMP_Fortran INTERFACE_LINK_LIBRARIES)
message(STATUS "OpenMP::OpenMP_Fortran is linking the following libraries:")
foreach(lib ${OpenMP_Fortran_LIBRARIES})
message(STATUS "${lib}")
endforeach()
endif()
# Check if we are using OpenBLAS (need a precompiler definition if yes)
if(LAPACK_LIBRARIES)
string(FIND "${LAPACK_LIBRARIES}" "openblas" _openblas_in_lapack)
if(NOT _openblas_in_lapack EQUAL -1)
message(STATUS "libopenblas was found as part of LAPACK")
add_compile_definitions(WITH_OPENBLAS)
endif()
endif()
# Fortran unit test interface (also used by other subprojects)
if(NOT TARGET "test-drive::test-drive" AND WITH_TESTS)
find_package("test-drive" REQUIRED)
endif()
# TOML-F (needs to be imported before tblite)
if(NOT TARGET "toml-f::toml-f" AND WITH_TOMLF)
find_package("toml-f" REQUIRED)
add_compile_definitions(WITH_TOMLF)
endif()
# tblite
if(NOT TARGET "tblite::tblite" AND WITH_TBLITE)
find_package("tblite" REQUIRED)
add_compile_definitions(WITH_TBLITE)
endif()
# GFN-FF
if(NOT TARGET "gfnff::gfnff" AND WITH_GFNFF)
find_package("gfnff" REQUIRED)
add_compile_definitions(WITH_GFNFF)
endif()
# GFN0-xTB
if(NOT TARGET "gfn0::gfn0" AND WITH_GFN0)
find_package("gfn0" REQUIRED)
add_compile_definitions(WITH_GFN0)
endif()
# XHCFF
if(NOT TARGET "xhcff::xhcff" AND WITH_XHCFF)
find_package("xhcff" REQUIRED)
add_compile_definitions(WITH_XHCFF)
endif()
# lwONIOM
if(NOT TARGET "lwoniom::lwoniom" AND WITH_LWONIOM)
find_package("lwoniom" REQUIRED)
add_compile_definitions(WITH_LWONIOM)
endif()
# Sources: initialize program sources (prog) and library sources (srcs) empty
set(prog)
set(srcs)
add_subdirectory("src")
if(NOT EXISTS "${PROJECT_BINARY_DIR}/include")
file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/include")
endif()
###############################################################################
########################## OBJECT LIBRARY #####################################
###############################################################################
if(WITH_OBJECT AND NOT STATICBUILD)
add_library(
"${PROJECT_NAME}-object"
OBJECT
${srcs}
)
# customize object library
set_target_properties(
"${PROJECT_NAME}-object"
PROPERTIES
Fortran_MODULE_DIRECTORY "${PROJECT_BINARY_DIR}/include"
POSITION_INDEPENDENT_CODE ON
)
# link object library conditionally against required
target_link_libraries(
"${PROJECT_NAME}-object"
PUBLIC
$<$<BOOL:${WITH_TBLITE}>:tblite::tblite>
$<$<BOOL:${WITH_GFN0}>:gfn0::gfn0>
$<$<BOOL:${WITH_GFNFF}>:gfnff::gfnff>
$<$<BOOL:${WITH_XHCFF}>:xhcff::xhcff>
$<$<BOOL:${WITH_TOMLF}>:toml-f::toml-f>
$<$<BOOL:${WITH_LWONIOM}>:lwoniom::lwoniom>
$<$<BOOL:${WITH_OpenMP}>:OpenMP::OpenMP_Fortran>
)
# include directories
target_include_directories(
"${PROJECT_NAME}-object"
PUBLIC
${crest-config-dir}
${PROJECT_BINARY_DIR}
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>
$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>
)
endif()
###############################################################################
############################### Static Library ################################
###############################################################################
if(WITH_OBJECT AND NOT STATICBUILD)
add_library(
"lib-${PROJECT_NAME}-static"
STATIC
$<TARGET_OBJECTS:${PROJECT_NAME}-object>
)
else()
add_library(
"lib-${PROJECT_NAME}-static"
STATIC
${srcs}
)
endif()
# workaround
set(LINK_OpenMP FALSE)
if(WITH_OpenMP AND NOT STATICBUILD)
set(LINK_OpenMP TRUE)
endif()
target_link_libraries(
"lib-${PROJECT_NAME}-static"
PUBLIC
${BLAS_LIBRARIES}
${LAPACK_LIBRARIES}
$<$<BOOL:${LINK_OpenMP}>:OpenMP::OpenMP_Fortran>
$<$<BOOL:${WITH_TBLITE}>:tblite::tblite>
$<$<BOOL:${WITH_GFN0}>:gfn0::gfn0>
$<$<BOOL:${WITH_GFNFF}>:gfnff::gfnff>
$<$<BOOL:${WITH_XHCFF}>:xhcff::xhcff>
$<$<BOOL:${WITH_TOMLF}>:toml-f::toml-f>
$<$<BOOL:${WITH_LWONIOM}>:lwoniom::lwoniom>
$<$<BOOL:${STATICBUILD}>:-static>
)
set_target_properties(
"lib-${PROJECT_NAME}-static"
PROPERTIES
Fortran_MODULE_DIRECTORY "${PROJECT_BINARY_DIR}/include"
ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}"
POSITION_INDEPENDENT_CODE ON
OUTPUT_NAME "${PROJECT_NAME}"
)
target_include_directories(
"lib-${PROJECT_NAME}-static"
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>
$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>
)
####################################################################################
############################# Shared Library #######################################
####################################################################################
if (WITH_OBJECT AND NOT STATICBUILD)
add_library(
"lib-${PROJECT_NAME}-shared"
SHARED
$<TARGET_OBJECTS:${PROJECT_NAME}-object>
)
target_link_libraries(
"lib-${PROJECT_NAME}-shared"
PUBLIC
${BLAS_LIBRARIES}
${LAPACK_LIBRARIES}
$<$<BOOL:${WITH_OpenMP}>:OpenMP::OpenMP_Fortran>
$<$<BOOL:${WITH_TBLITE}>:tblite::tblite>
$<$<BOOL:${WITH_GFN0}>:gfn0::gfn0>
$<$<BOOL:${WITH_GFNFF}>:gfnff::gfnff>
$<$<BOOL:${WITH_XHCFF}>:xhcff::xhcff>
$<$<BOOL:${WITH_TOMLF}>:toml-f::toml-f>
$<$<BOOL:${WITH_LWONIOM}>:lwoniom::lwoniom>
)
set_target_properties(
"lib-${PROJECT_NAME}-shared"
PROPERTIES
Fortran_MODULE_DIRECTORY "${PROJECT_BINARY_DIR}/include"
LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}"
OUTPUT_NAME "${PROJECT_NAME}"
VERSION "${PROJECT_VERSION}"
SOVERSION "${PROJECT_VERSION_MAJOR}"
)
target_include_directories(
"lib-${PROJECT_NAME}-shared"
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>
$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>
)
endif()
###############################################################################
############################### Executables ###################################
###############################################################################
add_executable(
${PROJECT_NAME}-exe
${prog}
)
target_link_libraries(
${PROJECT_NAME}-exe
PRIVATE
"lib-${PROJECT_NAME}-static"
$<$<BOOL:${STATICBUILD}>:-static>
)
set_target_properties(
${PROJECT_NAME}-exe
PROPERTIES
Fortran_MODULE_DIRECTORY ${PROJECT_BINARY_DIR}/include
RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}
OUTPUT_NAME "${PROJECT_NAME}"
)
target_include_directories(
${PROJECT_NAME}-exe
PRIVATE
${PROJECT_SOURCE_DIR}/include
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>
)
###############################################################################
########################### Install Instructions ##############################
###############################################################################
# Build output artifacts
if (WITH_OBJECT AND NOT STATICBUILD)
install(
TARGETS
"lib-${PROJECT_NAME}-static"
# "lib-${PROJECT_NAME}-shared"
"${PROJECT_NAME}-exe"
EXPORT "${PROJECT_NAME}-targets"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)
else()
install(
TARGETS
"lib-${PROJECT_NAME}-static"
"${PROJECT_NAME}-exe"
EXPORT "${PROJECT_NAME}-targets"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)
endif()
install(
EXPORT
"${PROJECT_NAME}-targets"
NAMESPACE
"${PROJECT_NAME}::"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
)
install(
DIRECTORY
"${CMAKE_CURRENT_SOURCE_DIR}/include/"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)
install(
DIRECTORY
"${CMAKE_CURRENT_BINARY_DIR}/include/"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${module-dir}"
)
# Package license files
install(
FILES
"COPYING"
"COPYING.LESSER"
DESTINATION "${CMAKE_INSTALL_DATADIR}/licenses/${PROJECT_NAME}"
)
##############################################################################
############################# Unit Tests #####################################
##############################################################################
if (WITH_TESTS)
enable_testing()
add_subdirectory("test")
endif()