Skip to content

Commit d7681df

Browse files
committed
dylib: add rustc arg --crate-type, no longer require crate-type to be set in Cargo.toml
1 parent 1b6f686 commit d7681df

File tree

1 file changed

+14
-0
lines changed
  • crates/spirv-builder/src

1 file changed

+14
-0
lines changed

crates/spirv-builder/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,10 @@ pub struct SpirvBuilder {
406406
/// The cargo command to run, formatted like `cargo {cargo_cmd} ...`. Defaults to `rustc`.
407407
#[cfg_attr(feature = "clap", clap(skip))]
408408
pub cargo_cmd: Option<String>,
409+
/// Whether the cargo command set in `cargo_cmd` behaves like `cargo rustc` and allows passing args such as
410+
/// `--crate-type dylib`. Defaults to true if `cargo_cmd` is `None` or `Some("rustc")`.
411+
#[cfg_attr(feature = "clap", clap(skip))]
412+
pub cargo_cmd_like_rustc: Option<bool>,
409413
/// Whether to print build.rs cargo metadata (e.g. cargo:rustc-env=var=val). Defaults to [`MetadataPrintout::None`].
410414
/// Within build scripts, set it to [`MetadataPrintout::DependencyOnly`] or [`MetadataPrintout::Full`] to ensure the build script is rerun on code changes.
411415
#[cfg_attr(feature = "clap", clap(skip))]
@@ -502,6 +506,7 @@ impl Default for SpirvBuilder {
502506
Self {
503507
path_to_crate: None,
504508
cargo_cmd: None,
509+
cargo_cmd_like_rustc: None,
505510
print_metadata: MetadataPrintout::default(),
506511
release: true,
507512
target: None,
@@ -1006,6 +1011,7 @@ fn invoke_rustc(builder: &SpirvBuilder) -> Result<PathBuf, SpirvBuilderError> {
10061011
}
10071012

10081013
let cargo_cmd = builder.cargo_cmd.as_ref().map_or("rustc", |s| s.as_str());
1014+
let cargo_cmd_like_rustc = builder.cargo_cmd_like_rustc.unwrap_or(cargo_cmd == "rustc");
10091015
let profile = if builder.release { "release" } else { "dev" };
10101016
cargo.args([
10111017
cargo_cmd,
@@ -1016,6 +1022,14 @@ fn invoke_rustc(builder: &SpirvBuilder) -> Result<PathBuf, SpirvBuilderError> {
10161022
"--profile",
10171023
profile,
10181024
]);
1025+
if cargo_cmd_like_rustc {
1026+
// About `crate-type`: We use it to determine whether the crate needs to be linked into shaders. For `rlib`,
1027+
// we're emitting regular rust libraries as is expected. For `dylib` or `cdylib`, we're linking all `rlib`s
1028+
// together, legalize them in many passes and emit a final `*.spv` file. Quirk: If you depend on a crate
1029+
// that has crate-type `dylib`, we also link it, and it will fail if it has no shaders, which may not be
1030+
// desired. (Gathered from reading source code and experimenting, @firestar99)
1031+
cargo.args(["--crate-type", "dylib"]);
1032+
}
10191033

10201034
if let Ok(extra_cargoflags) = tracked_env_var_get("RUSTGPU_CARGOFLAGS") {
10211035
cargo.args(extra_cargoflags.split_whitespace());

0 commit comments

Comments
 (0)