Skip to content

Commit

Permalink
Auto merge of #46435 - cuviper:min-llvm-3.9, r=rkruppe
Browse files Browse the repository at this point in the history
Assume at least LLVM 3.9 in rustllvm and rustc_llvm

We bumped the minimum LLVM to 3.9 in #45326.  This just cleans up the conditional code in the `rustllvm` C++ wrappers to assume that minimum, and similarly cleans up the `rustc_llvm` build script.
  • Loading branch information
bors committed Dec 3, 2017
2 parents f2b11f3 + 5c4452a commit 1956d55
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 180 deletions.
44 changes: 12 additions & 32 deletions src/librustc_llvm/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,14 @@ use std::path::{PathBuf, Path};

use build_helper::output;

fn detect_llvm_link(major: u32, minor: u32, llvm_config: &Path)
-> (&'static str, Option<&'static str>) {
if major > 3 || (major == 3 && minor >= 9) {
// Force the link mode we want, preferring static by default, but
// possibly overridden by `configure --enable-llvm-link-shared`.
if env::var_os("LLVM_LINK_SHARED").is_some() {
return ("dylib", Some("--link-shared"));
} else {
return ("static", Some("--link-static"));
}
} else if major == 3 && minor == 8 {
// Find out LLVM's default linking mode.
let mut mode_cmd = Command::new(llvm_config);
mode_cmd.arg("--shared-mode");
if output(&mut mode_cmd).trim() == "shared" {
return ("dylib", None);
} else {
return ("static", None);
}
fn detect_llvm_link() -> (&'static str, &'static str) {
// Force the link mode we want, preferring static by default, but
// possibly overridden by `configure --enable-llvm-link-shared`.
if env::var_os("LLVM_LINK_SHARED").is_some() {
("dylib", "--link-shared")
} else {
("static", "--link-static")
}
("static", None)
}

fn main() {
Expand Down Expand Up @@ -96,11 +83,11 @@ fn main() {
let version_output = output(&mut version_cmd);
let mut parts = version_output.split('.').take(2)
.filter_map(|s| s.parse::<u32>().ok());
let (major, minor) =
let (major, _minor) =
if let (Some(major), Some(minor)) = (parts.next(), parts.next()) {
(major, minor)
} else {
(3, 7)
(3, 9)
};

if major > 3 {
Expand Down Expand Up @@ -171,17 +158,13 @@ fn main() {
.cpp_link_stdlib(None) // we handle this below
.compile("rustllvm");

let (llvm_kind, llvm_link_arg) = detect_llvm_link(major, minor, &llvm_config);
let (llvm_kind, llvm_link_arg) = detect_llvm_link();

// Link in all LLVM libraries, if we're uwring the "wrong" llvm-config then
// we don't pick up system libs because unfortunately they're for the host
// of llvm-config, not the target that we're attempting to link.
let mut cmd = Command::new(&llvm_config);
cmd.arg("--libs");

if let Some(link_arg) = llvm_link_arg {
cmd.arg(link_arg);
}
cmd.arg(llvm_link_arg).arg("--libs");

if !is_crossed {
cmd.arg("--system-libs");
Expand Down Expand Up @@ -230,10 +213,7 @@ fn main() {
// hack around this by replacing the host triple with the target and pray
// that those -L directories are the same!
let mut cmd = Command::new(&llvm_config);
if let Some(link_arg) = llvm_link_arg {
cmd.arg(link_arg);
}
cmd.arg("--ldflags");
cmd.arg(llvm_link_arg).arg("--ldflags");
for lib in output(&mut cmd).split_whitespace() {
if lib.starts_with("-LIBPATH:") {
println!("cargo:rustc-link-search=native={}", &lib[9..]);
Expand Down
49 changes: 0 additions & 49 deletions src/rustllvm/ArchiveWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ struct RustArchiveMember {

RustArchiveMember()
: Filename(nullptr), Name(nullptr),
#if LLVM_VERSION_GE(3, 8)
Child(nullptr, nullptr, nullptr)
#else
Child(nullptr, nullptr)
#endif
{
}
~RustArchiveMember() {}
Expand All @@ -38,13 +34,9 @@ struct RustArchiveIterator {
bool First;
Archive::child_iterator Cur;
Archive::child_iterator End;
#if LLVM_VERSION_GE(3, 9)
Error Err;

RustArchiveIterator() : First(true), Err(Error::success()) {}
#else
RustArchiveIterator() : First(true) {}
#endif
};

enum class LLVMRustArchiveKind {
Expand Down Expand Up @@ -84,19 +76,11 @@ extern "C" LLVMRustArchiveRef LLVMRustOpenArchive(char *Path) {
return nullptr;
}

#if LLVM_VERSION_LE(3, 8)
ErrorOr<std::unique_ptr<Archive>> ArchiveOr =
#else
Expected<std::unique_ptr<Archive>> ArchiveOr =
#endif
Archive::create(BufOr.get()->getMemBufferRef());

if (!ArchiveOr) {
#if LLVM_VERSION_LE(3, 8)
LLVMRustSetLastError(ArchiveOr.getError().message().c_str());
#else
LLVMRustSetLastError(toString(ArchiveOr.takeError()).c_str());
#endif
return nullptr;
}

Expand All @@ -114,16 +98,12 @@ extern "C" LLVMRustArchiveIteratorRef
LLVMRustArchiveIteratorNew(LLVMRustArchiveRef RustArchive) {
Archive *Archive = RustArchive->getBinary();
RustArchiveIterator *RAI = new RustArchiveIterator();
#if LLVM_VERSION_LE(3, 8)
RAI->Cur = Archive->child_begin();
#else
RAI->Cur = Archive->child_begin(RAI->Err);
if (RAI->Err) {
LLVMRustSetLastError(toString(std::move(RAI->Err)).c_str());
delete RAI;
return nullptr;
}
#endif
RAI->End = Archive->child_end();
return RAI;
}
Expand All @@ -141,29 +121,18 @@ LLVMRustArchiveIteratorNext(LLVMRustArchiveIteratorRef RAI) {
// but instead advance it *before* fetching the child in all later calls.
if (!RAI->First) {
++RAI->Cur;
#if LLVM_VERSION_GE(3, 9)
if (RAI->Err) {
LLVMRustSetLastError(toString(std::move(RAI->Err)).c_str());
return nullptr;
}
#endif
} else {
RAI->First = false;
}

if (RAI->Cur == RAI->End)
return nullptr;

#if LLVM_VERSION_EQ(3, 8)
const ErrorOr<Archive::Child> *Cur = RAI->Cur.operator->();
if (!*Cur) {
LLVMRustSetLastError(Cur->getError().message().c_str());
return nullptr;
}
const Archive::Child &Child = Cur->get();
#else
const Archive::Child &Child = *RAI->Cur.operator->();
#endif
Archive::Child *Ret = new Archive::Child(Child);

return Ret;
Expand Down Expand Up @@ -239,18 +208,13 @@ LLVMRustWriteArchive(char *Dst, size_t NumMembers,
const LLVMRustArchiveMemberRef *NewMembers,
bool WriteSymbtab, LLVMRustArchiveKind RustKind) {

#if LLVM_VERSION_LE(3, 8)
std::vector<NewArchiveIterator> Members;
#else
std::vector<NewArchiveMember> Members;
#endif
auto Kind = fromRust(RustKind);

for (size_t I = 0; I < NumMembers; I++) {
auto Member = NewMembers[I];
assert(Member->Name);
if (Member->Filename) {
#if LLVM_VERSION_GE(3, 9)
Expected<NewArchiveMember> MOrErr =
NewArchiveMember::getFile(Member->Filename, true);
if (!MOrErr) {
Expand All @@ -261,30 +225,17 @@ LLVMRustWriteArchive(char *Dst, size_t NumMembers,
MOrErr->MemberName = sys::path::filename(MOrErr->MemberName);
#endif
Members.push_back(std::move(*MOrErr));
#elif LLVM_VERSION_EQ(3, 8)
Members.push_back(NewArchiveIterator(Member->Filename));
#else
Members.push_back(NewArchiveIterator(Member->Filename, Member->Name));
#endif
} else {
#if LLVM_VERSION_LE(3, 8)
Members.push_back(NewArchiveIterator(Member->Child, Member->Name));
#else
Expected<NewArchiveMember> MOrErr =
NewArchiveMember::getOldMember(Member->Child, true);
if (!MOrErr) {
LLVMRustSetLastError(toString(MOrErr.takeError()).c_str());
return LLVMRustResult::Failure;
}
Members.push_back(std::move(*MOrErr));
#endif
}
}
#if LLVM_VERSION_GE(3, 8)
auto Pair = writeArchive(Dst, Members, WriteSymbtab, Kind, true, false);
#else
auto Pair = writeArchive(Dst, Members, WriteSymbtab, Kind, true);
#endif
if (!Pair.second)
return LLVMRustResult::Success;
LLVMRustSetLastError(Pair.second.message().c_str());
Expand Down
21 changes: 0 additions & 21 deletions src/rustllvm/PassWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ extern "C" void LLVMInitializePasses() {
initializeVectorization(Registry);
initializeIPO(Registry);
initializeAnalysis(Registry);
#if LLVM_VERSION_EQ(3, 7)
initializeIPA(Registry);
#endif
initializeTransformUtils(Registry);
initializeInstCombine(Registry);
initializeInstrumentation(Registry);
Expand Down Expand Up @@ -273,18 +270,10 @@ enum class LLVMRustRelocMode {
ROPIRWPI,
};

#if LLVM_VERSION_LE(3, 8)
static Reloc::Model fromRust(LLVMRustRelocMode RustReloc) {
#else
static Optional<Reloc::Model> fromRust(LLVMRustRelocMode RustReloc) {
#endif
switch (RustReloc) {
case LLVMRustRelocMode::Default:
#if LLVM_VERSION_LE(3, 8)
return Reloc::Default;
#else
return None;
#endif
case LLVMRustRelocMode::Static:
return Reloc::Static;
case LLVMRustRelocMode::PIC:
Expand Down Expand Up @@ -390,9 +379,6 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
}

TargetOptions Options;
#if LLVM_VERSION_LE(3, 8)
Options.PositionIndependentExecutable = PositionIndependentExecutable;
#endif

Options.FloatABIType = FloatABI::Default;
if (UseSoftFloat) {
Expand Down Expand Up @@ -720,10 +706,6 @@ extern "C" void LLVMRustRunRestrictionPass(LLVMModuleRef M, char **Symbols,
size_t Len) {
llvm::legacy::PassManager passes;

#if LLVM_VERSION_LE(3, 8)
ArrayRef<const char *> Ref(Symbols, Len);
passes.add(llvm::createInternalizePass(Ref));
#else
auto PreserveFunctions = [=](const GlobalValue &GV) {
for (size_t I = 0; I < Len; I++) {
if (GV.getName() == Symbols[I]) {
Expand All @@ -734,7 +716,6 @@ extern "C" void LLVMRustRunRestrictionPass(LLVMModuleRef M, char **Symbols,
};

passes.add(llvm::createInternalizePass(PreserveFunctions));
#endif

passes.run(*unwrap(M));
}
Expand Down Expand Up @@ -770,9 +751,7 @@ extern "C" LLVMTargetDataRef LLVMRustGetModuleDataLayout(LLVMModuleRef M) {
}

extern "C" void LLVMRustSetModulePIELevel(LLVMModuleRef M) {
#if LLVM_VERSION_GE(3, 9)
unwrap(M)->setPIELevel(PIELevel::Level::Large);
#endif
}

extern "C" bool
Expand Down
Loading

0 comments on commit 1956d55

Please sign in to comment.