From 671614988e104107e238b7ed494d047857061137 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Mon, 29 Sep 2025 10:55:18 +0200 Subject: [PATCH 1/4] working static build --- Cargo.toml | 13 +++++------ crates/minislang/src/lib.rs | 4 +--- crates/slang-hal/build.rs | 38 -------------------------------- crates/slang-hal/src/function.rs | 2 +- 4 files changed, 8 insertions(+), 49 deletions(-) delete mode 100644 crates/slang-hal/build.rs diff --git a/Cargo.toml b/Cargo.toml index 80025aa..5d60cc3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,5 @@ [workspace] -members = [ - "crates/minislang", - "crates/slang-hal", - "crates/slang-hal-derive", -] +members = ["crates/minislang", "crates/slang-hal", "crates/slang-hal-derive"] resolver = "2" [workspace.dependencies] @@ -24,7 +20,7 @@ encase = "0.12" [workspace.lints] rust.unexpected_cfgs = { level = "warn", check-cfg = [ - 'cfg(feature, values("dim2", "dim3"))' + 'cfg(feature, values("dim2", "dim3"))', ] } [profile.release] @@ -32,4 +28,7 @@ opt-level = 'z' [patch.crates-io] #encase = { path = "../encase" } -#shader-slang = { path = "../slang-rs" } \ No newline at end of file +#shader-slang = { path = "../slang-rs" } +shader-slang = { git = "https://github.com/Vrixyz/slang-rs.git", branch = "static-build", default-features = false, features = [ + "static", +] } diff --git a/crates/minislang/src/lib.rs b/crates/minislang/src/lib.rs index aba90b7..014ebd3 100644 --- a/crates/minislang/src/lib.rs +++ b/crates/minislang/src/lib.rs @@ -97,9 +97,7 @@ impl SlangCompiler { let entry_points: Vec<_> = module .entry_points() - .filter(|e| { - entry_point.is_none() || Some(e.function_reflection().name()) == entry_point - }) + .filter(|e| entry_point.is_none() || e.function_reflection().name() == entry_point) .map(|e| e.downcast().clone()) .collect(); let program = session diff --git a/crates/slang-hal/build.rs b/crates/slang-hal/build.rs deleted file mode 100644 index 44325da..0000000 --- a/crates/slang-hal/build.rs +++ /dev/null @@ -1,38 +0,0 @@ -use std::env; -use std::path::Path; - -// Automatically copy the dynamic libraries from the slang dir to the target dir. -fn main() { - println!("cargo:rerun-if-env-changed=SLANG_LIB_DIR"); - - let lib_dir = if let Ok(dir) = env::var("SLANG_LIB_DIR") { - dir - } else if let Ok(dir) = env::var("SLANG_DIR") { - format!("{dir}/lib") - } else if let Ok(dir) = env::var("VULKAN_SDK") { - format!("{dir}/lib") - } else { - panic!("The environment variable SLANG_LIB_DIR, SLANG_DIR, or VULKAN_SDK must be set"); - }; - - let bin_dir = if let Ok(dir) = env::var("SLANG_BIN_DIR") { - dir - } else if let Ok(dir) = env::var("SLANG_DIR") { - format!("{dir}/bin") - } else if let Ok(dir) = env::var("VULKAN_SDK") { - format!("{dir}/bin") - } else { - panic!("The environment variable SLANG_BIN_DIR, SLANG_DIR, or VULKAN_SDK must be set"); - }; - - if !lib_dir.is_empty() { - println!("cargo:rustc-link-search=native={lib_dir}"); - } - - let out_dir = env::var("OUT_DIR").expect("Couldn't determine output directory."); - let out_dir = Path::new(&out_dir); - let cpy_target = out_dir.join("../../.."); - - dircpy::copy_dir(&lib_dir, &cpy_target).unwrap_or_else(|e| panic!("could not copy dynamic libraries from `{lib_dir:?}` to target directory `{cpy_target:?}`: {e}")); - dircpy::copy_dir(&bin_dir, &cpy_target).unwrap_or_else(|e| panic!("could not copy dynamic libraries from `{bin_dir:?}` to target directory `{cpy_target:?}`: {e}")); -} diff --git a/crates/slang-hal/src/function.rs b/crates/slang-hal/src/function.rs index a125474..38f6923 100644 --- a/crates/slang-hal/src/function.rs +++ b/crates/slang-hal/src/function.rs @@ -53,7 +53,7 @@ impl GpuFunction { buffers.push(( param_var .name() - // .expect("unnamed parameters not supported yet") + .expect("unnamed parameters not supported yet") .to_string(), binding, )); From 274d4c037b50869ea56504c30d4ee705c8486709 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Tue, 7 Oct 2025 15:51:25 +0200 Subject: [PATCH 2/4] avoid using patch as features can't be set in patches for downstream --- Cargo.toml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5d60cc3..8827c6d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,9 +9,12 @@ anyhow = "1" async-channel = "2" thiserror = "2" futures = "0.3" - -shader-slang = "0.1" -shader-slang-sys = "0.1" +shader-slang = { git = "https://github.com/Vrixyz/slang-rs.git", branch = "static-build", default-features = false, features = [ + "static", +] } +shader-slang-sys = { git = "https://github.com/Vrixyz/slang-rs.git", branch = "static-build", default-features = false, features = [ + "static", +] } # TODO: make the wgpu dependency optional too (all backends should be optional but with # wgpu enabled by default). From b778b3c0c832be1d7657bb6f9b63da415b7794f3 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Mon, 20 Oct 2025 22:08:08 +0200 Subject: [PATCH 3/4] feature gate static build ; default kept to dynamic --- Cargo.toml | 12 +++-------- crates/minislang/Cargo.toml | 5 +++++ crates/slang-hal/Cargo.toml | 19 +++++++++++------ crates/slang-hal/build.rs | 41 +++++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 15 deletions(-) create mode 100644 crates/slang-hal/build.rs diff --git a/Cargo.toml b/Cargo.toml index 8827c6d..7143eb3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,12 +9,8 @@ anyhow = "1" async-channel = "2" thiserror = "2" futures = "0.3" -shader-slang = { git = "https://github.com/Vrixyz/slang-rs.git", branch = "static-build", default-features = false, features = [ - "static", -] } -shader-slang-sys = { git = "https://github.com/Vrixyz/slang-rs.git", branch = "static-build", default-features = false, features = [ - "static", -] } +shader-slang = { git = "https://github.com/Vrixyz/slang-rs.git", branch = "static-build", default-features = false } +shader-slang-sys = { git = "https://github.com/Vrixyz/slang-rs.git", branch = "static-build", default-features = false } # TODO: make the wgpu dependency optional too (all backends should be optional but with # wgpu enabled by default). @@ -32,6 +28,4 @@ opt-level = 'z' [patch.crates-io] #encase = { path = "../encase" } #shader-slang = { path = "../slang-rs" } -shader-slang = { git = "https://github.com/Vrixyz/slang-rs.git", branch = "static-build", default-features = false, features = [ - "static", -] } +shader-slang = { git = "https://github.com/Vrixyz/slang-rs.git", branch = "static-build" } diff --git a/crates/minislang/Cargo.toml b/crates/minislang/Cargo.toml index 937c93f..2d9ffbb 100644 --- a/crates/minislang/Cargo.toml +++ b/crates/minislang/Cargo.toml @@ -7,6 +7,11 @@ version = "0.1.0" edition = "2024" license = "MIT OR Apache-2.0" +[features] +default = ["slang-static"] +slang-static = ["shader-slang/static"] +slang-dynamic = ["shader-slang/dynamic"] + [dependencies] shader-slang = { workspace = true } shader-slang-sys = { workspace = true } diff --git a/crates/slang-hal/Cargo.toml b/crates/slang-hal/Cargo.toml index 4b5d100..79a3bf5 100644 --- a/crates/slang-hal/Cargo.toml +++ b/crates/slang-hal/Cargo.toml @@ -8,9 +8,12 @@ edition = "2024" license = "MIT OR Apache-2.0" [features] +default = ["slang-dynamic"] derive = ["slang-hal-derive"] cuda = ["cudarc"] -cublas = [ "cudarc?/cublas"] +cublas = ["cudarc?/cublas"] +slang-static = ["minislang/slang-static"] +slang-dynamic = ["minislang/slang-dynamic"] [dependencies] nalgebra = { workspace = true } @@ -25,17 +28,21 @@ futures = { workspace = true } async-trait = "0.1" include_dir = "0.7" -minislang = { version = "0.1", path = "../minislang" } +minislang = { version = "0.1", path = "../minislang", default-features = false } slang-hal-derive = { version = "0.1", path = "../slang-hal-derive", optional = true } dashmap = "5" regex = "1" - # For test_shader_compilation paste = "1" # CUDA runtime -cudarc = { version = "0.16", default-features = false, features = ["std", "driver", "dynamic-loading", "cuda-version-from-build-system"], optional = true } +cudarc = { version = "0.16", default-features = false, features = [ + "std", + "driver", + "dynamic-loading", + "cuda-version-from-build-system", +], optional = true } log = "0.4.27" [dev-dependencies] @@ -44,7 +51,7 @@ futures-test = "0.3" serial_test = "3" approx = "0.5" async-std = { version = "1", features = ["attributes"] } -slang-hal = { path = ".", features = ["derive"] } +slang-hal = { path = ".", features = ["derive"], default-features = false } [build-dependencies] -dircpy = "0.3" \ No newline at end of file +dircpy = "0.3" diff --git a/crates/slang-hal/build.rs b/crates/slang-hal/build.rs new file mode 100644 index 0000000..4f8ec6b --- /dev/null +++ b/crates/slang-hal/build.rs @@ -0,0 +1,41 @@ +use std::env; +use std::path::Path; + +// When not linking statically: automatically copy the dynamic libraries from the slang dir to the target dir. +fn main() { + if cfg!(feature = "slang-static") { + return; + } + println!("cargo:rerun-if-env-changed=SLANG_LIB_DIR"); + + let lib_dir = if let Ok(dir) = env::var("SLANG_LIB_DIR") { + dir + } else if let Ok(dir) = env::var("SLANG_DIR") { + format!("{dir}/lib") + } else if let Ok(dir) = env::var("VULKAN_SDK") { + format!("{dir}/lib") + } else { + panic!("The environment variable SLANG_LIB_DIR, SLANG_DIR, or VULKAN_SDK must be set"); + }; + + let bin_dir = if let Ok(dir) = env::var("SLANG_BIN_DIR") { + dir + } else if let Ok(dir) = env::var("SLANG_DIR") { + format!("{dir}/bin") + } else if let Ok(dir) = env::var("VULKAN_SDK") { + format!("{dir}/bin") + } else { + panic!("The environment variable SLANG_BIN_DIR, SLANG_DIR, or VULKAN_SDK must be set"); + }; + + if !lib_dir.is_empty() { + println!("cargo:rustc-link-search=native={lib_dir}"); + } + + let out_dir = env::var("OUT_DIR").expect("Couldn't determine output directory."); + let out_dir = Path::new(&out_dir); + let cpy_target = out_dir.join("../../.."); + + dircpy::copy_dir(&lib_dir, &cpy_target).unwrap_or_else(|e| panic!("could not copy dynamic libraries from `{lib_dir:?}` to target directory `{cpy_target:?}`: {e}")); + dircpy::copy_dir(&bin_dir, &cpy_target).unwrap_or_else(|e| panic!("could not copy dynamic libraries from `{bin_dir:?}` to target directory `{cpy_target:?}`: {e}")); +} From f47272b0f1dd9cffd4ba2a8ab7a414915360cd7d Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Mon, 20 Oct 2025 22:18:48 +0200 Subject: [PATCH 4/4] update readme for static linking --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7c4e2fa..b944b04 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ In order to compile and run any slang project, be sure to define the `SLANG_DIR` 2. Unzip the downloaded directory, and use its path as value to the `SLANG_DIR` environment variable: `SLANG_DIR=/path/to/slang`. Note that the variable must point to the root of the slang installation (i.e. the directory that contains `bin` and `lib`). We recommend adding that as a system-wide environment variables so that it also becomes available to your IDE. +3. Linking statically requires you to compile with `--no-default features --features slang-static`, and an additional environment variable, check out https://github.com/FloatyMonkey/slang-rs/pull/25 for more info. ### Supported backends