Skip to content

Commit 8359491

Browse files
Build librustc_llvm as a dylib, and install it as part of the private rustc
crates. I had to remove `librustc_llvm`'s dependence on the `libc` crate as a part of this. This seems reasonable as the global changes required were quite minimal. Fix an FFI bug: `Bool` is signed.
1 parent 9823cb9 commit 8359491

File tree

8 files changed

+48
-30
lines changed

8 files changed

+48
-30
lines changed

src/Cargo.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1762,6 +1762,7 @@ dependencies = [
17621762
"graphviz 0.0.0",
17631763
"jobserver 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
17641764
"lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
1765+
"libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
17651766
"log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
17661767
"proc_macro 0.0.0",
17671768
"rustc_apfloat 0.0.0",
@@ -2092,7 +2093,6 @@ dependencies = [
20922093
"bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
20932094
"build_helper 0.1.0",
20942095
"cc 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
2095-
"libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
20962096
"rustc_cratesio_shim 0.0.0",
20972097
]
20982098

src/bootstrap/compile.rs

+29-17
Original file line numberDiff line numberDiff line change
@@ -656,8 +656,11 @@ impl Step for CodegenBackend {
656656
let mut files = files.into_iter()
657657
.filter(|f| {
658658
let filename = f.file_name().unwrap().to_str().unwrap();
659-
is_dylib(filename) && filename.contains("rustc_codegen_llvm-")
659+
is_dylib(filename) && (filename.contains("rustc_codegen_llvm-") ||
660+
filename.contains("rustc_llvm-"))
660661
});
662+
let llvm_crate = files.next()
663+
.unwrap_or_else(|| panic!("need the rustc_trans llvm dylib") );
661664
let codegen_backend = match files.next() {
662665
Some(f) => f,
663666
None => panic!("no dylibs built for codegen backend?"),
@@ -668,8 +671,13 @@ impl Step for CodegenBackend {
668671
f.display());
669672
}
670673
let stamp = codegen_backend_stamp(builder, compiler, target, backend);
674+
675+
let llvm_crate = llvm_crate.to_str().unwrap();
671676
let codegen_backend = codegen_backend.to_str().unwrap();
672-
t!(t!(File::create(&stamp)).write_all(codegen_backend.as_bytes()));
677+
let mut stamp = t!(File::create(&stamp));
678+
t!(stamp.write_all(codegen_backend.as_bytes()));
679+
t!(stamp.write_all(&[0]));
680+
t!(stamp.write_all(llvm_crate.as_bytes()));
673681
}
674682
}
675683

@@ -750,28 +758,32 @@ fn copy_codegen_backends_to_sysroot(builder: &Builder,
750758
// Here we're looking for the output dylib of the `CodegenBackend` step and
751759
// we're copying that into the `codegen-backends` folder.
752760
let dst = builder.sysroot_codegen_backends(target_compiler);
761+
let sysroot_dst = builder.sysroot_libdir(target_compiler, target);
753762
t!(fs::create_dir_all(&dst));
754-
763+
t!(fs::create_dir_all(&sysroot_dst));
755764
if builder.config.dry_run {
756765
return;
757766
}
758767

759768
for backend in builder.config.rust_codegen_backends.iter() {
760769
let stamp = codegen_backend_stamp(builder, compiler, target, *backend);
761-
let mut dylib = String::new();
762-
t!(t!(File::open(&stamp)).read_to_string(&mut dylib));
763-
let file = Path::new(&dylib);
764-
let filename = file.file_name().unwrap().to_str().unwrap();
765-
// change `librustc_codegen_llvm-xxxxxx.so` to `librustc_codegen_llvm-llvm.so`
766-
let target_filename = {
767-
let dash = filename.find("-").unwrap();
768-
let dot = filename.find(".").unwrap();
769-
format!("{}-{}{}",
770-
&filename[..dash],
771-
backend,
772-
&filename[dot..])
773-
};
774-
builder.copy(&file, &dst.join(target_filename));
770+
for file in builder.read_stamp_file(&stamp) {
771+
let filename = file.file_name().unwrap().to_str().unwrap();
772+
if filename.contains("rustc_llvm") {
773+
builder.copy(&file, &sysroot_dst.join(filename));
774+
} else {
775+
// change `librustc_codegen_llvm-xxxxxx.so` to `librustc_codegen_llvm-llvm.so`
776+
let target_filename = {
777+
let dash = filename.find("-").unwrap();
778+
let dot = filename.find(".").unwrap();
779+
format!("{}-{}{}",
780+
&filename[..dash],
781+
backend,
782+
&filename[dot..])
783+
};
784+
builder.copy(&file, &dst.join(target_filename));
785+
}
786+
}
775787
}
776788
}
777789

src/librustc/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ chalk-engine = { version = "0.6.0", default-features=false }
5757
# compiles, then please feel free to do so!
5858
flate2 = "1.0"
5959
tempdir = "0.3"
60-
60+
libc = "0.2"
6161

src/librustc_codegen_llvm/back/write.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ use std::sync::mpsc::{channel, Sender, Receiver};
5959
use std::slice;
6060
use std::time::Instant;
6161
use std::thread;
62-
use libc::{c_uint, c_void, c_char, size_t};
62+
use libc::{c_uint, c_char, size_t};
6363

6464
pub const RELOC_MODEL_ARGS : [(&'static str, llvm::RelocMode); 7] = [
6565
("pic", llvm::RelocMode::PIC),
@@ -436,7 +436,7 @@ unsafe extern "C" fn report_inline_asm<'a, 'b>(cgcx: &'a CodegenContext,
436436
}
437437

438438
unsafe extern "C" fn inline_asm_handler(diag: SMDiagnosticRef,
439-
user: *const c_void,
439+
user: *const ::std::os::raw::c_void,
440440
cookie: c_uint) {
441441
if user.is_null() {
442442
return
@@ -449,7 +449,8 @@ unsafe extern "C" fn inline_asm_handler(diag: SMDiagnosticRef,
449449
report_inline_asm(cgcx, &msg, cookie);
450450
}
451451

452-
unsafe extern "C" fn diagnostic_handler(info: DiagnosticInfoRef, user: *mut c_void) {
452+
unsafe extern "C" fn diagnostic_handler(info: DiagnosticInfoRef,
453+
user: *mut ::std::os::raw::c_void) {
453454
if user.is_null() {
454455
return
455456
}

src/librustc_llvm/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ build = "build.rs"
77
[lib]
88
name = "rustc_llvm"
99
path = "lib.rs"
10+
crate-type = ["dylib"]
1011

1112
[features]
1213
static-libstdcpp = []
1314
emscripten = []
1415

1516
[dependencies]
1617
bitflags = "1.0"
17-
libc = "0.2"
1818
rustc_cratesio_shim = { path = "../librustc_cratesio_shim" }
1919

2020
[build-dependencies]

src/librustc_llvm/diagnostic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
pub use self::OptimizationDiagnosticKind::*;
1414
pub use self::Diagnostic::*;
1515

16-
use libc::c_uint;
16+
use std::os::raw::c_uint;
1717
use std::ptr;
1818

1919
use {DiagnosticInfoRef, TwineRef, ValueRef};

src/librustc_llvm/ffi.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ use debuginfo::{DIBuilderRef, DIDescriptor, DIFile, DILexicalBlock, DISubprogram
1919
DIGlobalVariable, DIArray, DISubrange, DITemplateTypeParameter, DIEnumerator,
2020
DINameSpace, DIFlags};
2121

22-
use libc::{c_uint, c_int, size_t, c_char};
23-
use libc::{c_longlong, c_ulonglong, c_void};
22+
use std::os::raw::{c_uint, c_int, c_char};
23+
use std::os::raw::{c_longlong, c_ulonglong, c_void};
24+
25+
use super::size_t;
2426

2527
use RustStringRef;
2628

2729
pub type Opcode = u32;
28-
pub type Bool = c_uint;
30+
pub type Bool = c_int;
2931

3032
pub const True: Bool = 1 as Bool;
3133
pub const False: Bool = 0 as Bool;
@@ -482,7 +484,7 @@ pub mod debuginfo {
482484
bitflags! {
483485
#[repr(C)]
484486
#[derive(Default)]
485-
pub struct DIFlags: ::libc::uint32_t {
487+
pub struct DIFlags: u32 {
486488
const FlagZero = 0;
487489
const FlagPrivate = 1;
488490
const FlagProtected = 2;
@@ -1323,7 +1325,7 @@ extern "C" {
13231325

13241326
pub fn LLVMPassManagerBuilderCreate() -> PassManagerBuilderRef;
13251327
pub fn LLVMPassManagerBuilderDispose(PMB: PassManagerBuilderRef);
1326-
pub fn LLVMPassManagerBuilderSetSizeLevel(PMB: PassManagerBuilderRef, Value: Bool);
1328+
pub fn LLVMPassManagerBuilderSetSizeLevel(PMB: PassManagerBuilderRef, Value: c_uint);
13271329
pub fn LLVMPassManagerBuilderSetDisableUnrollLoops(PMB: PassManagerBuilderRef, Value: Bool);
13281330
pub fn LLVMPassManagerBuilderUseInlinerWithThreshold(PMB: PassManagerBuilderRef,
13291331
threshold: c_uint);

src/librustc_llvm/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -383,14 +383,17 @@ pub fn initialize_available_targets() {
383383
}
384384

385385
pub fn last_error() -> Option<String> {
386+
extern "C" {
387+
fn free(ptr: *mut std::os::raw::c_void);
388+
}
386389
unsafe {
387390
let cstr = LLVMRustGetLastError();
388391
if cstr.is_null() {
389392
None
390393
} else {
391394
let err = CStr::from_ptr(cstr).to_bytes();
392395
let err = String::from_utf8_lossy(err).to_string();
393-
libc::free(cstr as *mut _);
396+
free(cstr as *mut _);
394397
Some(err)
395398
}
396399
}

0 commit comments

Comments
 (0)