Skip to content

Commit 9c4caa3

Browse files
committed
Fix problems
1 parent e7f07c4 commit 9c4caa3

3 files changed

Lines changed: 51 additions & 3 deletions

File tree

Modules/cpython-build-helper/src/lib.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub fn print_linker_args() {
77
println!("cargo:rerun-if-env-changed=BLDSHARED_EXE");
88
println!("cargo:rerun-if-env-changed=BLDSHARED_ARGS");
99
println!("cargo:rerun-if-env-changed=LIBPYTHON");
10+
println!("cargo:rerun-if-env-changed=PY_CC");
1011
println!("cargo:rerun-if-env-changed=PYTHON_BUILD_DIR");
1112
println!("cargo:rerun-if-env-changed=CARGO_CFG_TARGET_OS");
1213

@@ -60,8 +61,8 @@ pub fn print_linker_args() {
6061

6162
fn print_link_args(raw: &str, skip_first: bool) {
6263
let mut args = shlex::split(raw).expect("Invalid linker args");
63-
if skip_first && !args.is_empty() {
64-
args.remove(0);
64+
if skip_first {
65+
args = strip_linker_executable(args, env::var("PY_CC").ok().as_deref());
6566
}
6667

6768
let build_dir = env::var("PYTHON_BUILD_DIR").ok();
@@ -98,3 +99,26 @@ fn print_link_args(raw: &str, skip_first: bool) {
9899
}
99100
}
100101
}
102+
103+
// BLDSHARED_EXE may start with a wrapper/compiler command such as
104+
// "xcrun --sdk iphoneos clang" or "ccache clang". Strip that prefix so only
105+
// linker flags are forwarded to rustc.
106+
fn strip_linker_executable(args: Vec<String>, compiler: Option<&str>) -> Vec<String> {
107+
if let Some(compiler) = compiler
108+
&& let Some(prefix) = compiler_command_prefix(compiler)
109+
&& args.as_slice().starts_with(prefix.as_slice())
110+
{
111+
return args[prefix.len()..].to_vec();
112+
}
113+
114+
if args.is_empty() {
115+
return args;
116+
}
117+
args[1..].to_vec()
118+
}
119+
120+
fn compiler_command_prefix(raw: &str) -> Option<Vec<String>> {
121+
let tokens = shlex::split(raw)?;
122+
let last_non_flag = tokens.iter().rposition(|token| !token.starts_with('-'))?;
123+
Some(tokens.into_iter().take(last_non_flag + 1).collect())
124+
}

Modules/cpython-sys/build.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,37 @@ fn main() {
99
.expect("expected Modules/cpython-sys to live under the source tree");
1010
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
1111
let builddir = env::var("PYTHON_BUILD_DIR").ok();
12+
emit_rerun_instructions(builddir.as_deref());
1213
if gil_disabled(srcdir, builddir.as_deref()) {
1314
println!("cargo:rustc-cfg=py_gil_disabled");
1415
}
1516
println!("cargo::rustc-check-cfg=cfg(py_gil_disabled)");
1617
generate_c_api_bindings(srcdir, builddir.as_deref(), out_path.as_path());
1718
}
1819

20+
// Bindgen depends on build-time env and, on iOS, can also inherit the
21+
// deployment target from the generated Makefile. Declare both so Cargo reruns
22+
// the build script when those inputs change.
23+
fn emit_rerun_instructions(builddir: Option<&str>) {
24+
for var in [
25+
"IPHONEOS_DEPLOYMENT_TARGET",
26+
"LLVM_TARGET",
27+
"PYTHON_BUILD_DIR",
28+
"PY_CC",
29+
"PY_CPPFLAGS",
30+
"PY_CFLAGS",
31+
"TARGET",
32+
"WASI_SDK_PATH",
33+
] {
34+
println!("cargo:rerun-if-env-changed={var}");
35+
}
36+
37+
if let Some(builddir) = builddir {
38+
let makefile = Path::new(builddir).join("Makefile");
39+
println!("cargo:rerun-if-changed={}", makefile.display());
40+
}
41+
}
42+
1943
fn gil_disabled(srcdir: &Path, builddir: Option<&str>) -> bool {
2044
let mut candidates = Vec::new();
2145
if let Some(build) = builddir {

Modules/makesetup

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ sed -e 's/[ ]*#.*//' -e '/^[ ]*$/d' |
307307
BUILT_SHARED="$BUILT_SHARED $mod"
308308
# depends on the headers through cpython-sys
309309
rule="$rust_shared: cpython-sys \$(srcdir)/Cargo.toml \$(srcdir)/Cargo.lock \$(srcdir)/$srcdir/$manifest $prefixed_srcs \$(PYTHON_HEADERS) \$(MODULE_${mods_upper}_LDEPS) \$(LIBRARY)"
310-
rule="$rule; CARGO_TARGET_DIR=\$(abs_builddir)/target PYTHON_BUILD_DIR=\$(abs_builddir) RUST_SHARED_BUILD=\$(PY_ENABLE_SHARED) IPHONEOS_DEPLOYMENT_TARGET=\$(IPHONEOS_DEPLOYMENT_TARGET) BLDSHARED_EXE=\"\$(BLDSHARED_EXE)\" BLDSHARED_ARGS=\"\$(BLDSHARED_ARGS)\" LIBPYTHON=\"\$(LIBPYTHON)\" \$(if \$(CARGO_TARGET_LINKER_ENV),\$(CARGO_TARGET_LINKER_ENV)=\$(CC)) \$(CARGO_HOME)/bin/cargo build -vvv --lib --locked --package ${mod} --profile \$(CARGO_PROFILE) \$(if \$(CARGO_TARGET),--target=\$(CARGO_TARGET)) --manifest-path \$(srcdir)/Cargo.toml"
310+
rule="$rule; CARGO_TARGET_DIR=\$(abs_builddir)/target PYTHON_BUILD_DIR=\$(abs_builddir) PY_CC=\"\$(CC)\" RUST_SHARED_BUILD=\$(PY_ENABLE_SHARED) IPHONEOS_DEPLOYMENT_TARGET=\$(IPHONEOS_DEPLOYMENT_TARGET) BLDSHARED_EXE=\"\$(BLDSHARED_EXE)\" BLDSHARED_ARGS=\"\$(BLDSHARED_ARGS)\" LIBPYTHON=\"\$(LIBPYTHON)\" \$(if \$(CARGO_TARGET_LINKER_ENV),\$(CARGO_TARGET_LINKER_ENV)=\$(CC)) \$(CARGO_HOME)/bin/cargo build -vvv --lib --locked --package ${mod} --profile \$(CARGO_PROFILE) \$(if \$(CARGO_TARGET),--target=\$(CARGO_TARGET)) --manifest-path \$(srcdir)/Cargo.toml"
311311
echo "$rule" >>$rulesf
312312
echo "$file: $rust_shared; mv $rust_shared $file" >>$rulesf
313313
done

0 commit comments

Comments
 (0)