forked from SimVascular/svZeroDSolver
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
284 lines (240 loc) · 10 KB
/
CMakeLists.txt
File metadata and controls
284 lines (240 loc) · 10 KB
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
# SPDX-FileCopyrightText: Copyright (c) Stanford University, The Regents of the University of California, and others.
# SPDX-License-Identifier: BSD-3-Clause
set(CMAKE_POLICY_VERSION_MINIMUM 3.18)
cmake_minimum_required(VERSION 3.18)
cmake_policy(SET CMP0077 NEW)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
project(svZeroDSolver LANGUAGES C CXX)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# Force Eigen & superprojects to skip docs/tests/fortran no matter what
set(BUILD_TESTING OFF CACHE BOOL "" FORCE)
set(EIGEN_BUILD_DOC OFF CACHE BOOL "" FORCE)
set(EIGEN_TEST_FORTRAN OFF CACHE BOOL "" FORCE)
set(EIGEN_BUILD_BLAS OFF CACHE BOOL "" FORCE)
include(FetchContent)
set(PYBIND11_FINDPYTHON NEW CACHE STRING "" FORCE)
find_package(Python COMPONENTS Interpreter Development REQUIRED)
message(STATUS "Using Python: ${Python_EXECUTABLE} (${Python_VERSION})")
# Enable code coverage
# -----------------------------------------------------------------------------
set(ENABLE_COVERAGE OFF CACHE BOOL "Enable code coverage")
# coverage
if(ENABLE_COVERAGE)
# set compiler flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -coverage -g")
# find required tools
find_program(LCOV lcov REQUIRED)
find_program(GENHTML genhtml REQUIRED)
# add coverage target
add_custom_target(coverage
# gather data
COMMAND ${LCOV} --directory . --capture --output-file coverage.info --ignore-errors gcov --exclude '/usr/include/*' --exclude '/usr/lib/*' --exclude '*/_deps/*'
# generate report
COMMAND ${GENHTML} --demangle-cpp -o coverage coverage.info
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
endif()
if (WIN32 AND MSVC)
# CMake ≥ 3.15 has a proper variable
# Use dynamic CRT (/MD or /MDd) so EXE and DLL share the same heap.
# This is required when passing STL containers across DLL boundaries.
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.15")
set(CMAKE_MSVC_RUNTIME_LIBRARY
"MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
else()
# CMake < 3.15: /MD is the default, so no changes needed
endif()
endif()
# -----------------------------------------------------------------------------
# Set the location to store the binaries and libraries created by this project.
# -----------------------------------------------------------------------------
#
#set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
#set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# Need to add the -fPIC flag to build the interface shared library.
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
endif()
# -----------------------------------------------------------------------------
# Fetch Eigen
# -----------------------------------------------------------------------------
# Eigen is a header-only C++ template library for linear algebra.
#
# Tell FetchContent not to re-check for updates once Eigen is downloaded
set(FETCHCONTENT_UPDATES_DISCONNECTED TRUE)
FetchContent_Declare(
Eigen
GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git
GIT_TAG 3.4.0
GIT_SHALLOW TRUE
GIT_PROGRESS TRUE)
set(EIGEN_BUILD_DOC OFF)
set(BUILD_TESTING OFF)
set(EIGEN_BUILD_PKGCONFIG OFF)
FetchContent_MakeAvailable(Eigen)
# -----------------------------------------------------------------------------
# Fetch pybind11
# -----------------------------------------------------------------------------
# pybind11 is a library used to create Python bindings of existing C++ code.
#
FetchContent_Declare(
pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11
GIT_TAG master
)
FetchContent_GetProperties(pybind11)
if(NOT pybind11_POPULATED)
FetchContent_Populate(pybind11)
add_subdirectory(${pybind11_SOURCE_DIR} ${pybind11_BINARY_DIR})
endif()
# -----------------------------------------------------------------------------
# Fetch nlohmann/json
# -----------------------------------------------------------------------------
# This is a C++ header-only library for reading JSON files.
#
FetchContent_Declare(
json
URL https://github.com/nlohmann/json/releases/download/v3.11.2/json.tar.xz
)
FetchContent_MakeAvailable(json)
# -----------------------------------------------------------------------------
# Fetch pybind11_json
# -----------------------------------------------------------------------------
# pybind11_json is an nlohmann::json to pybind11 bridge that converts
# between nlohmann::json and py::object objects.
#
FetchContent_Declare(
pybind11_json
GIT_REPOSITORY https://github.com/pybind/pybind11_json.git
GIT_TAG master
)
FetchContent_MakeAvailable(pybind11_json)
# -----------------------------------------------------------------------------
# Set executables for each application.
# -----------------------------------------------------------------------------
# There are two executables
# 1) svzerodsolver
# 2) svzerodcalibrator
#
add_executable(svzerodsolver applications/svzerodsolver.cpp
$<TARGET_OBJECTS:svzero_algebra_library>
$<TARGET_OBJECTS:svzero_model_library>
$<TARGET_OBJECTS:svzero_solve_library>
)
add_executable(svzerodcalibrator applications/svzerodcalibrator.cpp
$<TARGET_OBJECTS:svzero_algebra_library>
$<TARGET_OBJECTS:svzero_optimize_library>
$<TARGET_OBJECTS:svzero_model_library>
)
# -----------------------------------------------------------------------------
# Setup building of the pysvzerod Python extension module.
# -----------------------------------------------------------------------------
# Replace EXCLUDE_FROM_ALL with SHARED to test building the Python
# shared library.
pybind11_add_module(pysvzerod applications/pysvzerod.cpp)
if(WIN32 AND "${CMAKE_GENERATOR}" STREQUAL "MinGW Makefiles")
message(STATUS ">> Applying static‑link flags to pysvzerod")
include(CheckCXXCompilerFlag)
set(_static_flags
-static
-static-libgcc
-static-libstdc++
)
# test for winpthread support before adding
check_cxx_compiler_flag("-static-libwinpthread" HAVE_WINPTHREAD)
if(HAVE_WINPTHREAD)
list(APPEND _static_flags -static-libwinpthread)
else()
find_package(Threads REQUIRED)
target_link_libraries(pysvzerod PRIVATE Threads::Threads)
endif()
# apply to compile *and* link
foreach(_f IN LISTS _static_flags)
target_compile_options(pysvzerod PRIVATE ${_f})
# CMake ≥3.13:
target_link_options (pysvzerod PRIVATE ${_f})
endforeach()
# verify in the configure log for static compilation:
get_target_property(_ccopts pysvzerod COMPILE_OPTIONS)
message(STATUS ">>> pysvzerod COMPILE_OPTIONS = ${_ccopts}")
get_target_property(_lnkopts pysvzerod LINK_OPTIONS)
message(STATUS ">>> pysvzerod LINK_OPTIONS = ${_lnkopts}")
endif()
# -----------------------------------------------------------------------------
# Add source sub-directories.
# -----------------------------------------------------------------------------
add_subdirectory("src/algebra")
add_subdirectory("src/interface")
add_subdirectory("src/model")
add_subdirectory("src/optimize")
add_subdirectory("src/solve")
# -----------------------------------------------------------------------------
# Set header file include directories.
# -----------------------------------------------------------------------------
#
target_include_directories(svzerodsolver PUBLIC
${CMAKE_SOURCE_DIR}/src/algebra
${CMAKE_SOURCE_DIR}/src/model
${CMAKE_SOURCE_DIR}/src/solve
)
target_include_directories(svzerodcalibrator PUBLIC
${CMAKE_SOURCE_DIR}/src/algebra
${CMAKE_SOURCE_DIR}/src/model
${CMAKE_SOURCE_DIR}/src/optimize
${CMAKE_SOURCE_DIR}/src/solve
)
target_include_directories(pysvzerod PUBLIC
${CMAKE_SOURCE_DIR}/src/algebra
${CMAKE_SOURCE_DIR}/src/model
${CMAKE_SOURCE_DIR}/src/optimize
${CMAKE_SOURCE_DIR}/src/solve
)
# -----------------------------------------------------------------------------
# Set the libraries each application needs to link to.
# -----------------------------------------------------------------------------
# These are needed only for the header-only applications.
#
target_link_libraries(svzerodsolver PRIVATE Eigen3::Eigen)
target_link_libraries(svzerodsolver PRIVATE nlohmann_json::nlohmann_json)
target_link_libraries(svzerodcalibrator PRIVATE Eigen3::Eigen)
target_link_libraries(svzerodcalibrator PRIVATE nlohmann_json::nlohmann_json)
target_link_libraries(pysvzerod PRIVATE Eigen3::Eigen)
target_link_libraries(pysvzerod PRIVATE nlohmann_json::nlohmann_json)
target_link_libraries(pysvzerod PRIVATE pybind11_json)
target_link_libraries(pysvzerod PRIVATE svzero_algebra_library)
target_link_libraries(pysvzerod PRIVATE svzero_model_library)
target_link_libraries(pysvzerod PRIVATE svzero_optimize_library)
target_link_libraries(pysvzerod PRIVATE svzero_solve_library)
# Create distribution
set(ENABLE_DISTRIBUTION OFF CACHE BOOL "Enable installer build")
add_subdirectory("distribution")
# -----------------------------------------------------------------------------
# Enforce and check code format
# -----------------------------------------------------------------------------
# set paths of files to format
set(SDIR ${PROJECT_SOURCE_DIR}/src/**/)
# format the code
add_custom_target(codeformat
COMMAND npm run format
COMMAND find ${SDIR}/*.h ${SDIR}/*.cpp | xargs clang-format -style=file:${PROJECT_SOURCE_DIR}/.clang-format -i)
# check code format
add_custom_target(codecheck
COMMAND npm run check
COMMAND find ${SDIR}/*.h ${SDIR}/*.cpp | xargs clang-format -style=file:${PROJECT_SOURCE_DIR}/.clang-format --dry-run --Werror)
set_target_properties(pysvzerod PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/python
)
# standard install target locations
install(TARGETS pysvzerod
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_LIBDIR}"
)
# append correct suffix for windows python runtime files
if (WIN32)
set_target_properties(pysvzerod PROPERTIES
PREFIX ""
SUFFIX ".pyd"
)
endif()