-
Notifications
You must be signed in to change notification settings - Fork 8
/
CMakeLists.txt
316 lines (255 loc) · 7.62 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
cmake_minimum_required(VERSION 3.15...3.27)
project(pyoptinterface)
set(CMAKE_CXX_STANDARD 20)
# Linux: -fPIC
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# we need to ensure all shared libraries can look up its own directory to load other shared libraries
# this is very important for CppAD users because we extract its thread_alloc as a shared library
function(set_rpath target)
if(APPLE)
set(CMAKE_MACOSX_RPATH 1)
set_target_properties(${target} PROPERTIES INSTALL_RPATH "@loader_path")
elseif(UNIX)
set_target_properties(${target} PROPERTIES INSTALL_RPATH "$ORIGIN")
elseif(WIN32)
endif()
endfunction()
if(MSVC)
# Add /MP flag for multi-processor compilation
add_compile_options(/MP)
add_compile_options(/utf-8)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64")
# use AVX2
add_compile_options(/arch:AVX2)
endif()
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
# add_compile_options(-march=haswell)
endif()
endif()
add_subdirectory(thirdparty/fmt)
add_subdirectory(thirdparty/cppad)
set(POI_INSTALL_DIR ${SKBUILD_PLATLIB_DIR}/pyoptinterface/_src)
add_library(core STATIC)
target_sources(core PRIVATE
include/pyoptinterface/core.hpp
include/pyoptinterface/container.hpp
include/pyoptinterface/dylib.hpp
include/pyoptinterface/solver_common.hpp
lib/core.cpp
lib/cache_model.cpp
)
target_include_directories(core PUBLIC include thirdparty)
target_link_libraries(core PUBLIC fmt)
add_library(nlexpr STATIC)
target_sources(nlexpr PRIVATE
include/pyoptinterface/nlexpr.hpp
lib/nlexpr.cpp
)
target_include_directories(nlexpr PUBLIC include thirdparty)
target_link_libraries(nlexpr PUBLIC fmt)
add_library(nleval STATIC)
target_sources(nleval PRIVATE
include/pyoptinterface/nleval.hpp
lib/nleval.cpp
)
target_link_libraries(nleval PUBLIC core)
add_library(cppad_interface STATIC)
target_sources(cppad_interface PRIVATE
include/pyoptinterface/cppad_interface.hpp
lib/cppad_interface.cpp
)
target_include_directories(cppad_interface PUBLIC include thirdparty)
target_link_libraries(cppad_interface PUBLIC nlexpr cppad)
add_library(tcc_interface STATIC)
target_sources(tcc_interface PRIVATE
include/pyoptinterface/tcc_interface.hpp
lib/tcc_interface.cpp
)
target_include_directories(tcc_interface PUBLIC include thirdparty)
target_link_libraries(tcc_interface PUBLIC fmt)
# Build Python extensions
find_package(Python ${PYTHON_VERSION}
REQUIRED COMPONENTS Interpreter Development.Module
OPTIONAL_COMPONENTS Development.SABIModule)
# Import nanobind through CMake's find_package mechanism
find_package(nanobind CONFIG REQUIRED)
nanobind_add_module(
core_ext
STABLE_ABI NB_STATIC
lib/core_ext.cpp
)
target_link_libraries(core_ext PUBLIC core)
install(TARGETS core_ext LIBRARY DESTINATION ${POI_INSTALL_DIR})
nanobind_add_module(
nlexpr_ext
STABLE_ABI NB_STATIC
lib/nlexpr_ext.cpp
)
target_link_libraries(nlexpr_ext PUBLIC nlexpr)
install(TARGETS nlexpr_ext LIBRARY DESTINATION ${POI_INSTALL_DIR})
nanobind_add_module(
nleval_ext
STABLE_ABI NB_STATIC
lib/nleval_ext.cpp
)
target_link_libraries(nleval_ext PUBLIC nleval)
install(TARGETS nleval_ext LIBRARY DESTINATION ${POI_INSTALL_DIR})
nanobind_add_module(
cppad_interface_ext
STABLE_ABI NB_STATIC
lib/cppad_interface_ext.cpp
)
target_link_libraries(cppad_interface_ext PUBLIC cppad_interface)
install(TARGETS cppad_interface_ext LIBRARY DESTINATION ${POI_INSTALL_DIR})
nanobind_add_module(
tcc_interface_ext
STABLE_ABI NB_STATIC
lib/tcc_interface_ext.cpp
)
target_link_libraries(tcc_interface_ext PUBLIC tcc_interface)
install(TARGETS tcc_interface_ext LIBRARY DESTINATION ${POI_INSTALL_DIR})
# Solvers
# Gurobi
add_library(gurobi_model STATIC)
target_sources(gurobi_model PRIVATE
include/pyoptinterface/gurobi_model.hpp
lib/gurobi_model.cpp
)
target_link_libraries(gurobi_model PUBLIC core)
nanobind_add_module(
gurobi_model_ext
STABLE_ABI NB_STATIC
lib/gurobi_model_ext.cpp
lib/gurobi_model_ext_constants.cpp
)
target_link_libraries(gurobi_model_ext PUBLIC gurobi_model)
install(TARGETS gurobi_model_ext LIBRARY DESTINATION ${POI_INSTALL_DIR})
# COPT
add_library(copt_model STATIC)
target_sources(copt_model PRIVATE
include/pyoptinterface/copt_model.hpp
lib/copt_model.cpp
)
target_link_libraries(copt_model PUBLIC core)
nanobind_add_module(
copt_model_ext
STABLE_ABI NB_STATIC
lib/copt_model_ext.cpp
lib/copt_model_ext_constants.cpp
)
target_link_libraries(copt_model_ext PUBLIC copt_model)
install(TARGETS copt_model_ext LIBRARY DESTINATION ${POI_INSTALL_DIR})
# MOSEK
add_library(mosek_model STATIC)
target_sources(mosek_model PRIVATE
include/pyoptinterface/mosek_model.hpp
lib/mosek_model.cpp
)
target_link_libraries(mosek_model PUBLIC core)
nanobind_add_module(
mosek_model_ext
STABLE_ABI NB_STATIC
lib/mosek_model_ext.cpp
lib/mosek_model_ext_constants.cpp
)
target_link_libraries(mosek_model_ext PUBLIC mosek_model)
install(TARGETS mosek_model_ext LIBRARY DESTINATION ${POI_INSTALL_DIR})
# HiGHS
add_library(highs_model STATIC)
target_sources(highs_model PRIVATE
include/pyoptinterface/highs_model.hpp
lib/highs_model.cpp
)
target_include_directories(highs_model PUBLIC thirdparty/solvers/highs)
target_link_libraries(highs_model PUBLIC core)
# target_link_libraries(highs_model PUBLIC HiGHS::HiGHS)
nanobind_add_module(
highs_model_ext
STABLE_ABI NB_STATIC
lib/highs_model_ext.cpp
lib/highs_model_ext_constants.cpp
)
target_link_libraries(highs_model_ext PUBLIC highs_model)
install(TARGETS highs_model_ext LIBRARY DESTINATION ${POI_INSTALL_DIR})
# IPOPT
add_library(ipopt_model STATIC)
target_sources(ipopt_model PRIVATE
include/pyoptinterface/ipopt_model.hpp
lib/ipopt_model.cpp
)
target_link_libraries(ipopt_model PUBLIC nleval)
nanobind_add_module(
ipopt_model_ext
STABLE_ABI NB_STATIC
lib/ipopt_model_ext.cpp
)
target_link_libraries(ipopt_model_ext PUBLIC ipopt_model)
install(TARGETS ipopt_model_ext LIBRARY DESTINATION ${POI_INSTALL_DIR})
# stub
nanobind_add_stub(
core_ext_stub
INSTALL_TIME
MODULE pyoptinterface._src.core_ext
OUTPUT ${POI_INSTALL_DIR}/core_ext.pyi
)
nanobind_add_stub(
nlexpr_ext_stub
INSTALL_TIME
MODULE pyoptinterface._src.nlexpr_ext
OUTPUT ${POI_INSTALL_DIR}/nlexpr_ext.pyi
)
nanobind_add_stub(
nleval_ext_stub
INSTALL_TIME
MODULE pyoptinterface._src.nleval_ext
OUTPUT ${POI_INSTALL_DIR}/nleval_ext.pyi
)
nanobind_add_stub(
cppad_interface_ext_stub
INSTALL_TIME
MODULE pyoptinterface._src.cppad_interface_ext
OUTPUT ${POI_INSTALL_DIR}/cppad_interface_ext.pyi
)
nanobind_add_stub(
tcc_interface_ext_stub
INSTALL_TIME
MODULE pyoptinterface._src.tcc_interface_ext
OUTPUT ${POI_INSTALL_DIR}/tcc_interface_ext.pyi
)
nanobind_add_stub(
gurobi_model_ext_stub
INSTALL_TIME
MODULE pyoptinterface._src.gurobi_model_ext
OUTPUT ${POI_INSTALL_DIR}/gurobi_model_ext.pyi
)
nanobind_add_stub(
copt_model_ext_stub
INSTALL_TIME
MODULE pyoptinterface._src.copt_model_ext
OUTPUT ${POI_INSTALL_DIR}/copt_model_ext.pyi
)
nanobind_add_stub(
mosek_model_ext_stub
INSTALL_TIME
MODULE pyoptinterface._src.mosek_model_ext
OUTPUT ${POI_INSTALL_DIR}/mosek_model_ext.pyi
)
nanobind_add_stub(
highs_model_ext_stub
INSTALL_TIME
MODULE pyoptinterface._src.highs_model_ext
OUTPUT ${POI_INSTALL_DIR}/highs_model_ext.pyi
)
nanobind_add_stub(
ipopt_model_ext_stub
INSTALL_TIME
MODULE pyoptinterface._src.ipopt_model_ext
OUTPUT ${POI_INSTALL_DIR}/ipopt_model_ext.pyi
)
set(ENABLE_TEST_MAIN OFF CACHE BOOL "Enable test c++ function with a main.cpp")
if(ENABLE_TEST_MAIN)
add_executable(test_main lib/main.cpp)
target_link_libraries(test_main PUBLIC core gurobi_model copt_model mosek_model highs_model)
endif()