Skip to content
This repository was archived by the owner on May 8, 2018. It is now read-only.

Commit eb2f4bb

Browse files
committed
Add specs for data layout
1 parent 13af6ed commit eb2f4bb

5 files changed

Lines changed: 111 additions & 52 deletions

File tree

build/main.rs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
extern crate git2;
22

3-
use std::io::fs::PathExtensions;
3+
use std::io::TempDir;
4+
use std::io::fs::{File, PathExtensions};
45
use std::io::process;
56

67
fn main() {
@@ -11,39 +12,29 @@ fn main() {
1112
clone_rust(&rust_dir);
1213

1314
println!("Compiling libcore");
14-
if !out_dir.join("core.ll").exists() {
15-
compile(&rust_dir.join("src").join("libcore").join("lib.rs"), &out_dir);
16-
}
15+
compile(&rust_dir.join("src").join("libcore").join("lib.rs"), &out_dir);
1716

1817
println!("Compiling liblibc");
19-
if !out_dir.join("libc.ll").exists() {
20-
compile(&rust_dir.join("src").join("liblibc").join("lib.rs"), &out_dir);
21-
}
22-
18+
compile(&rust_dir.join("src").join("liblibc").join("lib.rs"), &out_dir);
19+
2320
println!("Compiling liballoc");
24-
if !out_dir.join("alloc.ll").exists() {
25-
compile(&rust_dir.join("src").join("liballoc").join("lib.rs"), &out_dir);
26-
}
27-
28-
println!("Compiling libcollections");
29-
if !out_dir.join("collections.ll").exists() {
30-
compile(&rust_dir.join("src").join("libcollections").join("lib.rs"), &out_dir);
31-
}
21+
compile(&rust_dir.join("src").join("liballoc").join("lib.rs"), &out_dir);
3222

3323
println!("Compiling libunicode");
34-
if !out_dir.join("unicode.ll").exists() {
35-
compile(&rust_dir.join("src").join("libunicode").join("lib.rs"), &out_dir);
36-
}
24+
compile(&rust_dir.join("src").join("libunicode").join("lib.rs"), &out_dir);
25+
26+
println!("Compiling libcollections");
27+
compile(&rust_dir.join("src").join("libcollections").join("lib.rs"), &out_dir);
3728

3829
println!("Compiling librand");
39-
if !out_dir.join("rand.ll").exists() {
40-
compile(&rust_dir.join("src").join("librand").join("lib.rs"), &out_dir);
41-
}
30+
compile(&rust_dir.join("src").join("librand").join("lib.rs"), &out_dir);
31+
32+
/*println!("Compiling rustrt");
33+
File::create(&out_dir.join("rustrt.rs")).write_str("#![no_std]\n#![crate_name = \"rust_builtin\"]\n#![crate_type = \"staticlib\"]\n").unwrap();
34+
compile(&out_dir.join("rustrt.rs"), &out_dir);
4235
4336
println!("Compiling libstd");
44-
if !out_dir.join("std.ll").exists() {
45-
compile(&rust_dir.join("src").join("libstd").join("lib.rs"), &out_dir);
46-
}
37+
compile(&rust_dir.join("src").join("libstd").join("lib.rs"), &out_dir);*/
4738
}
4839

4940
fn clone_rust(path: &Path) -> git2::Repository {
@@ -65,11 +56,21 @@ fn clone_rust(path: &Path) -> git2::Repository {
6556
}
6657

6758
fn compile(krate: &Path, out_dir: &Path) {
59+
let cwd = TempDir::new("cargo-emscripten-compilation").unwrap();
60+
std::io::fs::copy(&Path::new(std::os::getenv("CARGO_MANIFEST_DIR").unwrap()).join("specs-build.json"),
61+
&cwd.path().join("emscripten.json")).unwrap();
62+
6863
let mut command = process::Command::new("rustc");
6964
command.arg(krate);
7065
command.arg("--out-dir").arg(out_dir);
71-
command.arg("--emit=llvm-ir");
72-
66+
command.arg("-L").arg(out_dir);
67+
command.arg("--target").arg("emscripten.json");
68+
command.cwd(cwd.path());
69+
70+
let mut ir_command = command.clone();
71+
ir_command.arg("--emit=llvm-ir");
72+
73+
exec(ir_command);
7374
exec(command);
7475
}
7576

specs-build.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"data-layout": "e-p:32:32-i64:64-v128:32:128-n32-S128",
3+
"llvm-target": "i686-unknown-linux-gnu",
4+
"target-endian": "little",
5+
"target-word-size": "32",
6+
"arch": "x86",
7+
"os": "linux",
8+
"morestack": false,
9+
"dynamic-linking": true,
10+
"executables": true,
11+
"no-compiler-rt": true
12+
}

specs-target.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"data-layout": "e-p:32:32-i64:64-v128:32:128-n32-S128",
3+
"llvm-target": "i686-unknown-linux-gnu",
4+
"target-endian": "little",
5+
"target-word-size": "32",
6+
"arch": "x86",
7+
"os": "emscripten",
8+
"morestack": false,
9+
"dynamic-linking": true,
10+
"executables": true,
11+
"no-compiler-rt": true
12+
}

src/lib.rs

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ extern crate regex;
99
use cargo::ops::{ExecEngine, CommandPrototype};
1010
use cargo::util::{mod, CargoResult, ProcessError, ProcessBuilder};
1111

12+
use std::io::TempDir;
1213
use std::io::fs::{File, PathExtensions};
1314
use std::io::process::ProcessOutput;
1415

@@ -32,16 +33,35 @@ impl ExecEngine for EmscriptenEngine {
3233
fn exec(command: CommandPrototype, with_output: bool, engine: &EmscriptenEngine)
3334
-> Result<Option<ProcessOutput>, ProcessError>
3435
{
36+
let build_dir = TempDir::new("cargo-emscripten").unwrap();
37+
38+
// finding out dir
39+
let out_dir = command.get_args().windows(2)
40+
.filter_map(|args| {
41+
if args[0].as_str() == Some("--out-dir") {
42+
Some(args[1].as_str().unwrap().to_string())
43+
} else {
44+
None
45+
}
46+
})
47+
.next().unwrap();
48+
49+
// writing `specs.json` in the out dir
50+
File::create(&build_dir.path().join("emscripten.json")).write(include_bytes!("../specs-target.json")).unwrap();
51+
52+
// writing libstd in the target directory
53+
let (libs_ll, libs_rlib) = std_inc::write_std(&Path::new(build_dir.path()));
54+
3555
// if we don't find `--crate-type bin`, returning immediatly
36-
if command.get_args().windows(2)
56+
/*if command.get_args().windows(2)
3757
.find(|&args| {
3858
args[0].as_str() == Some("--crate-type") &&
3959
args[1].as_str() == Some("bin")
4060
}).is_none()
4161
{
4262
let p = command.into_process_builder().unwrap();
4363
return do_exec(p, with_output);
44-
}
64+
}*/
4565

4666
// finding crate name
4767
let crate_name = command.get_args().windows(2)
@@ -54,17 +74,6 @@ fn exec(command: CommandPrototype, with_output: bool, engine: &EmscriptenEngine)
5474
})
5575
.next().unwrap();
5676

57-
// finding out dir
58-
let out_dir = command.get_args().windows(2)
59-
.filter_map(|args| {
60-
if args[0].as_str() == Some("--out-dir") {
61-
Some(args[1].as_str().unwrap().to_string())
62-
} else {
63-
None
64-
}
65-
})
66-
.next().unwrap();
67-
6877
// executing compiler
6978
{
7079
let mut new_command = CommandPrototype::new(command.get_type().clone()).unwrap();
@@ -74,9 +83,19 @@ fn exec(command: CommandPrototype, with_output: bool, engine: &EmscriptenEngine)
7483
for (key, val) in command.get_envs().iter() {
7584
new_command = new_command.env(key.as_slice(), val.as_ref().map(|v| v.as_bytes_no_nul()));
7685
}
77-
new_command = new_command.cwd(command.get_cwd().clone());
86+
//new_command = new_command.cwd(command.get_cwd().clone());
87+
88+
new_command = new_command.arg("--emit=llvm-ir,dep-info");
89+
new_command = new_command.arg("--target").arg("emscripten.json");
90+
new_command = new_command.cwd(build_dir.path().clone());
91+
92+
for (name, path) in libs_rlib.into_iter() {
93+
new_command = new_command.arg("--extern").arg(format!("{}={}", name, path.as_str().unwrap()));
94+
}
95+
96+
// TODO: shouldn't be necessary with the "--extern" commands
97+
new_command = new_command.arg("-L").arg(build_dir.path());
7898

79-
let new_command = new_command.arg("--emit=llvm-ir,dep-info");
8099
try!(do_exec(new_command.into_process_builder().unwrap(), with_output));
81100
}
82101

@@ -94,12 +113,13 @@ fn exec(command: CommandPrototype, with_output: bool, engine: &EmscriptenEngine)
94113
let mut process = util::process(emcc).unwrap();
95114
process = process.arg(ll_output_file);
96115

97-
for lib in libstd.into_iter() {
116+
for lib in libs_ll.into_iter() {
98117
process = process.arg(lib);
99118
}
100119

101120
process = process.arg("-lGL").arg("-lSDL").arg("-s").arg("USE_SDL=2");
102121
process = process.arg("-o").arg(format!("{}/{}.html", out_dir, crate_name));
122+
process = process.cwd(build_dir.path().clone());
103123
process
104124
};
105125

src/std_inc.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,34 @@ static LIBRAND: &'static [u8] = include_bytes!(concat!(env!("OUT_DIR"), "/rand.l
77
static LIBSTD: &'static [u8] = include_bytes!(concat!(env!("OUT_DIR"), "/std.ll"));
88
static LIBUNICODE: &'static [u8] = include_bytes!(concat!(env!("OUT_DIR"), "/unicode.ll"));
99

10-
pub fn write_std(path: &Path) -> Vec<Path> {
10+
pub fn write_std(path: &Path) -> (Vec<Path>, Vec<(&'static str, Path)>) {
1111
File::create(&path.join("collections.ll")).write(LIBCOLLECTIONS).unwrap();
1212
File::create(&path.join("core.ll")).write(LIBCORE).unwrap();
1313
File::create(&path.join("libc.ll")).write(LIBLIBC).unwrap();
1414
File::create(&path.join("rand.ll")).write(LIBRAND).unwrap();
1515
File::create(&path.join("std.ll")).write(LIBSTD).unwrap();
1616
File::create(&path.join("unicode.ll")).write(LIBUNICODE).unwrap();
1717

18-
vec![
19-
path.join("collections.ll"),
20-
path.join("core.ll"),
21-
path.join("libc.ll"),
22-
path.join("rand.ll"),
23-
path.join("std.ll"),
24-
path.join("unicode.ll"),
25-
]
18+
File::create(&path.join("libcollections.rlib")).write(
19+
include_bytes!(concat!(env!("OUT_DIR"), "/libcollections.rlib"))).unwrap();
20+
File::create(&path.join("libcore.rlib")).write(
21+
include_bytes!(concat!(env!("OUT_DIR"), "/libcore.rlib"))).unwrap();
22+
File::create(&path.join("liblibc.rlib")).write(
23+
include_bytes!(concat!(env!("OUT_DIR"), "/liblibc.rlib"))).unwrap();
24+
25+
(
26+
vec![
27+
path.join("collections.ll"),
28+
path.join("core.ll"),
29+
path.join("libc.ll"),
30+
path.join("rand.ll"),
31+
path.join("std.ll"),
32+
path.join("unicode.ll"),
33+
],
34+
vec![
35+
("collections", path.join("libcollections.rlib")),
36+
("core", path.join("libcore.rlib")),
37+
("libc", path.join("liblibc.rlib"))
38+
]
39+
)
2640
}

0 commit comments

Comments
 (0)