-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
CMakeLists.txt
199 lines (176 loc) · 7.03 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
cmake_minimum_required(VERSION 3.13)
cmake_policy(SET CMP0074 NEW)
set(CMAKE_VERBOSE_MAKEFILE ON)
if(DEFINED Python_EXECUTABLE)
set(PYTHON_EXECUTABLE ${Python_EXECUTABLE})
endif()
if(DEFINED ENV{VCPKG_ROOT_DIR} AND NOT DEFINED VCPKG_ROOT_DIR)
set(VCPKG_ROOT_DIR "$ENV{VCPKG_ROOT_DIR}"
CACHE STRING "Vcpkg root directory")
endif()
if(DEFINED VCPKG_ROOT_DIR)
set(CMAKE_TOOLCHAIN_FILE ${VCPKG_ROOT_DIR}/scripts/buildsystems/vcpkg.cmake
CACHE STRING "Vcpkg toolchain file")
endif()
if(DEFINED ENV{VCPKG_DEFAULT_TRIPLET} AND NOT DEFINED VCPKG_TARGET_TRIPLET)
set(VCPKG_TARGET_TRIPLET "$ENV{VCPKG_DEFAULT_TRIPLET}"
CACHE STRING "Vcpkg target triplet")
endif()
project(idaklu)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
if(NOT MSVC)
# MSVC does not support variable length arrays (vla)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=vla")
endif()
# casadi seems to compile without the newer versions of std::string
add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0)
if(NOT PYBIND11_DIR)
set(PYBIND11_DIR pybind11)
endif()
add_subdirectory(${PYBIND11_DIR})
# Check Casadi build flag
if(NOT DEFINED PYBAMM_IDAKLU_EXPR_CASADI)
set(PYBAMM_IDAKLU_EXPR_CASADI ON)
endif()
message("PYBAMM_IDAKLU_EXPR_CASADI: ${PYBAMM_IDAKLU_EXPR_CASADI}")
# Casadi PyBaMM source files
set(IDAKLU_EXPR_CASADI_SOURCE_FILES "")
if(${PYBAMM_IDAKLU_EXPR_CASADI} STREQUAL "ON" )
add_compile_definitions(CASADI_ENABLE)
set(IDAKLU_EXPR_CASADI_SOURCE_FILES
src/pybammsolvers/idaklu_source/Expressions/Casadi/CasadiFunctions.cpp
src/pybammsolvers/idaklu_source/Expressions/Casadi/CasadiFunctions.hpp
)
endif()
# Check IREE build flag
if(NOT DEFINED PYBAMM_IDAKLU_EXPR_IREE)
set(PYBAMM_IDAKLU_EXPR_IREE OFF)
endif()
message("PYBAMM_IDAKLU_EXPR_IREE: ${PYBAMM_IDAKLU_EXPR_IREE}")
# IREE (MLIR expression evaluation) PyBaMM source files
set(IDAKLU_EXPR_IREE_SOURCE_FILES "")
if(${PYBAMM_IDAKLU_EXPR_IREE} STREQUAL "ON" )
add_compile_definitions(IREE_ENABLE)
# Source file list
set(IDAKLU_EXPR_IREE_SOURCE_FILES
src/pybammsolvers/idaklu_source/Expressions/IREE/iree_jit.cpp
src/pybammsolvers/idaklu_source/Expressions/IREE/iree_jit.hpp
src/pybammsolvers/idaklu_source/Expressions/IREE/IREEFunctions.cpp
src/pybammsolvers/idaklu_source/Expressions/IREE/IREEFunctions.hpp
src/pybammsolvers/idaklu_source/Expressions/IREE/ModuleParser.cpp
src/pybammsolvers/idaklu_source/Expressions/IREE/ModuleParser.hpp
)
endif()
# The complete (all dependencies) sources list should be mirrored in setup.py
pybind11_add_module(idaklu
# pybind11 interface
src/pybammsolvers/idaklu.cpp
# IDAKLU solver (SUNDIALS)
src/pybammsolvers/idaklu_source/idaklu_solver.hpp
src/pybammsolvers/idaklu_source/IDAKLUSolver.cpp
src/pybammsolvers/idaklu_source/IDAKLUSolver.hpp
src/pybammsolvers/idaklu_source/IDAKLUSolverGroup.cpp
src/pybammsolvers/idaklu_source/IDAKLUSolverGroup.hpp
src/pybammsolvers/idaklu_source/IDAKLUSolverOpenMP.inl
src/pybammsolvers/idaklu_source/IDAKLUSolverOpenMP.hpp
src/pybammsolvers/idaklu_source/IDAKLUSolverOpenMP_solvers.cpp
src/pybammsolvers/idaklu_source/IDAKLUSolverOpenMP_solvers.hpp
src/pybammsolvers/idaklu_source/sundials_functions.inl
src/pybammsolvers/idaklu_source/sundials_functions.hpp
src/pybammsolvers/idaklu_source/IdakluJax.cpp
src/pybammsolvers/idaklu_source/IdakluJax.hpp
src/pybammsolvers/idaklu_source/common.hpp
src/pybammsolvers/idaklu_source/common.cpp
src/pybammsolvers/idaklu_source/Solution.cpp
src/pybammsolvers/idaklu_source/Solution.hpp
src/pybammsolvers/idaklu_source/SolutionData.cpp
src/pybammsolvers/idaklu_source/SolutionData.hpp
src/pybammsolvers/idaklu_source/observe.cpp
src/pybammsolvers/idaklu_source/observe.hpp
src/pybammsolvers/idaklu_source/Options.hpp
src/pybammsolvers/idaklu_source/Options.cpp
# IDAKLU expressions / function evaluation [abstract]
src/pybammsolvers/idaklu_source/Expressions/Expressions.hpp
src/pybammsolvers/idaklu_source/Expressions/Base/Expression.hpp
src/pybammsolvers/idaklu_source/Expressions/Base/ExpressionSet.hpp
src/pybammsolvers/idaklu_source/Expressions/Base/ExpressionTypes.hpp
# IDAKLU expressions - concrete implementations
${IDAKLU_EXPR_CASADI_SOURCE_FILES}
${IDAKLU_EXPR_IREE_SOURCE_FILES}
)
if (NOT DEFINED USE_PYTHON_CASADI)
set(USE_PYTHON_CASADI TRUE)
endif()
execute_process(
COMMAND "${PYTHON_EXECUTABLE}" -c
"import os; import sysconfig; print(os.path.join(sysconfig.get_path('purelib'), 'casadi', 'cmake'))"
OUTPUT_VARIABLE CASADI_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE)
if (CASADI_DIR)
file(TO_CMAKE_PATH ${CASADI_DIR} CASADI_DIR)
message("Found Python casadi path: ${CASADI_DIR}")
else()
message(FATAL_ERROR "Did not find casadi path}")
endif()
if(${USE_PYTHON_CASADI})
message("Trying to link against Python casadi package in ${CASADI_DIR}")
find_package(casadi CONFIG PATHS ${CASADI_DIR} REQUIRED NO_DEFAULT_PATH)
else()
message("Trying to link against any casadi package apart from the Python one")
set(CMAKE_IGNORE_PATH "${CASADI_DIR}/cmake")
find_package(casadi CONFIG REQUIRED)
endif()
set_target_properties(
idaklu PROPERTIES
INSTALL_RPATH "${CASADI_DIR}"
INSTALL_RPATH_USE_LINK_PATH TRUE
)
# openmp
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
execute_process(
COMMAND "brew" "--prefix"
OUTPUT_VARIABLE HOMEBREW_PREFIX
OUTPUT_STRIP_TRAILING_WHITESPACE)
if (OpenMP_ROOT)
set(OpenMP_ROOT "${OpenMP_ROOT}:${HOMEBREW_PREFIX}/opt/libomp")
else()
set(OpenMP_ROOT "${HOMEBREW_PREFIX}/opt/libomp")
endif()
endif()
find_package(OpenMP)
if(OpenMP_CXX_FOUND)
target_link_libraries(idaklu PRIVATE OpenMP::OpenMP_CXX)
endif()
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR})
# Sundials
find_package(SUNDIALS REQUIRED)
message("SUNDIALS found in ${SUNDIALS_INCLUDE_DIR}: ${SUNDIALS_LIBRARIES}")
target_include_directories(idaklu PRIVATE ${SUNDIALS_INCLUDE_DIR})
target_link_libraries(idaklu PRIVATE ${SUNDIALS_LIBRARIES} casadi)
# link suitesparse
# if using vcpkg, use config mode to
# find suitesparse. Otherwise, use FindSuiteSparse module
if(DEFINED VCPKG_ROOT_DIR)
find_package(SuiteSparse CONFIG REQUIRED)
else()
find_package(SuiteSparse REQUIRED)
message("SuiteSparse found in ${SuiteSparse_INCLUDE_DIRS}: ${SuiteSparse_LIBRARIES}")
endif()
include_directories(${SuiteSparse_INCLUDE_DIRS})
target_link_libraries(idaklu PRIVATE ${SuiteSparse_LIBRARIES})
# IREE (MLIR compiler and runtime library) build settings
if(${PYBAMM_IDAKLU_EXPR_IREE} STREQUAL "ON" )
set(IREE_BUILD_COMPILER ON)
set(IREE_BUILD_TESTS OFF)
set(IREE_BUILD_SAMPLES OFF)
add_subdirectory(iree EXCLUDE_FROM_ALL)
set(IREE_COMPILER_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/iree/compiler")
target_include_directories(idaklu SYSTEM PRIVATE "${IREE_COMPILER_ROOT}/bindings/c/iree/compiler")
target_compile_options(idaklu PRIVATE ${IREE_DEFAULT_COPTS})
target_link_libraries(idaklu PRIVATE iree_compiler_bindings_c_loader)
target_link_libraries(idaklu PRIVATE iree_runtime_runtime)
endif()