Skip to content

Commit 80e1a37

Browse files
tamirdTamir Duberstein
authored and
Tamir Duberstein
committed
rustc_llvm: use rust-bindgen to generate FFI bindings
1 parent 0a2e9ad commit 80e1a37

12 files changed

+483
-1853
lines changed

src/Cargo.lock

+73-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,4 @@ cargo = { path = "tools/cargo" }
5858

5959
[patch.crates-io]
6060
rustfmt-nightly = { path = "tools/rustfmt" }
61+
bindgen = { path = "../../rust-bindgen" }

src/librustc_llvm/Cargo.toml

+2-5
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ crate-type = ["dylib"]
1212
[features]
1313
static-libstdcpp = []
1414

15-
[dependencies]
16-
bitflags = "1.0"
17-
rustc_cratesio_shim = { path = "../librustc_cratesio_shim" }
18-
1915
[build-dependencies]
20-
build_helper = { path = "../build_helper" }
16+
bindgen = "0.31.2"
2117
cc = "1.0.1"
18+
build_helper = { path = "../build_helper" }

src/librustc_llvm/archive_ro.rs

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ impl<'a> Iterator for Iter<'a> {
9393
if ptr.is_null() {
9494
::last_error().map(Err)
9595
} else {
96+
let ptr = ptr as *mut _;
9697
Some(Ok(Child {
9798
ptr,
9899
_data: marker::PhantomData,

src/librustc_llvm/build.rs

+128
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
extern crate bindgen;
1112
extern crate cc;
1213
extern crate build_helper;
1314

@@ -139,6 +140,127 @@ fn main() {
139140
let cxxflags = output(&mut cmd);
140141
let mut cfg = cc::Build::new();
141142
cfg.warnings(false);
143+
144+
let out_path = PathBuf::from(env::var("OUT_DIR")
145+
.expect("Failed to determine output directory"));
146+
147+
let mut builder = bindgen::builder()
148+
.header("../rustllvm/rustllvm.h")
149+
.clang_args(&["-x", "c++"])
150+
.rust_target(bindgen::RustTarget::Nightly)
151+
.whitelist_recursively(false)
152+
.whitelist_type("(LLVM)?Rust.*")
153+
.whitelist_function("(LLVM)?Rust.*")
154+
.rustfmt_bindings(true)
155+
.layout_tests(false)
156+
.with_codegen_config(bindgen::CodegenConfig{
157+
functions: true,
158+
types: true,
159+
vars: false,
160+
methods: false,
161+
constructors: false,
162+
destructors: false,
163+
});
164+
165+
for visible_type in [
166+
"LLVMBool",
167+
"LLVMDiagnosticHandler",
168+
"LLVMTwineRef",
169+
"LLVMValueRef",
170+
// https://github.com/rust-lang-nursery/rust-bindgen/issues/1164.
171+
"llvm::LLVMContext_InlineAsmDiagHandlerTy",
172+
].into_iter() {
173+
builder = builder
174+
.whitelist_type(visible_type);
175+
}
176+
177+
for opaque_type in [
178+
"LLVMBasicBlockRef",
179+
"LLVMBuilderRef",
180+
"LLVMContextRef",
181+
"LLVMDebugLocRef",
182+
"LLVMDiagnosticInfoRef",
183+
"LLVMMemoryBufferRef",
184+
"LLVMMetadataRef",
185+
"LLVMModuleRef",
186+
"LLVMObjectFileRef",
187+
"LLVMOpaqueTwine",
188+
"LLVMOpaqueValue",
189+
"LLVMPassManagerRef",
190+
"LLVMRustArchiveIteratorRef",
191+
"LLVMRustJITMemoryManagerRef",
192+
"LLVMRustOperandBundleDefRef",
193+
"LLVMSMDiagnosticRef",
194+
"LLVMSectionIteratorRef",
195+
"LLVMTargetDataRef",
196+
"LLVMTargetMachineRef",
197+
"LLVMTypeKind",
198+
"LLVMTypeRef",
199+
"RustStringRef",
200+
"llvm::DIBuilder",
201+
"llvm::DiagnosticInfo",
202+
"llvm::SMDiagnostic",
203+
// https://github.com/rust-lang-nursery/rust-bindgen/issues/1164.
204+
"llvm::object::Archive_Child",
205+
"llvm::object::OwningBinary",
206+
].into_iter() {
207+
builder = builder
208+
.opaque_type(opaque_type)
209+
.whitelist_type(opaque_type);
210+
}
211+
212+
for rustified_enum in [
213+
"LLVMAtomicOrdering",
214+
"LLVMAtomicRMWBinOp",
215+
"LLVMDLLStorageClass",
216+
"LLVMDiagnosticSeverity",
217+
"LLVMIntPredicate",
218+
"LLVMRealPredicate",
219+
"LLVMRelocMode",
220+
"LLVMRustArchiveKind",
221+
"LLVMRustAsmDialect",
222+
"LLVMRustAttribute",
223+
"LLVMRustCodeGenOptLevel",
224+
"LLVMRustCodeModel",
225+
"LLVMRustDIFlags",
226+
"LLVMRustDiagnosticKind",
227+
"LLVMRustFileType",
228+
"LLVMRustLinkage",
229+
"LLVMRustPassKind",
230+
"LLVMRustResult",
231+
"LLVMRustSynchronizationScope",
232+
"LLVMRustVisibility",
233+
"LLVMThreadLocalMode",
234+
"LLVMTypeKind",
235+
// https://github.com/rust-lang-nursery/rust-bindgen/issues/1161.
236+
"llvm::CallingConv::.*",
237+
// https://github.com/rust-lang-nursery/rust-bindgen/issues/1164.
238+
"llvm::LLVMContext.*",
239+
].into_iter() {
240+
builder = builder
241+
.whitelist_type(rustified_enum)
242+
.rustified_enum(rustified_enum)
243+
}
244+
245+
for llvm_function in [
246+
"LLVMCountParams",
247+
"LLVMGetParam",
248+
"LLVMCreateObjectFile",
249+
"LLVMCreateTargetData",
250+
"LLVMDisposeObjectFile",
251+
"LLVMDisposeSectionIterator",
252+
"LLVMDisposeTargetData",
253+
"LLVMGetSections",
254+
"LLVMGetValueName",
255+
"LLVMSetFunctionCallConv",
256+
"LLVMSetInstructionCallConv",
257+
"LLVMSetThreadLocal",
258+
"LLVMSetThreadLocalMode",
259+
"LLVMSetUnnamedAddr",
260+
].into_iter() {
261+
builder = builder.whitelist_function(llvm_function);
262+
}
263+
142264
for flag in cxxflags.split_whitespace() {
143265
// Ignore flags like `-m64` when we're doing a cross build
144266
if is_crossed && flag.starts_with("-m") {
@@ -151,8 +273,14 @@ fn main() {
151273
}
152274

153275
cfg.flag(flag);
276+
builder = builder.clang_arg(flag);
154277
}
155278

279+
builder.generate()
280+
.expect("Failed to generate bindings")
281+
.write_to_file(out_path.join("bindings.rs"))
282+
.expect("Failed to write bindings");
283+
156284
for component in &components {
157285
let mut flag = String::from("-DLLVM_COMPONENT_");
158286
flag.push_str(&component.to_uppercase());

src/librustc_llvm/diagnostic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl OptimizationDiagnostic {
9393
}
9494
}
9595

96-
#[derive(Copy, Clone)]
96+
#[derive(Clone)]
9797
pub struct InlineAsmDiagnostic {
9898
pub cookie: c_uint,
9999
pub message: TwineRef,

0 commit comments

Comments
 (0)