generated from EVerest/everest-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
python-package-helpers.cmake
339 lines (308 loc) · 11 KB
/
python-package-helpers.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
function (ev_add_pip_package)
set(one_value_args
NAME
SOURCE_DIRECTORY
)
cmake_parse_arguments(
"args"
"" # no optional arguments
"${one_value_args}"
""
${ARGN}
)
if ("${args_NAME}" STREQUAL "")
message(FATAL_ERROR "${CMAKE_CURRENT_FUNCTION}: NAME is required")
endif()
cmake_path(ABSOLUTE_PATH args_SOURCE_DIRECTORY NORMALIZE)
set(TARGET_NAME "ev_pip_package_${args_NAME}")
add_custom_target(${TARGET_NAME})
set_target_properties(${TARGET_NAME}
PROPERTIES
SOURCE_DIRECTORY "${args_SOURCE_DIRECTORY}"
)
endfunction()
function (ev_install_pip_package)
set(options
LOCAL
FORCE
)
set(one_value_args
NAME
)
cmake_parse_arguments(
"args"
"${options}"
"${one_value_args}"
""
${ARGN}
)
set(TARGET_NAME "ev_pip_package_${args_NAME}")
if (NOT TARGET ${TARGET_NAME})
message(FATAL_ERROR "${CMAKE_CURRENT_FUNCTION}: target ${TARGET_NAME} for package ${args_NAME} does not exist")
endif()
get_target_property(SOURCE_DIRECTORY ${TARGET_NAME} SOURCE_DIRECTORY)
# NOTE (aw): probably we also want to directly forward arguments
set(PIP_INSTALL_ARGS "")
if (args_LOCAL)
list(APPEND PIP_INSTALL_ARGS --user)
endif()
if (args_FORCE)
list(APPEND PIP_INSTALL_ARGS --force-reinstall)
endif()
# FIXME (aw): check wether a check done file is necessary here
# if so, it should be inside a separate function
# discussion of update behavior is needed before
execute_process(
COMMAND
${Python3_EXECUTABLE} -m pip install ${PIP_INSTALL_ARGS} .
WORKING_DIRECTORY
${SOURCE_DIRECTORY}
)
endfunction()
function (ev_create_pip_install_dist_target)
set(oneValueArgs
PACKAGE_NAME
PACKAGE_SOURCE_DIRECTORY
)
set(multiValueArgs
DEPENDS
)
cmake_parse_arguments(
"arg"
""
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)
if ("${arg_PACKAGE_NAME}" STREQUAL "")
message(FATAL_ERROR "PACKAGE_NAME is required")
endif()
if ("${arg_PACKAGE_SOURCE_DIRECTORY}" STREQUAL "")
set(arg_PACKAGE_SOURCE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
message(STATUS "ev_create_pip_install_dist_target: no PACKAGE_SOURCE_DIRECTORY provided, using: ${arg_PACKAGE_SOURCE_DIRECTORY}")
endif()
set(CHECK_DONE_FILE "${CMAKE_BINARY_DIR}/${arg_PACKAGE_NAME}_pip_install_dist_installed")
add_custom_command(
OUTPUT
"${CHECK_DONE_FILE}"
COMMENT
"Installing ${arg_PACKAGE_NAME} from distribution"
WORKING_DIRECTORY
${arg_PACKAGE_SOURCE_DIRECTORY}
# Remove build dir from pip
COMMAND
${CMAKE_COMMAND} -E remove_directory build
COMMAND
${Python3_EXECUTABLE} -m pip install --force-reinstall .
COMMAND
${CMAKE_COMMAND} -E touch "${CHECK_DONE_FILE}"
)
set(TARGET_NAME "${arg_PACKAGE_NAME}_pip_install_dist")
add_custom_target(${TARGET_NAME}
DEPENDS
"${CHECK_DONE_FILE}"
DEPENDS
${arg_DEPENDS}
)
set_target_properties(${TARGET_NAME}
PROPERTIES
PACKAGE_SOURCE_DIRECTORY "${arg_PACKAGE_SOURCE_DIRECTORY}"
)
endfunction()
function (ev_create_pip_install_local_target)
set(oneValueArgs
PACKAGE_NAME
PACKAGE_SOURCE_DIRECTORY
)
set(multiValueArgs
DEPENDS
)
cmake_parse_arguments(
"arg"
""
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)
if ("${arg_PACKAGE_NAME}" STREQUAL "")
message(FATAL_ERROR "PACKAGE_NAME is required")
endif()
if ("${arg_PACKAGE_SOURCE_DIRECTORY}" STREQUAL "")
set(arg_PACKAGE_SOURCE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
message(STATUS "ev_create_pip_install_local_target: no PACKAGE_SOURCE_DIRECTORY provided, using: ${arg_PACKAGE_SOURCE_DIRECTORY}")
endif()
set(TARGET_NAME "${arg_PACKAGE_NAME}_pip_install_local")
add_custom_target(${TARGET_NAME}
# Remove build dir from pip
COMMAND
${CMAKE_COMMAND} -E remove_directory build
COMMAND
${Python3_EXECUTABLE} -m pip install --force-reinstall -e .
WORKING_DIRECTORY
${arg_PACKAGE_SOURCE_DIRECTORY}
DEPENDS
${arg_DEPENDS}
COMMENT
"Installing ${arg_PACKAGE_NAME} via user-mode from build"
)
set_target_properties(${TARGET_NAME}
PROPERTIES
PACKAGE_SOURCE_DIRECTORY "${arg_PACKAGE_SOURCE_DIRECTORY}"
)
endfunction()
function(ev_create_pip_install_targets)
set(oneValueArgs
PACKAGE_NAME
PACKAGE_SOURCE_DIRECTORY
)
set(multiValueArgs
DIST_DEPENDS
LOCAL_DEPENDS
)
cmake_parse_arguments(
"arg"
""
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)
ev_create_pip_install_dist_target(
PACKAGE_NAME ${arg_PACKAGE_NAME}
PACKAGE_SOURCE_DIRECTORY ${arg_PACKAGE_SOURCE_DIRECTORY}
DEPENDS ${arg_DIST_DEPENDS}
)
ev_create_pip_install_local_target(
PACKAGE_NAME ${arg_PACKAGE_NAME}
PACKAGE_SOURCE_DIRECTORY ${arg_PACKAGE_SOURCE_DIRECTORY}
DEPENDS ${arg_LOCAL_DEPENDS}
)
endfunction()
function (ev_create_python_wheel_targets)
set(oneValueArgs
PACKAGE_NAME
PACKAGE_SOURCE_DIRECTORY
INSTALL_PREFIX
)
set(multiValueArgs
DEPENDS
)
cmake_parse_arguments(
"EV_CREATE_PYTHON_WHEEL_TARGETS"
""
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)
if ("${EV_CREATE_PYTHON_WHEEL_TARGETS_PACKAGE_NAME}" STREQUAL "")
message(FATAL_ERROR "PACKAGE_NAME is required")
endif()
if ("${EV_CREATE_PYTHON_WHEEL_TARGETS_PACKAGE_SOURCE_DIRECTORY}" STREQUAL "")
set(EV_CREATE_PYTHON_WHEEL_TARGETS_PACKAGE_SOURCE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
endif()
if (NOT DEFINED ${EV_CREATE_PYTHON_WHEEL_TARGETS_INSTALL_PREFIX})
if ("${${PROJECT_NAME}_WHEEL_INSTALL_PREFIX}" STREQUAL "")
message(FATAL_ERROR
"No install prefix for wheel specified, please set ${PROJECT_NAME}_WHEEL_INSTALL_PREFIX, use the INSTALL_PREFIX argument or use macro ev_setup_cmake_variables_python_wheel() to set a default value."
)
else()
set(EV_CREATE_PYTHON_WHEEL_TARGETS_INSTALL_PREFIX ${${PROJECT_NAME}_WHEEL_INSTALL_PREFIX})
endif()
endif()
set(WHEEL_OUTDIR ${CMAKE_CURRENT_BINARY_DIR}/dist_${EV_CREATE_PYTHON_WHEEL_TARGETS_PACKAGE_NAME})
set(CHECK_DONE_FILE ${CMAKE_CURRENT_BINARY_DIR}/${EV_CREATE_PYTHON_WHEEL_TARGETS_PACKAGE_NAME}_build_wheel_done)
add_custom_target(${EV_CREATE_PYTHON_WHEEL_TARGETS_PACKAGE_NAME}_build_wheel
DEPENDS
"${CHECK_DONE_FILE}"
)
add_custom_command(
OUTPUT
"${CHECK_DONE_FILE}"
# Remove build dir from pip
COMMAND
${CMAKE_COMMAND} -E remove_directory build
COMMAND
${Python3_EXECUTABLE} -m build --wheel --outdir ${WHEEL_OUTDIR} .
COMMAND
${CMAKE_COMMAND} -E touch "${CHECK_DONE_FILE}"
WORKING_DIRECTORY
${EV_CREATE_PYTHON_WHEEL_TARGETS_PACKAGE_SOURCE_DIRECTORY}
DEPENDS
${EV_CREATE_PYTHON_WHEEL_TARGETS_DEPENDS}
COMMENT
"Building wheel for ${EV_CREATE_PYTHON_WHEEL_TARGETS_PACKAGE_NAME}"
)
add_custom_target(${EV_CREATE_PYTHON_WHEEL_TARGETS_PACKAGE_NAME}_install_wheel
COMMAND
${CMAKE_COMMAND} -E make_directory ${EV_CREATE_PYTHON_WHEEL_TARGETS_INSTALL_PREFIX}
COMMAND
${CMAKE_COMMAND} -E copy_directory ${WHEEL_OUTDIR} ${EV_CREATE_PYTHON_WHEEL_TARGETS_INSTALL_PREFIX}/
WORKING_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS
${EV_CREATE_PYTHON_WHEEL_TARGETS_PACKAGE_NAME}_build_wheel
COMMENT
"Copy wheel for ${EV_CREATE_PYTHON_WHEEL_TARGETS_PACKAGE_NAME} to ${EV_CREATE_PYTHON_WHEEL_TARGETS_INSTALL_PREFIX}"
)
endfunction()
macro(ev_setup_cmake_variables_python_wheel)
set(${PROJECT_NAME}_WHEEL_INSTALL_PREFIX "" CACHE PATH "Path to install python wheels to")
if (${PROJECT_NAME}_WHEEL_INSTALL_PREFIX STREQUAL "")
if(NOT ${WHEEL_INSTALL_PREFIX} STREQUAL "")
set(${PROJECT_NAME}_DEFAULT_WHEEL_INSTALL_PREFIX "${WHEEL_INSTALL_PREFIX}")
message(STATUS "${PROJECT_NAME}_WHEEL_INSTALL_PREFIX not set, using: WHEEL_INSTALL_PREFIX=${${PROJECT_NAME}_DEFAULT_WHEEL_INSTALL_PREFIX}")
else()
set(${PROJECT_NAME}_DEFAULT_WHEEL_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}/../dist-wheels")
message(STATUS "${PROJECT_NAME}_WHEEL_INSTALL_PREFIX and WHEEL_INSTALL_PREFIX not set, using default: \${CMAKE_INSTALL_PREFIX}/../dist-wheels=${${PROJECT_NAME}_DEFAULT_WHEEL_INSTALL_PREFIX}")
endif()
set(${PROJECT_NAME}_WHEEL_INSTALL_PREFIX "${${PROJECT_NAME}_DEFAULT_WHEEL_INSTALL_PREFIX}" CACHE PATH "Path to install python wheels to" FORCE)
endif()
message(STATUS "${PROJECT_NAME}_WHEEL_INSTALL_PREFIX=${${PROJECT_NAME}_WHEEL_INSTALL_PREFIX}")
endmacro()
function(ev_pip_install_local)
set(oneValueArgs
PACKAGE_NAME
PACKAGE_SOURCE_DIRECTORY
)
set(multiValueArgs
DEPENDS
)
cmake_parse_arguments(
"EV_PIP_INSTALL_LOCAL"
""
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)
if ("${EV_PIP_INSTALL_LOCAL_PACKAGE_NAME}" STREQUAL "")
message(FATAL_ERROR "PACKAGE_NAME is required")
endif()
if ("${EV_PIP_INSTALL_LOCAL_PACKAGE_SOURCE_DIRECTORY}" STREQUAL "")
set(EV_PIP_INSTALL_LOCAL_PACKAGE_SOURCE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
endif()
execute_process(
COMMAND
${Python3_EXECUTABLE} -c "from setuptools import setup; setup()" --version
WORKING_DIRECTORY
${EV_PIP_INSTALL_LOCAL_PACKAGE_SOURCE_DIRECTORY}
OUTPUT_VARIABLE
EV_PIP_INSTALL_LOCAL_INSTALLED_PACKAGE_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
)
set(CHECK_DONE_FILE "${CMAKE_BINARY_DIR}/${EV_PIP_INSTALL_LOCAL_PACKAGE_NAME}_pip_install_local_installed_${EV_PIP_INSTALL_LOCAL_INSTALLED_PACKAGE_VERSION}")
if(NOT EXISTS "${CHECK_DONE_FILE}")
message(STATUS "${EV_PIP_INSTALL_LOCAL_PACKAGE_NAME} not found, installing.")
execute_process(
COMMAND
${Python3_EXECUTABLE} -m pip install --force-reinstall -e .
WORKING_DIRECTORY
${EV_PIP_INSTALL_LOCAL_PACKAGE_SOURCE_DIRECTORY}
RESULTS_VARIABLE EV_RESULTS
)
execute_process(
COMMAND
${CMAKE_COMMAND} -E touch "${CHECK_DONE_FILE}"
RESULTS_VARIABLE EV_RESULTS
)
endif()
message(STATUS "Using ${EV_PIP_INSTALL_LOCAL_PACKAGE_NAME} from ${CMAKE_CURRENT_SOURCE_DIR} version: ${EV_PIP_INSTALL_LOCAL_INSTALLED_PACKAGE_VERSION}")
endfunction()