Skip to content

Commit 3e450d0

Browse files
committed
Make crate configurable and add memcpy config flag
1 parent 88d33ad commit 3e450d0

File tree

7 files changed

+74
-50
lines changed

7 files changed

+74
-50
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.
44
This project adheres to [Semantic Versioning](http://semver.org/).
55

66
## [Unreleased]
7+
- Make behavior configurable through a `package.metadata.cargo-xbuild` table in the `Cargo.toml`
8+
- Add a `memcpy` flags that specifies whether the `compiler_builtins` crate should be built with the `mem` feature enabled. Defaults to true.
79

810
## [v0.4.2] - 2018-05-07
911
- Implement `--help`

Cargo.lock

Lines changed: 6 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ error-chain = "0.7.2"
1515
fs2 = "0.4.1"
1616
libc = "0.2.18"
1717
rustc_version = "0.1.7"
18-
serde_json = "0.8.4"
18+
serde = "1.0"
19+
serde_derive = "1.0"
20+
serde_json = "1.0"
1921
tempdir = "0.3.5"
2022
toml = "0.2.1"
2123
walkdir = "1.0.3"
22-
cargo_metadata = "0.5.4"
24+
cargo_metadata = "0.5.5"
2325

2426
[dev-dependencies]
2527
lazy_static = "0.2.8"

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,18 @@ Instead of the "can't find crate for `core`" error you would get with a plain `c
2727

2828
All additional arguments (e.g. `--release` or `--verbose`) are forwarded to `cargo build`.
2929

30-
### Dev channel
30+
## Configuration
31+
32+
To configure `cargo-xbuild` create a `package.metadata.cargo-xbuild` table in your `Cargo.toml`. The following options are available:
33+
34+
```toml
35+
[package.metadata.cargo-xbuild]
36+
memcpy = true
37+
```
38+
39+
- The `memcpy` flag defines whether the `mem` feature of the `compiler_builtins` crate should be activated. Turning this flag off allows to specify own versions of the `memcpy`, `memset` etc. functions.
40+
41+
## Dev channel
3142

3243
If you want to use a local Rust source instead of `rust-src` rustup component, you can set the `XARGO_RUST_SRC` environment variable.
3344

src/config.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use serde_json;
2+
use cargo_metadata;
3+
4+
#[derive(Debug, Deserialize, Hash)]
5+
pub struct Config {
6+
pub memcpy: bool,
7+
}
8+
9+
impl Default for Config {
10+
fn default() -> Self {
11+
Config {
12+
memcpy: true,
13+
}
14+
}
15+
}
16+
17+
impl Config {
18+
pub fn from_metadata(metadata: &cargo_metadata::Metadata) -> Result<Config, serde_json::Error> {
19+
let package_metadata = metadata.packages.first().map(|p| &p.metadata);
20+
let crate_metadata = package_metadata.as_ref().and_then(|m| m.get("cargo-xbuild"));
21+
match crate_metadata {
22+
Some(json) => serde_json::from_value(json.clone()),
23+
None => Ok(Config::default())
24+
}
25+
}
26+
}

src/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ extern crate fs2;
66
extern crate libc;
77
extern crate rustc_version;
88
extern crate serde_json;
9+
#[macro_use]
10+
extern crate serde_derive;
911
extern crate tempdir;
1012
extern crate toml;
1113
extern crate walkdir;
@@ -30,6 +32,7 @@ mod rustc;
3032
mod sysroot;
3133
mod util;
3234
mod xargo;
35+
mod config;
3336

3437
const HELP: &str = include_str!("help.txt");
3538

@@ -145,6 +148,8 @@ fn build(args: cli::Args) -> Result<(ExitStatus)> {
145148
cargo_metadata::metadata(args.manifest_path()).expect("cargo metadata invocation failed");
146149
let root = Path::new(&metadata.workspace_root);
147150
let target_directory = Path::new(&metadata.target_directory);
151+
let crate_config = config::Config::from_metadata(&metadata)
152+
.map_err(|_| "parsing package.metadata.cargo-xbuild section failed")?;
148153

149154
// We can't build sysroot with stable or beta due to unstable features
150155
let sysroot = rustc::sysroot(verbose)?;
@@ -195,6 +200,7 @@ fn build(args: cli::Args) -> Result<(ExitStatus)> {
195200
&cmode,
196201
&home,
197202
&root,
203+
&crate_config,
198204
&rustflags,
199205
&meta,
200206
&src,

src/sysroot.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc::{Src, Sysroot, Target};
1616
use util;
1717
use xargo::Home;
1818
use cargo;
19+
use config::Config;
1920

2021
#[cfg(feature = "dev")]
2122
fn profile() -> &'static str {
@@ -31,6 +32,7 @@ fn build(
3132
cmode: &CompilationMode,
3233
ctoml: &cargo::Toml,
3334
home: &Home,
35+
config: &Config,
3436
src: &Src,
3537
hash: u64,
3638
verbose: bool,
@@ -43,7 +45,7 @@ fn build(
4345
util::mkdir(&dst)?;
4446

4547
build_libcore(cmode, &ctoml, home, src, &dst, verbose)?;
46-
build_libcompiler_builtins(cmode, &ctoml, home, src, &dst, verbose)?;
48+
build_libcompiler_builtins(cmode, &ctoml, home, src, &dst, config, verbose)?;
4749
build_liballoc(cmode, &ctoml, home, src, &dst, verbose)?;
4850

4951
// Create hash file
@@ -165,6 +167,7 @@ fn build_libcompiler_builtins(
165167
home: &Home,
166168
src: &Src,
167169
dst: &Path,
170+
config: &Config,
168171
verbose: bool,
169172
) -> Result<()> {
170173
const TOML: &'static str = r#"
@@ -182,13 +185,17 @@ version = "0.0.0"
182185
.to_string();
183186
let mut compiler_builtin_dep = Table::new();
184187
compiler_builtin_dep.insert("path".to_owned(), Value::String(path));
188+
189+
let mut features = vec![
190+
Value::String("compiler-builtins".to_owned()),
191+
];
192+
if config.memcpy {
193+
features.push(Value::String("mem".to_owned()));
194+
}
185195
compiler_builtin_dep.insert("default-features".to_owned(), Value::Boolean(false));
186196
compiler_builtin_dep.insert(
187197
"features".to_owned(),
188-
Value::Array(vec![
189-
Value::String("mem".to_owned()),
190-
Value::String("compiler-builtins".to_owned()),
191-
]),
198+
Value::Array(features),
192199
);
193200
let mut deps = Table::new();
194201
deps.insert(
@@ -255,6 +262,7 @@ fn hash(
255262
rustflags: &Rustflags,
256263
ctoml: &cargo::Toml,
257264
meta: &VersionMeta,
265+
config: &Config,
258266
) -> Result<u64> {
259267
let mut hasher = DefaultHasher::new();
260268

@@ -270,13 +278,16 @@ fn hash(
270278
hash.hash(&mut hasher);
271279
}
272280

281+
config.hash(&mut hasher);
282+
273283
Ok(hasher.finish())
274284
}
275285

276286
pub fn update(
277287
cmode: &CompilationMode,
278288
home: &Home,
279289
root: &Path,
290+
config: &Config,
280291
rustflags: &Rustflags,
281292
meta: &VersionMeta,
282293
src: &Src,
@@ -285,10 +296,10 @@ pub fn update(
285296
) -> Result<()> {
286297
let ctoml = cargo::toml(root)?;
287298

288-
let hash = hash(cmode, rustflags, &ctoml, meta)?;
299+
let hash = hash(cmode, rustflags, &ctoml, meta, config)?;
289300

290301
if old_hash(cmode, home)? != Some(hash) {
291-
build(cmode, &ctoml, home, src, hash, verbose)?;
302+
build(cmode, &ctoml, home, config, src, hash, verbose)?;
292303
}
293304

294305
// copy host artifacts into the sysroot, if necessary

0 commit comments

Comments
 (0)