Skip to content

Commit 4b6f790

Browse files
committed
feat: Add unstable -Zdetect-antivirus option
With a build.detect-antivirus config option to disable the warning.
1 parent 332db70 commit 4b6f790

File tree

6 files changed

+44
-3
lines changed

6 files changed

+44
-3
lines changed

src/cargo/core/compiler/build_config.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ pub struct BuildConfig {
5252
pub sbom: bool,
5353
/// Build compile time dependencies only, e.g., build scripts and proc macros
5454
pub compile_time_deps_only: bool,
55+
/// Whether we should try to detect and warn when antivirus software might
56+
/// make newly created binaries slow to launch.
57+
pub detect_antivirus: bool,
5558
}
5659

5760
fn default_parallelism() -> CargoResult<u32> {
@@ -127,6 +130,19 @@ impl BuildConfig {
127130
_ => Vec::new(),
128131
};
129132

133+
let detect_antivirus = match (cfg.detect_antivirus, gctx.cli_unstable().detect_antivirus) {
134+
// Enabled by default (for now only when the flag is set).
135+
(None, unstable_flag) => unstable_flag,
136+
// But allow overriding with configuration option.
137+
(Some(cfg_option), true) => cfg_option,
138+
(Some(_), false) => {
139+
gctx.shell().warn(
140+
"ignoring 'build.detect-antivirus' config, pass `-Zdetect-antivirus` to enable it",
141+
)?;
142+
false
143+
}
144+
};
145+
130146
Ok(BuildConfig {
131147
requested_kinds,
132148
jobs,
@@ -145,6 +161,7 @@ impl BuildConfig {
145161
timing_outputs,
146162
sbom,
147163
compile_time_deps_only: false,
164+
detect_antivirus,
148165
})
149166
}
150167

src/cargo/core/features.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,7 @@ unstable_cli_options!(
854854
checksum_freshness: bool = ("Use a checksum to determine if output is fresh rather than filesystem mtime"),
855855
codegen_backend: bool = ("Enable the `codegen-backend` option in profiles in .cargo/config.toml file"),
856856
config_include: bool = ("Enable the `include` key in config files"),
857+
detect_antivirus: bool = ("Enable the experimental antivirus detection and the config option to disable it"),
857858
direct_minimal_versions: bool = ("Resolve minimal dependency versions instead of maximum (direct dependencies only)"),
858859
dual_proc_macros: bool = ("Build proc-macros for both the host and the target"),
859860
feature_unification: bool = ("Enable new feature unification modes in workspaces"),
@@ -1373,6 +1374,7 @@ impl CliUnstable {
13731374
"codegen-backend" => self.codegen_backend = parse_empty(k, v)?,
13741375
"config-include" => self.config_include = parse_empty(k, v)?,
13751376
"direct-minimal-versions" => self.direct_minimal_versions = parse_empty(k, v)?,
1377+
"detect-antivirus" => self.detect_antivirus = parse_empty(k, v)?,
13761378
"dual-proc-macros" => self.dual_proc_macros = parse_empty(k, v)?,
13771379
"feature-unification" => self.feature_unification = parse_empty(k, v)?,
13781380
"fix-edition" => {

src/cargo/ops/cargo_compile/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,8 +556,7 @@ where `<compatible-ver>` is the latest version supporting rustc {rustc_version}"
556556
}
557557
}
558558

559-
// TODO(madsmtm): Add some sort of option for this.
560-
if false {
559+
if build_config.detect_antivirus {
561560
// TODO(madsmtm): Maybe only do this when we have above a certain
562561
// number of build scripts or test binaries to run?
563562

src/cargo/util/context/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2770,6 +2770,8 @@ pub struct CargoBuildConfig {
27702770
pub sbom: Option<bool>,
27712771
/// Unstable feature `-Zbuild-analysis`.
27722772
pub analysis: Option<CargoBuildAnalysis>,
2773+
/// Unstable feature `-Zdetect-antivirus`.
2774+
pub detect_antivirus: Option<bool>,
27732775
}
27742776

27752777
/// Metrics collection for build analysis.

src/cargo/util/detect_antivirus/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ pub fn detect_and_report(gtcx: &GlobalContext) -> CargoResult<()> {
117117
We recommend this option by default, since it will make your \
118118
iteration time lower, though please be aware of the security \
119119
implications in doing this. Another option is to disable this \
120-
warning with TODO config.toml option.\n\
120+
warning by add `build.detect-antivirus = false` to your \
121+
~/.cargo/config.toml.\n\
121122
\
122123
See <https://support.apple.com/en-gb/guide/security/sec469d47bd8/web> \
123124
for more information.",

src/doc/src/reference/unstable.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ Each new feature described below should explain how to use it.
131131
* [Package message format](#package-message-format) --- Message format for `cargo package`.
132132
* [`fix-edition`](#fix-edition) --- A permanently unstable edition migration helper.
133133
* [Plumbing subcommands](https://github.com/crate-ci/cargo-plumbing) --- Low, level commands that act as APIs for Cargo, like `cargo metadata`
134+
* [Detect antivirus](#detect-antivirus) --- Detect whether newly created binaries may be slow to launch due to antivirus.
134135

135136
## allow-features
136137

@@ -1947,6 +1948,25 @@ enabled = true
19471948
Enables the new build-dir filesystem layout.
19481949
This layout change unblocks work towards caching and locking improvements.
19491950

1951+
## Detect Antivirus
1952+
1953+
* Tracking Issue: [#0](https://github.com/rust-lang/cargo/issues/0)
1954+
1955+
The `-Zdetect-antivirus` flag enables detection of antivirus software that might make launching a binary for the first time slower (which in turn makes Cargo's build scripts and tests slower), and outputs a descriptive warning to the user if this is the case.
1956+
1957+
This feature will be enabled by default in the future, with the `build.detect-antivirus` option to opt-out.
1958+
1959+
Currently only implemented for macOS' XProtect/Gatekeeper, but could be expanded to Windows Defender in the future.
1960+
1961+
```toml
1962+
# Example ~/.cargo/config.toml
1963+
1964+
# Disable warning, e.g. if using a workplace-issued Mac that
1965+
# doesn't allow granting Developer Tool permissions.
1966+
[build]
1967+
detect-antivirus = false
1968+
```
1969+
19501970

19511971
# Stabilized and removed features
19521972

0 commit comments

Comments
 (0)