Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for cargo configurations. #931

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add various support for cargo configuration.
Also adds support for parsing and reading configuration options/environment variables from cargo, and allows users to ignore config files in the package.

This adds the `[build.env.cargo-config]` option, which can be set to `complete`, `ignore`, or `default`. If set to `complete`, cross will have access to every to every cargo config file on the host (if using remote cross, a config file is written to a temporary file which is mounted on the data volume at `/.cargo/config.toml`). If set to ignore, any `.cargo/config.toml` files outside of `CARGO_HOME` are ignored, by mounting anonymous data volumes to hide config files in any `.cargo` directories. The default behavior uses the backwards-compatible behavior, allowing cross to access any config files in the package and `CARGO_HOME` directories. If the build is called outside the workspace root or at the workspace root, then we only mount an anonymous volume at `$PWD/.cargo`.

The alias support includes recursive subcommand detection, and errors before it invokes cargo. A sample error message is `[cross] error: alias y has unresolvable recursive definition: x -> y -> z -> a -> y`, and therefore can handle non-trivial recursive subcommands.
Alexhuszagh committed Jul 19, 2022
commit 08fbafa481a5d54b805ec66731e69d9203f756da
16 changes: 12 additions & 4 deletions .changes/931.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
{
"description": "deny installation of debian packages that conflict with our cross-compiler toolchains.",
"type": "fixed"
}
[
{
"description": "add support for cargo aliases.",
"type": "added",
"issues": [562]
},
{
"description": "allow users to ignore config files in the package.",
"type": "added",
"issues": [621]
}
]
4 changes: 4 additions & 0 deletions .changes/933.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"description": "deny installation of debian packages that conflict with our cross-compiler toolchains.",
"type": "fixed"
}
2 changes: 2 additions & 0 deletions docs/cross_toml.md
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ For example:

```toml
[build.env]
cargo-config = "complete"
volumes = ["VOL1_ARG", "VOL2_ARG"]
passthrough = ["IMPORTANT_ENV_VARIABLES"]
```
@@ -74,6 +75,7 @@ This is similar to `build.env`, but allows you to be more specific per target.

```toml
[target.x86_64-unknown-linux-gnu.env]
cargo-config = "ignore"
volumes = ["VOL1_ARG", "VOL2_ARG"]
passthrough = ["IMPORTANT_ENV_VARIABLES"]
```
4 changes: 2 additions & 2 deletions src/bin/commands/containers.rs
Original file line number Diff line number Diff line change
@@ -392,7 +392,7 @@ pub fn create_persistent_volume(
channel: Option<&Toolchain>,
msg_info: &mut MessageInfo,
) -> cross::Result<()> {
let config = cross::config::Config::new(None);
let config = cross::CrossConfig::new(None);
let toolchain_host: cross::Target = toolchain.into();
let mut toolchain = QualifiedToolchain::default(&config, msg_info)?;
toolchain.replace_host(&ImagePlatform::from_target(
@@ -483,7 +483,7 @@ pub fn remove_persistent_volume(
channel: Option<&Toolchain>,
msg_info: &mut MessageInfo,
) -> cross::Result<()> {
let config = cross::config::Config::new(None);
let config = cross::CrossConfig::new(None);
let target_host: cross::Target = toolchain.into();
let mut toolchain = QualifiedToolchain::default(&config, msg_info)?;
toolchain.replace_host(&ImagePlatform::from_target(target_host.target().clone())?);
8 changes: 5 additions & 3 deletions src/bin/cross.rs
Original file line number Diff line number Diff line change
@@ -8,18 +8,20 @@ use std::{
use cross::{
cargo, cli, rustc,
shell::{self, Verbosity},
OutputExt, Subcommand,
CargoConfig, CargoToml, OutputExt, Subcommand,
};

pub fn main() -> cross::Result<()> {
cross::install_panic_hook()?;
cross::install_termination_hook()?;

let target_list = rustc::target_list(&mut Verbosity::Quiet.into())?;
let args = cli::parse(&target_list)?;
let cargo_toml = CargoToml::read()?;
let cargo_config = CargoConfig::new(cargo_toml);
let args = cli::parse(&target_list, &cargo_config)?;
let subcommand = args.subcommand;
let mut msg_info = shell::MessageInfo::create(args.verbose, args.quiet, args.color.as_deref())?;
let status = match cross::run(args, target_list, &mut msg_info)? {
let status = match cross::run(args, target_list, cargo_config, &mut msg_info)? {
Some(status) => status,
None => {
// if we fallback to the host cargo, use the same invocation that was made to cross
64 changes: 64 additions & 0 deletions src/cargo_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use std::collections::HashMap;

use crate::cargo_toml::CargoToml;
use crate::config::{split_to_cloned_by_ws, Environment};
use crate::errors::*;

pub const CARGO_NO_PREFIX_ENVVARS: &[&str] = &[
"http_proxy",
"TERM",
"RUSTDOCFLAGS",
"RUSTFLAGS",
"BROWSER",
"HTTPS_PROXY",
"HTTP_TIMEOUT",
"https_proxy",
];

#[derive(Debug)]
struct CargoEnvironment(Environment);

impl CargoEnvironment {
fn new(map: Option<HashMap<&'static str, &'static str>>) -> Self {
CargoEnvironment(Environment::new("CARGO", map))
}

pub fn alias(&self, name: &str) -> Option<Vec<String>> {
let key = format!("ALIAS_{name}");
self.0
.get_var(&self.0.var_name(&key))
.map(|x| split_to_cloned_by_ws(&x))
}
}

#[derive(Debug)]
pub struct CargoConfig {
toml: Option<CargoToml>,
env: CargoEnvironment,
}

impl CargoConfig {
pub fn new(toml: Option<CargoToml>) -> Self {
CargoConfig {
toml,
env: CargoEnvironment::new(None),
}
}

pub fn alias(&self, name: &str) -> Result<Option<Vec<String>>> {
match self.env.alias(name) {
Some(alias) => Ok(Some(alias)),
None => match self.toml.as_ref() {
Some(t) => t.alias(name),
None => Ok(None),
},
}
}

pub fn to_toml(&self) -> Result<Option<String>> {
match self.toml.as_ref() {
Some(t) => Ok(Some(t.to_toml()?)),
None => Ok(None),
}
}
}
Loading