forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
250 lines (229 loc) · 9.43 KB
/
build.rs
File metadata and controls
250 lines (229 loc) · 9.43 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
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());
if gil_disabled(srcdir, builddir.as_deref()) {
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());
}
// 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",
"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 {
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 generate_c_api_bindings(srcdir: &Path, builddir: Option<&str>, out_path: &Path) {
let mut builder = bindgen::Builder::default().header("wrapper.h");
// Suppress all clang warnings (deprecation warnings, etc.)
builder = builder.clang_arg("-w");
// 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");
// Write the bindings to the $OUT_DIR/c_api.rs file.
bindings
.write_to_file(out_path.join("c_api.rs"))
.expect("Couldn't write bindings!");
}
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
}