From 853ecfa0d3a581fe0fc36c66e8b585d7039ba9b6 Mon Sep 17 00:00:00 2001 From: "Dr. Rubisco" <76263371+thedocruby@users.noreply.github.com> Date: Fri, 3 Nov 2023 19:36:09 -0400 Subject: [PATCH] Add builder --- .vscode/settings.json | 5 +- Cargo.lock | 19 ++++--- Cargo.toml | 6 +-- build.rs | 52 +++++++++++++++++-- examples/shaders/Cargo.toml | 6 +-- gravylib-builder/Cargo.toml | 17 ++++++ gravylib-builder/src/bin/main.rs | 8 +++ gravylib-builder/src/lib.rs | 18 +++++++ .../Cargo.toml | 4 +- .../src/lib.rs | 0 .../Cargo.toml | 2 +- .../src/lib.rs | 0 12 files changed, 117 insertions(+), 20 deletions(-) create mode 100644 gravylib-builder/Cargo.toml create mode 100644 gravylib-builder/src/bin/main.rs create mode 100644 gravylib-builder/src/lib.rs rename {gravylib_helpers => gravylib-helpers}/Cargo.toml (81%) rename {gravylib_helpers => gravylib-helpers}/src/lib.rs (100%) rename {gravylib_macros => gravylib-macros}/Cargo.toml (90%) rename {gravylib_macros => gravylib-macros}/src/lib.rs (100%) diff --git a/.vscode/settings.json b/.vscode/settings.json index 57c47b2..1f7f307 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,8 +1,9 @@ { "rust-analyzer.linkedProjects": [ "./examples/shaders/Cargo.toml", - "./gravylib_helpers/Cargo.toml", - "./gravylib_macros/Cargo.toml", + "./gravylib-helpers/Cargo.toml", + "./gravylib-macros/Cargo.toml", + "./gravylib-builder/Cargo.toml" ], "evenbettercomments.tags": [ { diff --git a/Cargo.lock b/Cargo.lock index dbd0097..96a4a5f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -831,8 +831,8 @@ dependencies = [ "cfg-if", "env_logger", "futures", - "gravylib_helpers", - "gravylib_macros", + "gravylib-helpers", + "gravylib-macros", "shaders", "spirv-builder", "structopt", @@ -842,7 +842,14 @@ dependencies = [ ] [[package]] -name = "gravylib_helpers" +name = "gravylib-builder" +version = "0.1.0-alpha" +dependencies = [ + "spirv-builder", +] + +[[package]] +name = "gravylib-helpers" version = "0.1.0-alpha" dependencies = [ "bytemuck", @@ -850,7 +857,7 @@ dependencies = [ ] [[package]] -name = "gravylib_macros" +name = "gravylib-macros" version = "0.1.0-alpha" dependencies = [ "proc-macro2", @@ -1853,8 +1860,8 @@ dependencies = [ name = "shaders" version = "0.0.0" dependencies = [ - "gravylib_helpers", - "gravylib_macros", + "gravylib-helpers", + "gravylib-macros", "spirv-std", ] diff --git a/Cargo.toml b/Cargo.toml index 1b1b02e..94898ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,8 +29,8 @@ winit = { version = "0.29", features = [ "rwh_05" # This is to maintain support with the latest version of wgpu. Should be removed once wgpu implements rwh_06. ] } cfg-if = "1.0.0" -gravylib_helpers = { path = "./gravylib_helpers" } -gravylib_macros = { path = "./gravylib_macros" } +gravylib-helpers = { path = "./gravylib-helpers" } +gravylib-macros = { path = "./gravylib-macros" } structopt = "0.3" strum = { version = "0.25", default_features = false, features = [ "std", @@ -48,7 +48,7 @@ shaders = { path = "./examples/shaders" } [workspace] resolver = "2" -members = ["examples/shaders", "gravylib_helpers", "gravylib_macros"] +members = ["examples/shaders", "gravylib-helpers", "gravylib-macros", "gravylib-builder"] [[example]] name = "standalone" diff --git a/build.rs b/build.rs index aaea9d7..8e36c16 100644 --- a/build.rs +++ b/build.rs @@ -1,10 +1,56 @@ -use std::error::Error; - use spirv_builder::{SpirvBuilder, MetadataPrintout}; +use std::env; +use std::error::Error; +use std::path::PathBuf; + fn main() -> Result<(), Box> { + println!("cargo:rerun-if-changed=build.rs"); + println!("cargo:rerun-if-env-changed=CARGO_CFG_TARGET_OS"); + println!("cargo:rerun-if-env-changed=CARGO_CFG_TARGET_ARCH"); + // While OUT_DIR is set for both build.rs and compiling the crate, PROFILE is only set in + // build.rs. So, export it to crate compilation as well. + let profile = env::var("PROFILE").unwrap(); + println!("cargo:rustc-env=PROFILE={profile}"); + + let mut dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); + // Strip `$profile/build/*/out`. + let ok = dir.ends_with("out") + && dir.pop() + && dir.pop() + && dir.ends_with("build") + && dir.pop() + && dir.ends_with(profile) + && dir.pop(); + assert!(ok); + // NOTE(eddyb) this needs to be distinct from the `--target-dir` value that + // `spirv-builder` generates in a similar way from `$OUT_DIR` and `$PROFILE`, + // otherwise repeated `cargo build`s will cause build script reruns and the + // rebuilding of `rustc_codegen_spirv` (likely due to common proc macro deps). + let dir = dir.join("gravylib-builder"); + let status = std::process::Command::new("cargo") + .args([ + "run", + "--release", + "-p", + "gravylib-builder", + "--target-dir", + ]) + .arg(dir) + .env_remove("CARGO_ENCODED_RUSTFLAGS") + .stderr(std::process::Stdio::inherit()) + .stdout(std::process::Stdio::inherit()) + .status()?; + if !status.success() { + if let Some(code) = status.code() { + std::process::exit(code); + } else { + std::process::exit(1); + } + } + // Internal shader-like dependencies, built alongside `gravylib` - SpirvBuilder::new("gravylib_helpers", "spirv-unknown-vulkan1.1") + SpirvBuilder::new("gravylib-helpers", "spirv-unknown-vulkan1.1") .print_metadata(MetadataPrintout::Full) .build()?; diff --git a/examples/shaders/Cargo.toml b/examples/shaders/Cargo.toml index 384318b..e9f1d00 100644 --- a/examples/shaders/Cargo.toml +++ b/examples/shaders/Cargo.toml @@ -11,7 +11,7 @@ repository.workspace = true crate-type = ["lib", "dylib"] [dependencies] -spirv-std = { workspace = true } +spirv-std.workspace = true # use `gravylib` instead of these when creating your own shader crate -gravylib_helpers = { path = "../../gravylib_helpers" } -gravylib_macros = { path = "../../gravylib_macros" } +gravylib-helpers = { path = "../../gravylib-helpers" } +gravylib-macros = { path = "../../gravylib-macros" } diff --git a/gravylib-builder/Cargo.toml b/gravylib-builder/Cargo.toml new file mode 100644 index 0000000..490c5c5 --- /dev/null +++ b/gravylib-builder/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "gravylib-builder" +version = "0.1.0-alpha" +publish = false +authors.workspace = true +edition.workspace = true +license.workspace = true +repository.workspace = true + +# See rustc_codegen_spirv/Cargo.toml for details on these features +[features] +default = ["use-compiled-tools"] +use-installed-tools = ["spirv-builder/use-installed-tools"] +use-compiled-tools = ["spirv-builder/use-compiled-tools"] + +[dependencies] +spirv-builder.workspace = true \ No newline at end of file diff --git a/gravylib-builder/src/bin/main.rs b/gravylib-builder/src/bin/main.rs new file mode 100644 index 0000000..b6ec094 --- /dev/null +++ b/gravylib-builder/src/bin/main.rs @@ -0,0 +1,8 @@ +use gravylib_builder::*; +use std::error::Error; + +fn main() -> Result<(), Box> { + build_shader("../gravylib-helpers")?; + build_shader("../examples/shaders")?; + Ok(()) +} \ No newline at end of file diff --git a/gravylib-builder/src/lib.rs b/gravylib-builder/src/lib.rs new file mode 100644 index 0000000..97fb461 --- /dev/null +++ b/gravylib-builder/src/lib.rs @@ -0,0 +1,18 @@ +use spirv_builder::SpirvBuilder; +use std::env; +use std::error::Error; +// use std::fs; +use std::path::Path; + +pub fn build_shader(path_to_crate: &str, /*codegen_names: bool*/) -> Result<(), Box> { + let builder_dir = &Path::new(env!("CARGO_MANIFEST_DIR")); + let path_to_crate = builder_dir.join(path_to_crate); + let _result = SpirvBuilder::new(path_to_crate, "spirv-unknown-vulkan1.1").build()?; + /* if codegen_names { + let out_dir = env::var_os("OUT_DIR").unwrap(); + let dest_path = Path::new(&out_dir).join("entry_points.rs"); + fs::create_dir_all(&out_dir).unwrap(); + fs::write(dest_path, result.codegen_entry_point_strings()).unwrap(); + } // TODO: Look into dynamic loading of entry points */ + Ok(()) +} \ No newline at end of file diff --git a/gravylib_helpers/Cargo.toml b/gravylib-helpers/Cargo.toml similarity index 81% rename from gravylib_helpers/Cargo.toml rename to gravylib-helpers/Cargo.toml index b26a7ce..0dbdf68 100644 --- a/gravylib_helpers/Cargo.toml +++ b/gravylib-helpers/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "gravylib_helpers" +name = "gravylib-helpers" version = "0.1.0-alpha" publish = false authors.workspace = true @@ -11,5 +11,5 @@ repository.workspace = true crate-type = ["lib", "dylib"] [dependencies] -spirv-std = { workspace = true } +spirv-std.workspace = true bytemuck = { version = "1.6.3", features = ["derive"] } \ No newline at end of file diff --git a/gravylib_helpers/src/lib.rs b/gravylib-helpers/src/lib.rs similarity index 100% rename from gravylib_helpers/src/lib.rs rename to gravylib-helpers/src/lib.rs diff --git a/gravylib_macros/Cargo.toml b/gravylib-macros/Cargo.toml similarity index 90% rename from gravylib_macros/Cargo.toml rename to gravylib-macros/Cargo.toml index d163a1c..9758e49 100644 --- a/gravylib_macros/Cargo.toml +++ b/gravylib-macros/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "gravylib_macros" +name = "gravylib-macros" version = "0.1.0-alpha" publish = false authors.workspace = true diff --git a/gravylib_macros/src/lib.rs b/gravylib-macros/src/lib.rs similarity index 100% rename from gravylib_macros/src/lib.rs rename to gravylib-macros/src/lib.rs