-
Notifications
You must be signed in to change notification settings - Fork 6
/
CMakeLists.txt
254 lines (225 loc) · 7.19 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
cmake_minimum_required(VERSION 3.10)
project(
coffe
VERSION ${SKBUILD_PROJECT_VERSION}
LANGUAGES C)
# Default build type
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
# Add the option to disable silent rules
option(DISABLE_SILENT_RULES "Disable silent rules" OFF)
if(NOT DISABLE_SILENT_RULES)
set(CMAKE_VERBOSE_MAKEFILE OFF)
endif()
# Get the current arch (because MacOS can have both)
execute_process(
COMMAND uname -m
COMMAND tr -d '\n'
OUTPUT_VARIABLE BUILD_ARCHITECTURE)
# Check whether the user requested Python
if(COFFE_ENABLE_PYTHON)
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin" AND (DEFINED CONAN_C_FLAGS
OR DEFINED CONAN_CXX_FLAGS))
# For some reason, Conan does not find Python from the venv on MacOS, so we
# need to use this annoying hack. Note that this does not guarantee that the
# Python executable is the one from the venv, but at least it finds the one
# compatible with all of the packages from the venv
if(NOT COFFE_PYTHON_MINOR_VERSION)
execute_process(
COMMAND python -c "import sys;print(sys.version_info.minor)"
OUTPUT_VARIABLE COFFE_PYTHON_MINOR_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE)
endif()
find_package(
Python "3.${COFFE_PYTHON_MINOR_VERSION}" EXACT
COMPONENTS Interpreter Development.Module
REQUIRED)
else()
find_package(
Python
COMPONENTS Interpreter Development.Module
REQUIRED)
endif()
include(cmake/FindCython.cmake)
endif()
# Define source directory
set(SRC_DIR ${CMAKE_SOURCE_DIR}/src)
# Compiler flags
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wpedantic")
set(CMAKE_C_STANDARD 99)
# Check for libraries
find_package(GSL 2.1 REQUIRED)
find_library(FFTW3_LIB fftw3 NAMES fftw fftw3 libfftw libfftw3 REQUIRED)
find_library(CONFIG_LIB config NAMES config config++ libconfig libconfig++
REQUIRED)
find_library(M_LIB m NAMES m libm REQUIRED)
set(CONFIG_INCLUDE_DIRS)
set(FFTW3_INCLUDE_DIRS)
set(CUBA_INCLUDE_DIRS)
set(CLASS_INCLUDE_DIRS)
# Check for optional libraries
if(COFFE_ENABLE_CUBA)
find_library(
CUBA_LIB cuba
NAMES cuba libcuba
PATHS /opt/cuba_${BUILD_ARCHITECTURE}/lib $ENV{CUBA_INSTALL_DIR}/lib
REQUIRED)
endif()
if(COFFE_ENABLE_CLASS)
find_library(
CLASS_LIB class
NAMES class libclass
PATHS /opt/class_${BUILD_ARCHITECTURE}/lib
/opt/class_public_${BUILD_ARCHITECTURE}/lib
$ENV{CLASS_INSTALL_DIR}/lib REQUIRED)
endif()
get_filename_component(BASE_DIR ${CONFIG_LIB} DIRECTORY)
string(REGEX REPLACE "/lib$" "/include" CONFIG_INCLUDE_DIRS ${BASE_DIR})
get_filename_component(BASE_DIR ${FFTW3_LIB} DIRECTORY)
string(REGEX REPLACE "/lib$" "/include" FFTW3_INCLUDE_DIRS ${BASE_DIR})
get_filename_component(BASE_DIR ${CLASS_LIB} DIRECTORY)
string(REGEX REPLACE "/lib$" "/include" CLASS_INCLUDE_DIRS ${BASE_DIR})
get_filename_component(BASE_DIR ${CUBA_LIB} DIRECTORY)
string(REGEX REPLACE "/lib$" "/include" CUBA_INCLUDE_DIRS ${BASE_DIR})
# OpenMP support
find_package(OpenMP)
if(OpenMP_C_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
endif()
# Optimization flags
include(CheckCCompilerFlag)
if(COFFE_ENABLE_MATHOPTS)
if(CMAKE_C_COMPILER_ID STREQUAL "Clang" OR CMAKE_C_COMPILER_ID STREQUAL "GNU")
set(COFFE_MATHOPTS "-ffast-math")
endif()
else()
set(COFFE_MATHOPTS)
endif()
# Add definitions for conditional compilation
if(COFFE_ENABLE_CLASS)
add_compile_definitions(-DHAVE_CLASS)
endif()
if(COFFE_ENABLE_CUBA)
add_compile_definitions(-DHAVE_CUBA)
endif()
message("COFFE configuration")
message("============================")
message("Build architecture: ${BUILD_ARCHITECTURE}")
message("Math library: ${M_LIB}")
message("GSL library: ${GSL_LIBRARIES}")
message("FFTW library: ${FFTW3_LIB}")
message("libconfig library: ${CONFIG_LIB}")
if(COFFE_ENABLE_PYTHON)
message("Python path: ${Python_EXECUTABLE}")
message("Cython path: ${CYTHON_EXECUTABLE}")
endif()
if(COFFE_ENABLE_CLASS)
message("CLASS library: ${CLASS_LIB}")
endif()
if(COFFE_ENABLE_CUBA)
message("CUBA library: ${CUBA_LIB}")
endif()
message("OpenMP enabled: ${OpenMP_C_FOUND}")
message("Additional optimizations: ${COFFE_MATHOPTS}")
set(COFFE_HEADERS
${SRC_DIR}/common.h
${SRC_DIR}/covariance.h
${SRC_DIR}/utils.h
${SRC_DIR}/twobessel.h
${SRC_DIR}/errors.h
${SRC_DIR}/parser.h
${SRC_DIR}/twofast.h
${SRC_DIR}/integrals.h
${SRC_DIR}/background.h
${SRC_DIR}/functions.h
${SRC_DIR}/tanhsinh.h
${SRC_DIR}/signal.h
${SRC_DIR}/corrfunc.h
${SRC_DIR}/multipoles.h
${SRC_DIR}/average_multipoles.h)
set(COFFE_LIB_SOURCES
${SRC_DIR}/common.c
${SRC_DIR}/covariance.c
${SRC_DIR}/utils.c
${SRC_DIR}/twobessel.c
${SRC_DIR}/errors.c
${SRC_DIR}/parser.c
${SRC_DIR}/twofast.c
${SRC_DIR}/integrals.c
${SRC_DIR}/background.c
${SRC_DIR}/functions.c
${SRC_DIR}/tanhsinh.c
${SRC_DIR}/signal.c
${SRC_DIR}/corrfunc.c
${SRC_DIR}/multipoles.c
${SRC_DIR}/average_multipoles.c)
# Main program
add_executable(coffe-cli ${SRC_DIR}/main.c ${COFFE_LIB_SOURCES}
${SRC_DIR}/output.c)
# Include directories
target_include_directories(
coffe-cli PRIVATE ${CONFIG_INCLUDE_DIRS} ${FFTW3_INCLUDE_DIRS}
${CUBA_INCLUDE_DIRS} ${SRC_DIR} ${CLASS_INCLUDE_DIRS})
# Add definition
target_compile_definitions(coffe-cli PRIVATE PACKAGE_STRING="${VERSION}")
# Link libraries
target_link_libraries(
coffe-cli
${M_LIB}
${GSL_LIBRARIES}
${FFTW3_LIBRARIES}
${FFTW3_LIB}
${libconfig_LIBRARIES}
${CONFIG_LIB}
${CUBA_LIB}
${CLASS_LIB})
target_compile_options(coffe-cli PRIVATE ${COFFE_MATHOPTS})
# Tests
if(COFFE_ENABLE_TESTS)
enable_testing()
if(NOT DEFINED COFFE_TEST_DATADIR)
set(COFFE_TEST_DATADIR ${CMAKE_SOURCE_DIR})
endif()
set(COFFE_TESTS background integrals corrfunc multipoles covariance)
foreach(name IN LISTS COFFE_TESTS)
# Add the test executable
add_executable(test_${name} ${CMAKE_SOURCE_DIR}/tests/test_${name}.c
${COFFE_LIB_SOURCES})
target_include_directories(
test_${name}
PRIVATE ${CONFIG_INCLUDE_DIRS} ${FFTW3_INCLUDE_DIRS} ${CUBA_INCLUDE_DIRS}
${SRC_DIR} ${CLASS_INCLUDE_DIRS} ${CMAKE_SOURCE_DIR}/tests)
# Add defines
target_compile_definitions(
test_${name} PRIVATE COFFE_TEST_DATADIR="${COFFE_TEST_DATADIR}")
# Link libraries
target_link_libraries(
test_${name}
${M_LIB}
${GSL_LIBRARIES}
${FFTW3_LIB}
${CONFIG_LIB}
${CUBA_LIB}
${CLASS_LIB})
# Register tests
add_test(NAME test_${name} COMMAND test_${name})
endforeach()
endif()
if(COFFE_ENABLE_PYTHON)
add_subdirectory(${CMAKE_SOURCE_DIR}/python/coffe)
endif()
# Installation
if(NOT SKBUILD)
install(
TARGETS coffe-cli
COMPONENT Runtime
DESTINATION bin)
# Install headers and other files
install(FILES ${COFFE_HEADERS} DESTINATION include/coffe)
install(
FILES ${SRC_DIR}/WAVENUMBER_HEADER.dat ${SRC_DIR}/POWER_SPECTRUM_HEADER.dat
${CMAKE_SOURCE_DIR}/settings.cfg ${CMAKE_SOURCE_DIR}/separations.dat
${CMAKE_SOURCE_DIR}/PkL_CLASS.dat ${CMAKE_SOURCE_DIR}/README.md
DESTINATION share/coffe)
endif()