[go: up one dir, main page]

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 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
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.

reference

§build: BuildConfig

The [build] table.

reference

§doc: DocConfig

The [doc] table.

reference

§env: BTreeMap<String, EnvConfigValue>

The [env] table.

reference

§future_incompat_report: FutureIncompatReportConfig

The [future-incompat-report] table.

reference

§net: NetConfig

The [net] table.

reference

§registries: BTreeMap<String, RegistriesConfigValue>

The [registries] table.

reference

§registry: RegistryConfig

The [registry] table.

reference

§term: TermConfig

The [term] table.

reference

Implementations§

source§

impl Config

source

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

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

source

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

Read 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>

Read 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 (host)

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 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 that method.

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 that method.

source

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

Returns the host triple.

Trait Implementations§

source§

impl Clone for Config

source§

fn clone(&self) -> Config

Returns a copy 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 !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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.