Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
LegNeato opened this issue Nov 13, 2024 · 2 comments
Closed

Comments

@LegNeato
Copy link
Collaborator

LegNeato commented Nov 13, 2024

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 LegNeato changed the title Fail to build with master due to undefined cargo environment cariables Fail to build with master due to undefined cargo environment variables Nov 13, 2024
@LegNeato
Copy link
Collaborator Author

LegNeato commented Nov 14, 2024

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:

rust-gpu/Cargo.toml

Lines 49 to 62 in 0da80f8

# Enable incremental by default in release mode.
[profile.release]
incremental = true
# HACK(eddyb) this is the default but without explicitly specifying it, Cargo
# will treat the identical settings in `[profile.release.build-override]` below
# as different sets of `rustc` flags and will not reuse artifacts between them.
codegen-units = 256
# Compile build-dependencies in release mode with the same settings
# as regular dependencies (including the incremental enabled above).
[profile.release.build-override]
opt-level = 3
incremental = true
codegen-units = 256

@eddyb
Copy link
Collaborator

eddyb commented Nov 14, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants