Skip to content

Commit 8655f8d

Browse files
committed
Allow static and dynamic linking
Currently bindgen always uses clang-sys with the "runtime" feature - that is, load libclang at runtime with dlopen (or similar) at runtime. This PR keeps this default, but also - adds "static" to statically link libclang - without either "runtime" or "static", link with the shared library Many distributions don't ship with a static libclang, but linking with the dynamic library will use normal ld.so mechanisms to define where the .so file should be found. (Ditto for the Mac and Windows equivalents.)
1 parent ac99aa1 commit 8655f8d

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

Cargo.toml

+5-4
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ cexpr = "0.3.3"
4949
cfg-if = "0.1.0"
5050
# This kinda sucks: https://github.com/rust-lang/cargo/issues/1982
5151
clap = { version = "2", optional = true }
52-
clang-sys = { version = "0.28.0", features = ["runtime", "clang_6_0"] }
52+
clang-sys = { version = "0.28.0", features = ["clang_6_0"] }
5353
lazy_static = "1"
5454
peeking_take_while = "0.1.2"
5555
quote = { version = "1", default-features = false }
@@ -70,9 +70,10 @@ optional = true
7070
version = "0.4"
7171

7272
[features]
73-
default = ["logging", "clap"]
73+
default = ["logging", "clap", "runtime"]
7474
logging = ["env_logger", "log"]
75-
static = []
75+
static = ["clang-sys/static"]
76+
runtime = ["clang-sys/runtime"]
7677

7778
# These features only exist for CI testing -- don't use them if you're not hacking
7879
# on bindgen!
@@ -81,4 +82,4 @@ testing_only_extra_assertions = []
8182
testing_only_libclang_5 = []
8283
testing_only_libclang_4 = []
8384
testing_only_libclang_3_9 = []
84-
testing_only_libclang_3_8 = []
85+
testing_only_libclang_3_8 = []

src/lib.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ use std::io::{self, Write};
9595
use std::{env, iter};
9696
use std::path::{Path, PathBuf};
9797
use std::process::{Command, Stdio};
98-
use std::sync::Arc;
9998

10099
// Some convenient typedefs for a fast hash map and hash set.
101100
type HashMap<K, V> = ::fxhash::FxHashMap<K, V>;
@@ -1697,6 +1696,7 @@ impl Default for BindgenOptions {
16971696
}
16981697
}
16991698

1699+
#[cfg(feature = "runtime")]
17001700
fn ensure_libclang_is_loaded() {
17011701
if clang_sys::is_loaded() {
17021702
return;
@@ -1707,7 +1707,7 @@ fn ensure_libclang_is_loaded() {
17071707
// across different threads.
17081708

17091709
lazy_static! {
1710-
static ref LIBCLANG: Arc<clang_sys::SharedLibrary> = {
1710+
static ref LIBCLANG: std::sync::Arc<clang_sys::SharedLibrary> = {
17111711
clang_sys::load().expect("Unable to find libclang");
17121712
clang_sys::get_library()
17131713
.expect("We just loaded libclang and it had better still be \
@@ -1718,6 +1718,10 @@ fn ensure_libclang_is_loaded() {
17181718
clang_sys::set_library(Some(LIBCLANG.clone()));
17191719
}
17201720

1721+
#[cfg(not(feature = "runtime"))]
1722+
fn ensure_libclang_is_loaded() {
1723+
}
1724+
17211725
/// Generated Rust bindings.
17221726
#[derive(Debug)]
17231727
pub struct Bindings {
@@ -1732,7 +1736,10 @@ impl Bindings {
17321736
) -> Result<Bindings, ()> {
17331737
ensure_libclang_is_loaded();
17341738

1739+
#[cfg(feature = "runtime")]
17351740
debug!("Generating bindings, libclang at {}", clang_sys::get_library().unwrap().path().display());
1741+
#[cfg(not(feature = "runtime"))]
1742+
debug!("Generating bindings, libclang linked");
17361743

17371744
options.build();
17381745

@@ -2066,10 +2073,7 @@ pub struct ClangVersion {
20662073

20672074
/// Get the major and the minor semver numbers of Clang's version
20682075
pub fn clang_version() -> ClangVersion {
2069-
if !clang_sys::is_loaded() {
2070-
// TODO(emilio): Return meaningful error (breaking).
2071-
clang_sys::load().expect("Unable to find libclang");
2072-
}
2076+
ensure_libclang_is_loaded();
20732077

20742078
let raw_v: String = clang::extract_clang_version();
20752079
let split_v: Option<Vec<&str>> = raw_v.split_whitespace().nth(2).map(|v| {

0 commit comments

Comments
 (0)