/
smoke.rs
805 lines (634 loc) · 18.1 KB
/
smoke.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
#![cfg_attr(not(feature = "dev"), allow(dead_code))]
#![deny(warnings)]
#![feature(const_fn)]
#[macro_use]
extern crate error_chain;
#[macro_use]
extern crate lazy_static;
extern crate parking_lot;
extern crate rustc_version;
extern crate tempdir;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::{env, fs};
use parking_lot::{Mutex, MutexGuard};
use tempdir::TempDir;
use errors::*;
mod errors {
#![allow(unused_doc_comment)]
error_chain!();
}
macro_rules! run {
() => {
if let Err(e) = run() {
panic!("{}", e)
}
}
}
// Returns Xargo's "home"
fn home() -> Result<PathBuf> {
if let Some(h) = env::var_os("XARGO_HOME") {
Ok(PathBuf::from(h))
} else {
Ok(
env::home_dir()
.ok_or_else(|| "couldn't find your home directory. Is $HOME set?")?
.join(".xargo"),
)
}
}
fn cleanup(target: &str) -> Result<()> {
let p = home()?.join("lib/rustlib").join(target);
if p.exists() {
fs::remove_dir_all(&p).chain_err(|| format!("couldn't clean sysroot for {}", target))
} else {
Ok(())
}
}
fn exists(krate: &str, target: &str) -> Result<bool> {
let p = home()?.join("lib/rustlib").join(target).join("lib");
for e in fs::read_dir(&p).chain_err(|| format!("couldn't read the directory {}", p.display()))?
{
let e = e.chain_err(|| {
format!(
"error reading the contents of the directory {}",
p.display()
)
})?;
if e.file_name().to_string_lossy().contains(krate) {
return Ok(true);
}
}
Ok(false)
}
fn host() -> String {
rustc_version::version_meta().host
}
fn mkdir(path: &Path) -> Result<()> {
fs::create_dir(path).chain_err(|| {
format!("couldn't create the directory {}", path.display())
})
}
fn sysroot_was_built(stderr: &str, target: &str) -> bool {
stderr.lines().filter(|l| l.starts_with("+")).any(|l| {
l.contains("cargo") && l.contains("build") && l.contains("--target") && l.contains(target)
&& l.contains("-p") && l.contains("core")
})
}
fn write(path: &Path, append: bool, contents: &str) -> Result<()> {
let p = path.display();
let mut opts = OpenOptions::new();
if append {
opts.append(true);
} else {
opts.create(true);
opts.truncate(true);
}
opts.write(true)
.open(path)
.chain_err(|| format!("couldn't open {}", p))?
.write_all(contents.as_bytes())
.chain_err(|| format!("couldn't write to {}", p))
}
fn xargo() -> Result<Command> {
let mut p = env::current_exe().chain_err(|| "couldn't get path to current executable")?;
p.pop();
p.pop();
p.push("xargo");
Ok(Command::new(p))
}
trait CommandExt {
fn run(&mut self) -> Result<()>;
fn run_and_get_stderr(&mut self) -> Result<String>;
}
impl CommandExt for Command {
fn run(&mut self) -> Result<()> {
let status = self.status()
.chain_err(|| format!("couldn't execute `{:?}`", self))?;
if status.success() {
Ok(())
} else {
Err(format!(
"`{:?}` failed with exit code: {:?}",
self,
status.code()
))?
}
}
fn run_and_get_stderr(&mut self) -> Result<String> {
let out = self.output()
.chain_err(|| format!("couldn't execute `{:?}`", self))?;
if out.status.success() {
Ok(String::from_utf8(out.stderr)
.chain_err(|| format!("`{:?}` output was not UTF-8", self))?)
} else {
Err(format!(
"`{:?}` failed with exit code: {:?}",
self,
out.status.code()
))?
}
}
}
struct Project {
name: &'static str,
td: TempDir,
}
impl Project {
fn new(name: &'static str) -> Result<Self> {
const JSON: &'static str = r#"
{
"arch": "arm",
"data-layout": "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64",
"linker-flavor": "gcc",
"llvm-target": "thumbv6m-none-eabi",
"max-atomic-width": 0,
"os": "none",
"target-c-int-width": "32",
"target-endian": "little",
"target-pointer-width": "32"
}
"#;
let td = TempDir::new("xargo").chain_err(|| "couldn't create a temporary directory")?;
xargo()?
.args(&["init", "--lib", "--vcs", "none", "--name", name])
.current_dir(td.path())
.run()?;
write(&td.path().join("src/lib.rs"), false, "#![no_std]")?;
write(&td.path().join(format!("{}.json", name)), false, JSON)?;
Ok(Project { name: name, td: td })
}
/// Calls `xargo build`
fn build(&self, target: &str) -> Result<()> {
xargo()?
.args(&["build", "--target", target])
.current_dir(self.td.path())
.run()
}
/// Calls `xargo build` and collects STDERR
fn build_and_get_stderr(&self, target: Option<&str>) -> Result<String> {
let mut cmd = xargo()?;
cmd.arg("build");
if let Some(target) = target {
cmd.args(&["--target", target]);
}
cmd.arg("-v")
.current_dir(self.td.path())
.run_and_get_stderr()
}
/// Appends a string to the project `Cargo.toml`
fn cargo_toml(&self, contents: &str) -> Result<()> {
write(&self.td.path().join("Cargo.toml"), true, contents)
}
/// Adds a `.cargo/config` to the project
fn config(&self, contents: &str) -> Result<()> {
mkdir(&self.td.path().join(".cargo"))?;
write(&self.td.path().join(".cargo/config"), false, contents)
}
/// Calls `xargo doc`
fn doc(&self, target: &str) -> Result<()> {
xargo()?
.args(&["doc", "--target", target])
.current_dir(self.td.path())
.run()
}
/// Adds a `Xargo.toml` to the project
fn xargo_toml(&self, toml: &str) -> Result<()> {
write(&self.td.path().join("Xargo.toml"), false, toml)
}
}
impl Drop for Project {
fn drop(&mut self) {
cleanup(self.name).unwrap()
}
}
fn hcleanup(triple: &str) -> Result<()> {
let p = home()?.join("HOST/lib/rustlib").join(triple);
if p.exists() {
fs::remove_dir_all(&p).chain_err(|| format!("couldn't clean sysroot for {}", triple))
} else {
Ok(())
}
}
struct HProject {
_guard: MutexGuard<'static, ()>,
host: String,
td: TempDir,
}
impl HProject {
fn new(test: bool) -> Result<Self> {
// There can only be one instance of this type at any point in time
lazy_static! {
static ref ONCE: Mutex<()> = Mutex::new(());
}
let guard = ONCE.lock();
let td = TempDir::new("xargo").chain_err(|| "couldn't create a temporary directory")?;
xargo()?
.args(&["init", "--lib", "--vcs", "none", "--name", "host"])
.current_dir(td.path())
.run()?;
if test {
write(
&td.path().join("src/lib.rs"),
false,
"#![feature(alloc_system)]\nextern crate alloc_system;",
)?;
} else {
write(&td.path().join("src/lib.rs"), false, "#![no_std]")?;
}
Ok(HProject {
_guard: guard,
host: host(),
td: td,
})
}
/// Calls `xargo build` and collects STDERR
fn build_and_get_stderr(&self) -> Result<String> {
let mut cmd = xargo()?;
cmd.arg("build");
cmd.arg("-v")
.current_dir(self.td.path())
.run_and_get_stderr()
}
/// Adds a `Xargo.toml` to the project
fn xargo_toml(&self, toml: &str) -> Result<()> {
write(&self.td.path().join("Xargo.toml"), false, toml)
}
}
impl Drop for HProject {
fn drop(&mut self) {
hcleanup(&self.host).unwrap()
}
}
/// Test vanilla `xargo build`
#[cfg(feature = "dev")]
#[test]
fn simple() {
fn run() -> Result<()> {
const TARGET: &'static str = "__simple";
let project = Project::new(TARGET)?;
project.build(TARGET)?;
assert!(exists("core", TARGET)?);
Ok(())
}
run!()
}
/// Test building a dependency specified as `target.{}.dependencies` in
/// Xargo.toml
#[cfg(feature = "dev")]
#[test]
fn target_dependencies() {
fn run() -> Result<()> {
const TARGET: &'static str = "__target_dependencies";
let project = Project::new(TARGET)?;
project.xargo_toml(
r#"
[target.__target_dependencies.dependencies.alloc]
"#,
)?;
project.build(TARGET)?;
assert!(exists("core", TARGET)?);
assert!(exists("alloc", TARGET)?);
Ok(())
}
run!()
}
/// Test building a dependency specified as `dependencies` in Xargo.toml
#[cfg(feature = "dev")]
#[test]
fn dependencies() {
fn run() -> Result<()> {
const TARGET: &'static str = "__dependencies";
let project = Project::new(TARGET)?;
project.xargo_toml(
r#"
[dependencies.alloc]
"#,
)?;
project.build(TARGET)?;
assert!(exists("core", TARGET)?);
assert!(exists("alloc", TARGET)?);
Ok(())
}
run!()
}
/// Test `xargo doc`
#[cfg(feature = "dev")]
#[test]
fn doc() {
fn run() -> Result<()> {
const TARGET: &'static str = "__doc";
let project = Project::new(TARGET)?;
project.doc(TARGET)?;
assert!(exists("core", TARGET)?);
Ok(())
}
run!()
}
/// Check that calling `xargo build` a second time doesn't rebuild the sysroot
#[cfg(feature = "dev")]
#[test]
fn twice() {
fn run() -> Result<()> {
const TARGET: &'static str = "__twice";
let project = Project::new(TARGET)?;
let stderr = project.build_and_get_stderr(Some(TARGET))?;
assert!(sysroot_was_built(&stderr, TARGET));
let stderr = project.build_and_get_stderr(Some(TARGET))?;
assert!(!sysroot_was_built(&stderr, TARGET));
Ok(())
}
run!()
}
/// Check that if `build.target` is set in `.cargo/config`, that target will be
/// used to build the sysroot
#[cfg(feature = "dev")]
#[test]
fn build_target() {
fn run() -> Result<()> {
const TARGET: &'static str = "__build_target";
let project = Project::new(TARGET)?;
project.config(
r#"
[build]
target = "__build_target"
"#,
)?;
let stderr = project.build_and_get_stderr(None)?;
assert!(sysroot_was_built(&stderr, TARGET));
Ok(())
}
run!()
}
/// Check that `--target` overrides `build.target`
#[cfg(feature = "dev")]
#[test]
fn override_build_target() {
fn run() -> Result<()> {
const TARGET: &'static str = "__override_build_target";
let project = Project::new(TARGET)?;
project.config(
r#"
[build]
target = "BAD"
"#,
)?;
let stderr = project.build_and_get_stderr(Some(TARGET))?;
assert!(sysroot_was_built(&stderr, TARGET));
Ok(())
}
run!()
}
/// We shouldn't rebuild the sysroot if `profile.release.lto` changed
#[cfg(feature = "dev")]
#[test]
fn lto_changed() {
fn run() -> Result<()> {
const TARGET: &'static str = "__lto_changed";
let project = Project::new(TARGET)?;
let stderr = project.build_and_get_stderr(Some(TARGET))?;
assert!(sysroot_was_built(&stderr, TARGET));
project.cargo_toml(
r#"
[profile.release]
lto = true
"#,
)?;
let stderr = project.build_and_get_stderr(Some(TARGET))?;
assert!(!sysroot_was_built(&stderr, TARGET));
Ok(())
}
run!()
}
/// Modifying RUSTFLAGS should trigger a rebuild of the sysroot
#[cfg(feature = "dev")]
#[test]
fn rustflags_changed() {
fn run() -> Result<()> {
const TARGET: &'static str = "__rustflags_changed";
let project = Project::new(TARGET)?;
let stderr = project.build_and_get_stderr(Some(TARGET))?;
assert!(sysroot_was_built(&stderr, TARGET));
project.config(
r#"
[build]
rustflags = ["--cfg", "xargo"]
"#,
)?;
let stderr = project.build_and_get_stderr(Some(TARGET))?;
assert!(sysroot_was_built(&stderr, TARGET));
Ok(())
}
run!()
}
/// Check that RUSTFLAGS are passed to all `rustc`s
#[cfg(feature = "dev")]
#[test]
fn rustflags() {
fn run() -> Result<()> {
const TARGET: &'static str = "__rustflags";
let project = Project::new(TARGET)?;
project.config(
r#"
[build]
rustflags = ["--cfg", "xargo"]
"#,
)?;
let stderr = project.build_and_get_stderr(Some(TARGET))?;
assert!(
stderr
.lines()
.filter(|l| !l.starts_with("+") && l.contains("rustc"))
.all(|l| l.contains("--cfg") && l.contains("xargo"))
);
Ok(())
}
run!()
}
/// Check that `-C panic=abort` is passed to `rustc` when `panic = "abort"` is
/// set in `profile.release`
#[cfg(not(feature = "dev"))]
#[test]
fn panic_abort() {
fn run() -> Result<()> {
const TARGET: &'static str = "__panic_abort";
let project = Project::new(TARGET)?;
project.cargo_toml(
r#"
[profile.release]
panic = "abort"
"#,
)?;
let stderr = project.build_and_get_stderr(Some(TARGET))?;
assert!(
stderr
.lines()
.filter(|l| !l.starts_with("+") && l.contains("--release"))
.all(|l| l.contains("-C") && l.contains("panic=abort"))
);
Ok(())
}
run!()
}
/// Check that adding linker arguments doesn't trigger a sysroot rebuild
#[cfg(feature = "dev")]
#[test]
fn link_arg() {
fn run() -> Result<()> {
const TARGET: &'static str = "__link_arg";
let project = Project::new(TARGET)?;
let stderr = project.build_and_get_stderr(Some(TARGET))?;
assert!(sysroot_was_built(&stderr, TARGET));
project.config(
r#"
[target.__link_arg]
rustflags = ["-C", "link-arg=-lfoo"]
"#,
)?;
let stderr = project.build_and_get_stderr(Some(TARGET))?;
assert!(!sysroot_was_built(&stderr, TARGET));
Ok(())
}
run!()
}
/// The sysroot should be rebuilt if the target specification changed
#[cfg(feature = "dev")]
#[test]
fn specification_changed() {
fn run() -> Result<()> {
const JSON: &'static str = r#"
{
"arch": "arm",
"data-layout": "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64",
"linker-flavor": "gcc",
"llvm-target": "thumbv6m-none-eabi",
"max-atomic-width": 0,
"os": "none",
"panic-strategy": "abort",
"target-c-int-width": "32",
"target-endian": "little",
"target-pointer-width": "32"
}
"#;
const TARGET: &'static str = "__specification_changed";
let project = Project::new(TARGET)?;
let stderr = project.build_and_get_stderr(Some(TARGET))?;
assert!(sysroot_was_built(&stderr, TARGET));
write(
&project.td.path().join("__specification_changed.json"),
false,
JSON,
)?;
let stderr = project.build_and_get_stderr(Some(TARGET))?;
assert!(sysroot_was_built(&stderr, TARGET));
Ok(())
}
run!()
}
/// The sysroot should NOT be rebuilt if the target specification didn't really
/// changed, e.g. some fields were moved around
#[cfg(feature = "dev")]
#[test]
fn unchanged_specification() {
fn run() -> Result<()> {
const JSON: &'static str = r#"
{
"arch": "arm",
"data-layout": "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64",
"linker-flavor": "gcc",
"llvm-target": "thumbv6m-none-eabi",
"os": "none",
"max-atomic-width": 0,
"target-c-int-width": "32",
"target-endian": "little",
"target-pointer-width": "32"
}
"#;
const TARGET: &'static str = "__unchanged_specification";
let project = Project::new(TARGET)?;
let stderr = project.build_and_get_stderr(Some(TARGET))?;
assert!(sysroot_was_built(&stderr, TARGET));
write(
&project.td.path().join("__unchanged_specification.json"),
false,
JSON,
)?;
let stderr = project.build_and_get_stderr(Some(TARGET))?;
assert!(!sysroot_was_built(&stderr, TARGET));
Ok(())
}
run!()
}
/// Check that a sysroot is built for the host
#[cfg(feature = "dev")]
#[test]
fn host_once() {
fn run() -> Result<()> {
let target = host();
let project = HProject::new(false)?;
let stderr = project.build_and_get_stderr()?;
assert!(sysroot_was_built(&stderr, &target));
Ok(())
}
run!()
}
/// Check that the sysroot is not rebuilt when `xargo build` is called a second
/// time
#[cfg(feature = "dev")]
#[test]
fn host_twice() {
fn run() -> Result<()> {
let target = host();
let project = HProject::new(false)?;
let stderr = project.build_and_get_stderr()?;
assert!(sysroot_was_built(&stderr, &target));
let stderr = project.build_and_get_stderr()?;
assert!(!sysroot_was_built(&stderr, &target));
Ok(())
}
run!()
}
/// Check multi stage sysroot builds with `xargo test`
// We avoid Windows here just because it would be tricky to modify the rust-src
// component (cf. #36501) from within the appveyor environment
#[cfg(feature = "dev")]
#[cfg(not(windows))]
#[ignore]
#[test]
fn test() {
fn run() -> Result<()> {
let project = HProject::new(true)?;
project.xargo_toml(
"
[dependencies.std]
features = [\"panic_unwind\"]
[dependencies.test]
stage = 1
",
)?;
xargo()?.arg("test").current_dir(project.td.path()).run()?;
Ok(())
}
run!()
}
/// Check multi stage sysroot builds with `xargo test`
#[cfg(feature = "dev")]
#[test]
fn compiler_builtins() {
fn run() -> Result<()> {
let project = HProject::new(false)?;
project.xargo_toml(
"
[dependencies.core]
stage = 0
[dependencies.compiler_builtins]
stage = 1
",
)?;
xargo()?.arg("build").current_dir(project.td.path()).run()?;
Ok(())
}
run!()
}