Skip to content

Commit d96f9d4

Browse files
committed
Cargo workspace support
1 parent 089c828 commit d96f9d4

16 files changed

Lines changed: 360 additions & 238 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ anyhow = "1.0.66"
1717
current_platform = "0.2.0"
1818
clap = { version = "4.0.29", features = ["derive", "deprecated"] }
1919
tempfile = "3.3.0"
20-
toml = "0.5.9"
2120
rustc_version = "0.4.0"
2221
cargo_metadata = "0.18.1"
2322

src/options.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub use self::{
1616

1717
use anyhow::{bail, Error, Result};
1818
use clap::{Parser, ValueEnum};
19-
use std::{fmt as stdfmt, path::PathBuf, str::FromStr};
19+
use std::{fmt as stdfmt, ops::Deref, path::PathBuf, str::FromStr};
2020

2121
#[derive(Copy, Clone, Debug, Eq, PartialEq, ValueEnum)]
2222
pub enum Sanitizer {
@@ -51,6 +51,9 @@ pub enum BuildMode {
5151

5252
#[derive(Clone, Debug, Eq, PartialEq, Parser)]
5353
pub struct BuildOptions {
54+
#[command(flatten)]
55+
pub fuzz_dir_wrapper: FuzzDirWrapper,
56+
5457
/// Build artifacts in development mode, without optimizations
5558
#[arg(short = 'D', long, conflicts_with = "release")]
5659
pub dev: bool,
@@ -209,6 +212,8 @@ pub struct BuildOptions {
209212

210213
impl stdfmt::Display for BuildOptions {
211214
fn fmt(&self, f: &mut stdfmt::Formatter) -> stdfmt::Result {
215+
self.fuzz_dir_wrapper.fmt(f)?;
216+
212217
if self.dev {
213218
write!(f, " -D")?;
214219
}
@@ -270,10 +275,36 @@ pub struct FuzzDirWrapper {
270275
pub fuzz_dir: Option<PathBuf>,
271276
}
272277

278+
#[derive(Debug, Clone)]
279+
pub struct ManifestPath(PathBuf);
280+
281+
impl ManifestPath {
282+
pub fn new(path_buf: PathBuf) -> Self {
283+
Self(path_buf)
284+
}
285+
}
286+
287+
impl Deref for ManifestPath {
288+
type Target = PathBuf;
289+
290+
fn deref(&self) -> &Self::Target {
291+
&self.0
292+
}
293+
}
294+
295+
impl FuzzDirWrapper {
296+
pub fn get_manifest_path(&self) -> Option<ManifestPath> {
297+
if let Some(ref fuzz_dir) = self.fuzz_dir {
298+
return Some(ManifestPath(fuzz_dir.join("Cargo.toml")));
299+
}
300+
None
301+
}
302+
}
303+
273304
impl stdfmt::Display for FuzzDirWrapper {
274305
fn fmt(&self, f: &mut stdfmt::Formatter) -> stdfmt::Result {
275306
if let Some(ref elem) = self.fuzz_dir {
276-
write!(f, " --fuzz-dir={}", elem.display())?;
307+
write!(f, " --fuzz-dir {}", elem.display())?;
277308
}
278309

279310
Ok(())
@@ -305,6 +336,7 @@ mod test {
305336
#[test]
306337
fn display_build_options() {
307338
let default_opts = BuildOptions {
339+
fuzz_dir_wrapper: FuzzDirWrapper { fuzz_dir: None },
308340
dev: false,
309341
release: false,
310342
debug_assertions: false,

src/options/add.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub struct Add {
1414

1515
impl RunCommand for Add {
1616
fn run_command(&mut self) -> Result<()> {
17-
let project = FuzzProject::new(self.fuzz_dir_wrapper.fuzz_dir.to_owned())?;
17+
let project = FuzzProject::new(self.fuzz_dir_wrapper.get_manifest_path())?;
1818
let manifest = Manifest::parse()?;
1919
project.add_target(self, &manifest)
2020
}

src/options/build.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
options::{BuildMode, BuildOptions, FuzzDirWrapper},
2+
options::{BuildMode, BuildOptions},
33
project::FuzzProject,
44
RunCommand,
55
};
@@ -11,16 +11,13 @@ pub struct Build {
1111
#[command(flatten)]
1212
pub build: BuildOptions,
1313

14-
#[command(flatten)]
15-
pub fuzz_dir_wrapper: FuzzDirWrapper,
16-
1714
/// Name of the fuzz target to build, or build all targets if not supplied
1815
pub target: Option<String>,
1916
}
2017

2118
impl RunCommand for Build {
2219
fn run_command(&mut self) -> Result<()> {
23-
let project = FuzzProject::new(self.fuzz_dir_wrapper.fuzz_dir.to_owned())?;
20+
let project = FuzzProject::new(self.build.fuzz_dir_wrapper.get_manifest_path())?;
2421
project.exec_build(BuildMode::Build, &self.build, self.target.as_deref())
2522
}
2623
}

src/options/check.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
options::{BuildMode, BuildOptions, FuzzDirWrapper},
2+
options::{BuildMode, BuildOptions},
33
project::FuzzProject,
44
RunCommand,
55
};
@@ -11,16 +11,13 @@ pub struct Check {
1111
#[command(flatten)]
1212
pub build: BuildOptions,
1313

14-
#[command(flatten)]
15-
pub fuzz_dir_wrapper: FuzzDirWrapper,
16-
1714
/// Name of the fuzz target to check, or check all targets if not supplied
1815
pub target: Option<String>,
1916
}
2017

2118
impl RunCommand for Check {
2219
fn run_command(&mut self) -> Result<()> {
23-
let project = FuzzProject::new(self.fuzz_dir_wrapper.fuzz_dir.to_owned())?;
20+
let project = FuzzProject::new(self.build.fuzz_dir_wrapper.get_manifest_path())?;
2421
project.exec_build(BuildMode::Check, &self.build, self.target.as_deref())
2522
}
2623
}

src/options/cmin.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
use crate::{
2-
options::{BuildOptions, FuzzDirWrapper},
3-
project::FuzzProject,
4-
RunCommand,
5-
};
1+
use crate::{options::BuildOptions, project::FuzzProject, RunCommand};
62
use anyhow::Result;
73
use clap::Parser;
84
use std::path::PathBuf;
@@ -12,9 +8,6 @@ pub struct Cmin {
128
#[command(flatten)]
139
pub build: BuildOptions,
1410

15-
#[command(flatten)]
16-
pub fuzz_dir_wrapper: FuzzDirWrapper,
17-
1811
/// Name of the fuzz target
1912
pub target: String,
2013

@@ -29,7 +22,7 @@ pub struct Cmin {
2922

3023
impl RunCommand for Cmin {
3124
fn run_command(&mut self) -> Result<()> {
32-
let project = FuzzProject::new(self.fuzz_dir_wrapper.fuzz_dir.to_owned())?;
25+
let project = FuzzProject::new(self.build.fuzz_dir_wrapper.get_manifest_path())?;
3326
project.exec_cmin(self)
3427
}
3528
}

src/options/coverage.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
use std::path::PathBuf;
22

3-
use crate::{
4-
options::{BuildOptions, FuzzDirWrapper},
5-
project::FuzzProject,
6-
RunCommand,
7-
};
3+
use crate::{options::BuildOptions, project::FuzzProject, RunCommand};
84
use anyhow::{bail, Result};
95
use clap::Parser;
106

@@ -13,9 +9,6 @@ pub struct Coverage {
139
#[command(flatten)]
1410
pub build: BuildOptions,
1511

16-
#[command(flatten)]
17-
pub fuzz_dir_wrapper: FuzzDirWrapper,
18-
1912
/// Sets the path to the LLVM bin directory. By default, it will use the one installed with rustc
2013
#[arg(long)]
2114
pub llvm_path: Option<PathBuf>,
@@ -39,7 +32,7 @@ impl RunCommand for Coverage {
3932
see https://github.com/rust-lang/wg-cargo-std-aware/issues/63"
4033
);
4134
}
42-
let project = FuzzProject::new(self.fuzz_dir_wrapper.fuzz_dir.to_owned())?;
35+
let project = FuzzProject::new(self.build.fuzz_dir_wrapper.get_manifest_path())?;
4336
self.build.coverage = true;
4437
project.exec_coverage(self)
4538
}

src/options/fmt.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
use crate::{
2-
options::{BuildOptions, FuzzDirWrapper},
3-
project::FuzzProject,
4-
RunCommand,
5-
};
1+
use crate::{options::BuildOptions, project::FuzzProject, RunCommand};
62
use anyhow::Result;
73
use clap::Parser;
84
use std::path::PathBuf;
@@ -12,9 +8,6 @@ pub struct Fmt {
128
#[command(flatten)]
139
pub build: BuildOptions,
1410

15-
#[command(flatten)]
16-
pub fuzz_dir_wrapper: FuzzDirWrapper,
17-
1811
/// Name of fuzz target
1912
pub target: String,
2013

@@ -24,7 +17,7 @@ pub struct Fmt {
2417

2518
impl RunCommand for Fmt {
2619
fn run_command(&mut self) -> Result<()> {
27-
let project = FuzzProject::new(self.fuzz_dir_wrapper.fuzz_dir.to_owned())?;
20+
let project = FuzzProject::new(self.build.fuzz_dir_wrapper.get_manifest_path())?;
2821
project.debug_fmt_input(self)
2922
}
3023
}

src/options/init.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
options::{FuzzDirWrapper, FuzzEngine},
2+
options::{FuzzDirWrapper, FuzzEngine, ManifestPath},
33
project::FuzzProject,
44
RunCommand,
55
};
@@ -28,7 +28,20 @@ pub struct Init {
2828

2929
impl RunCommand for Init {
3030
fn run_command(&mut self) -> Result<()> {
31-
FuzzProject::init(self, self.fuzz_dir_wrapper.fuzz_dir.to_owned())?;
31+
let manifest_path = if let Some(manifest_path) = self.fuzz_dir_wrapper.get_manifest_path() {
32+
manifest_path
33+
} else {
34+
let metadata = cargo_metadata::MetadataCommand::new().no_deps().exec()?;
35+
ManifestPath(
36+
metadata
37+
.workspace_root
38+
.to_path_buf()
39+
.join("fuzz")
40+
.join("Cargo.toml")
41+
.into(),
42+
)
43+
};
44+
FuzzProject::init(manifest_path, self)?;
3245
Ok(())
3346
}
3447
}

0 commit comments

Comments
 (0)