1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
mod audit;
mod build;
mod cli;
mod config;
mod meta;
mod out_dir_archive;
mod report;
mod self_update;
mod store;
mod zig;
use anyhow::Result;
use clap::{CommandFactory, Parser};
impl From<cli::TargetSelectionArgs> for build::TargetSelection {
fn from(args: cli::TargetSelectionArgs) -> Self {
Self {
lib: args.lib,
bins: args.bins,
tests: args.tests,
benches: args.all_benches,
examples: args.examples,
all_targets: args.all_targets,
}
}
}
fn main() {
if let Err(e) = real_main() {
if let Some(exit) = e.downcast_ref::<build::RunExit>() {
std::process::exit(exit.code);
}
eprintln!("corgi error: {e:#}");
std::process::exit(1);
}
}
fn real_main() -> Result<()> {
let argv: Vec<_> = std::env::args_os().collect();
if zig::is_linker_invocation(&argv) {
return zig::run_linker_invocation(&argv);
}
let had_argument_delimiter = argv.iter().any(|arg| arg == "--");
let invocation_dir = match cli::invocation_directory(&argv) {
Some(dir) if dir.is_absolute() => dir,
Some(dir) => std::env::current_dir()?.join(dir),
None => std::env::current_dir()?,
};
self_update::update_if_required(&invocation_dir, &argv)?;
let cli::Cli {
dir,
manifest_path,
verbose,
command,
} = cli::Cli::parse_from(argv);
let dir = if let Some(manifest) = manifest_path {
anyhow::ensure!(
manifest
.file_name()
.is_some_and(|name| name == "Cargo.toml")
&& manifest.is_file(),
"--manifest-path must name an existing Cargo.toml: {}",
manifest.display()
);
Some(
manifest
.parent()
.filter(|parent| !parent.as_os_str().is_empty())
.unwrap_or(std::path::Path::new("."))
.to_path_buf(),
)
} else {
dir
};
let dir = match dir {
Some(d) => d,
None => std::env::current_dir()?,
};
let Some(command) = command else {
cli::Cli::command().print_help()?;
return Ok(());
};
// Default: the store lives *directly at* the canonical machine-wide
// path, so embedded OUT_DIR paths are canonical with no indirection.
// CORGI_STORE relocates it (a symlink alias then preserves the
// canonical spelling).
let store_root = store::default_root()?;
match command {
cli::Command::Audit(args) => audit::audit(
&dir,
args.release,
verbose,
args.target.as_deref(),
args.root.as_deref(),
),
cli::Command::Pin => {
let path = build::pin_corgi_version(&dir, env!("CARGO_PKG_VERSION"))?;
eprintln!(
"{:>12} corgi {} in {}",
"Pinned",
env!("CARGO_PKG_VERSION"),
path.display()
);
Ok(())
}
command => {
let store = store::Store::new(store_root)?;
let run_build = |store: store::Store,
args: cli::BuildArgs,
workspace: bool,
benches: Vec<String>,
mode: build::Mode,
targets: cli::TargetSelectionArgs,
clippy_args: Vec<String>,
force_tests: bool,
test_timeout: Option<std::time::Duration>,
test_filters: Vec<String>,
exec_args: Vec<String>| {
if args.all_features {
anyhow::bail!(
"--all-features is not supported. Use corgi roots and explicitly test the feature combinations you care about."
);
}
build::build(
store,
&dir,
build::BuildOpts {
verbose,
release: args.release,
profile: args.profile,
workspace,
packages: args.packages,
bin: args.bin,
benches,
features: args.features,
target: args.target,
target_dir: args.target_dir,
root: args.root,
mode,
targets: targets.into(),
clippy_args,
timings: args.timings,
no_incremental: args.no_incremental,
force_tests,
test_timeout,
test_filters,
exec_args,
},
)
};
match command {
cli::Command::Build(args) => run_build(
store,
args.build,
args.workspace,
args.benches,
build::Mode::Build,
args.targets,
Vec::new(),
false,
None,
Vec::new(),
Vec::new(),
),
cli::Command::Bench(mut args) => {
if args.build.build.release {
anyhow::bail!("`corgi bench` does not accept `--release`");
}
if let Some(filter) = args.filter {
args.exec_args.insert(0, filter);
}
run_build(
store,
args.build.build,
args.build.workspace,
args.build.benches,
build::Mode::Bench,
args.build.targets,
Vec::new(),
false,
None,
Vec::new(),
args.exec_args,
)
}
cli::Command::Check(args) => run_build(
store,
args.build,
args.workspace,
args.benches,
build::Mode::Check,
args.targets,
Vec::new(),
false,
None,
Vec::new(),
Vec::new(),
),
cli::Command::Clippy(args) => run_build(
store,
args.build.build,
args.build.workspace,
args.build.benches,
build::Mode::Clippy,
args.build.targets,
args.clippy_args,
false,
None,
Vec::new(),
Vec::new(),
),
cli::Command::Run(args) => run_build(
store,
args.build,
false,
Vec::new(),
build::Mode::Run,
cli::TargetSelectionArgs::default(),
Vec::new(),
false,
None,
Vec::new(),
args.exec_args,
),
cli::Command::Test(args) => run_build(
store,
args.build,
args.workspace,
Vec::new(),
build::Mode::Test,
args.targets,
Vec::new(),
args.force,
args.timeout.map(std::time::Duration::from_secs),
args.filters,
args.exec_args,
),
cli::Command::Fmt(mut args) => {
if had_argument_delimiter {
args.args.insert(0, "--".to_string());
}
build::fmt(
store,
&dir,
args.workspace,
&args.packages,
verbose,
&args.args,
)
}
cli::Command::Clean(args) => build::clean(&store, args.cache, args.older_than),
cli::Command::Audit(_) | cli::Command::Pin => unreachable!(),
}
}
}
}