Repository navigation
Expand file tree
/
Copy pathbuild.mcpp
More file actions
296 lines (261 loc) · 10.9 KB
/
Copy pathbuild.mcpp
File metadata and controls
296 lines (261 loc) · 10.9 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
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
// build.mcpp — find <Python.h> and, for this package's own tests, link the
// Python embedding runtime.
//
// This program never links Python into a consumer: outside the `tests`
// feature it only adds a private include directory. A Linux CPython extension
// module must not link libpython. The executable tests in tests/ are embedded
// interpreters, so those link it — behind a feature no consumer activates.
//
// Resolution order for <Python.h> (docs/usage.md):
// 1. MCPP_PYTHON_INCLUDE_DIR
// 2. xim:python, as declared here or by a package in the graph
// 3. MCPP_PYTHON_EXECUTABLE, VIRTUAL_ENV, then python3/python on PATH
// 4. a non-zero exit naming every way to supply it
import std;
import mcpp;
namespace {
namespace fs = std::filesystem;
struct Python {
fs::path include;
fs::path home;
fs::path library_dir;
std::string library;
};
bool has_python_h(const fs::path& dir) {
std::error_code ec;
return fs::is_regular_file(dir / "Python.h", ec);
}
std::string trim(std::string text) {
while (!text.empty() && (text.back() == '\n' || text.back() == '\r' || text.back() == ' ' || text.back() == '\t'))
text.pop_back();
return text;
}
// The payload's include/Python.h (Windows) or include/python3.*/Python.h
// (Unix and macOS), preferring the highest version by numeric minor.
int python_minor(const fs::path& directory) {
const std::string name = directory.filename().string();
if (name.rfind("python3.", 0) != 0)
return -1;
int value = 0;
for (char c : name.substr(8)) {
if (c < '0' || c > '9')
return -1;
value = value * 10 + (c - '0');
}
return value;
}
std::optional<fs::path> include_from_root(const fs::path& root) {
if (has_python_h(root / "include"))
return root / "include";
std::vector<fs::path> candidates;
std::error_code ec;
for (fs::directory_iterator it(root / "include", ec), end; !ec && it != end; it.increment(ec)) {
const auto name = it->path().filename().string();
if (it->is_directory() && name.rfind("python3.", 0) == 0 && has_python_h(it->path()))
candidates.push_back(it->path());
}
if (candidates.empty())
return std::nullopt;
std::sort(candidates.begin(), candidates.end(), [](const fs::path& a, const fs::path& b) {
return python_minor(a) > python_minor(b); // python3.13 > python3.9, numerically
});
return candidates.front();
}
std::optional<fs::path> include_from_prefix(const fs::path& prefix) {
if (const auto found = include_from_root(prefix))
return found;
std::error_code ec;
for (fs::directory_iterator it(prefix / "include", ec), end; !ec && it != end; it.increment(ec)) {
if (it->is_directory() && has_python_h(it->path()))
return it->path();
}
return std::nullopt;
}
std::optional<fs::path> include_from_interpreter(const fs::path& executable) {
for (const fs::path& prefix : {executable.parent_path(), executable.parent_path().parent_path()}) {
if (const auto found = include_from_prefix(prefix))
return found;
}
return std::nullopt;
}
std::optional<fs::path> interpreter_on_path() {
const bool is_windows = std::string_view(mcpp::target_os()) == "windows";
const char separator = is_windows ? ';' : ':';
const std::vector<std::string> names = is_windows
? std::vector<std::string>{"python.exe", "python3.exe"}
: std::vector<std::string>{"python3", "python"};
const char* path = std::getenv("PATH");
if (!path)
return std::nullopt;
std::string_view text(path);
for (std::size_t begin = 0; begin <= text.size();) {
const std::size_t end = text.find(separator, begin);
const auto directory = text.substr(begin, end == std::string_view::npos ? text.size() - begin : end - begin);
for (const auto& name : names) {
std::error_code ec;
const fs::path candidate = fs::path(directory) / name;
if (fs::is_regular_file(candidate, ec))
return candidate;
}
if (end == std::string_view::npos)
break;
begin = end + 1;
}
return std::nullopt;
}
std::optional<fs::path> find_include() {
if (const char* value = std::getenv("MCPP_PYTHON_INCLUDE_DIR"); value && *value) {
const fs::path dir(value);
if (has_python_h(dir))
return dir;
std::println(std::cerr, "nanobind-m: MCPP_PYTHON_INCLUDE_DIR={} has no Python.h", value);
}
if (const char* value = mcpp::xpkg_dir("xim", "python"); value && *value) {
if (const auto found = include_from_root(value))
return found;
}
if (const char* value = std::getenv("MCPP_PYTHON_EXECUTABLE"); value && *value) {
if (const auto found = include_from_interpreter(fs::path(value)))
return found;
}
if (const char* value = std::getenv("VIRTUAL_ENV"); value && *value) {
if (const auto found = include_from_prefix(value))
return found;
}
if (const auto interpreter = interpreter_on_path()) {
if (const auto found = include_from_interpreter(*interpreter))
return found;
}
return std::nullopt;
}
fs::path root_of_include(const fs::path& include) {
fs::path directory = include;
if (directory.filename().string().rfind("python3.", 0) == 0)
directory = directory.parent_path();
if (directory.filename() == "include")
directory = directory.parent_path();
return directory;
}
std::string version_suffix(const fs::path& include) {
const std::string name = include.filename().string();
if (name.rfind("python3.", 0) == 0)
return name.substr(6); // "3.13"
return {};
}
std::string library_name(const fs::path& include, const fs::path& library_dir) {
if (const char* value = std::getenv("MCPP_PYTHON_LIBRARY"); value && *value)
return value;
const std::string suffix = version_suffix(include);
const bool is_windows = std::string_view(mcpp::target_os()) == "windows";
if (!suffix.empty()) {
if (!is_windows)
return "python" + suffix;
std::string compact;
for (char c : suffix)
if (c != '.')
compact.push_back(c);
return "python" + compact;
}
// An include directory whose name does not carry the version (some
// distributions use include/ directly): ask the library directory.
std::error_code ec;
for (fs::directory_iterator it(library_dir, ec), end; !ec && it != end; it.increment(ec)) {
const std::string name = it->path().filename().string();
if (name.rfind("libpython3.", 0) == 0) {
const auto dot = name.find('.', 11);
if (dot != std::string::npos)
return "python" + name.substr(9, dot - 9);
}
if (is_windows && it->path().extension() == ".lib") {
const std::string stem = it->path().stem().string();
if (stem.size() > 7 && stem.rfind("python3", 0) == 0 &&
std::all_of(stem.begin() + 7, stem.end(), [](char c) { return c >= '0' && c <= '9'; }))
return stem;
}
}
return {};
}
std::string escaped(std::string value) {
std::string out;
for (char c : value) {
if (c == '\\' || c == '"')
out.push_back('\\');
out.push_back(c);
}
return out;
}
void write_if_changed(const fs::path& path, const std::string& text) {
{
std::ifstream in(path, std::ios::binary);
if (in && std::string(std::istreambuf_iterator<char>(in), {}) == text)
return;
}
std::ofstream(path, std::ios::binary | std::ios::trunc) << text;
}
void rerun_inputs() {
for (const char* name : {"MCPP_PYTHON_INCLUDE_DIR", "MCPP_PYTHON_EXECUTABLE", "MCPP_PYTHON_HOME",
"MCPP_PYTHON_LIBRARY_DIR", "MCPP_PYTHON_LIBRARY", "VIRTUAL_ENV"})
mcpp::rerun_if_env_changed(name);
}
} // namespace
int main() {
rerun_inputs();
const bool is_windows = std::string_view(mcpp::target_os()) == "windows";
const std::optional<fs::path> include = find_include();
if (!include) {
std::println(std::cerr,
"nanobind-m: no Python development headers found.\n"
" Declare the SDK in mcpp.toml:\n"
" [xlings.workspace]\n"
" \"xim:python\" = \"3.13.12\"\n"
" or point at an exact directory:\n"
" MCPP_PYTHON_INCLUDE_DIR=/path/to/include/python3.13 mcpp build\n"
" or point at an interpreter whose sysconfig include directory has Python.h:\n"
" MCPP_PYTHON_EXECUTABLE=/path/to/python3 mcpp build\n"
" A system Python needs its development package (python3-dev).");
return 1;
}
mcpp::include_dir(include->string().c_str());
if (!mcpp::has_feature("tests"))
return 0;
Python python{*include, root_of_include(*include), {}, {}};
if (const char* value = std::getenv("MCPP_PYTHON_HOME"); value && *value)
python.home = value;
if (const char* value = std::getenv("MCPP_PYTHON_LIBRARY_DIR"); value && *value)
python.library_dir = value;
if (python.library_dir.empty()) {
if (is_windows)
python.library_dir = fs::exists(python.home / "libs") ? python.home / "libs" : python.home;
else
python.library_dir = python.home / "lib";
}
python.library = library_name(python.include, python.library_dir);
if (python.library.empty() || !fs::is_directory(python.library_dir)) {
std::println(std::cerr,
"nanobind-m: the embedded-Python tests need a Python library, but none was found\n"
" under {}. Set MCPP_PYTHON_LIBRARY_DIR and MCPP_PYTHON_LIBRARY.",
python.library_dir.string());
return 1;
}
const fs::path out = fs::path(mcpp::out_dir()) / "nanobind-tests";
std::error_code ec;
fs::create_directories(out, ec);
write_if_changed(out / "python_sdk.h",
"#pragma once\n"
"// Written by nanobind-m's build.mcpp; private to the test configuration.\n"
"#define MCPP_PYTHON_HOME \"" +
escaped(python.home.generic_string()) + "\"\n" +
"#define MCPP_PYTHON_INCLUDE_DIR \"" + escaped(python.include.generic_string()) + "\"\n" +
"#define MCPP_PYTHON_LIBRARY \"" + escaped(python.library) + "\"\n");
mcpp::include_dir(out.string().c_str());
mcpp::link_search(python.library_dir.string().c_str());
if (is_windows) {
// python-build-standalone keeps python3XX.dll at the payload root and
// the import library in libs/.
mcpp::runtime_search_dir(python.home.string().c_str());
} else {
mcpp::runtime_search_dir(python.library_dir.string().c_str());
}
mcpp::link_lib(python.library.c_str());
return 0;
}