-
Notifications
You must be signed in to change notification settings - Fork 29
/
python.cmake
261 lines (241 loc) · 8.58 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
if(NOT BUILD_PYTHON)
return()
endif()
# Use latest UseSWIG module (3.14) and Python3 module (3.18)
cmake_minimum_required(VERSION 3.18)
# 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)
list(APPEND CMAKE_SWIG_FLAGS "-DSWIGWORDSIZE64")
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()
set(PYTHON_PROJECT cmakeswig)
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 Foo Bar FooBar)
add_subdirectory(${SUBPROJECT}/python)
endforeach()
#######################
## Python Packaging ##
#######################
#file(MAKE_DIRECTORY python/${PYTHON_PROJECT})
file(GENERATE OUTPUT ${PYTHON_PROJECT_DIR}/__init__.py CONTENT "__version__ = \"${PROJECT_VERSION}\"\n")
file(GENERATE OUTPUT ${PYTHON_PROJECT_DIR}/foo/__init__.py CONTENT "")
file(GENERATE OUTPUT ${PYTHON_PROJECT_DIR}/bar/__init__.py CONTENT "")
file(GENERATE OUTPUT ${PYTHON_PROJECT_DIR}/foobar/__init__.py CONTENT "")
# setup.py.in contains cmake variable e.g. @PYTHON_PROJECT@ and
# generator expression e.g. $<TARGET_FILE_NAME:pyFoo>
configure_file(
${PROJECT_SOURCE_DIR}/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)
# Look for python module wheel
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:Foo,TYPE>,SHARED_LIBRARY>,copy,true>
$<$<STREQUAL:$<TARGET_PROPERTY:Foo,TYPE>,SHARED_LIBRARY>:$<TARGET_SONAME_FILE:Foo>>
${PYTHON_PROJECT}/.libs
COMMAND ${CMAKE_COMMAND} -E $<IF:$<STREQUAL:$<TARGET_PROPERTY:Bar,TYPE>,SHARED_LIBRARY>,copy,true>
$<$<STREQUAL:$<TARGET_PROPERTY:Bar,TYPE>,SHARED_LIBRARY>:$<TARGET_SONAME_FILE:Bar>>
${PYTHON_PROJECT}/.libs
COMMAND ${CMAKE_COMMAND} -E $<IF:$<STREQUAL:$<TARGET_PROPERTY:FooBar,TYPE>,SHARED_LIBRARY>,copy,true>
$<$<STREQUAL:$<TARGET_PROPERTY:FooBar,TYPE>,SHARED_LIBRARY>:$<TARGET_SONAME_FILE:FooBar>>
${PYTHON_PROJECT}/.libs
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:pyFoo> ${PYTHON_PROJECT}/foo
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:pyBar> ${PYTHON_PROJECT}/bar
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:pyFooBar> ${PYTHON_PROJECT}/foobar
#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
python/setup.py.in
DEPENDS
python/setup.py
${PROJECT_NAMESPACE}::Foo
${PROJECT_NAMESPACE}::pyFoo
${PROJECT_NAMESPACE}::pyBar
${PROJECT_NAMESPACE}::pyFooBar
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)
###################
## Python Test ##
###################
if(BUILD_TESTING)
search_python_module(NAME virtualenv PACKAGE virtualenv)
# venv not working on github windows 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()
# 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} ${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}
BYPRODUCTS ${VENV_DIR}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Create venv and install ${PYTHON_PROJECT}"
VERBATIM)
endif()
# 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(EXAMPLE_NAME ${FILE_NAME} NAME_WE)
if(BUILD_TESTING)
add_test(
NAME python_test_${EXAMPLE_NAME}
COMMAND ${VENV_Python3_EXECUTABLE} ${FILE_NAME}
WORKING_DIRECTORY ${VENV_DIR})
endif()
message(STATUS "Configuring test ${FILE_NAME} done")
endfunction()
# 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)
if(BUILD_TESTING)
add_test(
NAME python_example_${EXAMPLE_NAME}
COMMAND ${VENV_Python3_EXECUTABLE} ${FILE_NAME}
WORKING_DIRECTORY ${VENV_DIR})
endif()
message(STATUS "Configuring example ${FILE_NAME} done")
endfunction()