forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.rs
More file actions
375 lines (339 loc) · 13.4 KB
/
build.rs
File metadata and controls
375 lines (339 loc) · 13.4 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
use std::env;
use std::path::{Path, PathBuf};
fn main() {
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let srcdir = manifest_dir
.parent()
.and_then(Path::parent)
.expect("expected Modules/cpython-sys to live under the source tree");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let builddir = env::var("PYTHON_BUILD_DIR").ok();
emit_rerun_instructions(builddir.as_deref());
let gil_disabled = gil_disabled(srcdir, builddir.as_deref());
if gil_disabled {
println!("cargo:rustc-cfg=py_gil_disabled");
}
println!("cargo::rustc-check-cfg=cfg(py_gil_disabled)");
generate_c_api_bindings(
srcdir,
builddir.as_deref(),
out_path.as_path(),
gil_disabled,
);
}
// Bindgen depends on build-time env and, on iOS, can also inherit the
// deployment target from the generated Makefile. Declare both so Cargo reruns
// the build script when those inputs change.
fn emit_rerun_instructions(builddir: Option<&str>) {
for var in [
"IPHONEOS_DEPLOYMENT_TARGET",
"LLVM_TARGET",
"PY_DEBUG",
"PY_GIL_DISABLED",
"PYTHON_BUILD_DIR",
"PY_CC",
"PY_CPPFLAGS",
"PY_CFLAGS",
"TARGET",
"WASI_SDK_PATH",
] {
println!("cargo:rerun-if-env-changed={var}");
}
if let Some(builddir) = builddir {
let makefile = Path::new(builddir).join("Makefile");
println!("cargo:rerun-if-changed={}", makefile.display());
}
}
/// Find the newest clang resource directory on the system.
///
/// Ubuntu 24.04 ships libclang-18 whose built-in headers (stdatomic.h,
/// mmintrin.h) are broken. CI jobs install a newer clang (e.g. clang-20)
/// whose resource directory at /usr/lib/llvm-20/lib/clang/20 has working
/// headers. We pass -resource-dir to bindgen's clang so it picks up those
/// headers instead of the broken libclang-18 ones.
fn newest_clang_resource_dir() -> Option<PathBuf> {
let base = Path::new("/usr/lib");
let mut best: Option<(u32, PathBuf)> = None;
for entry in std::fs::read_dir(base).ok()?.flatten() {
let name = entry.file_name();
let name = name.to_string_lossy();
if let Some(ver_str) = name.strip_prefix("llvm-")
&& let Ok(ver) = ver_str.parse::<u32>()
{
// Resource dir: /usr/lib/llvm-<N>/lib/clang/<N>
let resource_dir = entry.path().join("lib").join("clang").join(ver_str);
if resource_dir.join("include").is_dir()
&& best.as_ref().map_or(true, |(v, _)| ver > *v)
{
best = Some((ver, resource_dir));
}
}
}
best.map(|(_, p)| p)
}
fn gil_disabled(srcdir: &Path, builddir: Option<&str>) -> bool {
if env_var_is_truthy("PY_GIL_DISABLED") {
return true;
}
let mut candidates = Vec::new();
if let Some(build) = builddir {
candidates.push(PathBuf::from(build));
}
candidates.push(srcdir.to_path_buf());
for base in candidates {
let path = base.join("pyconfig.h");
if let Ok(contents) = std::fs::read_to_string(&path)
&& contents.contains("Py_GIL_DISABLED 1")
{
return true;
}
}
false
}
fn env_var_is_truthy(name: &str) -> bool {
env::var(name)
.map(|v| matches!(v.to_ascii_lowercase().as_str(), "1" | "true" | "yes"))
.unwrap_or(false)
}
fn generate_c_api_bindings(
srcdir: &Path,
builddir: Option<&str>,
out_path: &Path,
gil_disabled: bool,
) {
let mut builder = bindgen::Builder::default().header("wrapper.h");
// Suppress all clang warnings (deprecation warnings, etc.)
builder = builder.clang_arg("-w");
if env_var_is_truthy("PY_DEBUG") {
builder = builder.clang_arg("-D_DEBUG");
}
if gil_disabled {
builder = builder.clang_arg("-DPy_GIL_DISABLED=1");
}
// Use the newest clang resource directory available on the system.
// Bindgen links against whatever libclang it finds (often an older
// system version), but the built-in headers in that version may be
// broken (e.g. libclang-18 on Ubuntu 24.04 has broken stdatomic.h
// and mmintrin.h). Overriding -resource-dir makes clang use a
// newer set of built-in headers without changing which libclang.so
// is loaded.
if let Some(resource_dir) = newest_clang_resource_dir() {
eprintln!(
"cpython-sys: using clang resource dir {}",
resource_dir.display()
);
builder = builder.clang_arg(format!("-resource-dir={}", resource_dir.display()));
}
// Tell clang the correct target triple for cross-compilation when we have
// an LLVM-specific triple. Otherwise let bindgen translate Cargo's TARGET
// itself (e.g. aarch64-apple-ios-sim -> arm64-apple-ios-simulator).
let cargo_target = env::var("TARGET").unwrap_or_default();
let llvm_target = env::var("LLVM_TARGET").unwrap_or_default();
if !llvm_target.is_empty() && llvm_target != cargo_target {
builder = builder.clang_arg(format!("--target={llvm_target}"));
}
// Extract cross-compilation flags from the C compiler command (PY_CC),
// preprocessor flags (PY_CPPFLAGS), and compiler flags (PY_CFLAGS).
// These provide the sysroot, include paths, and defines that bindgen's
// clang needs when cross-compiling.
//
// - WASI: sysroot in CC, -D_WASI_EMULATED_SIGNAL in CFLAGS
// - iOS: -isysroot in CPPFLAGS
let mut have_sysroot = false;
for env_name in ["PY_CC", "PY_CPPFLAGS", "PY_CFLAGS"] {
if let Ok(value) = env::var(env_name)
&& let Some(flags) = shlex::split(&value)
{
let mut iter = flags.iter().peekable();
while let Some(flag) = iter.next() {
if flag.starts_with("--sysroot") || flag.starts_with("-isysroot") {
builder = builder.clang_arg(flag);
have_sysroot = true;
// Handle "-isysroot <path>" (space-separated)
if (flag == "-isysroot" || flag == "--sysroot")
&& let Some(path) = iter.next()
{
builder = builder.clang_arg(path);
}
} else if flag.starts_with("-I")
|| flag.starts_with("-D")
|| flag.starts_with("-std=")
|| flag.starts_with("-isystem")
{
builder = builder.clang_arg(flag);
}
}
}
}
// WASI SDK: WASI_SDK_PATH is set by Tools/wasm/wasi/__main__.py.
// The sysroot is at $WASI_SDK_PATH/share/wasi-sysroot.
if !have_sysroot
&& cargo_target.contains("wasi")
&& let Ok(sdk_path) = env::var("WASI_SDK_PATH")
{
let sysroot = PathBuf::from(&sdk_path).join("share").join("wasi-sysroot");
if sysroot.is_dir() {
builder = builder.clang_arg(format!("--sysroot={}", sysroot.display()));
have_sysroot = true;
}
}
// Android NDK: ANDROID_HOME is set by the CI/user environment, and
// Android/android-env.sh sets CC to the NDK clang binary at:
// $ANDROID_HOME/ndk/<ver>/toolchains/llvm/prebuilt/<host>/bin/<triple>-clang
// The sysroot is a sibling of bin/:
// .../toolchains/llvm/prebuilt/<host>/sysroot
if !have_sysroot
&& cargo_target.contains("android")
&& let Ok(cc) = env::var("PY_CC")
&& let Some(parts) = shlex::split(&cc)
&& let Some(binary) = parts.first()
&& let Some(bin_dir) = Path::new(binary).parent()
{
let sysroot = bin_dir.with_file_name("sysroot");
if sysroot.is_dir() {
builder = builder.clang_arg(format!("--sysroot={}", sysroot.display()));
}
}
// Include the build directory first so that cross-build pyconfig.h
// takes precedence over any pyconfig.h in the source tree (which may
// be from a native build with different settings like LONG_BIT).
let mut include_dirs = Vec::new();
if let Some(build) = builddir {
include_dirs.push(PathBuf::from(build));
}
include_dirs.push(srcdir.to_path_buf());
include_dirs.push(srcdir.join("Include"));
for dir in include_dirs {
builder = builder.clang_arg(format!("-I{}", dir.display()));
}
builder = add_target_clang_args(builder, builddir);
let bindings = builder
.allowlist_function("_?Py.*")
.allowlist_type("_?Py.*")
.allowlist_var("_?Py.*")
.blocklist_type("^PyMethodDef$")
.blocklist_type("PyObject")
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Unable to generate bindings");
let dll_name = python_dll_name(srcdir, env_var_is_truthy("PY_DEBUG"), gil_disabled);
let bindings = patch_windows_imported_pointer_globals(bindings.to_string(), &dll_name);
// Write the bindings to the $OUT_DIR/c_api.rs file.
std::fs::write(out_path.join("c_api.rs"), bindings).expect("Couldn't write bindings!");
}
/// Build the Windows DLL base name: `python{major}{minor}[t][_d]`.
fn python_dll_name(srcdir: &Path, debug: bool, gil_disabled: bool) -> String {
let patchlevel = srcdir.join("Include").join("patchlevel.h");
let contents =
std::fs::read_to_string(&patchlevel).expect("failed to read Include/patchlevel.h");
let major = extract_define_int(&contents, "PY_MAJOR_VERSION");
let minor = extract_define_int(&contents, "PY_MINOR_VERSION");
let mut name = format!("python{major}{minor}");
if gil_disabled {
name.push('t');
}
if debug {
name.push_str("_d");
}
name
}
fn extract_define_int(contents: &str, name: &str) -> u32 {
for line in contents.lines() {
let trimmed = line.trim();
if let Some(rest) = trimmed.strip_prefix("#define")
&& let Some(value) = rest.trim().strip_prefix(name)
&& let Ok(n) = value.trim().parse()
{
return n;
}
}
panic!("could not find #define {name} in patchlevel.h");
}
fn patch_windows_imported_pointer_globals(bindings: String, dll_name: &str) -> String {
// On Windows/MSVC, exported data is imported through a synthetic
// "__imp_<symbol>" pointer in the import address table (IAT). A plain
// `extern { pub static X: *mut T; }` linked via import library fails for
// data symbols because the import library only defines `__imp_X`, not `X`.
//
// Using `#[link_name = "__imp_X"]` links successfully but produces a
// single load — returning the *address* of the variable (the IAT slot
// value) rather than the variable's value. C's `__declspec(dllimport)`
// generates two loads to chase the indirection.
//
// The fix: annotate pointer-valued extern statics with `raw-dylib` on
// Windows so Rust generates the import thunk itself and handles the IAT
// indirection correctly — two loads, matching `__declspec(dllimport)`.
let lines: Vec<_> = bindings.lines().collect();
let mut patched = String::with_capacity(bindings.len());
let mut index = 0;
while index < lines.len() {
if lines[index] == "unsafe extern \"C\" {"
&& lines
.get(index + 1)
.and_then(|l| parse_pointer_static_decl(l))
.is_some()
&& lines.get(index + 2).is_some_and(|l| l.trim() == "}")
{
patched.push_str(&format!(
"#[cfg_attr(windows, link(name = \"{dll_name}\", kind = \"raw-dylib\"))]\n"
));
// Keep the original extern block unchanged.
for i in index..index + 3 {
patched.push_str(lines[i]);
patched.push('\n');
}
index += 3;
continue;
}
patched.push_str(lines[index]);
patched.push('\n');
index += 1;
}
patched
}
fn parse_pointer_static_decl(line: &str) -> Option<(&str, bool, &str)> {
let mut decl = line.trim().strip_prefix("pub static ")?;
let is_mut = decl.starts_with("mut ");
if is_mut {
decl = decl.strip_prefix("mut ")?;
}
let (name, ty) = decl.split_once(':')?;
let ty = ty.trim().strip_suffix(';')?;
if !ty.starts_with('*') {
return None;
}
Some((name.trim(), is_mut, ty))
}
fn add_target_clang_args(
mut builder: bindgen::Builder,
builddir: Option<&str>,
) -> bindgen::Builder {
let target = env::var("TARGET").unwrap_or_default();
if !target.contains("apple-ios") {
return builder;
}
// For iOS targets, bindgen may parse headers with an iOS simulator/device
// target but without a deployment minimum, which disables TLS support.
let deployment_target = ios_deployment_target(builddir).unwrap_or_else(|| "13.0".to_string());
builder = builder.clang_arg(format!("-mios-version-min={deployment_target}"));
builder
}
fn ios_deployment_target(builddir: Option<&str>) -> Option<String> {
if let Ok(value) = env::var("IPHONEOS_DEPLOYMENT_TARGET")
&& !value.is_empty()
{
return Some(value);
}
let builddir = builddir?;
let makefile = Path::new(builddir).join("Makefile");
let text = std::fs::read_to_string(makefile).ok()?;
for line in text.lines() {
if let Some(value) = line.strip_prefix("IPHONEOS_DEPLOYMENT_TARGET=") {
let value = value.trim();
if !value.is_empty() {
return Some(value.to_string());
}
}
}
None
}