Skip to content

Commit

Permalink
Add builder
Browse files Browse the repository at this point in the history
  • Loading branch information
thedocruby committed Nov 3, 2023
1 parent abd1f3e commit 853ecfa
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 20 deletions.
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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": [
{
Expand Down
19 changes: 13 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down
52 changes: 49 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
@@ -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<dyn Error>> {
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()?;

Expand Down
6 changes: 3 additions & 3 deletions examples/shaders/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
17 changes: 17 additions & 0 deletions gravylib-builder/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions gravylib-builder/src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use gravylib_builder::*;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
build_shader("../gravylib-helpers")?;
build_shader("../examples/shaders")?;
Ok(())
}
18 changes: 18 additions & 0 deletions gravylib-builder/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<dyn Error>> {
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(())
}
4 changes: 2 additions & 2 deletions gravylib_helpers/Cargo.toml → gravylib-helpers/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "gravylib_helpers"
name = "gravylib-helpers"
version = "0.1.0-alpha"
publish = false
authors.workspace = true
Expand All @@ -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"] }
File renamed without changes.
2 changes: 1 addition & 1 deletion gravylib_macros/Cargo.toml → gravylib-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "gravylib_macros"
name = "gravylib-macros"
version = "0.1.0-alpha"
publish = false
authors.workspace = true
Expand Down
File renamed without changes.

0 comments on commit 853ecfa

Please sign in to comment.