-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathSetupPython.cmake
95 lines (82 loc) · 4.4 KB
/
SetupPython.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
option(BUILD_PYTHON_PIP_PACKAGE "Prepare a folder to build a pip module" OFF)
# TODO: Prefer static libraries (on Unix, no effect on Windows)
# Can't make it work on Mac right now (untested on Ubuntu), I get issues building the swig bindings
set(Python_USE_STATIC_LIBS OFF)
# find_package(Python) has the problem that on github actions in particular it'll pick up the most recent python (eg 3.9) from the tool cache
# even if you have used the setup-python action and set it to 3.8
if (PYTHON_VERSION)
find_package(Python ${PYTHON_VERSION} EXACT REQUIRED COMPONENTS Interpreter Development OPTIONAL_COMPONENTS NumPy)
else()
find_package(Python 3 EXACT REQUIRED COMPONENTS Interpreter Development OPTIONAL_COMPONENTS NumPy)
endif()
execute_process(COMMAND ${Python_EXECUTABLE} -m pytest --version
RESULT_VARIABLE _Pytest_STATUS
OUTPUT_VARIABLE Pytest_Version
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(_Pytest_STATUS AND NOT _Pytest_STATUS EQUAL 0)
message(AUTHOR_WARNING "Pytest isn't installed on your system python, so some tests won't be run. Run `pip install pytest`")
set(Pytest_AVAILABLE OFF)
else()
message("Found Pytest: ${Pytest_Version}")
set(Pytest_AVAILABLE ON)
execute_process(COMMAND ${Python_EXECUTABLE} -m pip show pytest-xdist -q
RESULT_VARIABLE _Pytest_xdist_STATUS
ERROR_QUIET
)
if(_Pytest_xdist_STATUS AND NOT _Pytest_xdist_STATUS EQUAL 0)
message(AUTHOR_WARNING "Pytest-xdist isn't installed on your system python, so the pytest runs will not be parallelized")
else()
set(Pytest_XDIST_OPTS -n auto)
endif()
endif()
get_filename_component(Python_PROGRAM_NAME ${Python_EXECUTABLE} NAME)
get_filename_component(RESOLVED_PYTHON_LIBRARY "${Python_LIBRARIES}" REALPATH)
message("RESOLVED_PYTHON_LIBRARY=${RESOLVED_PYTHON_LIBRARY}")
if(WIN32)
# In case you have both release and debug Python libraries on your system, Python_LIBRARIES might be "optimized;C:/.../python38.lib;debug;C:/.../python38_d.lib"
# so it can't be used directly, we could use a generator expression to find the $<TARGET_FILE:Python::Python> library used and that'd point directly
# to the DLL. But it won't work nicely for the install(code ...) command below... so we do a hack, get the optimized one always...
list(LENGTH Python_LIBRARIES _LEN)
if (_LEN GREATER 1)
set (_Python_DOING_RELEASE FALSE)
foreach(_currentArg ${Python_LIBRARIES})
if ("x${_currentArg}" STREQUAL "xoptimized")
set(_Python_DOING_RELEASE TRUE)
elseif(_Python_DOING_RELEASE)
get_filename_component(RESOLVED_PYTHON_LIBRARY "${_currentArg}" REALPATH)
break()
endif()
endforeach()
# else()
# No-op, already done above
endif()
else()
get_target_property(PYTHON_LIB_TYPE Python::Python TYPE)
# Does NOT follow symlinks. Works fine on windows and mac, but on ubuntu it ships a libpython3.8.so which is just a symlink
#install(IMPORTED_RUNTIME_ARTIFACTS Python::Python LIBRARY DESTINATION lib)
#install(FILES ${RESOLVED_PYTHON_LIBRARY} DESTINATION lib)
# if(NOT PYTHON_LIB_TYPE STREQUAL STATIC_LIBRARY)
# message(FATAL_ERROR "We expect to find a static libpython on Unix, but ${PYTHON_LIB_TYPE} found")
# endif()
endif()
include_directories(SYSTEM ${Python_INCLUDE_DIRS})
set(ALL_PYTHON_BINDING_TARGETS "") # global list of python bindings
set(ALL_PYTHON_BINDING_DEPENDS "") # global list of library dependencies of the generated wrapper cxx files
set(ALL_PYTHON_WRAPPER_FILES "") # global list of generated wrapper cxx files
set(ALL_PYTHON_WRAPPER_TARGETS "") # global list targets that build generated wrapper cxx files
set(ALL_PYTHON_GENERATED_SRCS "") # global list of generated .py files
if(BUILD_PYTHON_PIP_PACKAGE)
set(PipRepositoryValues "testpypi;pypi" CACHE INTERNAL
"List of possible values for which repository to upload to")
set(PYTHON_PIP_REPOSITORY "testpypi" CACHE STRING "Which pip repository to use for upload")
set_property(CACHE PYTHON_PIP_REPOSITORY PROPERTY STRINGS ${PipRepositoryValues})
if(NOT PYTHON_PIP_REPOSITORY IN_LIST PipRepositoryValues)
message(FATAL_ERROR "PYTHON_PIP_REPOSITORY must be one of ${PipRepositoryValues}")
endif()
set(PYTHON_PACKAGE_FOLDER "${PROJECT_BINARY_DIR}/Products/python_package")
file(MAKE_DIRECTORY "${PYTHON_PACKAGE_FOLDER}")
set(ALL_PYTHON_PACKAGE_TARGETS "") # global list of python bindings
set(ALL_PYTHON_PACKAGE_GENERATED_SRCS "") # global list of generated .py files for pyhton_package
endif()