forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
python.cmake
475 lines (441 loc) · 16.7 KB
/
python.cmake
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# Copyright 2010-2022 Google LLC
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
if(NOT BUILD_PYTHON)
return()
endif()
# Use latest UseSWIG module (3.14) and Python3 module (3.18)
cmake_minimum_required(VERSION 3.18)
if(NOT TARGET ${PROJECT_NAMESPACE}::ortools)
message(FATAL_ERROR "Python: missing ortools TARGET")
endif()
# Will need swig
set(CMAKE_SWIG_FLAGS)
find_package(SWIG REQUIRED)
include(UseSWIG)
if(${SWIG_VERSION} VERSION_GREATER_EQUAL 4)
list(APPEND CMAKE_SWIG_FLAGS "-doxygen")
endif()
if(UNIX AND NOT APPLE)
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
list(APPEND CMAKE_SWIG_FLAGS "-DSWIGWORDSIZE64")
else()
list(APPEND CMAKE_SWIG_FLAGS "-DSWIGWORDSIZE32")
endif()
endif()
# Find Python 3
find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Module)
list(APPEND CMAKE_SWIG_FLAGS "-py3" "-DPY3")
# Find if the python module is available,
# otherwise install it (PACKAGE_NAME) to the Python3 user install directory.
# If CMake option FETCH_PYTHON_DEPS is OFF then issue a fatal error instead.
# e.g
# search_python_module(
# NAME
# mypy_protobuf
# PACKAGE
# mypy-protobuf
# NO_VERSION
# )
function(search_python_module)
set(options NO_VERSION)
set(oneValueArgs NAME PACKAGE)
set(multiValueArgs "")
cmake_parse_arguments(MODULE
"${options}"
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)
message(STATUS "Searching python module: \"${MODULE_NAME}\"")
if(${MODULE_NO_VERSION})
execute_process(
COMMAND ${Python3_EXECUTABLE} -c "import ${MODULE_NAME}"
RESULT_VARIABLE _RESULT
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
set(MODULE_VERSION "unknown")
else()
execute_process(
COMMAND ${Python3_EXECUTABLE} -c "import ${MODULE_NAME}; print(${MODULE_NAME}.__version__)"
RESULT_VARIABLE _RESULT
OUTPUT_VARIABLE MODULE_VERSION
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
endif()
if(${_RESULT} STREQUAL "0")
message(STATUS "Found python module: \"${MODULE_NAME}\" (found version \"${MODULE_VERSION}\")")
else()
if(FETCH_PYTHON_DEPS)
message(WARNING "Can't find python module: \"${MODULE_NAME}\", install it using pip...")
execute_process(
COMMAND ${Python3_EXECUTABLE} -m pip install --user ${MODULE_PACKAGE}
OUTPUT_STRIP_TRAILING_WHITESPACE
)
else()
message(FATAL_ERROR "Can't find python module: \"${MODULE_NAME}\", please install it using your system package manager.")
endif()
endif()
endfunction()
# Find if a python builtin module is available.
# e.g
# search_python_internal_module(
# NAME
# mypy_protobuf
# )
function(search_python_internal_module)
set(options "")
set(oneValueArgs NAME)
set(multiValueArgs "")
cmake_parse_arguments(MODULE
"${options}"
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)
message(STATUS "Searching python module: \"${MODULE_NAME}\"")
execute_process(
COMMAND ${Python3_EXECUTABLE} -c "import ${MODULE_NAME}"
RESULT_VARIABLE _RESULT
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(${_RESULT} STREQUAL "0")
message(STATUS "Found python internal module: \"${MODULE_NAME}\"")
else()
message(FATAL_ERROR "Can't find python internal module \"${MODULE_NAME}\", please install it using your system package manager.")
endif()
endfunction()
##################
## PROTO FILE ##
##################
# Generate Protobuf py sources with mypy support
search_python_module(
NAME mypy_protobuf
PACKAGE mypy-protobuf
NO_VERSION)
set(PROTO_PYS)
file(GLOB_RECURSE proto_py_files RELATIVE ${PROJECT_SOURCE_DIR}
"ortools/bop/*.proto"
"ortools/constraint_solver/*.proto"
"ortools/glop/*.proto"
"ortools/graph/*.proto"
"ortools/linear_solver/*.proto"
"ortools/packing/*.proto"
"ortools/sat/*.proto"
"ortools/scheduling/*.proto"
"ortools/util/*.proto"
)
if(USE_PDLP)
file(GLOB_RECURSE pdlp_proto_py_files RELATIVE ${PROJECT_SOURCE_DIR} "ortools/pdlp/*.proto")
list(APPEND proto_py_files ${pdlp_proto_py_files})
endif()
list(REMOVE_ITEM proto_py_files "ortools/constraint_solver/demon_profiler.proto")
foreach(PROTO_FILE IN LISTS proto_py_files)
#message(STATUS "protoc proto(py): ${PROTO_FILE}")
get_filename_component(PROTO_DIR ${PROTO_FILE} DIRECTORY)
get_filename_component(PROTO_NAME ${PROTO_FILE} NAME_WE)
set(PROTO_PY ${PROJECT_BINARY_DIR}/python/${PROTO_DIR}/${PROTO_NAME}_pb2.py)
#message(STATUS "protoc py: ${PROTO_PY}")
add_custom_command(
OUTPUT ${PROTO_PY}
COMMAND ${PROTOC_PRG}
"--proto_path=${PROJECT_SOURCE_DIR}"
${PROTO_DIRS}
"--python_out=${PROJECT_BINARY_DIR}/python"
"--mypy_out=${PROJECT_BINARY_DIR}/python"
${PROTO_FILE}
DEPENDS ${PROTO_FILE} ${PROTOC_PRG}
COMMENT "Generate Python 3 protocol buffer for ${PROTO_FILE}"
VERBATIM)
list(APPEND PROTO_PYS ${PROTO_PY})
endforeach()
add_custom_target(Py${PROJECT_NAME}_proto
DEPENDS
${PROTO_PYS}
${PROJECT_NAMESPACE}::ortools)
###################
## Python Test ##
###################
if(BUILD_VENV)
search_python_module(NAME virtualenv PACKAGE virtualenv)
# venv not working on github runners
# search_python_internal_module(NAME venv)
# Testing using a vitual environment
set(VENV_EXECUTABLE ${Python3_EXECUTABLE} -m virtualenv)
#set(VENV_EXECUTABLE ${Python3_EXECUTABLE} -m venv)
set(VENV_DIR ${CMAKE_CURRENT_BINARY_DIR}/python/venv)
if(WIN32)
set(VENV_Python3_EXECUTABLE ${VENV_DIR}/Scripts/python.exe)
else()
set(VENV_Python3_EXECUTABLE ${VENV_DIR}/bin/python)
endif()
endif()
if(BUILD_TESTING)
# add_python_test()
# CMake function to generate and build python test.
# Parameters:
# the python filename
# e.g.:
# add_python_test(foo.py)
function(add_python_test FILE_NAME)
message(STATUS "Configuring test ${FILE_NAME} ...")
get_filename_component(TEST_NAME ${FILE_NAME} NAME_WE)
get_filename_component(WRAPPER_DIR ${FILE_NAME} DIRECTORY)
get_filename_component(COMPONENT_DIR ${WRAPPER_DIR} DIRECTORY)
get_filename_component(COMPONENT_NAME ${COMPONENT_DIR} NAME)
add_test(
NAME python_${COMPONENT_NAME}_${TEST_NAME}
COMMAND ${VENV_Python3_EXECUTABLE} -m pytest ${FILE_NAME}
WORKING_DIRECTORY ${VENV_DIR})
message(STATUS "Configuring test ${FILE_NAME} done")
endfunction()
endif()
#######################
## PYTHON WRAPPERS ##
#######################
list(APPEND CMAKE_SWIG_FLAGS "-I${PROJECT_SOURCE_DIR}")
set(PYTHON_PROJECT ${PROJECT_NAME})
message(STATUS "Python project: ${PYTHON_PROJECT}")
set(PYTHON_PROJECT_DIR ${PROJECT_BINARY_DIR}/python/${PYTHON_PROJECT})
message(STATUS "Python project build path: ${PYTHON_PROJECT_DIR}")
# Swig wrap all libraries
foreach(SUBPROJECT IN ITEMS init algorithms graph linear_solver constraint_solver pdlp sat scheduling util)
add_subdirectory(ortools/${SUBPROJECT}/python)
endforeach()
#######################
## Python Packaging ##
#######################
#file(MAKE_DIRECTORY python/${PYTHON_PROJECT})
configure_file(
${PROJECT_SOURCE_DIR}/ortools/python/__init__.py.in
${PROJECT_BINARY_DIR}/python/__init__.py.in
@ONLY)
file(GENERATE
OUTPUT ${PYTHON_PROJECT_DIR}/__init__.py
INPUT ${PROJECT_BINARY_DIR}/python/__init__.py.in)
file(GENERATE OUTPUT ${PYTHON_PROJECT_DIR}/algorithms/__init__.py CONTENT "")
file(GENERATE OUTPUT ${PYTHON_PROJECT_DIR}/bop/__init__.py CONTENT "")
file(GENERATE OUTPUT ${PYTHON_PROJECT_DIR}/constraint_solver/__init__.py CONTENT "")
file(GENERATE OUTPUT ${PYTHON_PROJECT_DIR}/glop/__init__.py CONTENT "")
file(GENERATE OUTPUT ${PYTHON_PROJECT_DIR}/graph/__init__.py CONTENT "")
file(GENERATE OUTPUT ${PYTHON_PROJECT_DIR}/graph/python/__init__.py CONTENT "")
file(GENERATE OUTPUT ${PYTHON_PROJECT_DIR}/init/__init__.py CONTENT "")
file(GENERATE OUTPUT ${PYTHON_PROJECT_DIR}/linear_solver/__init__.py CONTENT "")
file(GENERATE OUTPUT ${PYTHON_PROJECT_DIR}/linear_solver/python/__init__.py CONTENT "")
file(GENERATE OUTPUT ${PYTHON_PROJECT_DIR}/packing/__init__.py CONTENT "")
file(GENERATE OUTPUT ${PYTHON_PROJECT_DIR}/pdlp/__init__.py CONTENT "")
file(GENERATE OUTPUT ${PYTHON_PROJECT_DIR}/pdlp/python/__init__.py CONTENT "")
file(GENERATE OUTPUT ${PYTHON_PROJECT_DIR}/sat/__init__.py CONTENT "")
file(GENERATE OUTPUT ${PYTHON_PROJECT_DIR}/sat/python/__init__.py CONTENT "")
file(GENERATE OUTPUT ${PYTHON_PROJECT_DIR}/sat/colab/__init__.py CONTENT "")
file(GENERATE OUTPUT ${PYTHON_PROJECT_DIR}/scheduling/__init__.py CONTENT "")
file(GENERATE OUTPUT ${PYTHON_PROJECT_DIR}/util/__init__.py CONTENT "")
file(GENERATE OUTPUT ${PYTHON_PROJECT_DIR}/util/python/__init__.py CONTENT "")
file(COPY
ortools/linear_solver/linear_solver_natural_api.py
DESTINATION ${PYTHON_PROJECT_DIR}/linear_solver)
file(COPY
ortools/linear_solver/python/model_builder.py
ortools/linear_solver/python/model_builder_helper.py
DESTINATION ${PYTHON_PROJECT_DIR}/linear_solver/python)
file(COPY
ortools/sat/python/cp_model.py
ortools/sat/python/cp_model_helper.py
DESTINATION ${PYTHON_PROJECT_DIR}/sat/python)
file(COPY
ortools/sat/colab/visualization.py
DESTINATION ${PYTHON_PROJECT_DIR}/sat/colab)
# setup.py.in contains cmake variable e.g. @PYTHON_PROJECT@ and
# generator expression e.g. $<TARGET_FILE_NAME:pyFoo>
configure_file(
${PROJECT_SOURCE_DIR}/ortools/python/setup.py.in
${PROJECT_BINARY_DIR}/python/setup.py.in
@ONLY)
file(GENERATE
OUTPUT ${PROJECT_BINARY_DIR}/python/setup.py
INPUT ${PROJECT_BINARY_DIR}/python/setup.py.in)
#add_custom_command(
# OUTPUT python/setup.py
# DEPENDS ${PROJECT_BINARY_DIR}/python/setup.py
# COMMAND ${CMAKE_COMMAND} -E copy setup.py setup.py
# WORKING_DIRECTORY python)
configure_file(
${PROJECT_SOURCE_DIR}/ortools/python/README.pypi.txt
${PROJECT_BINARY_DIR}/python/README.txt
COPYONLY)
# Look for required python modules
search_python_module(
NAME setuptools
PACKAGE setuptools)
search_python_module(
NAME wheel
PACKAGE wheel)
add_custom_command(
OUTPUT python/dist/timestamp
COMMAND ${CMAKE_COMMAND} -E remove_directory dist
COMMAND ${CMAKE_COMMAND} -E make_directory ${PYTHON_PROJECT}/.libs
# Don't need to copy static lib on Windows.
COMMAND ${CMAKE_COMMAND} -E $<IF:$<STREQUAL:$<TARGET_PROPERTY:ortools,TYPE>,SHARED_LIBRARY>,copy,true>
$<$<STREQUAL:$<TARGET_PROPERTY:ortools,TYPE>,SHARED_LIBRARY>:$<TARGET_SONAME_FILE:ortools>>
${PYTHON_PROJECT}/.libs
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:pywrapinit> ${PYTHON_PROJECT}/init
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:pywrapknapsack_solver> ${PYTHON_PROJECT}/algorithms
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:linear_sum_assignment_pybind11> ${PYTHON_PROJECT}/graph/python
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:max_flow_pybind11> ${PYTHON_PROJECT}/graph/python
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:min_cost_flow_pybind11> ${PYTHON_PROJECT}/graph/python
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:pywrapcp> ${PYTHON_PROJECT}/constraint_solver
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:pywraplp> ${PYTHON_PROJECT}/linear_solver
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:pywrap_model_builder_helper> ${PYTHON_PROJECT}/linear_solver/python
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:pywrap_pdlp_pybind11> ${PYTHON_PROJECT}/pdlp/python
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:swig_helper> ${PYTHON_PROJECT}/sat/python
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:pywraprcpsp> ${PYTHON_PROJECT}/scheduling
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:sorted_interval_list> ${PYTHON_PROJECT}/util/python
#COMMAND ${Python3_EXECUTABLE} setup.py bdist_egg bdist_wheel
COMMAND ${Python3_EXECUTABLE} setup.py bdist_wheel
COMMAND ${CMAKE_COMMAND} -E touch ${PROJECT_BINARY_DIR}/python/dist/timestamp
MAIN_DEPENDENCY
ortools/python/setup.py.in
DEPENDS
python/setup.py
Py${PROJECT_NAME}_proto
${PROJECT_NAMESPACE}::ortools
pywrapinit
pywrapknapsack_solver
linear_sum_assignment_pybind11
max_flow_pybind11
min_cost_flow_pybind11
pywrapcp
pywraplp
pywrap_model_builder_helper
pywrap_pdlp_pybind11
swig_helper
pywraprcpsp
sorted_interval_list
BYPRODUCTS
python/${PYTHON_PROJECT}
python/${PYTHON_PROJECT}.egg-info
python/build
python/dist
WORKING_DIRECTORY python
COMMAND_EXPAND_LISTS)
# Main Target
add_custom_target(python_package ALL
DEPENDS
python/dist/timestamp
WORKING_DIRECTORY python)
# Install rules
configure_file(
${PROJECT_SOURCE_DIR}/cmake/python-install.cmake.in
${PROJECT_BINARY_DIR}/python/python-install.cmake
@ONLY)
install(SCRIPT ${PROJECT_BINARY_DIR}/python/python-install.cmake)
if(BUILD_VENV)
# make a virtualenv to install our python package in it
add_custom_command(TARGET python_package POST_BUILD
# Clean previous install otherwise pip install may do nothing
COMMAND ${CMAKE_COMMAND} -E remove_directory ${VENV_DIR}
COMMAND ${VENV_EXECUTABLE} -p ${Python3_EXECUTABLE}
$<IF:$<BOOL:${VENV_USE_SYSTEM_SITE_PACKAGES}>,--system-site-packages,-q>
${VENV_DIR}
#COMMAND ${VENV_EXECUTABLE} ${VENV_DIR}
# Must NOT call it in a folder containing the setup.py otherwise pip call it
# (i.e. "python setup.py bdist") while we want to consume the wheel package
COMMAND ${VENV_Python3_EXECUTABLE} -m pip install
--find-links=${CMAKE_CURRENT_BINARY_DIR}/python/dist ${PYTHON_PROJECT}==${PROJECT_VERSION}
# install modules only required to run examples
COMMAND ${VENV_Python3_EXECUTABLE} -m pip install pandas matplotlib pytest
BYPRODUCTS ${VENV_DIR}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Create venv and install ${PYTHON_PROJECT}"
VERBATIM)
endif()
if(BUILD_TESTING)
configure_file(
${PROJECT_SOURCE_DIR}/ortools/init/python/version_test.py.in
${PROJECT_BINARY_DIR}/python/version_test.py
@ONLY)
# run the tests within the virtualenv
add_test(NAME python_init_version_test
COMMAND ${VENV_Python3_EXECUTABLE} ${PROJECT_BINARY_DIR}/python/version_test.py)
endif()
###############
## Doc rules ##
###############
if(BUILD_PYTHON_DOC)
find_program(PDOC_PRG NAMES pdoc)
if (PDOC_PRG)
# add a target to generate API documentation with pdoc
add_custom_target(${PROJECT_NAME}_python_doc ALL
#COMMAND ${CMAKE_COMMAND} -E rm -rf ${PROJECT_BINARY_DIR}/docs/python
COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}/docs/python
COMMAND ${PDOC_PRG}
--logo https://developers.google.com/optimization/images/orLogo.png
--no-search -d google
--footer-text "OR-Tools v${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}"
-o ${PROJECT_BINARY_DIR}/docs/python
${PROJECT_BINARY_DIR}/python/ortools
DEPENDS python_package
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
COMMENT "Generating Python API documentation with pdoc"
VERBATIM)
else()
message(WARNING "cmd `pdoc` not found, Python doc generation is disable!")
endif()
endif()
#####################
## Python Sample ##
#####################
# add_python_sample()
# CMake function to generate and build python sample.
# Parameters:
# the python filename
# e.g.:
# add_python_sample(foo.py)
function(add_python_sample FILE_NAME)
message(STATUS "Configuring sample ${FILE_NAME} ...")
get_filename_component(SAMPLE_NAME ${FILE_NAME} NAME_WE)
get_filename_component(SAMPLE_DIR ${FILE_NAME} DIRECTORY)
get_filename_component(COMPONENT_DIR ${SAMPLE_DIR} DIRECTORY)
get_filename_component(COMPONENT_NAME ${COMPONENT_DIR} NAME)
if(BUILD_TESTING)
add_test(
NAME python_${COMPONENT_NAME}_${SAMPLE_NAME}
COMMAND ${VENV_Python3_EXECUTABLE} ${FILE_NAME}
WORKING_DIRECTORY ${VENV_DIR})
endif()
message(STATUS "Configuring sample ${FILE_NAME} done")
endfunction()
######################
## Python Example ##
######################
# add_python_example()
# CMake function to generate and build python example.
# Parameters:
# the python filename
# e.g.:
# add_python_example(foo.py)
function(add_python_example FILE_NAME)
message(STATUS "Configuring example ${FILE_NAME} ...")
get_filename_component(EXAMPLE_NAME ${FILE_NAME} NAME_WE)
get_filename_component(COMPONENT_DIR ${FILE_NAME} DIRECTORY)
get_filename_component(COMPONENT_NAME ${COMPONENT_DIR} NAME)
if(BUILD_TESTING)
add_test(
NAME python_${COMPONENT_NAME}_${EXAMPLE_NAME}
COMMAND ${VENV_Python3_EXECUTABLE} ${FILE_NAME}
WORKING_DIRECTORY ${VENV_DIR})
endif()
message(STATUS "Configuring example ${FILE_NAME} done")
endfunction()