-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
CMakeLists.txt
312 lines (240 loc) · 9.91 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
# ==========================================================================================
# Main CMakeLists.txt
# ==========================================================================================
# ------------------------------------------------------------------------------------------
# CMake Configuration
# ------------------------------------------------------------------------------------------
# --- Initialization
cmake_minimum_required(VERSION 3.14)
project(LSMLIB
LANGUAGES C CXX Fortran
VERSION 2.0.0)
# --- Configuration parameters
# Build type
set(CMAKE_BUILD_TYPE Release CACHE STRING "CMake build type")
# Compiler requirements
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED True)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# ------------------------------------------------------------------------------------------
# Build Options
# ------------------------------------------------------------------------------------------
# Build shared libraries
# TODO: fix shared library build
#option(BUILD_SHARED_LIBS "Build shared libraries" ON)
# Floating-point precision
option(USE_SINGLE_PRECISION "Build single-precision libraries" OFF)
if (USE_SINGLE_PRECISION)
message("-- Setting floating-point precision to 'single'")
else (USE_SINGLE_PRECISION)
message("-- Setting floating-point precision to 'double'")
endif (USE_SINGLE_PRECISION)
# ------------------------------------------------------------------------------------------
# Imported Modules
# ------------------------------------------------------------------------------------------
# --- CMake modules
# Packaging
include(CMakePackageConfigHelpers)
# Enable populating external projects at configure time
include(FetchContent)
# Make cache variables for install destinations
include(GNUInstallDirs)
# --- External CMake-compatible packages
find_package(Threads REQUIRED)
find_package(Git)
# --- include-what-you-use
find_program(IWYU NAMES include-what-you-use iwyu)
if (IWYU)
message("-- Found include-what-you-use: ${IWYU}")
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${IWYU} ${CMAKE_CXX_FLAGS})
else (IWYU)
message("-- Could not find include-what-you-use. "
"Analysis of #includes will be skipped.")
endif (IWYU)
# --- GoogleTest
find_package(GTest)
if (NOT GTest_FOUND)
message("-- Attempting to fetch GTest from GitHub...")
FetchContent_Declare(
GTest
URL https://github.com/google/googletest/archive/refs/tags/release-1.11.0.tar.gz
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(GTest)
set(GTest_LOCAL True)
set(GTEST_INCLUDE_DIRS ${gtest_SOURCE_DIR}/googletest/include/)
set(GTEST_LIBRARIES GTest::gtest)
set(GTEST_MAIN_LIBRARIES GTest::gtest_main)
endif (NOT GTest_FOUND)
# ------------------------------------------------------------------------------------------
# Parameters
# ------------------------------------------------------------------------------------------
# --- Build parameters
# Package name
set(PKG_NAME ${CMAKE_PROJECT_NAME})
# Construct name to use for package-specific directories (e.g., header files)
execute_process(
COMMAND "echo" ${PKG_NAME}
COMMAND "tr" '[:upper:]' '[:lower:]'
OUTPUT_VARIABLE get_project_name_lower_out
RESULT_VARIABLE get_project_name_lower_result_var
)
if (NOT "${get_project_name_lower_result_var}" STREQUAL 0)
message(FATAL_ERROR "Error constructing directory name for package")
endif (NOT "${get_project_name_lower_result_var}" STREQUAL 0)
string(STRIP ${get_project_name_lower_out} PKG_DIR_NAME)
# Set path variables
set(BINARY_INCLUDEDIR ${CMAKE_BINARY_DIR}/include)
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
# Compiler flags
set(CMAKE_Fortran_FLAGS "${FFLAGS} ${CMAKE_Fortran_FLAGS}")
if (NOT USE_SINGLE_PRECISION)
# Set Fortran compiler flags
get_filename_component(Fortran_COMPILER_NAME ${CMAKE_Fortran_COMPILER} NAME)
message("-- Fortran compiler: " ${CMAKE_Fortran_COMPILER})
if (Fortran_COMPILER_NAME MATCHES "gfortran.*")
# gfortran
set(CMAKE_Fortran_FLAGS
"-fdefault-real-8 -fdefault-double-8 ${CMAKE_Fortran_FLAG}")
elseif (Fortran_COMPILER_NAME MATCHES "g77.*")
# g77
set(CMAKE_Fortran_FLAGS
"-fdefault-real-8 -fdefault-double-8 ${CMAKE_Fortran_FLAG}")
elseif (Fortran_COMPILER_NAME MATCHES "ifort.*")
# ifort
set(CMAKE_Fortran_FLAGS "-r8 ${CMAKE_Fortran_FLAG}")
else (Fortran_COMPILER_NAME MATCHES "gfortran.*")
# Unknown fortran compiler
message("Trying GNU Fortran flags.")
set(CMAKE_Fortran_FLAGS
"-fdefault-real-8 -fdefault-double-8 ${CMAKE_Fortran_FLAG}")
endif (Fortran_COMPILER_NAME MATCHES "gfortran.*")
endif (NOT USE_SINGLE_PRECISION)
# --- Install parameters
# Set directory (relative) for installed header files
set(INSTALL_INCLUDEDIR ${CMAKE_INSTALL_INCLUDEDIR}/${PKG_DIR_NAME})
# ------------------------------------------------------------------------------------------
# Build
# ------------------------------------------------------------------------------------------
# --- Process template files
# Floating-point precision
if (USE_SINGLE_PRECISION)
set(LSMLIB_REAL "float")
set(LSMLIB_ZERO_TOL 1.e-5)
set(LSMLIB_REAL_MAX "FLT_MAX")
set(LSMLIB_REAL_MIN "FLT_MIN")
set(LSMLIB_REAL_EPSILON "FLT_EPSILON")
else (USE_SINGLE_PRECISION)
set(LSMLIB_DOUBLE_PRECISION ON)
set(LSMLIB_REAL "double")
set(LSMLIB_ZERO_TOL 1.e-11)
set(LSMLIB_REAL_MAX "DBL_MAX")
set(LSMLIB_REAL_MIN "DBL_MIN")
set(LSMLIB_REAL_EPSILON "DBL_EPSILON")
endif (USE_SINGLE_PRECISION)
# Generate configuration header files
configure_file(include/lsmlib_config.h.in
${BINARY_INCLUDEDIR}/lsmlib_config.h)
# Copy CMake configuration files
file(COPY ${CMAKE_SOURCE_DIR}/cmake/${PKG_NAME}ConfigExtras.cmake
DESTINATION ${CMAKE_BINARY_DIR}/cmake
)
# --- Targets
# Libraries
add_subdirectory(src)
# Examples
add_subdirectory(examples EXCLUDE_FROM_ALL)
# CMake configuration files
add_custom_target(cmake-config-files ALL
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_SOURCE_DIR}/cmake/${PKG_NAME}ConfigExtras.cmake
${CMAKE_BINARY_DIR}/cmake/${PKG_NAME}ConfigExtras.cmake
)
# ------------------------------------------------------------------------------------------
# Test
# ------------------------------------------------------------------------------------------
# Enable CMake testing
enable_testing()
# Tests
add_subdirectory(tests EXCLUDE_FROM_ALL)
# ------------------------------------------------------------------------------------------
# Package
# ------------------------------------------------------------------------------------------
# Create package configuration file
configure_package_config_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/Config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/cmake/${PKG_NAME}Config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PKG_NAME}
)
# Create package version file
set_property(TARGET lsm PROPERTY VERSION ${LSMLIB_VERSION})
set_property(TARGET lsm PROPERTY INTERFACE_LSMLIB_MAJOR_VERSION ${LSMLIB_VERSION_MAJOR})
set_property(TARGET lsm APPEND PROPERTY COMPATIBLE_INTERFACE_STRING LSMLIB_MAJOR_VERSION)
write_basic_package_version_file(
${CMAKE_BINARY_DIR}/cmake/${PKG_NAME}ConfigVersion.cmake
VERSION "${version}"
COMPATIBILITY AnyNewerVersion
)
# Create source code distribution
if (GIT_FOUND)
# Add target to build source code distribution
set(ARCHIVE_NAME ${PKG_NAME}-${PROJECT_VERSION})
set(ARCHIVE_TARBALL ${ARCHIVE_NAME}.tar.gz)
add_custom_target(dist
COMMAND git archive --prefix=${ARCHIVE_NAME}/ HEAD |
gzip > ${CMAKE_BINARY_DIR}/${ARCHIVE_TARBALL}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
)
# Add target to verify source code distribution
if (UNIX)
file(GENERATE OUTPUT check-uninstall.sh
CONTENT "
remaining=\"`find \${PWD}/${ARCHIVE_NAME}/build/_inst -type f | wc -l`\"
if test \"\${remaining}\" -ne 0; then
echo \"** Stage directory not empty: \${remaining} file (s) remaining!\"
exit 1
fi"
)
add_custom_target(distcheck
COMMAND rm -rf ${ARCHIVE_NAME}
COMMAND gzip -cd ${ARCHIVE_TARBALL} | tar xfz -
COMMAND cd ${ARCHIVE_NAME} && cmake -H. -Bbuild
COMMAND cd ${ARCHIVE_NAME}/build && make
COMMAND cd ${ARCHIVE_NAME}/build && make test
COMMAND cd ${ARCHIVE_NAME}/build && make DESTDIR=$${PWD}/_inst install
COMMAND cd ${ARCHIVE_NAME}/build && make DESTDIR=$${PWD}/_inst uninstall
COMMAND sh check-uninstall.sh
COMMAND cd ${ARCHIVE_NAME}/build && make clean
COMMAND rm -rf ${ARCHIVE_NAME}
COMMAND echo "*** Package ${ARCHIVE_TARBALL} ready for distribution."
)
add_dependencies(distcheck dist)
endif (UNIX)
endif (GIT_FOUND)
# ------------------------------------------------------------------------------------------
# Install
# ------------------------------------------------------------------------------------------
# --- Files
# Configuration header file
install(FILES ${BINARY_INCLUDEDIR}/lsmlib_config.h
DESTINATION ${INSTALL_INCLUDEDIR})
# CMake packaging files
install(FILES
${CMAKE_BINARY_DIR}/cmake/${PKG_NAME}Config.cmake
${CMAKE_BINARY_DIR}/cmake/${PKG_NAME}ConfigVersion.cmake
${CMAKE_BINARY_DIR}/cmake/${PKG_NAME}ConfigExtras.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PKG_NAME})
# --- `uninstall`
# Generate cmake_uninstall.cmake file
configure_file(
"${CMAKE_SOURCE_DIR}/cmake_uninstall.cmake.in"
"${CMAKE_BINARY_DIR}/cmake_uninstall.cmake"
IMMEDIATE @ONLY
)
# Define `uninstall` target
add_custom_target(uninstall
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/cmake_uninstall.cmake
)