Skip to content

Commit 3ebdda6

Browse files
committed
feat(toml): Add autolib
PR rust-lang#5335 added `autobins`, etc for rust-lang#5330. Nowhere in there is discussion of `autolib`. Cargo script disables support for additional build-targets by disabling discovery. Except we don't have a way to disable discovery of `autolib`, leading to rust-lang#14476. By adding `autolib`, we can continue in that direction. This also allows us to bypass inferring of libs on published packages, like all other build-targets which were handled in rust-lang#13849. As this seems fairly low controversy, this insta-stabilizes the field. In prior versions of Cargo, users will get an "unused manifest key" warning. For packags where this is set by `cargo publish`, the warning will be suppressed and things will work as normal. For `cargo vendor`, the same except there will be some churn in the vendored source as this field will now be set. For local development, it should be rare to set `autolib` so the lack of error by discovering a file when this is set shouldn't be a problem. Fixes rust-lang#14476
1 parent ebd326f commit 3ebdda6

File tree

16 files changed

+104
-15
lines changed

16 files changed

+104
-15
lines changed

crates/cargo-util-schemas/src/manifest/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ pub struct TomlPackage {
173173
pub publish: Option<InheritableVecStringOrBool>,
174174
pub workspace: Option<String>,
175175
pub im_a_teapot: Option<bool>,
176+
pub autolib: Option<bool>,
176177
pub autobins: Option<bool>,
177178
pub autoexamples: Option<bool>,
178179
pub autotests: Option<bool>,
@@ -217,6 +218,7 @@ impl TomlPackage {
217218
publish: None,
218219
workspace: None,
219220
im_a_teapot: None,
221+
autolib: None,
220222
autobins: None,
221223
autoexamples: None,
222224
autotests: None,

src/cargo/util/toml/embedded.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ use crate::GlobalContext;
88

99
const DEFAULT_EDITION: crate::core::features::Edition =
1010
crate::core::features::Edition::LATEST_STABLE;
11-
const AUTO_FIELDS: &[&str] = &["autobins", "autoexamples", "autotests", "autobenches"];
11+
const AUTO_FIELDS: &[&str] = &[
12+
"autolib",
13+
"autobins",
14+
"autoexamples",
15+
"autotests",
16+
"autobenches",
17+
];
1218

1319
pub(super) fn expand_manifest(
1420
content: &str,
@@ -289,6 +295,7 @@ path = "/home/me/test.rs"
289295
autobenches = false
290296
autobins = false
291297
autoexamples = false
298+
autolib = false
292299
autotests = false
293300
build = false
294301
edition = "2021"
@@ -324,6 +331,7 @@ time = "0.1.25"
324331
autobenches = false
325332
autobins = false
326333
autoexamples = false
334+
autolib = false
327335
autotests = false
328336
build = false
329337
edition = "2021"
@@ -359,6 +367,7 @@ time = "0.1.25"
359367
autobenches = false
360368
autobins = false
361369
autoexamples = false
370+
autolib = false
362371
autotests = false
363372
build = false
364373
edition = "2021"

src/cargo/util/toml/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ fn normalize_toml(
334334
package_root,
335335
&original_package.name,
336336
edition,
337+
original_package.autolib,
337338
warnings,
338339
)?;
339340
normalized_toml.bin = Some(targets::normalize_bins(
@@ -624,6 +625,7 @@ fn normalize_package_toml<'a>(
624625
.map(manifest::InheritableField::Value),
625626
workspace: original_package.workspace.clone(),
626627
im_a_teapot: original_package.im_a_teapot.clone(),
628+
autolib: Some(false),
627629
autobins: Some(false),
628630
autoexamples: Some(false),
629631
autotests: Some(false),

src/cargo/util/toml/targets.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,22 @@ pub fn normalize_lib(
129129
package_root: &Path,
130130
package_name: &str,
131131
edition: Edition,
132+
autodiscover: Option<bool>,
132133
warnings: &mut Vec<String>,
133134
) -> CargoResult<Option<TomlLibTarget>> {
134-
{
135+
if is_normalized(original_lib, autodiscover) {
136+
let Some(lib) = original_lib.cloned() else {
137+
return Ok(None);
138+
};
139+
140+
// Check early to improve error messages
141+
validate_lib_name(&lib, warnings)?;
142+
143+
validate_proc_macro(&lib, "library", edition, warnings)?;
144+
validate_crate_types(&lib, "library", edition, warnings)?;
145+
146+
Ok(Some(lib))
147+
} else {
135148
let inferred = inferred_lib(package_root);
136149
let lib = original_lib.cloned().or_else(|| {
137150
inferred.as_ref().map(|lib| TomlTarget {
@@ -528,6 +541,17 @@ fn to_bench_targets(
528541
Ok(result)
529542
}
530543

544+
fn is_normalized(toml_target: Option<&TomlTarget>, autodiscover: Option<bool>) -> bool {
545+
if autodiscover != Some(false) {
546+
return false;
547+
}
548+
549+
let Some(toml_target) = toml_target else {
550+
return true;
551+
};
552+
toml_target.name.is_some() && toml_target.path.is_some()
553+
}
554+
531555
fn are_normalized(toml_targets: Option<&Vec<TomlTarget>>, autodiscover: Option<bool>) -> bool {
532556
if autodiscover != Some(false) {
533557
return false;

src/doc/src/reference/cargo-targets.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,14 @@ configuration tables, such as `[lib]`, `[[bin]]`, `[[test]]`, `[[bench]]`, or
323323
standard directory layout.
324324

325325
The automatic target discovery can be disabled so that only manually
326-
configured targets will be built. Setting the keys `autobins`, `autoexamples`,
326+
configured targets will be built. Setting the keys `autolib`, `autobins`, `autoexamples`,
327327
`autotests`, or `autobenches` to `false` in the `[package]` section will
328328
disable auto-discovery of the corresponding target type.
329329

330330
```toml
331331
[package]
332332
# ...
333+
autolib = false
333334
autobins = false
334335
autoexamples = false
335336
autotests = false
@@ -363,6 +364,9 @@ autobins = false
363364
> is `false` if at least one target is manually defined in `Cargo.toml`.
364365
> Beginning with the 2018 edition, the default is always `true`.
365366
367+
> **MSRV:** Respected as of 1.27 for `autobins`, `autoexamples`, `autotests`, and `autobenches`
368+
369+
> **MSRV:** Respected as of 1.83 for `autolib`
366370
367371
[Build cache]: ../guide/build-cache.md
368372
[Rust Edition]: ../../edition-guide/index.html

src/doc/src/reference/manifest.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Every manifest file consists of the following sections:
3030
* [`publish`](#the-publish-field) --- Can be used to prevent publishing the package.
3131
* [`metadata`](#the-metadata-table) --- Extra settings for external tools.
3232
* [`default-run`](#the-default-run-field) --- The default binary to run by [`cargo run`].
33+
* [`autolib`](cargo-targets.md#target-auto-discovery) --- Disables library auto discovery.
3334
* [`autobins`](cargo-targets.md#target-auto-discovery) --- Disables binary auto discovery.
3435
* [`autoexamples`](cargo-targets.md#target-auto-discovery) --- Disables example auto discovery.
3536
* [`autotests`](cargo-targets.md#target-auto-discovery) --- Disables test auto discovery.

src/doc/src/reference/unstable.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,7 @@ Inferred / defaulted manifest fields:
13141314

13151315
Disallowed manifest fields:
13161316
- `[workspace]`, `[lib]`, `[[bin]]`, `[[example]]`, `[[test]]`, `[[bench]]`
1317-
- `package.workspace`, `package.build`, `package.links`, `package.autobins`, `package.autoexamples`, `package.autotests`, `package.autobenches`
1317+
- `package.workspace`, `package.build`, `package.links`, `package.autolib`, `package.autobins`, `package.autoexamples`, `package.autotests`, `package.autobenches`
13181318

13191319
The default `CARGO_TARGET_DIR` for single-file packages is at `$CARGO_HOME/target/<hash>`:
13201320
- Avoid conflicts from multiple single-file packages being in the same directory

tests/testsuite/artifact_dep.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2272,6 +2272,7 @@ name = "foo"
22722272
version = "0.1.0"
22732273
authors = []
22742274
build = false
2275+
autolib = false
22752276
autobins = false
22762277
autoexamples = false
22772278
autotests = false

tests/testsuite/features2.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1800,6 +1800,7 @@ name = "a"
18001800
version = "0.1.0"
18011801
authors = ["Zzz"]
18021802
build = false
1803+
autolib = false
18031804
autobins = false
18041805
autoexamples = false
18051806
autotests = false

tests/testsuite/features_namespaced.rs

+2
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,7 @@ edition = "2015"
985985
name = "foo"
986986
version = "0.1.0"
987987
build = false
988+
autolib = false
988989
autobins = false
989990
autoexamples = false
990991
autotests = false
@@ -1112,6 +1113,7 @@ edition = "2015"
11121113
name = "foo"
11131114
version = "0.1.0"
11141115
build = false
1116+
autolib = false
11151117
autobins = false
11161118
autoexamples = false
11171119
autotests = false

tests/testsuite/inheritable_workspace_fields.rs

+5
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ include = [
224224
"Cargo.toml",
225225
]
226226
publish = true
227+
autolib = false
227228
autobins = false
228229
autoexamples = false
229230
autotests = false
@@ -394,6 +395,7 @@ name = "bar"
394395
version = "0.2.0"
395396
authors = []
396397
build = false
398+
autolib = false
397399
autobins = false
398400
autoexamples = false
399401
autotests = false
@@ -533,6 +535,7 @@ name = "bar"
533535
version = "0.2.0"
534536
authors = []
535537
build = false
538+
autolib = false
536539
autobins = false
537540
autoexamples = false
538541
autotests = false
@@ -793,6 +796,7 @@ include = [
793796
"README.md",
794797
]
795798
publish = true
799+
autolib = false
796800
autobins = false
797801
autoexamples = false
798802
autotests = false
@@ -966,6 +970,7 @@ name = "bar"
966970
version = "0.2.0"
967971
authors = []
968972
build = false
973+
autolib = false
969974
autobins = false
970975
autoexamples = false
971976
autotests = false

0 commit comments

Comments
 (0)