Rust-GPU / rust-gpu

πŸ‰ Making Rust a first-class language and ecosystem for GPU shaders 🚧
https://rust-gpu.github.io
Apache License 2.0
956 stars 27 forks source link

Fail to build with master due to undefined cargo environment variables #143

Closed LegNeato closed 1 week ago

LegNeato commented 1 week ago

My code fails to compile because several environment variables set by Cargo (CARGO_PKG_NAME, CARGO_PKG_VERSION_MAJOR, CARGO_PKG_VERSION_MINOR, CARGO_PKG_VERSION_PATCH, and CARGO_PKG_VERSION) are unavailable.

An example error:

{"$message_type":"diagnostic","message":"environment variable CARGO_PKG_NAME not defined at compile time","code":null,"level":"error","spans":[{"file_name":"/Users/legnitto/.cargo/git/checkouts/rust-gpu-11142fd2aadc2318/0da80f8/crates/rustc_codegen_spirv/src/custom_insts.rs","byte_start":739,"byte_end":761,"line_start":16,"line_end":16,"column_start":63,"column_end":85,"is_primary":true,"text":[{"text":"pub const CUSTOM_EXT_INST_SET_PREFIX: &str = concat!(\"Rust.\", env!(\"CARGO_PKG_NAME\"), \".\");","highlight_start":63,"highlight_end":85}]

I'm not doing anything fancy but I'm sure I am doing something wrong...this code in rustc_codegen_spirv hasn't changed in a long time. the gpu crate builds fine on its own and the shared crate builds file on its own but the cpu crate fails. Even when I blank out all code except the build.rs for the cpu crate it still fails the same way.

Here is my layout:

β”œβ”€β”€ Cargo.lock
β”œβ”€β”€ Cargo.toml
β”œβ”€β”€ benches
β”‚Β Β  β”œβ”€β”€ Cargo.toml
β”‚Β Β  └── src
β”‚Β Β      └── matmul.rs
β”œβ”€β”€ crates
β”‚Β Β  β”œβ”€β”€ cpu
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ Cargo.toml
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ build.rs
β”‚Β Β  β”‚Β Β  └── src
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ lib.rs
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ vulkan.rs
β”‚Β Β  β”‚Β Β      └── wgpu.rs
β”‚Β Β  β”œβ”€β”€ gpu
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ Cargo.toml
β”‚Β Β  β”‚Β Β  └── src
β”‚Β Β  β”‚Β Β      └── lib.rs
β”‚Β Β  └── shared
β”‚Β Β      β”œβ”€β”€ Cargo.toml
β”‚Β Β      └── src
β”‚Β Β          └── lib.rs
β”œβ”€β”€ rust-toolchain.toml
└── src
    └── main.rs

The build.rs:

use spirv_builder::{MetadataPrintout, SpirvBuilder};
use std::path::Path;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Specify the path to the `gpu` crate, which contains the shader code
    let gpu_crate_path = Path::new("../gpu");

    // Compile the shader crate with SpirvBuilder
    let result = SpirvBuilder::new(gpu_crate_path, "spirv-unknown-vulkan1.3)
        .print_metadata(MetadataPrintout::Full)
        .build()?;

    // Set an environment variable for the compiled shader path
    println!(
        "cargo:rustc-env=SHADER_PATH={}",
        result.module.unwrap_single()
    );

    println!("cargo:rerun-if-changed=crates/gpu/src/");
    println!("cargo:rerun-if-changed=crates/shared/src/");

    println!("{result:#?}");
    result
}

I'm not sure how to debug this...

LegNeato commented 1 week ago

Fake news.

This is caused by another bug crashing the compiler and then me rerunning the command without the proper things set. For posterity, to run the deps of the build script in release (required to work around a bug) need:

https://github.com/Rust-GPU/rust-gpu/blob/0da80f8a61867590a0824873fa45dc8983e49da8/Cargo.toml#L49-L62

eddyb commented 1 week ago

For the record, the bug in question, causing @LegNeato to try to run a failing rustc invocation without the proper env vars, was: