forked from jrl-umi3218/jrl-cmakemodules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstubs.cmake
121 lines (108 loc) · 3.74 KB
/
stubs.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
# Copyright (C) 2021 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/>.
set(CURRENT_FILE_PATH
${CMAKE_CURRENT_LIST_DIR}
CACHE INTERNAL "")
# .rst: .. command:: LOAD_STUBGEN([GIT_TAG])
#
# GIT_TAG: the git tag of stubgen. This optional argument allows to use a
# precise version of stubgen (not necessarily the last master branch).
#
# Download and configure the stub generator module.
#
macro(LOAD_STUBGEN)
# Handle optional argument
set(GIT_TAG "master")
set(extra_macro_args ${ARGN})
list(LENGTH extra_macro_args num_extra_args)
if(${num_extra_args} GREATER 0)
list(GET extra_macro_args 0 GIT_TAG)
endif()
# Download at configure time
set(STUBGEN_DIR ${CMAKE_CURRENT_BINARY_DIR}/stubgen)
configure_file(${CURRENT_FILE_PATH}/stubgen/CMakeLists.txt.in
stubgen/CMakeLists.txt)
execute_process(
COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${STUBGEN_DIR})
if(result)
message(FATAL_ERROR "CMake step for stubgen failed: ${result}")
endif()
execute_process(
COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${STUBGEN_DIR})
if(result)
message(FATAL_ERROR "Build step for stubgen failed: ${result}")
endif()
set(STUBGEN_MAIN_FILE ${STUBGEN_DIR}/src/pybind11_stubgen/__init__.py)
endmacro(LOAD_STUBGEN)
# .rst: .. command:: LOAD_STUBGEN(module_path module_name module_install_dir)
#
# Generate the stubs associated to a given project. If the TARGET python exists,
# then the stubs generation will be performed after python target.
#
# .rst: .. variable:: module_path
#
# Path pointing to the module
#
# .rst: .. variable:: module_name
#
# Name of the module
#
# .rst: .. variable:: module_install_dir
#
# Where the module is installed
#
function(GENERATE_STUBS module_path module_name module_install_dir)
if(NOT STUBGEN_MAIN_FILE)
message(
FATAL_ERROR "You need to first load the stubgen module via LOAD_STUBGEN.")
endif(NOT STUBGEN_MAIN_FILE)
# Regex from IsValidTargetName in CMake/Source/cmGeneratorExpression.cxx
if(NOT module_path)
string(REGEX REPLACE "[^A-Za-z0-9_.+-]" "_" target_name
"generate_stubs_${module_name}")
else()
string(REGEX REPLACE "[^A-Za-z0-9_.+-]" "_" target_name
"generate_stubs_${module_path}_${module_name}")
endif()
if($ENV{PYTHONPATH})
set(PYTHONPATH ${module_path};$ENV{PYTHONPATH})
else()
set(PYTHONPATH ${module_path})
endif($ENV{PYTHONPATH})
add_custom_target(
${target_name} ALL
COMMAND
${CMAKE_COMMAND} -E env PYTHONPATH=${PYTHONPATH} "${PYTHON_EXECUTABLE}"
"${STUBGEN_MAIN_FILE}" "-o" "${module_path}" "${module_name}"
"--boost-python" --ignore-invalid signature "--no-setup-py"
"--root-module-suffix" ""
VERBATIM)
if(TARGET python)
add_dependencies(${target_name} python)
endif(TARGET python)
install(
DIRECTORY ${module_path}/${module_name}
DESTINATION ${module_install_dir}
FILES_MATCHING
PATTERN "*.pyi")
set_property(
TARGET ${target_name}
APPEND
PROPERTY ADDITIONAL_CLEAN_FILES FILES_MATCHING PATTERN "*.pyi")
endfunction(GENERATE_STUBS module_name)