| 1 |
// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 |
|
| 3 |
/*! |
| 4 |
<!-- Note: Document from sync-markdown-to-rustdoc:start through sync-markdown-to-rustdoc:end |
| 5 |
is synchronized from README.md. Any changes to that range are not preserved. --> |
| 6 |
<!-- tidy:sync-markdown-to-rustdoc:start --> |
| 7 |
|
| 8 |
Load and resolve [Cargo configuration](https://doc.rust-lang.org/nightly/cargo/reference/config.html). |
| 9 |
|
| 10 |
This library is intended to accurately emulate the actual behavior of Cargo configuration, for example, this supports the following behaviors: |
| 11 |
|
| 12 |
- [Hierarchical structure and merge](https://doc.rust-lang.org/nightly/cargo/reference/config.html#hierarchical-structure) |
| 13 |
- [Environment variables](https://doc.rust-lang.org/nightly/cargo/reference/config.html#environment-variables) and [relative paths](https://doc.rust-lang.org/nightly/cargo/reference/config.html#config-relative-paths) resolution. |
| 14 |
- `target.<triple>` and `target.<cfg>` resolution. |
| 15 |
|
| 16 |
Supported tables and fields are mainly based on [cargo-llvm-cov](https://github.com/taiki-e/cargo-llvm-cov)'s use cases, but feel free to submit an issue if you see something missing in your use case. |
| 17 |
|
| 18 |
## Usage |
| 19 |
|
| 20 |
Add this to your `Cargo.toml`: |
| 21 |
|
| 22 |
```toml |
| 23 |
[dependencies] |
| 24 |
cargo-config2 = "0.1" |
| 25 |
``` |
| 26 |
|
| 27 |
`cargo-config2` is usually runnable with Cargo versions older than the Rust version required for build. (e.g., a cargo subcommand using `cargo-config2` could work with older versions such as `cargo +1.59 <subcommand>`.) |
| 28 |
|
| 29 |
## Examples |
| 30 |
|
| 31 |
``` |
| 32 |
// Read config files hierarchically from the current directory, merge them, |
| 33 |
// apply environment variables, and resolve relative paths. |
| 34 |
let config = cargo_config2::Config::load().unwrap(); |
| 35 |
let target = "x86_64-unknown-linux-gnu"; |
| 36 |
// Resolve target-specific configuration (`target.<triple>` and `target.<cfg>`), |
| 37 |
// and returns the resolved rustflags for `target`. |
| 38 |
let rustflags = config.rustflags(target).unwrap(); |
| 39 |
println!("{rustflags:?}"); |
| 40 |
``` |
| 41 |
|
| 42 |
See also the [`get` example](https://github.com/taiki-e/cargo-config2/blob/HEAD/examples/get.rs) that partial re-implementation of `cargo config get` using cargo-config2. |
| 43 |
|
| 44 |
<!-- tidy:sync-markdown-to-rustdoc:end --> |
| 45 |
*/ |
| 46 |
|
| 47 |
#![no_std] |
| 48 |
#![doc(test( |
| 49 |
no_crate_inject, |
| 50 |
attr(allow( |
| 51 |
dead_code, |
| 52 |
unused_variables, |
| 53 |
clippy::undocumented_unsafe_blocks, |
| 54 |
clippy::unused_trait_names, |
| 55 |
)) |
| 56 |
))] |
| 57 |
#![forbid(unsafe_code)] |
| 58 |
#![warn( |
| 59 |
// Lints that may help when writing public library. |
| 60 |
missing_debug_implementations, |
| 61 |
// missing_docs, |
| 62 |
clippy::alloc_instead_of_core, |
| 63 |
clippy::exhaustive_enums, |
| 64 |
clippy::exhaustive_structs, |
| 65 |
clippy::impl_trait_in_params, |
| 66 |
clippy::std_instead_of_alloc, |
| 67 |
clippy::std_instead_of_core, |
| 68 |
// clippy::missing_inline_in_public_items, |
| 69 |
)] |
| 70 |
#![allow(clippy::must_use_candidate)] |
| 71 |
|
| 72 |
// Refs: |
| 73 |
// - https://doc.rust-lang.org/nightly/cargo/reference/config.html |
| 74 |
|
| 75 |
extern crate alloc; |
| 76 |
extern crate std; |
| 77 |
|
| 78 |
#[macro_use] |
| 79 |
mod error; |
| 80 |
|
| 81 |
#[cfg(test)] |
| 82 |
#[path = "gen/tests/assert_impl.rs"] |
| 83 |
mod assert_impl; |
| 84 |
#[path = "gen/cfg.rs"] |
| 85 |
pub mod cfg; |
| 86 |
#[path = "gen/is_none.rs"] |
| 87 |
mod is_none_impl; |
| 88 |
#[cfg(test)] |
| 89 |
#[cfg(not(windows))] // TODO: size is large for some reason. |
| 90 |
#[path = "gen/tests/track_size.rs"] |
| 91 |
mod track_size; |
| 92 |
|
| 93 |
#[macro_use] |
| 94 |
mod process; |
| 95 |
|
| 96 |
mod cfg_expr; |
| 97 |
pub mod de; |
| 98 |
mod easy; |
| 99 |
mod env; |
| 100 |
mod merge; |
| 101 |
mod resolve; |
| 102 |
mod value; |
| 103 |
mod walk; |
| 104 |
|
| 105 |
#[doc(no_inline)] |
| 106 |
pub use self::de::{Color, Frequency, RegistriesProtocol, VersionControlSoftware, When}; |
| 107 |
pub use self::{ |
| 108 |
easy::{ |
| 109 |
BuildConfig, CargoNewConfig, Config, CredentialProvider, DocConfig, EnvConfigValue, Flags, |
| 110 |
FutureIncompatReportConfig, GlobalCredentialProviders, HttpConfig, NetConfig, PathAndArgs, |
| 111 |
RegistriesConfigValue, RegistryConfig, SourceConfigValue, StringList, TargetConfig, |
| 112 |
TermConfig, TermProgressConfig, |
| 113 |
}, |
| 114 |
error::Error, |
| 115 |
resolve::{CargoVersion, ResolveOptions, RustcVersion, TargetTriple, TargetTripleRef}, |
| 116 |
walk::{Walk, cargo_home_with_cwd, home_dir, rustup_home_with_cwd}, |
| 117 |
}; |