Struct cargo_config2::Config
source · #[non_exhaustive]pub struct Config {
pub alias: BTreeMap<String, StringList>,
pub build: BuildConfig,
pub doc: DocConfig,
pub env: BTreeMap<String, EnvConfigValue>,
pub future_incompat_report: FutureIncompatReportConfig,
pub cargo_new: CargoNewConfig,
pub http: HttpConfig,
pub net: NetConfig,
pub registries: BTreeMap<String, RegistriesConfigValue>,
pub registry: RegistryConfig,
pub term: TermConfig,
/* private fields */
}Expand description
Cargo configuration.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.alias: BTreeMap<String, StringList>The [alias] table.
build: BuildConfigThe [build] table.
doc: DocConfigThe [doc] table.
env: BTreeMap<String, EnvConfigValue>The [env] table.
future_incompat_report: FutureIncompatReportConfigThe [future-incompat-report] table.
cargo_new: CargoNewConfigThe [cargo-new] table.
http: HttpConfigThe [http] table.
net: NetConfigThe [net] table.
registries: BTreeMap<String, RegistriesConfigValue>The [registries] table.
registry: RegistryConfigThe [registry] table.
term: TermConfigThe [term] table.
Implementations§
source§impl Config
impl Config
sourcepub fn load() -> Result<Self, Error>
pub fn load() -> Result<Self, Error>
Read config files hierarchically from the current directory and merges them.
sourcepub fn load_with_cwd<P: AsRef<Path>>(cwd: P) -> Result<Self, Error>
pub fn load_with_cwd<P: AsRef<Path>>(cwd: P) -> Result<Self, Error>
Read config files hierarchically from the given directory and merges them.
sourcepub fn load_with_options<P: AsRef<Path>>(
cwd: P,
options: ResolveOptions,
) -> Result<Self, Error>
pub fn load_with_options<P: AsRef<Path>>( cwd: P, options: ResolveOptions, ) -> Result<Self, Error>
Read config files hierarchically from the given directory and merges them.
sourcepub fn build_target_for_config<'a, I: IntoIterator<Item = T>, T: Into<TargetTripleRef<'a>>>(
&self,
targets: I,
) -> Result<Vec<TargetTriple>, Error>
pub fn build_target_for_config<'a, I: IntoIterator<Item = T>, T: Into<TargetTripleRef<'a>>>( &self, targets: I, ) -> Result<Vec<TargetTriple>, Error>
Selects target triples to build.
The targets returned are based on the order of priority in which cargo selects the target to be used for the build.
--targetoption (targets)CARGO_BUILD_TARGETenvironment variablebuild.targetconfig- host triple
Note: The result of this function is intended to handle target-specific
configurations and is not always appropriate to propagate directly to Cargo.
See build_target_for_cli for more.
§Multi-target support
Cargo 1.64+ supports multi-target builds.
Therefore, this function may return multiple targets if multiple targets
are specified in targets or build.target config.
§Custom target support
rustc allows you to build a custom target by specifying a target-spec file. If a target-spec file is specified as the target, rustc considers the file stem of that file to be the target triple name.
Since target-specific configs are referred by target triple name, this function also converts the target specified in the path to a target triple name.
§Examples
With single-target:
use anyhow::bail;
use clap::Parser;
#[derive(Parser)]
struct Args {
#[clap(long)]
target: Option<String>,
}
let args = Args::parse();
let config = cargo_config2::Config::load()?;
let mut targets = config.build_target_for_config(args.target.as_ref())?;
if targets.len() != 1 {
bail!("multi-target build is not supported: {targets:?}");
}
let target = targets.pop().unwrap();
println!("{:?}", config.rustflags(target));With multi-target:
use clap::Parser;
#[derive(Parser)]
struct Args {
#[clap(long)]
target: Vec<String>,
}
let args = Args::parse();
let config = cargo_config2::Config::load()?;
let targets = config.build_target_for_config(&args.target)?;
for target in targets {
println!("{:?}", config.rustflags(target)?);
}sourcepub fn build_target_for_cli<I: IntoIterator<Item = S>, S: AsRef<str>>(
&self,
targets: I,
) -> Result<Vec<String>, Error>
pub fn build_target_for_cli<I: IntoIterator<Item = S>, S: AsRef<str>>( &self, targets: I, ) -> Result<Vec<String>, Error>
Selects target triples to pass to CLI.
The targets returned are based on the order of priority in which cargo selects the target to be used for the build.
--targetoption (targets)CARGO_BUILD_TARGETenvironment variablebuild.targetconfig
Unlike build_target_for_config,
host triple is not referenced. This is because the behavior of Cargo
changes depending on whether or not --target option (or one of the
above) is set.
Also, Unlike build_target_for_config
the target name specified in path is preserved.
sourcepub fn target<'a, T: Into<TargetTripleRef<'a>>>(
&self,
target: T,
) -> Result<TargetConfig, Error>
pub fn target<'a, T: Into<TargetTripleRef<'a>>>( &self, target: T, ) -> Result<TargetConfig, Error>
Returns the resolved [target] table for the given target.
sourcepub fn linker<'a, T: Into<TargetTripleRef<'a>>>(
&self,
target: T,
) -> Result<Option<PathBuf>, Error>
pub fn linker<'a, T: Into<TargetTripleRef<'a>>>( &self, target: T, ) -> Result<Option<PathBuf>, Error>
Returns the resolved linker path for the given target.
sourcepub fn runner<'a, T: Into<TargetTripleRef<'a>>>(
&self,
target: T,
) -> Result<Option<PathAndArgs>, Error>
pub fn runner<'a, T: Into<TargetTripleRef<'a>>>( &self, target: T, ) -> Result<Option<PathAndArgs>, Error>
Returns the resolved runner path and args for the given target.
sourcepub fn rustflags<'a, T: Into<TargetTripleRef<'a>>>(
&self,
target: T,
) -> Result<Option<Flags>, Error>
pub fn rustflags<'a, T: Into<TargetTripleRef<'a>>>( &self, target: T, ) -> Result<Option<Flags>, Error>
Returns the resolved rustflags for the given target.
sourcepub fn rustdocflags<'a, T: Into<TargetTripleRef<'a>>>(
&self,
target: T,
) -> Result<Option<Flags>, Error>
pub fn rustdocflags<'a, T: Into<TargetTripleRef<'a>>>( &self, target: T, ) -> Result<Option<Flags>, Error>
Returns the resolved rustdocflags for the given target.
sourcepub fn rustc(&self) -> &PathAndArgs
pub fn rustc(&self) -> &PathAndArgs
Returns the path and args that calls rustc.
If RUSTC_WRAPPER or
RUSTC_WORKSPACE_WRAPPER is set,
the path is the wrapper path and the argument is the rustc path.
Otherwise, the path is the rustc path.
If you set rustc path by ResolveOptions::rustc, this returns the path set by it.
sourcepub fn cargo(&self) -> &OsStr
pub fn cargo(&self) -> &OsStr
Returns the path to cargo.
The returned path is the value of the CARGO environment variable if it is set. Otherwise, “cargo”.
If you set cargo path by ResolveOptions::cargo, this returns the path set by it.
sourcepub fn host_triple(&self) -> Result<&str, Error>
pub fn host_triple(&self) -> Result<&str, Error>
Returns the host triple.
sourcepub fn rustc_version(&self) -> Result<RustcVersion, Error>
pub fn rustc_version(&self) -> Result<RustcVersion, Error>
Returns the version of the current rustc.
The result is usually the same as cargo_version,
but it may differ if a different rustc is specified in config or if the
user is manipulating the output of the rustc.
§rustc_version vs cargo_version
Which is the preferred to use depends on the situation:
- You will need to know the rustc version to determine whether options passed to rustc
via RUSTFLAGS or RUSTDOCFLAGS like
-C instrument-coverageare available. - You will need to know the cargo version to determine whether fields in
Cargo.tomlor cargo’s CLI options are available.
sourcepub fn cargo_version(&self) -> Result<CargoVersion, Error>
pub fn cargo_version(&self) -> Result<CargoVersion, Error>
Returns the version of the current cargo.
See also rustc_version.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Config
impl !RefUnwindSafe for Config
impl Send for Config
impl !Sync for Config
impl Unpin for Config
impl UnwindSafe for Config
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)