-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinstall-python.cmake
More file actions
83 lines (62 loc) · 2.09 KB
/
install-python.cmake
File metadata and controls
83 lines (62 loc) · 2.09 KB
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
# Download Windows executable
cmake_minimum_required(VERSION 3.24)
include(FetchContent)
if(NOT python_version)
file(READ ${CMAKE_CURRENT_LIST_DIR}/../libraries.json json)
string(JSON python_version GET ${json} "python_version")
endif()
if(NOT prefix)
file(REAL_PATH "~/python-${python_version}" prefix EXPAND_TILDE)
endif()
if(NOT WIN32)
message(FATAL_ERROR "Windows only")
endif()
if(NOT DEFINED ENV{PROCESSOR_ARCHITECTURE})
message(FATAL_ERROR "PROCESSOR_ARCHITECTURE not set, could not determine CPU arch")
endif()
set(arch $ENV{PROCESSOR_ARCHITECTURE})
if(arch STREQUAL "ARM64")
set(pyarch arm64)
elseif(arch STREQUAL "AMD64")
set(pyarch amd64)
elseif(arch STREQUAL "x86")
set(pyarch win32)
else()
message(FATAL_ERROR "unknown arch: ${arch}")
endif()
if(NOT python_url)
# omit "rc*" from the url dir
string(REGEX REPLACE "rc[0-9]+$" "" python_url_dir "${python_version}")
set( https://www.python.org/ftp/python/${python_url_dir}/python-${python_version}-embed-${pyarch}.zip)
endif()
message(STATUS "Python ${python_version} ${python_url}")
message(STATUS "Python ${python_version} ${prefix}")
set(FETCHCONTENT_QUIET false)
FetchContent_Populate(cmake URL ${python_url} SOURCE_DIR ${prefix})
file(MAKE_DIRECTORY ${prefix})
file(COPY ${cmake_SOURCE_DIR}/ DESTINATION ${prefix})
# --- so Python can find libs
string(REGEX MATCH "(^[0-9]+)\\.([0-9]+)" python_version_short ${python_version})
set(python_version_short ${CMAKE_MATCH_1}${CMAKE_MATCH_2})
find_file(pth
NAMES python${python_version_short}._pth
HINTS ${prefix}
NO_DEFAULT_PATH
REQUIRED
)
file(RENAME ${pth} ${prefix}/python${python_version_short}.pth)
set(pth ${prefix}/python${python_version_short}.pth)
# --- verify
find_program(python_exe
NAMES python3 python
HINTS ${prefix}
NO_DEFAULT_PATH
REQUIRED
)
# --- add paths to Python
file(APPEND "${pth}" "${prefix}/Lib\n")
# --- pip
file(DOWNLOAD https://bootstrap.pypa.io/get-pip.py ${prefix}/get-pip.py)
execute_process(COMMAND ${python_exe} ${prefix}/get-pip.py)
cmake_path(GET python_exe PARENT_PATH bindir)
message(STATUS "installed Python ${python_version} to ${bindir}")