[go: up one dir, main page]

Config

Struct Config 

Source
#[non_exhaustive]
pub struct Config {
Show 13 fields pub alias: BTreeMap<String, StringList>, pub build: BuildConfig, pub credential_alias: BTreeMap<String, PathAndArgs>, 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 source: BTreeMap<String, SourceConfigValue>, pub term: TermConfig, /* private fields */
}
Expand description

Cargo configuration.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§alias: BTreeMap<String, StringList>

The [alias] table.

Cargo Reference

§build: BuildConfig

The [build] table.

Cargo Reference

§credential_alias: BTreeMap<String, PathAndArgs>

The [credential-alias] table.

Cargo Reference

§doc: DocConfig

The [doc] table.

Cargo Reference

§env: BTreeMap<String, EnvConfigValue>

The [env] table.

Cargo Reference

§future_incompat_report: FutureIncompatReportConfig

The [future-incompat-report] table.

Cargo Reference

§cargo_new: CargoNewConfig

The [cargo-new] table.

Cargo Reference

§http: HttpConfig

The [http] table.

Cargo Reference

§net: NetConfig

The [net] table.

Cargo Reference

§registries: BTreeMap<String, RegistriesConfigValue>

The [registries] table.

Cargo Reference

§registry: RegistryConfig

The [registry] table.

Cargo Reference

§source: BTreeMap<String, SourceConfigValue>

The [source] table.

Cargo Reference

§term: TermConfig

The [term] table.

Cargo Reference

Implementations§

Source§

impl Config

Source

pub fn load() -> Result<Self, Error>

Reads config files hierarchically from the current directory and merges them.

Source

pub fn load_with_cwd<P: AsRef<Path>>(cwd: P) -> Result<Self, Error>

Reads config files hierarchically from the given directory and merges them.

Source

pub fn load_with_options<P: AsRef<Path>>( cwd: P, options: ResolveOptions, ) -> Result<Self, Error>

Reads config files hierarchically from the given directory and merges them.

Source

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.

  1. --target option (targets)
  2. CARGO_BUILD_TARGET environment variable
  3. build.target config
  4. 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)?);
}
Source

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.

  1. --target option (targets)
  2. CARGO_BUILD_TARGET environment variable
  3. build.target config

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.

Source

pub fn target<'a, T: Into<TargetTripleRef<'a>>>( &self, target: T, ) -> Result<TargetConfig, Error>

Returns the resolved [target] table for the given target.

Source

pub fn linker<'a, T: Into<TargetTripleRef<'a>>>( &self, target: T, ) -> Result<Option<PathBuf>, Error>

Returns the resolved linker path for the given target.

Source

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.

Source

pub fn rustflags<'a, T: Into<TargetTripleRef<'a>>>( &self, target: T, ) -> Result<Option<Flags>, Error>

Returns the resolved rustflags for the given target.

Source

pub fn rustdocflags<'a, T: Into<TargetTripleRef<'a>>>( &self, target: T, ) -> Result<Option<Flags>, Error>

Returns the resolved rustdocflags for the given target.

Source

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.

Source

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.

Source

pub fn host_triple(&self) -> Result<&str, Error>

Returns the host triple.

Source

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-coverage are available.
  • You will need to know the cargo version to determine whether fields in Cargo.toml or cargo’s CLI options are available.
Source

pub fn cargo_version(&self) -> Result<CargoVersion, Error>

Returns the version of the current cargo.

See also rustc_version.

Trait Implementations§

Source§

impl Clone for Config

Source§

fn clone(&self) -> Config

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Config

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Serialize for Config

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.