From 7ef503d866e91b79c5143133fe7041820258266c Mon Sep 17 00:00:00 2001 From: Josh Thomas Date: Wed, 30 Apr 2025 15:02:00 -0500 Subject: [PATCH] fix release build failures due to libpython linking (#125) --- .github/workflows/release.yml | 8 ++++---- crates/djls-dev/src/build.rs | 32 ++++++++++++++++++++------------ crates/djls-project/Cargo.toml | 4 ++++ crates/djls-server/Cargo.toml | 4 ++++ crates/djls/Cargo.toml | 11 ++++++++++- 5 files changed, 42 insertions(+), 17 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 9130ddd..1fc2282 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -48,7 +48,7 @@ jobs: uses: PyO3/maturin-action@v1 with: target: ${{ matrix.platform.target }} - args: --release --out dist --find-interpreter + args: --release --out dist --find-interpreter --features extension-module sccache: "true" manylinux: auto @@ -82,7 +82,7 @@ jobs: uses: PyO3/maturin-action@v1 with: target: ${{ matrix.platform.target }} - args: --release --out dist --find-interpreter + args: --release --out dist --find-interpreter --features extension-module manylinux: musllinux_1_2 - name: Upload wheels @@ -112,7 +112,7 @@ jobs: uses: PyO3/maturin-action@v1 with: target: ${{ matrix.platform.target }} - args: --release --out dist --find-interpreter + args: --release --out dist --find-interpreter --features extension-module sccache: "true" - name: Upload wheels @@ -141,7 +141,7 @@ jobs: uses: PyO3/maturin-action@v1 with: target: ${{ matrix.platform.target }} - args: --release --out dist --find-interpreter + args: --release --out dist --find-interpreter --features extension-module sccache: "true" - name: Upload wheels diff --git a/crates/djls-dev/src/build.rs b/crates/djls-dev/src/build.rs index 230e85a..e1f3ec3 100644 --- a/crates/djls-dev/src/build.rs +++ b/crates/djls-dev/src/build.rs @@ -26,22 +26,30 @@ pub fn setup_python_linking() { // Get the Python interpreter configuration directly let config = pyo3_build_config::get(); - // Add the library search path if available + let is_extension_module = std::env::var("CARGO_FEATURE_EXTENSION_MODULE").is_ok(); + + // Only link libpython explicitly if we are NOT building an extension module. + if !is_extension_module { + if let Some(lib_name) = &config.lib_name { + println!("cargo:rustc-link-lib=dylib={}", lib_name); + } else { + // Warn only if linking is actually needed but we can't find the lib name + println!("cargo:warning=Python library name not found in config (needed for non-extension module +builds)."); + } + } + + // Add the library search path and RPATH if available. + // These are needed for test executables and potential future standalone binaries, + // and generally harmless for extension modules. if let Some(lib_dir) = &config.lib_dir { println!("cargo:rustc-link-search=native={}", lib_dir); - - // Add RPATH linker argument for Unix-like systems (Linux, macOS) - // This helps the test executable find the Python library at runtime. #[cfg(not(windows))] println!("cargo:rustc-link-arg=-Wl,-rpath,{}", lib_dir); } else { - println!("cargo:warning=Python library directory not found in config."); - } - - // Add the library link directive if available - if let Some(lib_name) = &config.lib_name { - println!("cargo:rustc-link-lib=dylib={}", lib_name); - } else { - println!("cargo:warning=Python library name not found in config."); + // Warn only if linking is actually needed but we can't find the lib dir + if !is_extension_module { + println!("cargo:warning=Python library directory not found in config."); + } } } diff --git a/crates/djls-project/Cargo.toml b/crates/djls-project/Cargo.toml index 28d5864..e61d946 100644 --- a/crates/djls-project/Cargo.toml +++ b/crates/djls-project/Cargo.toml @@ -3,6 +3,10 @@ name = "djls-project" version = "0.1.0" edition = "2021" +[features] +extension-module = [] +default = [] + [dependencies] pyo3 = { workspace = true } tower-lsp-server = { workspace = true, features = ["proposed"] } diff --git a/crates/djls-server/Cargo.toml b/crates/djls-server/Cargo.toml index f1faf7f..aadf292 100644 --- a/crates/djls-server/Cargo.toml +++ b/crates/djls-server/Cargo.toml @@ -3,6 +3,10 @@ name = "djls-server" version = "0.1.0" edition = "2021" +[features] +extension-module = [] +default = [] + [dependencies] djls-conf = { workspace = true } djls-project = { workspace = true } diff --git a/crates/djls/Cargo.toml b/crates/djls/Cargo.toml index 52f62f8..868eb9d 100644 --- a/crates/djls/Cargo.toml +++ b/crates/djls/Cargo.toml @@ -7,11 +7,20 @@ edition = "2021" name = "djls" crate-type = ["cdylib"] +[features] +extension-module = [ + "djls-server/extension-module", + "djls-project/extension-module", + "pyo3/extension-module" +] +default = [] + [dependencies] +djls-project = { workspace = true } djls-server = { workspace = true } anyhow = { workspace = true } -pyo3 = { workspace = true, features = ["extension-module"] } +pyo3 = { workspace = true } pyo3-async-runtimes = { workspace = true, features = ["tokio-runtime"] } serde_json = { workspace = true } tokio = { workspace = true }