Skip to content

Commit 3983720

Browse files
committed
Rewrite configuration parsing to remove special sysroot_path treatment
1 parent b3ccf62 commit 3983720

File tree

3 files changed

+21
-26
lines changed

3 files changed

+21
-26
lines changed

src/config.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
use serde_json;
22
use cargo_metadata;
3+
use std::path::PathBuf;
34

4-
#[derive(Debug, Deserialize, Hash)]
5+
#[derive(Debug, Hash)]
56
pub struct Config {
67
pub memcpy: bool,
7-
pub sysroot_path: Option<String>,
8+
pub sysroot_path: PathBuf,
89
}
910

10-
impl Default for Config {
11-
fn default() -> Self {
12-
Config {
13-
memcpy: true,
14-
sysroot_path: None,
15-
}
16-
}
11+
#[derive(Debug, Deserialize, Default)]
12+
struct ParseConfig {
13+
pub memcpy: Option<bool>,
14+
pub sysroot_path: Option<String>,
1715
}
1816

1917
impl Config {
2018
pub fn from_metadata(metadata: &cargo_metadata::Metadata) -> Result<Config, serde_json::Error> {
2119
let package_metadata = metadata.packages.first().map(|p| &p.metadata);
2220
let crate_metadata = package_metadata.as_ref().and_then(|m| m.get("cargo-xbuild"));
23-
match crate_metadata {
24-
Some(json) => serde_json::from_value(json.clone()),
25-
None => Ok(Config::default())
26-
}
21+
let config = match crate_metadata {
22+
Some(json) => serde_json::from_value(json.clone())?,
23+
None => ParseConfig::default(),
24+
};
25+
26+
Ok(Config {
27+
memcpy: config.memcpy.unwrap_or(true),
28+
sysroot_path: PathBuf::from(config.sysroot_path.unwrap_or("target/sysroot".into())),
29+
})
2730
}
2831
}

src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ fn build(args: cli::Args) -> Result<(ExitStatus)> {
147147
let metadata =
148148
cargo_metadata::metadata(args.manifest_path()).expect("cargo metadata invocation failed");
149149
let root = Path::new(&metadata.workspace_root);
150-
let target_directory = Path::new(&metadata.target_directory);
151150
let crate_config = config::Config::from_metadata(&metadata)
152151
.map_err(|_| "parsing package.metadata.cargo-xbuild section failed")?;
153152

@@ -193,7 +192,7 @@ fn build(args: cli::Args) -> Result<(ExitStatus)> {
193192
};
194193

195194
if let Some(cmode) = cmode {
196-
let home = xargo::home(target_directory, &crate_config)?;
195+
let home = xargo::home(root, &crate_config)?;
197196
let rustflags = cargo::rustflags(config.as_ref(), cmode.triple())?;
198197

199198
sysroot::update(

src/xargo.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::path::{Display, PathBuf};
22
use std::process::{Command, ExitStatus};
3-
use std::{fs, mem};
3+
use std::mem;
44
use std::io::{self, Write};
55
use std::path::Path;
66

@@ -69,16 +69,9 @@ impl Home {
6969
}
7070
}
7171

72-
pub fn home(target_directory: &Path, config: &Config) -> Result<Home> {
73-
let path = if let Some(ref p) = config.sysroot_path {
74-
let path = Path::new(p);
75-
fs::create_dir_all(&path).map_err(|_| String::from("Could not create sysroot folder"))?;
76-
path.canonicalize().map_err(|_| String::from("Invalid sysroot path"))?
77-
} else {
78-
let mut p = PathBuf::from(target_directory);
79-
p.push("sysroot");
80-
p
81-
};
72+
pub fn home(root: &Path, config: &Config) -> Result<Home> {
73+
let mut path = PathBuf::from(root);
74+
path.push(&config.sysroot_path);
8275

8376
Ok(Home {
8477
path: Filesystem::new(path),

0 commit comments

Comments
 (0)