forked from jrl-umi3218/jrl-cmakemodules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
python-helpers.cmake
180 lines (159 loc) · 4.58 KB
/
python-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
# Copyright (C) 2008-2023 LAAS-CNRS, JRL AIST-CNRS, INRIA.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
# .rst:
#
# ~~~
# .. command:: PYTHON_INSTALL(<module> <file> <dest> COMPONENT <component>)
# ~~~
#
# Compile and install a Python file.
#
# :param module: Python module name.
#
# :param file: Python file name.
#
# :param file: Installation directory.
#
# :param component: Component to forward to install command.
macro(PYTHON_INSTALL MODULE FILE DEST)
set(options)
set(oneValueArgs COMPONENT)
set(multiValueArgs)
cmake_parse_arguments(
ARGS
"${options}"
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)
if(ARGS_COMPONENT)
set(_COMPONENT_NAME ${ARGS_COMPONENT})
else()
set(_COMPONENT_NAME ${CMAKE_INSTALL_DEFAULT_COMPONENT_NAME})
endif()
python_build("${MODULE}" "${FILE}")
install(
FILES "${CMAKE_CURRENT_SOURCE_DIR}/${MODULE}/${FILE}"
DESTINATION "${DEST}/${MODULE}"
COMPONENT ${_COMPONENT_NAME}
)
endmacro()
# .rst:
#
# ~~~
# .. command:: PYTHON_INSTALL_ON_SITE (<module> <file> COMPONENT <component>)
# ~~~
#
# Compile and install a Python file in :cmake:variable:`PYTHON_SITELIB`.
#
# :param module: Python module name.
#
# :param file: Python file name.
#
# :param component: Component to forward to install command.
macro(PYTHON_INSTALL_ON_SITE MODULE FILE)
set(options)
set(oneValueArgs COMPONENT)
set(multiValueArgs)
cmake_parse_arguments(
ARGS
"${options}"
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)
if(ARGS_COMPONENT)
set(_COMPONENT_NAME ${ARGS_COMPONENT})
else()
set(_COMPONENT_NAME ${CMAKE_INSTALL_DEFAULT_COMPONENT_NAME})
endif()
python_install("${MODULE}" "${FILE}" ${PYTHON_SITELIB} COMPONENT
${_COMPONENT_NAME}
)
endmacro()
# PYTHON_BUILD_GET_TARGET(TARGET)
# -----------------------------------------
#
# Get the target associated to the PYTHON_BUILD procedure
#
function(PYTHON_BUILD_GET_TARGET python_build_target)
# Regex from IsValidTargetName in CMake/Source/cmGeneratorExpression.cxx
string(
REGEX REPLACE
"[^A-Za-z0-9_.+-]"
"_"
compile_pyc
"${PROJECT_NAME}_compile_pyc_${CMAKE_CURRENT_SOURCE_DIR}"
)
if(NOT TARGET ${compile_pyc})
add_custom_target(${compile_pyc} ALL)
endif()
set(${python_build_target} ${compile_pyc} PARENT_SCOPE)
endfunction(PYTHON_BUILD_GET_TARGET NAME)
# PYTHON_BUILD(MODULE FILE DEST)
# --------------------------------------
#
# Build a Python file from the source directory in the build directory.
#
macro(PYTHON_BUILD MODULE FILE)
set(python_build_target "")
python_build_get_target(python_build_target)
set(INPUT_FILE "${CMAKE_CURRENT_SOURCE_DIR}/${MODULE}/${FILE}")
if(CMAKE_GENERATOR MATCHES "Visual Studio|Xcode")
set(OUTPUT_FILE_DIR "${CMAKE_CURRENT_BINARY_DIR}/${MODULE}/$<CONFIG>")
else()
set(OUTPUT_FILE_DIR "${CMAKE_CURRENT_BINARY_DIR}/${MODULE}")
endif()
set(OUTPUT_FILE "${OUTPUT_FILE_DIR}/${FILE}c")
# Create directory accounting for the generator expression contained in
# ${OUTPUT_FILE_DIR}
add_custom_command(
TARGET ${python_build_target}
PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory "${OUTPUT_FILE_DIR}"
)
python_build_file(${INPUT_FILE} ${OUTPUT_FILE})
endmacro()
# PYTHON_BUILD_FILE(FILE [OUTPUT_FILE])
# --------------------------------------
#
# Build a Python a given file.
#
macro(PYTHON_BUILD_FILE FILE)
set(python_build_target "")
python_build_get_target(python_build_target)
set(extra_var "${ARGV1}")
if(NOT extra_var STREQUAL "")
set(OUTPUT_FILE "${ARGV1}")
else()
set(OUTPUT_FILE "${FILE}c")
endif()
add_custom_command(
TARGET ${python_build_target}
PRE_BUILD
COMMAND
"${PYTHON_EXECUTABLE}" -c
"import py_compile; py_compile.compile(\"${FILE}\",\"${OUTPUT_FILE}\")"
VERBATIM
)
# Tag pyc file as generated.
set_source_files_properties("${OUTPUT_FILE}" PROPERTIES GENERATED TRUE)
# Clean generated files.
set_property(
DIRECTORY
APPEND
PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${OUTPUT_FILE}"
)
endmacro()