Skip to content

Commit

Permalink
Fix weird implicit dependency between rustllvm and rustc_codegen_llvm
Browse files Browse the repository at this point in the history
rustllvm relies on the `LLVMRustStringWriteImpl` symbol existing, but
this symbol was previously defined in a *downstream* crate
(rustc_codegen_llvm, which depends on rustc_llvm.

While this somehow worked under the old 'separate bootstrap step for
codegen' scheme, it meant that rustc_llvm could not actually be built by
itself, since it relied linking to the downstream rustc_codegen_llvm
crate.

Now that librustc_codegen_llvm is just a normal crate, we actually try
to build a standalone rustc_llvm when we run tests. This commit moves
`LLVMRustStringWriteImpl` into rustc_llvm (technically the rustllvm
directory, which has its contents built by rustc_llvm). This ensures
that we can build each crate in the graph by itself, without requiring
that any downstream crates be linked in as well.
  • Loading branch information
Aaron1011 committed Dec 12, 2019
1 parent 150d328 commit 47e932b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3672,6 +3672,7 @@ version = "0.0.0"
dependencies = [
"build_helper",
"cc",
"libc",
]

[[package]]
Expand Down
19 changes: 2 additions & 17 deletions src/librustc_codegen_llvm/llvm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ pub use self::Linkage::*;

use std::str::FromStr;
use std::string::FromUtf8Error;
use std::slice;
use std::ffi::CStr;
use std::cell::RefCell;
use libc::{c_uint, c_char, size_t};
use libc::c_uint;
use rustc_data_structures::small_c_str::SmallCStr;
use rustc_llvm::RustString;

pub mod archive_ro;
pub mod diagnostic;
Expand Down Expand Up @@ -81,21 +81,6 @@ impl FromStr for ArchiveKind {
}
}

#[repr(C)]
pub struct RustString {
bytes: RefCell<Vec<u8>>,
}

/// Appending to a Rust string -- used by RawRustStringOstream.
#[no_mangle]
pub unsafe extern "C" fn LLVMRustStringWriteImpl(sr: &RustString,
ptr: *const c_char,
size: size_t) {
let slice = slice::from_raw_parts(ptr as *const u8, size as usize);

sr.bytes.borrow_mut().extend_from_slice(slice);
}

pub fn SetInstructionCallConv(instr: &'a Value, cc: CallConv) {
unsafe {
LLVMSetInstructionCallConv(instr, cc as c_uint);
Expand Down
3 changes: 3 additions & 0 deletions src/librustc_llvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ path = "lib.rs"
static-libstdcpp = []
emscripten = []

[dependencies]
libc = "0.2"

[build-dependencies]
build_helper = { path = "../build_helper" }
cc = "1.0.1"
20 changes: 20 additions & 0 deletions src/librustc_llvm/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@

// NOTE: This crate only exists to allow linking on mingw targets.

use std::cell::RefCell;
use std::slice;
use libc::{c_char, size_t};


#[repr(C)]
pub struct RustString {
pub bytes: RefCell<Vec<u8>>,
}

/// Appending to a Rust string -- used by RawRustStringOstream.
#[no_mangle]
pub unsafe extern "C" fn LLVMRustStringWriteImpl(sr: &RustString,
ptr: *const c_char,
size: size_t) {
let slice = slice::from_raw_parts(ptr as *const u8, size as usize);

sr.bytes.borrow_mut().extend_from_slice(slice);
}

/// Initialize targets enabled by the build script via `cfg(llvm_component = "...")`.
/// N.B., this function can't be moved to `rustc_codegen_llvm` because of the `cfg`s.
pub fn initialize_available_targets() {
Expand Down

0 comments on commit 47e932b

Please sign in to comment.