]> git.lizzy.rs Git - rust.git/blob - src/rustllvm/Linker.cpp
Rollup merge of #55630 - petrochenkov:noprelude, r=Centril
[rust.git] / src / rustllvm / Linker.cpp
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #include "llvm/Linker/Linker.h"
12
13 #include "rustllvm.h"
14
15 using namespace llvm;
16
17 struct RustLinker {
18   Linker L;
19   LLVMContext &Ctx;
20
21   RustLinker(Module &M) :
22     L(M),
23     Ctx(M.getContext())
24   {}
25 };
26
27 extern "C" RustLinker*
28 LLVMRustLinkerNew(LLVMModuleRef DstRef) {
29   Module *Dst = unwrap(DstRef);
30
31   auto Ret = llvm::make_unique<RustLinker>(*Dst);
32   return Ret.release();
33 }
34
35 extern "C" void
36 LLVMRustLinkerFree(RustLinker *L) {
37   delete L;
38 }
39
40 extern "C" bool
41 LLVMRustLinkerAdd(RustLinker *L, char *BC, size_t Len) {
42   std::unique_ptr<MemoryBuffer> Buf =
43       MemoryBuffer::getMemBufferCopy(StringRef(BC, Len));
44
45   Expected<std::unique_ptr<Module>> SrcOrError =
46       llvm::getLazyBitcodeModule(Buf->getMemBufferRef(), L->Ctx);
47   if (!SrcOrError) {
48     LLVMRustSetLastError(toString(SrcOrError.takeError()).c_str());
49     return false;
50   }
51
52   auto Src = std::move(*SrcOrError);
53
54   if (L->L.linkInModule(std::move(Src))) {
55     LLVMRustSetLastError("");
56     return false;
57   }
58   return true;
59 }