Skip to content

Commit 67851c1

Browse files
committed
Add clap feature for cargo-gpu library
1 parent 45676b9 commit 67851c1

File tree

10 files changed

+63
-29
lines changed

10 files changed

+63
-29
lines changed

crates/cargo-gpu-cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ name = "cargo-gpu"
1414
path = "src/main.rs"
1515

1616
[dependencies]
17-
cargo-gpu = { path = "../cargo-gpu" }
17+
cargo-gpu = { path = "../cargo-gpu", features = ["clap"] }
1818
clap.workspace = true
1919
log.workspace = true
2020
env_logger.workspace = true

crates/cargo-gpu/Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ license.workspace = true
1111
[dependencies]
1212
cargo_metadata.workspace = true
1313
anyhow.workspace = true
14-
spirv-builder = { workspace = true, features = ["clap", "watch"] }
14+
spirv-builder = { workspace = true, features = ["watch"] }
1515
legacy_target_specs.workspace = true
16-
clap.workspace = true
16+
clap = { workspace = true, optional = true }
1717
directories.workspace = true
1818
log.workspace = true
1919
relative-path.workspace = true
@@ -28,5 +28,9 @@ test-log.workspace = true
2828
cargo_metadata = { workspace = true, features = ["builder"] }
2929
cargo-util-schemas.workspace = true
3030

31+
[features]
32+
# Enables `clap` support for public structs
33+
clap = ["dep:clap", "spirv-builder/clap"]
34+
3135
[lints]
3236
workspace = true

crates/cargo-gpu/src/build.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,24 @@ use std::io::Write as _;
1111
use std::path::PathBuf;
1212

1313
/// Args for just a build
14-
#[derive(clap::Parser, Debug, Clone, serde::Deserialize, serde::Serialize)]
14+
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
15+
#[cfg_attr(feature = "clap", derive(clap::Parser))]
1516
pub struct BuildArgs {
1617
/// Path to the output directory for the compiled shaders.
17-
#[clap(long, short, default_value = "./")]
18+
#[cfg_attr(feature = "clap", clap(long, short, default_value = "./"))]
1819
pub output_dir: PathBuf,
1920

2021
/// Watch the shader crate directory and automatically recompile on changes.
21-
#[clap(long, short, action)]
22+
#[cfg_attr(feature = "clap", clap(long, short, action))]
2223
pub watch: bool,
2324

2425
/// the flattened [`SpirvBuilder`]
25-
#[clap(flatten)]
26+
#[cfg_attr(feature = "clap", clap(flatten))]
2627
#[serde(flatten)]
2728
pub spirv_builder: SpirvBuilder,
2829

2930
///Renames the manifest.json file to the given name
30-
#[clap(long, short, default_value = "manifest.json")]
31+
#[cfg_attr(feature = "clap", clap(long, short, default_value = "manifest.json"))]
3132
pub manifest_file: String,
3233
}
3334

@@ -44,14 +45,15 @@ impl Default for BuildArgs {
4445
}
4546

4647
/// `cargo build` subcommands
47-
#[derive(Clone, clap::Parser, Debug, serde::Deserialize, serde::Serialize)]
48+
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
49+
#[cfg_attr(feature = "clap", derive(clap::Parser))]
4850
pub struct Build {
4951
/// CLI args for install the `rust-gpu` compiler and components
50-
#[clap(flatten)]
52+
#[cfg_attr(feature = "clap", clap(flatten))]
5153
pub install: Install,
5254

5355
/// CLI args for configuring the build of the shader
54-
#[clap(flatten)]
56+
#[cfg_attr(feature = "clap", clap(flatten))]
5557
pub build: BuildArgs,
5658
}
5759

@@ -175,6 +177,8 @@ impl Build {
175177

176178
#[cfg(test)]
177179
mod test {
180+
#![cfg(feature = "clap")]
181+
178182
use clap::Parser as _;
179183

180184
use crate::{Cli, Command};

crates/cargo-gpu/src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
//! Manage and merge the various sources of config: shader crate's `Cargo.toml`(s) and CLI args.
2+
3+
#![cfg(feature = "clap")]
4+
25
use anyhow::Context as _;
36
use clap::Parser as _;
47

crates/cargo-gpu/src/dump_usage.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Convenience function for internal use. Dumps all the CLI usage instructions. Useful for
22
//! updating the README.
33
4+
#![cfg(feature = "clap")]
5+
46
use crate::{user_output, Cli};
57

68
/// main dump usage function

crates/cargo-gpu/src/install.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,15 @@ impl InstalledBackend {
6161
clippy::struct_excessive_bools,
6262
reason = "cmdline args have many bools"
6363
)]
64-
#[derive(clap::Parser, Debug, Clone, serde::Deserialize, serde::Serialize)]
64+
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
65+
#[cfg_attr(feature = "clap", derive(clap::Parser))]
6566
#[non_exhaustive]
6667
pub struct Install {
6768
/// Directory containing the shader crate to compile.
68-
#[clap(long, alias("package"), short_alias('p'), default_value = "./")]
69+
#[cfg_attr(
70+
feature = "clap",
71+
clap(long, alias("package"), short_alias('p'), default_value = "./")
72+
)]
6973
#[serde(alias = "package")]
7074
pub shader_crate: PathBuf,
7175

@@ -75,30 +79,30 @@ pub struct Install {
7579
)]
7680
/// Source of `spirv-builder` dependency
7781
/// Eg: "https://github.com/Rust-GPU/rust-gpu"
78-
#[clap(long)]
82+
#[cfg_attr(feature = "clap", clap(long))]
7983
pub spirv_builder_source: Option<String>,
8084

8185
/// Version of `spirv-builder` dependency.
8286
/// * If `--spirv-builder-source` is not set, then this is assumed to be a crates.io semantic
8387
/// version such as "0.9.0".
8488
/// * If `--spirv-builder-source` is set, then this is assumed to be a Git "commitsh", such
8589
/// as a Git commit hash or a Git tag, therefore anything that `git checkout` can resolve.
86-
#[clap(long, verbatim_doc_comment)]
90+
#[cfg_attr(feature = "clap", clap(long, verbatim_doc_comment))]
8791
pub spirv_builder_version: Option<String>,
8892

8993
/// Force `rustc_codegen_spirv` to be rebuilt.
90-
#[clap(long)]
94+
#[cfg_attr(feature = "clap", clap(long))]
9195
pub rebuild_codegen: bool,
9296

9397
/// Assume "yes" to "Install Rust toolchain: [y/n]" prompt.
9498
///
9599
/// Defaults to `false` in cli, `true` in [`Default`]
96-
#[clap(long, action)]
100+
#[cfg_attr(feature = "clap", clap(long, action))]
97101
pub auto_install_rust_toolchain: bool,
98102

99103
/// Clear target dir of `rustc_codegen_spirv` build after a successful build, saves about
100104
/// 200MiB of disk space.
101-
#[clap(long = "no-clear-target", default_value = "true", action = clap::ArgAction::SetFalse)]
105+
#[cfg_attr(feature = "clap", clap(long = "no-clear-target", default_value = "true", action = clap::ArgAction::SetFalse))]
102106
pub clear_target: bool,
103107

104108
/// There is a tricky situation where a shader crate that depends on workspace config can have
@@ -122,7 +126,7 @@ pub struct Install {
122126
/// way source URLs are encoded. See these PRs for more details:
123127
/// * <https://github.com/rust-lang/cargo/pull/12280>
124128
/// * <https://github.com/rust-lang/cargo/pull/14595>
125-
#[clap(long, action, verbatim_doc_comment)]
129+
#[cfg_attr(feature = "clap", clap(long, action, verbatim_doc_comment))]
126130
pub force_overwrite_lockfiles_v4_to_v3: bool,
127131
}
128132

crates/cargo-gpu/src/lib.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@
5252
5353
use anyhow::Context as _;
5454

55-
use crate::dump_usage::dump_full_usage_for_readme;
5655
use build::Build;
5756
use show::Show;
5857

58+
#[cfg(feature = "clap")]
59+
use crate::dump_usage::dump_full_usage_for_readme;
60+
5961
mod build;
6062
mod config;
6163
mod dump_usage;
@@ -97,7 +99,7 @@ macro_rules! user_output {
9799
}
98100

99101
/// All of the available subcommands for `cargo gpu`
100-
#[derive(clap::Subcommand)]
102+
#[cfg_attr(feature = "clap", derive(clap::Subcommand))]
101103
#[non_exhaustive]
102104
pub enum Command {
103105
/// Install rust-gpu compiler artifacts.
@@ -112,6 +114,7 @@ pub enum Command {
112114
/// A hidden command that can be used to recursively print out all the subcommand help messages:
113115
/// `cargo gpu dump-usage`
114116
/// Useful for updating the README.
117+
#[cfg(feature = "clap")]
115118
#[clap(hide(true))]
116119
DumpUsage,
117120
}
@@ -122,6 +125,7 @@ impl Command {
122125
/// # Errors
123126
/// Any errors during execution, usually printed to the user
124127
#[inline]
128+
#[cfg(feature = "clap")]
125129
pub fn run(&self, env_args: Vec<String>) -> anyhow::Result<()> {
126130
match &self {
127131
Self::Install(install) => {
@@ -149,6 +153,7 @@ impl Command {
149153
command.run()?;
150154
}
151155
Self::Show(show) => show.run()?,
156+
#[cfg(feature = "clap")]
152157
Self::DumpUsage => dump_full_usage_for_readme()?,
153158
}
154159

@@ -157,12 +162,15 @@ impl Command {
157162
}
158163

159164
/// the Cli struct representing the main cli
160-
#[derive(clap::Parser)]
161-
#[clap(author, version, about, subcommand_required = true)]
165+
#[cfg_attr(feature = "clap", derive(clap::Parser))]
166+
#[cfg_attr(
167+
feature = "clap",
168+
clap(author, version, about, subcommand_required = true)
169+
)]
162170
#[non_exhaustive]
163171
pub struct Cli {
164172
/// The command to run.
165-
#[clap(subcommand)]
173+
#[cfg_attr(feature = "clap", clap(subcommand))]
166174
pub command: Command,
167175
}
168176

crates/cargo-gpu/src/metadata.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Get config from the shader crate's `Cargo.toml` `[*.metadata.rust-gpu.*]`
22
3+
#![cfg(feature = "clap")]
4+
35
use cargo_metadata::MetadataCommand;
46
use serde_json::Value;
57

crates/cargo-gpu/src/show.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ use std::fs;
88
use std::path::Path;
99

1010
/// Show the computed source of the spirv-std dependency.
11-
#[derive(Clone, Debug, clap::Parser)]
11+
#[derive(Clone, Debug)]
12+
#[cfg_attr(feature = "clap", derive(clap::Parser))]
1213
pub struct SpirvSourceDep {
1314
/// The location of the shader-crate to inspect to determine its spirv-std dependency.
14-
#[clap(long, default_value = "./")]
15+
#[cfg_attr(feature = "clap", clap(long, default_value = "./"))]
1516
pub shader_crate: std::path::PathBuf,
1617
}
1718

1819
/// Different tidbits of information that can be queried at the command line.
19-
#[derive(Clone, Debug, clap::Subcommand)]
20+
#[derive(Clone, Debug)]
21+
#[cfg_attr(feature = "clap", derive(clap::Subcommand))]
22+
#[cfg_attr(not(feature = "clap"), allow(dead_code))]
2023
pub enum Info {
2124
/// Displays the location of the cache directory
2225
CacheDirectory,
@@ -32,10 +35,10 @@ pub enum Info {
3235
}
3336

3437
/// `cargo gpu show`
35-
#[derive(clap::Parser)]
38+
#[cfg_attr(feature = "clap", derive(clap::Parser))]
3639
pub struct Show {
3740
/// Display information about rust-gpu
38-
#[clap(subcommand)]
41+
#[cfg_attr(feature = "clap", clap(subcommand))]
3942
command: Info,
4043
}
4144

crates/cargo-gpu/src/test.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use crate::cache_dir;
55
use std::io::Write as _;
66

7+
#[cfg_attr(not(feature = "clap"), allow(dead_code))]
78
fn copy_dir_all(
89
src: impl AsRef<std::path::Path>,
910
dst: impl AsRef<std::path::Path>,
@@ -26,12 +27,14 @@ pub fn shader_crate_template_path() -> std::path::PathBuf {
2627
project_base.join("../shader-crate-template")
2728
}
2829

30+
#[cfg_attr(not(feature = "clap"), allow(dead_code))]
2931
pub fn shader_crate_test_path() -> std::path::PathBuf {
3032
let shader_crate_path = crate::cache_dir().unwrap().join("shader_crate");
3133
copy_dir_all(shader_crate_template_path(), shader_crate_path.clone()).unwrap();
3234
shader_crate_path
3335
}
3436

37+
#[cfg_attr(not(feature = "clap"), allow(dead_code))]
3538
pub fn overwrite_shader_cargo_toml(shader_crate_path: &std::path::Path) -> std::fs::File {
3639
let cargo_toml = shader_crate_path.join("Cargo.toml");
3740
let mut file = std::fs::OpenOptions::new()
@@ -44,6 +47,7 @@ pub fn overwrite_shader_cargo_toml(shader_crate_path: &std::path::Path) -> std::
4447
file
4548
}
4649

50+
#[cfg_attr(not(feature = "clap"), allow(dead_code))]
4751
pub fn tests_teardown() {
4852
let cache_dir = cache_dir().unwrap();
4953
if !cache_dir.exists() {

0 commit comments

Comments
 (0)