-
Notifications
You must be signed in to change notification settings - Fork 8
/
CMakeLists.txt
251 lines (204 loc) · 6.04 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
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)
# install the DLL of cppad
if(MSVC)
install(TARGETS cppad_thread_alloc RUNTIME DESTINATION ${POI_INSTALL_DIR})
else()
install(TARGETS cppad_thread_alloc LIBRARY DESTINATION ${POI_INSTALL_DIR})
endif()
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(nlcore STATIC)
target_sources(nlcore PRIVATE
include/pyoptinterface/nlcore.hpp
lib/nlcore.cpp
)
target_link_libraries(nlcore PUBLIC core cppad)
# 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(
nlcore_ext
STABLE_ABI NB_STATIC
lib/nlcore_ext.cpp
)
target_link_libraries(nlcore_ext PUBLIC nlcore)
install(TARGETS nlcore_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 nlcore)
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})
# Set RPATH
set_rpath(nlcore_ext)
set_rpath(ipopt_model_ext)
# stub
nanobind_add_stub(
core_ext_stub
INSTALL_TIME
MODULE pyoptinterface._src.core_ext
OUTPUT ${POI_INSTALL_DIR}/core_ext.pyi
)
nanobind_add_stub(
nlcore_ext_stub
INSTALL_TIME
MODULE pyoptinterface._src.nlcore_ext
OUTPUT ${POI_INSTALL_DIR}/nlcore_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()