Skip to content

Commit 8c6b13e

Browse files
Changes to librust_pnacl_trans for hosting inside PNaCl.
1 parent d3a7395 commit 8c6b13e

File tree

1 file changed

+122
-54
lines changed

1 file changed

+122
-54
lines changed

src/librust_pnacl_trans/lib.rs

Lines changed: 122 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
1515
html_favicon_url = "http://www.rust-lang.org/favicon.ico",
1616
html_root_url = "http://doc.rust-lang.org/master/")]
17+
#![cfg_attr(target_os = "nacl", allow(dead_code))]
1718

1819
#![feature(rustc_private, libc, collections, exit_status)]
1920

@@ -32,7 +33,7 @@ use std::env;
3233
use std::ptr;
3334
use std::ffi;
3435
use std::fmt::Display;
35-
use std::path::Path;
36+
use std::path::{Path, PathBuf};
3637

3738
// From librustc:
3839
pub fn host_triple() -> &'static str {
@@ -56,17 +57,30 @@ fn get_native_arch() -> &'static str {
5657
}
5758
}
5859

59-
fn optgroups() -> Vec<OptGroup> {
60+
fn inner_optgroups() -> Vec<OptGroup> {
6061
vec!(optflag("h", "help", "Display this message"),
6162
reqopt("o", "", "Write the linked output to this file", ""),
6263
optopt("", "opt-level", "Optimize with possible levels 0-3", "LEVEL"),
6364
optflag("", "save-temps", "Save temp files"),
6465
optopt("", "target", "The target triple to codegen for", ""),
65-
reqopt("", "cross-path", "The path to the Pepper SDK", ""),
6666
optmulti("", "raw", "The specified bitcodes have had none of the usual PNaCl IR \
6767
legalization passes run on them", ""),
6868
optflag("", "all-raw", "All input bitcodes are of raw form"))
6969
}
70+
#[cfg(not(target_os = "nacl"))]
71+
fn optgroups() -> Vec<OptGroup> {
72+
let mut opts = inner_optgroups();
73+
opts.push(reqopt("", "cross-path", "The path to the Pepper SDK", ""));
74+
opts
75+
}
76+
#[cfg(target_os = "nacl")]
77+
fn optgroups() -> Vec<OptGroup> {
78+
let mut opts = inner_optgroups();
79+
// on nacl
80+
opts.push(optopt("", "cross-path", "Ignored", ""));
81+
opts
82+
}
83+
7084
fn fatal<T: Display>(msg: T) -> ! {
7185
println!("error: {}", msg);
7286
env::set_exit_status(1);
@@ -93,6 +107,9 @@ pub fn llvm_warn<T: Display>(msg: T) {
93107

94108
pub fn main() {
95109
let args: Vec<String> = env::args().collect();
110+
111+
let is_host_nacl = host_triple().ends_with("nacl");
112+
96113
let opts = optgroups();
97114

98115
let matches = match getopts(args.tail(), &opts[..]) {
@@ -130,10 +147,16 @@ pub fn main() {
130147
format!("{}-none-nacl-gnu", get_native_arch())
131148
}
132149
};
133-
let cross_path = matches.opt_str("cross-path").unwrap();
134-
let cross_path = env::current_dir()
135-
.unwrap()
136-
.join(&cross_path[..]);
150+
151+
let cross_path = if !is_host_nacl {
152+
let cross_path = matches.opt_str("cross-path").unwrap();
153+
env::current_dir()
154+
.unwrap()
155+
.join(&cross_path[..])
156+
} else {
157+
Path::new("/")
158+
.to_path_buf()
159+
};
137160

138161
let all_raw = matches.opt_present("all-raw");
139162
let mut input: Vec<(String, bool)> = matches.free
@@ -225,10 +248,10 @@ pub fn main() {
225248
}
226249
}
227250

228-
// Only initialize the platforms supported by Rust here, because
229-
// using --llvm-root will have multiple platforms that rustllvm
230-
// doesn't actually link to and it's pointless to put target info
231-
// into the registry that Rust cannot generate machine code for.
251+
// Only initialize the platforms supported by PNaCl && Rust here,
252+
// because using --llvm-root will have multiple platforms that rustllvm
253+
// doesn't actually link to and it's pointless to put target info into
254+
// the registry that Rust cannot generate machine code for.
232255
llvm::LLVMInitializeX86TargetInfo();
233256
llvm::LLVMInitializeX86Target();
234257
llvm::LLVMInitializeX86TargetMC();
@@ -334,45 +357,66 @@ pub fn main() {
334357

335358
debug!("linking");
336359

337-
let mut toolchain_path = cross_path.clone();
338-
toolchain_path.push("toolchain");
339-
toolchain_path.push(&{
340-
let mut s = pnacl_toolchain_prefix();
341-
s.push_str("_pnacl");
342-
s
343-
});
344-
345-
346-
let mut tcp = toolchain_path.clone();
347-
tcp.push("translator");
348-
tcp.push(arch);
349-
tcp.push("lib");
350-
351-
let crtbegin = tcp.clone()
352-
.join("crtbegin.o")
353-
.into_os_string()
354-
.into_string()
355-
.unwrap();
356-
let pnacl_irt_shim = tcp.clone()
357-
.join("libpnacl_irt_shim.a")
358-
.into_os_string()
359-
.into_string()
360-
.unwrap();
361-
let gcc = tcp.clone()
362-
.join("libgcc.a")
363-
.into_os_string()
364-
.into_string()
365-
.unwrap();
366-
let crt_platform = tcp.clone()
367-
.join("libcrt_platform.a")
368-
.into_os_string()
369-
.into_string()
370-
.unwrap();
371-
let crtend = tcp.clone()
372-
.join("crtend.o")
373-
.into_os_string()
374-
.into_string()
375-
.unwrap();
360+
static NATIVE_SUPPORT_LIBS: &'static [&'static str] =
361+
&["crtbegin.o",
362+
"libpnacl_irt_shim.a",
363+
"libgcc.a",
364+
"libcrt_platform.a",
365+
"crtend.o"];
366+
367+
#[cfg(not(target_os = "nacl"))]
368+
fn get_native_support_lib_paths<P: AsRef<Path>>(cross_path: P, arch: &str)
369+
-> Vec<String>
370+
{
371+
let mut toolchain_path = cross_path
372+
.as_ref()
373+
.join("toolchain")
374+
.to_path_buf();
375+
toolchain_path.push(&{
376+
let mut s = pnacl_toolchain_prefix();
377+
s.push_str("_pnacl");
378+
s
379+
});
380+
381+
382+
let mut tcp = toolchain_path;
383+
tcp.push("translator");
384+
tcp.push(arch);
385+
tcp.push("lib");
386+
387+
NATIVE_SUPPORT_LIBS.iter()
388+
.map(|lib| {
389+
tcp.clone()
390+
.join(lib)
391+
.into_os_string()
392+
.into_string()
393+
.unwrap()
394+
})
395+
.collect()
396+
}
397+
#[cfg(target_os = "nacl")]
398+
fn get_native_support_lib_paths<P: AsRef<Path>>(_: P, arch: &str) -> Vec<String> {
399+
let lib_path = Path::new("/lib")
400+
.join("translator")
401+
.join(arch);
402+
NATIVE_SUPPORT_LIBS.iter()
403+
.map(|lib| {
404+
lib_path
405+
.join(lib)
406+
.into_os_string()
407+
.into_string()
408+
.unwrap()
409+
})
410+
.collect()
411+
}
412+
413+
let libs = get_native_support_lib_paths(&cross_path, arch);
414+
debug_assert!(libs.len() == 5);
415+
let crtbegin = libs[0].clone();
416+
let pnacl_irt_shim = libs[1].clone();
417+
let gcc = libs[2].clone();
418+
let crt_platform = libs[3].clone();
419+
let crtend = libs[4].clone();
376420

377421
let nexe_link_args = vec!("-nostdlib".to_string(),
378422
"--no-fix-cortex-a8".to_string(),
@@ -397,10 +441,34 @@ pub fn main() {
397441
.chain(nexe_link_args_suffix.into_iter())
398442
.collect();
399443

400-
let mut gold = toolchain_path.clone();
401-
gold.push("bin");
402-
gold.push("le32-nacl-ld.gold");
403-
let gold = gold;
444+
#[cfg(not(target_os = "nacl"))]
445+
fn get_linker<P: AsRef<Path>>(cross_path: P) -> PathBuf {
446+
let mut toolchain_path = cross_path
447+
.as_ref()
448+
.to_path_buf();
449+
toolchain_path.push("toolchain");
450+
toolchain_path.push(&{
451+
let mut s = pnacl_toolchain_prefix();
452+
s.push_str("_pnacl");
453+
s
454+
});
455+
456+
let mut gold = toolchain_path;
457+
gold.push("bin");
458+
gold.push("le32-nacl-ld.gold");
459+
gold
460+
}
461+
#[cfg(target_os = "nacl")]
462+
fn get_linker<P: AsRef<Path>>(_: Path) -> PathBuf {
463+
use std::env::consts::EXE_SUFFIX;
464+
let linker = format!("ld.gold{}",
465+
EXE_SUFFIX);
466+
Path::new("/bin")
467+
.join(&linker)
468+
.to_path_buf()
469+
}
470+
471+
let gold = get_linker(&cross_path);
404472

405473
fn cleanup_objs(m: &Matches, objs: Vec<String>) {
406474
use std::fs::remove_file;

0 commit comments

Comments
 (0)