|
1 | 1 | // Build helper for plain Cargo workflows. |
2 | | -// Maturin usually sets Python link flags; this script queries `python3-config` |
3 | | -// and emits equivalent linker directives so `cargo build/test` can link PyO3. |
4 | | -// This is needed because Cargo does not automatically inherit Maturin's linker setup. |
5 | | -use std::process::Command; |
6 | | - |
7 | 2 | fn main() { |
| 3 | + pyo3_build_config::add_extension_module_link_args(); |
| 4 | + |
8 | 5 | println!("cargo:rerun-if-env-changed=PYO3_PYTHON"); |
9 | 6 | println!("cargo:rerun-if-env-changed=PYTHON_SYS_EXECUTABLE"); |
10 | 7 |
|
11 | | - let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default(); |
12 | | - if target_os != "macos" { |
13 | | - return; |
14 | | - } |
15 | | - |
16 | | - let flags = python3_config_ldflags(); |
17 | | - if let Some(flags) = flags { |
18 | | - emit_link_flags(&flags); |
19 | | - } |
20 | | -} |
21 | | - |
22 | | -fn python3_config_ldflags() -> Option<String> { |
23 | | - run_python3_config(&["--embed", "--ldflags"]).or_else(|| run_python3_config(&["--ldflags"])) |
24 | | -} |
25 | | - |
26 | | -fn run_python3_config(args: &[&str]) -> Option<String> { |
27 | | - let output = Command::new("python3-config").args(args).output().ok()?; |
28 | | - if !output.status.success() { |
29 | | - return None; |
30 | | - } |
31 | | - String::from_utf8(output.stdout).ok() |
32 | | -} |
33 | | - |
34 | | -fn emit_link_flags(flags: &str) { |
35 | | - let mut iter = flags.split_whitespace().peekable(); |
36 | | - while let Some(flag) = iter.next() { |
37 | | - if let Some(path) = flag.strip_prefix("-L") { |
38 | | - println!("cargo:rustc-link-search=native={path}"); |
39 | | - } else if let Some(lib) = flag.strip_prefix("-l") { |
40 | | - println!("cargo:rustc-link-lib={lib}"); |
41 | | - } else if flag == "-framework" { |
42 | | - if let Some(name) = iter.next() { |
43 | | - println!("cargo:rustc-link-lib=framework={name}"); |
44 | | - } |
45 | | - } |
46 | | - } |
| 8 | + println!("cargo:rerun-if-changed=build.rs"); |
| 9 | + println!("cargo:rerun-if-env-changed=CC"); |
| 10 | + println!("cargo:rerun-if-env-changed=CFLAGS"); |
| 11 | + println!("cargo:rerun-if-env-changed=LDFLAGS"); |
| 12 | + println!("cargo:rerun-if-env-changed=RUSTFLAGS"); |
| 13 | + println!("cargo:rustc-check-cfg=cfg(Py_3_10)"); |
| 14 | + println!("cargo:rustc-check-cfg=cfg(Py_3_11)"); |
| 15 | + println!("cargo:rustc-check-cfg=cfg(Py_3_12)"); |
| 16 | + println!("cargo:rustc-check-cfg=cfg(Py_3_13)"); |
| 17 | + println!("cargo:rustc-check-cfg=cfg(Py_3_14)"); |
| 18 | + println!("cargo:rustc-check-cfg=cfg(Py_3_15)"); |
47 | 19 | } |
0 commit comments