Skip to content

Commit 5cc7dca

Browse files
committed
Respect CARGO_TARGET_DIR
This applies also to cargo config (`build.target-dir` key) via cargo_metadata.
1 parent 0bb5e20 commit 5cc7dca

6 files changed

Lines changed: 61 additions & 53 deletions

File tree

‎src/bin/cargo-ziggy/build.rs‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,8 @@ impl Build {
2121

2222
if !self.no_afl {
2323
eprintln!(" {} afl", style("Building").red().bold());
24-
let mut afl_args = vec![
25-
"afl",
26-
"build",
27-
"--features=ziggy/afl",
28-
"--target-dir=target/afl",
29-
];
24+
let target_dir = format!("--target-dir={}", super::target_dir().join("afl"));
25+
let mut afl_args = vec!["afl", "build", "--features=ziggy/afl", &target_dir];
3026

3127
// Add the --release argument if self.release is true
3228
if self.release {
@@ -103,7 +99,7 @@ impl Build {
10399
// Second fuzzer we build: Honggfuzz
104100
let run = process::Command::new(&cargo)
105101
.args(hfuzz_args)
106-
.env("CARGO_TARGET_DIR", "./target/honggfuzz")
102+
.env("CARGO_TARGET_DIR", super::target_dir().join("honggfuzz"))
107103
.env("HFUZZ_BUILD_ARGS", "--features=ziggy/honggfuzz")
108104
.env("RUSTFLAGS", env::var("RUSTFLAGS").unwrap_or_default())
109105
.stdout(process::Stdio::piped())

‎src/bin/cargo-ziggy/coverage.rs‎

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ impl Cover {
3939

4040
// Get the absolute path for the coverage directory to ensure .profraw files
4141
// are created in the correct location, even in workspace scenarios
42-
let coverage_target_dir = env::current_dir()
43-
.unwrap()
44-
.join("target/coverage/debug/deps");
42+
let coverage_target_dir = super::target_dir().join("coverage/debug/deps");
4543
let profile_file = coverage_target_dir.join("coverage-%p-%m.profraw");
4644

4745
let coverage_corpus = if input_path.is_dir() {
@@ -55,13 +53,15 @@ impl Cover {
5553
};
5654

5755
for file in coverage_corpus {
58-
let _ = process::Command::new(format!("./target/coverage/debug/{}", &self.target))
59-
.arg(file.display().to_string())
60-
.env("LLVM_PROFILE_FILE", profile_file.display().to_string())
61-
.spawn()
62-
.unwrap()
63-
.wait_with_output()
64-
.unwrap();
56+
let _ = process::Command::new(
57+
super::target_dir().join(format!("coverage/debug/{}", &self.target)),
58+
)
59+
.arg(file.display().to_string())
60+
.env("LLVM_PROFILE_FILE", &profile_file)
61+
.spawn()
62+
.unwrap()
63+
.wait_with_output()
64+
.unwrap();
6565
}
6666

6767
let source_or_workspace_root = self.source.as_ref().map_or_else(
@@ -107,13 +107,10 @@ impl Cover {
107107
env::var("COVERAGE_RUSTFLAGS").unwrap_or_else(|_| "-Cinstrument-coverage".to_string());
108108
coverage_rustflags.push(' ');
109109
coverage_rustflags.push_str(&env::var("RUSTFLAGS").unwrap_or_default());
110+
let target_dir = format!("--target-dir={}", super::target_dir().join("coverage"));
110111

111112
let build = process::Command::new(&cargo)
112-
.args([
113-
"rustc",
114-
"--target-dir=target/coverage",
115-
"--features=ziggy/coverage",
116-
])
113+
.args(["rustc", "--features=ziggy/coverage", &target_dir])
117114
.env("RUSTFLAGS", coverage_rustflags)
118115
.spawn()
119116
.context("⚠️ couldn't spawn rustc for coverage")?
@@ -134,14 +131,15 @@ impl Cover {
134131
let coverage = process::Command::new("grcov")
135132
.args([
136133
".",
137-
&format!("-b=./target/coverage/debug/{target}"),
134+
&format!("-b={}/coverage/debug/{target}", super::target_dir()),
138135
&format!("-s={source_or_workspace_root}"),
139136
&format!("-t={output_types}"),
140137
"--llvm",
141138
"--branch",
142139
"--ignore-not-existing",
143140
&format!("-o={coverage_dir}"),
144141
])
142+
.current_dir(super::target_dir().join("coverage/debug/deps"))
145143
.spawn()
146144
.context("⚠️ cannot find grcov in your path, please install it")?
147145
.wait()
@@ -154,12 +152,10 @@ impl Cover {
154152

155153
pub fn clean_old_cov() -> Result<(), anyhow::Error> {
156154
// Use absolute path to ensure we clean the correct location in workspaces
157-
let coverage_deps_dir = env::current_dir()
158-
.unwrap()
159-
.join("target/coverage/debug/deps");
155+
let coverage_deps_dir = super::target_dir().join("coverage/debug/deps");
160156
let pattern = coverage_deps_dir.join("*.profraw");
161157

162-
if let Ok(profile_files) = glob(&pattern.display().to_string()) {
158+
if let Ok(profile_files) = glob(pattern.as_str()) {
163159
for file in profile_files.flatten() {
164160
let file_string = &file.display();
165161
fs::remove_file(&file)

‎src/bin/cargo-ziggy/fuzz.rs‎

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,9 @@ impl Fuzz {
210210
.elapsed()
211211
.unwrap_or_default();
212212
if prev_start_time.map_or(Duration::MAX, |s| s.elapsed()) >= created {
213-
let _ = process::Command::new(format!(
214-
"./target/coverage/debug/{}",
215-
&target
216-
))
213+
let _ = process::Command::new(
214+
super::target_dir().join("coverage/debug").join(&target),
215+
)
217216
.arg(format!("{}", entry.display()))
218217
.stdout(Stdio::null())
219218
.stderr(Stdio::null())
@@ -224,8 +223,7 @@ impl Fuzz {
224223
if seen_new_entry {
225224
let coverage_dir = output_target + "/coverage";
226225
let _ = fs::remove_dir_all(&coverage_dir);
227-
Cover::run_grcov(&target.clone(), "html", &coverage_dir, &workspace_root)
228-
.unwrap();
226+
Cover::run_grcov(&target, "html", &coverage_dir, &workspace_root).unwrap();
229227
}
230228

231229
*cov_end_time.lock().unwrap() = Instant::now();
@@ -461,18 +459,21 @@ impl Fuzz {
461459
0 => "AFL_FINAL_SYNC",
462460
_ => "_DUMMY_VAR",
463461
};
464-
let target_path = match self.fuzz_binary() {
465-
true => self.target.clone(),
466-
false => {
467-
if self.release {
468-
format!("./target/afl/release/{}", self.target)
469-
} else if self.asan && job_num == 0 {
470-
format!("./target/afl/{ASAN_TARGET}/debug/{}", self.target)
471-
} else {
472-
format!("./target/afl/debug/{}", self.target)
473-
}
462+
let target_path = self.binary.clone().unwrap_or_else(|| {
463+
if self.release {
464+
super::target_dir()
465+
.join(format!("afl/release/{}", self.target))
466+
.into_std_path_buf()
467+
} else if self.asan && job_num == 0 {
468+
super::target_dir()
469+
.join(format!("afl/{ASAN_TARGET}/debug/{}", self.target))
470+
.into_std_path_buf()
471+
} else {
472+
super::target_dir()
473+
.join(format!("afl/debug/{}", self.target))
474+
.into_std_path_buf()
474475
}
475-
};
476+
});
476477

477478
let mut afl_flags = self.afl_flags.clone();
478479
if is_main_instance {
@@ -533,7 +534,7 @@ impl Fuzz {
533534
let hfuzz_help = process::Command::new(&cargo)
534535
.args(["hfuzz", "run", &self.target])
535536
.env("HFUZZ_BUILD_ARGS", "--features=ziggy/honggfuzz")
536-
.env("CARGO_TARGET_DIR", "./target/honggfuzz")
537+
.env("CARGO_TARGET_DIR", super::target_dir().join("honggfuzz"))
537538
.env(
538539
"HFUZZ_WORKSPACE",
539540
format!("{}/honggfuzz", self.output_target()),
@@ -578,7 +579,7 @@ impl Fuzz {
578579
"/dev/null",
579580
])
580581
.env("HFUZZ_BUILD_ARGS", "--features=ziggy/honggfuzz")
581-
.env("CARGO_TARGET_DIR", "./target/honggfuzz")
582+
.env("CARGO_TARGET_DIR", super::target_dir().join("honggfuzz"))
582583
.env(
583584
"HFUZZ_WORKSPACE",
584585
format!("{}/honggfuzz", self.output_target()),
@@ -587,8 +588,8 @@ impl Fuzz {
587588
"HFUZZ_RUN_ARGS",
588589
format!(
589590
"--input={} -o{}/honggfuzz/corpus -n{honggfuzz_jobs} -F{} --dynamic_input={}/queue {timeout_option} {dictionary_option} {memory_option}",
590-
&self.corpus(),
591-
&self.output_target(),
591+
self.corpus(),
592+
self.output_target(),
592593
self.max_length,
593594
self.output_target(),
594595
),

‎src/bin/cargo-ziggy/main.rs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,3 +401,16 @@ fn guess_target() -> Result<String> {
401401

402402
Err(anyhow!("Please specify a target"))
403403
}
404+
405+
fn target_dir() -> &'static cargo_metadata::camino::Utf8PathBuf {
406+
use std::sync::LazyLock;
407+
408+
static TARGET_DIR: LazyLock<cargo_metadata::camino::Utf8PathBuf> = LazyLock::new(|| {
409+
cargo_metadata::MetadataCommand::new().exec().map_or_else(
410+
|_| cargo_metadata::camino::Utf8PathBuf::from("target"),
411+
|metadata| metadata.target_directory,
412+
)
413+
});
414+
415+
&TARGET_DIR
416+
}

‎src/bin/cargo-ziggy/minimize.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ impl Minimize {
9797
0 | 1 => String::from("all"),
9898
t => format!("{t}"),
9999
};
100+
let target_dir = super::target_dir().join("afl/debug").join(&self.target);
100101

101102
// AFL++ minimization
102103
process::Command::new(&cargo)
@@ -112,7 +113,7 @@ impl Minimize {
112113
"-t",
113114
&format!("{}", self.timeout),
114115
"--",
115-
&format!("./target/afl/debug/{}", &self.target),
116+
target_dir.as_str(),
116117
])
117118
.stderr(File::create(format!(
118119
"{}/{}/logs/minimization_afl.log",
@@ -137,7 +138,7 @@ impl Minimize {
137138

138139
process::Command::new(&cargo)
139140
.args(["hfuzz", "run", &self.target])
140-
.env("CARGO_TARGET_DIR", "./target/honggfuzz")
141+
.env("CARGO_TARGET_DIR", super::target_dir().join("honggfuzz"))
141142
.env("HFUZZ_BUILD_ARGS", "--features=ziggy/honggfuzz")
142143
.env(
143144
"HFUZZ_WORKSPACE",

‎src/bin/cargo-ziggy/run.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ impl Run {
1414
pub fn run(&mut self) -> Result<(), anyhow::Error> {
1515
let cargo = env::var("CARGO").unwrap_or_else(|_| String::from("cargo"));
1616
let target = find_target(&self.target)?;
17+
let target_dir = format!("--target-dir={}", super::target_dir().join("runner"));
1718

18-
let mut args = vec!["rustc", "--target-dir=target/runner"];
19+
let mut args = vec!["rustc", &target_dir];
1920
let asan_target_str = format!("--target={ASAN_TARGET}");
2021
let mut rust_flags = env::var("RUSTFLAGS").unwrap_or_default();
2122
let mut rust_doc_flags = env::var("RUSTDOCFLAGS").unwrap_or_default();
@@ -89,9 +90,9 @@ impl Run {
8990
.collect();
9091

9192
let runner_path = if self.asan {
92-
format!("./target/runner/{ASAN_TARGET}/debug/{target}")
93+
super::target_dir().join(format!("runner/{ASAN_TARGET}/debug/{target}"))
9394
} else {
94-
format!("./target/runner/debug/{target}")
95+
super::target_dir().join(format!("runner/debug/{target}"))
9596
};
9697

9798
for file in input_files {

0 commit comments

Comments
 (0)