-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCMakeLists.txt
417 lines (352 loc) · 15 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
cmake_minimum_required(VERSION 3.3)
project(ampls)
set(ampls_VERSION 0.2.2)
set(CMAKE_CXX_STANDARD 98)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Set the path to CMake modules.
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
${CMAKE_CURRENT_SOURCE_DIR}/support/cmake)
set(AMPLS_BASE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
option(BUILD_SWIG_WRAPPERS "Build swig wrappers" ON)
option(BUILD_AMPLS_DOC "Build AMPLS documentation" OFF)
option(BUILD_AMPLS_TESTS "Build cpp tests" OFF)
# Set solvers to build.
function(defSolver name solvername libtargetname)
cmake_parse_arguments(DEFSOLVER "" "" "ADDITIONALINCLUDES" ${ARGN})
list(APPEND AMPLS_SOLVER_NAMES ${name})
set(AMPLS_SOLVER_NAMES ${AMPLS_SOLVER_NAMES} PARENT_SCOPE)
set(${name}_SOLVER ${solvername} PARENT_SCOPE)
set(${name}_TARGET ${libtargetname} PARENT_SCOPE)
set(${name}_ADDITIONALINCLUDES ${DEFSOLVER_ADDITIONALINCLUDES} PARENT_SCOPE)
endfunction()
defSolver(gurobi gurobi gurobi)
defSolver(cplex cplex cplexmp)
defSolver(xpress xpress xpress)
defSolver(cbcmp cbc cbcmp)
defSolver(copt copt copt)
defSolver(scip scip scip)
defSolver(highs highs highs)
set(BUILD_AMPLS "" CACHE STRING "Comma-separated list of solver drivers to build, set to \"all\" to build all")
if (BUILD AND NOT BUILD_AMPLS)
set(BUILD_AMPLS ${BUILD})
endif()
set(ENABLED_SOLVERS "")
if (BUILD_AMPLS)
if (BUILD_AMPLS STREQUAL all)
message(STATUS "Building all solvers")
foreach(s ${AMPLS_SOLVER_NAMES})
set(ENABLE_${s} on)
set(ENABLED_SOLVERS ${ENABLED_SOLVERS} ${s})
endforeach()
else()
string(REGEX MATCHALL "[^,]+" SOLVERS_TOBUILD "${BUILD_AMPLS}")
# Disable all
foreach(s ${AMPLS_SOLVER_NAMES})
set(ENABLE_${s} OFF)
endforeach()
# Enable only the ones specified
foreach(s ${SOLVERS_TOBUILD})
if (NOT ${s} IN_LIST AMPLS_SOLVER_NAMES)
message(WARNING "${s} is not a valid solver library name.")
endif()
set(ENABLE_${s} ON)
set(ENABLED_SOLVERS ${ENABLED_SOLVERS} ${s})
message(STATUS "Building library for solver ${s}")
endforeach()
endif()
else()
message(WARNING "Specify the solvers to build with -DBUILD_AMPLS=solvername1,..,solvernamen or -DBUILD=all")
endif()
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
include(addPrefix) # for add_prefix and add_to_folder
# Set global compiler settings
if(MSVC)
get_directory_property(MYDEFS COMPILE_DEFINITIONS)
if(NOT MYDEFS MATCHES "_CRT_SECURE_NO_WARNINGS")
# Disable useless MSVC warnings suggesting nonportable "secure"
# alternatives.
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
# Disable warning about osx file format
add_compile_options(/wd4335)
endif()
endif()
# Set output directories.
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
# Set paths
set(BASESRCDIR ${CMAKE_CURRENT_SOURCE_DIR}/src/solvers) # Base dir for all
# Swig
if(BUILD_SWIG_WRAPPERS)
find_package(SWIG 3.0)
if(SWIG_FOUND)
if(POLICY CMP0078)
cmake_policy(SET CMP0078 NEW)
endif()
if(POLICY CMP0086)
cmake_policy(SET CMP0086 NEW)
endif()
if(POLICY CMP0122)
cmake_policy(SET CMP0122 NEW)
endif()
include(UseSWIG)
set(BUILD_PYTHON_WRAPPERS ON)
else()
message(WARNING "SWIG not found, Python and C# bindings will not be generated")
set(BUILD_SWIG_WRAPPERS OFF)
set(BUILD_PYTHON_WRAPPERS OFF)
endif()
else()
message(WARNING "SWIG bindings not generated. Set option BUILD_SWIG_WRAPPERS to ON")
endif()
if(BUILD_PYTHON_WRAPPERS)
message("Looking for Python")
# Names and paths
find_package(Python3 COMPONENTS Development)
if (NOT ${Python_Development_FOUND})
message("Python libs not found" FATAL_ERROR)
endif()
message("Python Libraries: ${Python3_LIBRARIES}")
message("Python include headers: ${Python3_INCLUDE_DIRS}")
endif()
set(GENERATED_INCLUDE_DIR ${CMAKE_BINARY_DIR}/include) # For generated files
file(WRITE ${GENERATED_INCLUDE_DIR}/test-config.h
"#ifndef _TEST_CONFIG_\n#define _TEST_CONFIG_\nconst char MODELS_DIR[] = \"${PROJECT_SOURCE_DIR}/test/models/\";\n#endif")
# Solver vendors libraries and headers locations
get_directory_property(hasParent PARENT_DIRECTORY)
if(hasParent)
set(SOLVERLIBS ${BASE_SOLVER_LIBS})
set(gurobi_BASE_DIR ${SOLVERLIBS}/gurobi/1200)
set(cplex_BASE_DIR ${SOLVERLIBS}/cplex/2211)
set(copt_BASE_DIR ${SOLVERLIBS}/copt/7.1.3)
set(xpress_BASE_DIR ${SOLVERLIBS}/xpress/42.01.05)
set(scip_BASE_DIR ${SOLVERLIBS}/scip)
set(highs_BASE_DIR ${SOLVERLIBS}/highs/1.7.1)
else()
set(SOLVERLIBS ${CMAKE_CURRENT_SOURCE_DIR}/libs)
set(gurobi_BASE_DIR ${SOLVERLIBS}/gurobi)
set(cplex_BASE_DIR ${SOLVERLIBS}/cplex)
set(copt_BASE_DIR ${SOLVERLIBS}/copt)
set(xpress_BASE_DIR ${SOLVERLIBS}/xpress)
set(scip_BASE_DIR ${SOLVERLIBS}/scip)
set(highs_BASE_DIR ${SOLVERLIBS}/highs)
endif()
function(CreateSolverLib name includedirs lib)
cmake_parse_arguments(CSLIB "" "IMPORTLIB" "" ${ARGN})
add_library(${name} SHARED IMPORTED)
if(WIN32)
set_property(TARGET ${name} PROPERTY IMPORTED_LOCATION ${lib})
if(NOT CSLIB_IMPORTLIB)
message(ERROR "Import lib not specified")
endif()
set_property(TARGET ${name} PROPERTY IMPORTED_IMPLIB ${CSLIB_IMPORTLIB})
else()
set_property(TARGET ${name} PROPERTY IMPORTED_LOCATION ${lib})
endif()
set_target_properties(${name} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${includedirs})
endfunction()
function(FindSolverLib OUTPUTTARGETNAME BASELIBFILENAME)
if(WIN32)
string(TOLOWER ${BASELIBFILENAME} LIBFILENAME)
string(REPLACE ".dll" ".lib" DLLFILENAME ${LIBFILENAME})
else()
set(LIBFILENAME ${BASELIBFILENAME})
endif()
find_library(LIB_LIBFILE_${OUTPUTTARGETNAME} ${LIBFILENAME} NAMES ${LIBFILENAME}-lib PATHS ${AMPLS_DIR} REQUIRED NO_DEFAULT_PATH)
if(NOT LIB_LIBFILE_${OUTPUTTARGETNAME})
message(FATAL_ERROR "Library ${LIBFILENAME} not found in ${AMPLS_DIR}")
endif()
if(WIN32)
find_file(LIB_DLLFILE_${OUTPUTTARGETNAME} ${BASELIBFILENAME}-lib.dll PATHS ${AMPLS_DIR} REQUIRED NO_DEFAULT_PATH)
endif()
add_library(${OUTPUTTARGETNAME} SHARED IMPORTED)
if(WIN32)
set_property(TARGET ${OUTPUTTARGETNAME} PROPERTY IMPORTED_LOCATION ${LIB_DLLFILE_${OUTPUTTARGETNAME}})
set_property(TARGET ${OUTPUTTARGETNAME} PROPERTY IMPORTED_IMPLIB ${LIB_LIBFILE_${OUTPUTTARGETNAME}})
else()
set_property(TARGET ${OUTPUTTARGETNAME} PROPERTY IMPORTED_LOCATION ${LIB_LIBFILE_${OUTPUTTARGETNAME}})
endif()
endfunction()
set(GRBINC ${gurobi_BASE_DIR}/include)
if(hasParent)
set(GRBINC ${GRBINC}/gurobi)
endif()
if(WIN32)
set(sys "lib/win64")
CreateSolverLib(gurobi-solverlib ${GRBINC}
${gurobi_BASE_DIR}/${sys}/gurobi120.dll
IMPORTLIB ${gurobi_BASE_DIR}/${sys}/gurobi120.lib)
CreateSolverLib(cplex-solverlib ${cplex_BASE_DIR}/include
${cplex_BASE_DIR}/${sys}/cplex2211.dll
IMPORTLIB ${cplex_BASE_DIR}/${sys}/cplex2211.lib)
CreateSolverLib(copt-solverlib ${copt_BASE_DIR}/include
${copt_BASE_DIR}/${sys}/copt.dll
IMPORTLIB ${copt_BASE_DIR}/${sys}/copt.lib)
CreateSolverLib(xpress-solverlib ${xpress_BASE_DIR}/include
${xpress_BASE_DIR}/${sys}/xprs.dll
IMPORTLIB ${xpress_BASE_DIR}/${sys}/xprs.lib)
CreateSolverLib(scip-solverlib ${scip_BASE_DIR}/include
${scip_BASE_DIR}/${sys}/libscip.dll
IMPORTLIB ${scip_BASE_DIR}/${sys}/libscip.lib)
CreateSolverLib(highs-solverlib ${highs_BASE_DIR}/include
${highs_BASE_DIR}/${sys}/highs.dll
IMPORTLIB ${highs_BASE_DIR}/${sys}/highs.lib)
set(cbc-solver-deps "")
set(d debug/)
#set(d "/")
foreach(lib libCbcSolver libCbc libCgl libClp libCoinUtils libOsi libOsiCbc libOsiClp)
CreateSolverLib(cbc-${lib} ${cbc_BASE_DIR}/include
${cbc_BASE_DIR}/${sys}/${d}${lib}.lib
IMPORTLIB ${cbc_BASE_DIR}/${sys}/${d}${lib}.lib)
set(cbc-solver-deps ${cbc-solver-deps} cbc-${lib})
endforeach()
set(AMPLS_DIR ${SOLVERLIBS}/ampls/win64) # AMPL base driver library locations
elseif(APPLE)
set(sys "lib/osx64")
CreateSolverLib(gurobi-solverlib ${GRBINC}
${gurobi_BASE_DIR}/${sys}/libgurobi100.dylib)
CreateSolverLib(cplex-solverlib ${cplex_BASE_DIR}/include
${cplex_BASE_DIR}/${sys}/libcplex2211.dylib)
CreateSolverLib(copt-solverlib ${copt_BASE_DIR}/include
${copt_BASE_DIR}/${sys}/libcopt.dylib)
CreateSolverLib(xpress-solverlib ${xpress_BASE_DIR}/include
${xpress_BASE_DIR}/${sys}/libxprs.dylib)
CreateSolverLib(scip-solverlib ${scip_BASE_DIR}/include
${scip_BASE_DIR}/${sys}/libscip.8.0.3.0.dylib)
CreateSolverLib(highs-solverlib ${highs_BASE_DIR}/include
${highs_BASE_DIR}/${sys}/libhighs.dylib)
set(AMPLS_DIR ${SOLVERLIBS}/ampls/osx64) # AMPL base driver library locations
else()
set(sys "lib/linux64")
CreateSolverLib(gurobi-solverlib ${GRBINC}
${gurobi_BASE_DIR}/${sys}/libgurobi100.so)
CreateSolverLib(cplex-solverlib ${cplex_BASE_DIR}/include
${cplex_BASE_DIR}/${sys}/libcplex2211.so)
CreateSolverLib(copt-solverlib ${copt_BASE_DIR}/include
${copt_BASE_DIR}/${sys}/libcopt.so)
CreateSolverLib(xpress-solverlib ${xpress_BASE_DIR}/include
${xpress_BASE_DIR}/${sys}/libxprs.so.43)
CreateSolverLib(scip-solverlib ${scip_BASE_DIR}/include
${scip_BASE_DIR}/${sys}/libscip.8.0.3.0.so)
CreateSolverLib(highs-solverlib ${highs_BASE_DIR}/include
${copt_BASE_DIR}/${sys}/libhighs.so)
set(AMPLS_DIR ${SOLVERLIBS}/ampls/linux64) # AMPL base driver library locations
endif()
set(gurobi-solver-deps gurobi-solverlib)
set(cplex-solver-deps cplex-solverlib)
set(copt-solver-deps copt-solverlib)
set(xpress-solver-deps xpress-solverlib)
set(cbc-solver-deps Coin::CbcSolver)
set(scip-solver-deps scip-solverlib)
set(highs-solver-deps highs-solverlib)
# Add ampls (generic) project
add_subdirectory(cpp/ampls)
function(AddSolverStub solver targetbase libname)
cmake_parse_arguments(ADDSOLVER "" "LIBS" "ADDITIONALINCLUDES" ${ARGN})
set(TARGETNAME ${libname}-drv)
set(DIR_CPP ${CMAKE_CURRENT_SOURCE_DIR}/cpp/${libname})
set(DIR_CPP_SRC ${CMAKE_CURRENT_SOURCE_DIR}/cpp/${libname}/src)
set(DIR_CPP_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR}/cpp/${libname}/include)
set(DIR_SWIG ${CMAKE_CURRENT_SOURCE_DIR}/cpp/${libname}/swig)
set(DIR_PYTHON ${CMAKE_CURRENT_SOURCE_DIR}/python/${libname})
set(DIR_CSHARP ${CMAKE_CURRENT_SOURCE_DIR}/csharp/${libname})
# On windows I find both import lib and dll file on other systems
# i'll need just one
set(IMPORTLIB ${targetbase}-lib)
if(NOT TARGET ${IMPORTLIB})
set(IMPORTLIB ${targetbase}mp-lib)
if(NOT TARGET ${IMPORTLIB})
set(IMPORTLIB ${targetbase}-lib)
FindSolverLib(${IMPORTLIB} ${targetbase})
endif()
endif()
add_prefix(
DRV_SOURCES ${DIR_CPP}/ include/${libname}_interface.h
src/${libname}_interface.cpp include/${libname}_callback.h
src/${libname}_callback.cpp)
add_library(${TARGETNAME} STATIC ${DRV_SOURCES})
target_link_libraries(${TARGETNAME} PUBLIC
ampls
${ADDSOLVER_LIBS}
${IMPORTLIB})
target_include_directories(${TARGETNAME} PUBLIC
${DIR_CPP_INCLUDE}
${ampls_INCLUDE}
${ADDSOLVER_ADDITIONALINCLUDES})
add_to_folder(${targetbase} ${TARGETNAME})
if(NOT WIN32)
target_compile_options(${TARGETNAME} PRIVATE -fPIC)
endif()
set(EXAMPLES loadModel getInformation simpleCuts genericbranch-cplex genericbranch-ampls)
foreach(exname ${EXAMPLES})
set(tar ${libname}-${exname})
set(src ${DIR_CPP}/examples/${exname}.cpp)
if(EXISTS ${src})
add_executable(${tar} ${src})
target_include_directories(${tar} PRIVATE
${GENERATED_INCLUDE_DIR}) # for test-config
target_link_libraries(${tar} ampls ${TARGETNAME})
add_to_folder(${targetbase}/examples ${tar})
endif()
endforeach()
# Tests
add_prefix(TEST_SOURCES ${DIR_CPP}/test/ ${libname}-test.cpp)
add_executable(${TARGETNAME}-test ${TEST_SOURCES})
target_link_libraries(${TARGETNAME}-test ${TARGETNAME})
target_include_directories(
${TARGETNAME}-test
PRIVATE ${GENERATED_INCLUDE_DIR} # for test-config
${DIR_CPP_INCLUDE} ${ampls_INCLUDE} ${includes})
add_to_folder(${targetbase} ${TARGETNAME}-test)
target_compile_definitions(${TARGETNAME}-test PRIVATE SWIG)
# Copy runtime dependencies to binary directory
foreach(s ${${TARGETNAME}_LIBRARY} $<TARGET_FILE:${IMPORTLIB}>)
add_custom_command(
TARGET ${TARGETNAME}-test POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${s}"
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/"
COMMENT "Copying ${solvername}-lib to python directory")
endforeach()
if(BUILD_SWIG_WRAPPERS)
if (EXISTS ${DIR_SWIG})
# Add a custom target just to show the following common sources in some IDEs
add_custom_target(${solver}-common SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/cpp/ampls/swig/ampls-common.i
${DIR_SWIG}/${libname}-common.i
${CMAKE_CURRENT_SOURCE_DIR}/python/common-python.i
${CMAKE_CURRENT_SOURCE_DIR}/python/exceptions-python.i
${CMAKE_CURRENT_SOURCE_DIR}/python/common-python-extensions.i
${CMAKE_CURRENT_SOURCE_DIR}/python/createPythonWrapper.cmake
${CMAKE_CURRENT_SOURCE_DIR}/csharp/createCsharpProject.cmake
${CMAKE_CURRENT_SOURCE_DIR}/csharp/common-csharp.i
${CMAKE_CURRENT_SOURCE_DIR}/python/common-python-overrides.i
${CMAKE_CURRENT_SOURCE_DIR}/csharp/exceptions-csharp.i
)
add_to_folder(${targetbase}/swig ${solver}-common)
add_subdirectory(${DIR_PYTHON})
if(WIN32)
# add_subdirectory(${DIR_CSHARP})
endif()
endif()
endif()
endfunction()
# Add the enabled modules
foreach(s ${AMPLS_SOLVER_NAMES})
if(ENABLE_${s})
set(SOLVERNAME ${${s}_SOLVER})
addsolverstub(${SOLVERNAME} ${${s}_TARGET} ${s} LIBS ${${SOLVERNAME}-solver-deps}
ADDITIONALINCLUDES ${${s}_ADDITIONALINCLUDES})
endif()
endforeach()
# Add ampls swig bindings (must do after the targets below, as )
if(BUILD_SWIG_WRAPPERS AND SWIG_FOUND)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/python/ampls)
if(WIN32)
#add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/csharp/ampls)
endif()
endif()
# Add the documentation, check for the required programs is implemented
# in the inner CMake file
if(BUILD_AMPLS_DOC)
add_subdirectory("doc")
endif()