Skip to content

Commit 0f2a98c

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 6100b5b commit 0f2a98c

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

Cargo.toml

+4-3
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", "which-rustfmt"]
73+
default = ["logging", "clap", "runtime", "which-rustfmt"]
7474
logging = ["env_logger", "log"]
75-
static = []
75+
static = ["clang-sys/static"]
76+
runtime = ["clang-sys/runtime"]
7677
# Dynamically discover a `rustfmt` binary using the `which` crate
7778
which-rustfmt = ["which"]
7879

src/lib.rs

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

101100
// Some convenient typedefs for a fast hash map and hash set.
@@ -1717,6 +1716,7 @@ impl Default for BindgenOptions {
17171716
}
17181717
}
17191718

1719+
#[cfg(feature = "runtime")]
17201720
fn ensure_libclang_is_loaded() {
17211721
if clang_sys::is_loaded() {
17221722
return;
@@ -1727,7 +1727,7 @@ fn ensure_libclang_is_loaded() {
17271727
// across different threads.
17281728

17291729
lazy_static! {
1730-
static ref LIBCLANG: Arc<clang_sys::SharedLibrary> = {
1730+
static ref LIBCLANG: std::sync::Arc<clang_sys::SharedLibrary> = {
17311731
clang_sys::load().expect("Unable to find libclang");
17321732
clang_sys::get_library().expect(
17331733
"We just loaded libclang and it had better still be \
@@ -1739,6 +1739,10 @@ fn ensure_libclang_is_loaded() {
17391739
clang_sys::set_library(Some(LIBCLANG.clone()));
17401740
}
17411741

1742+
#[cfg(not(feature = "runtime"))]
1743+
fn ensure_libclang_is_loaded() {
1744+
}
1745+
17421746
/// Generated Rust bindings.
17431747
#[derive(Debug)]
17441748
pub struct Bindings {
@@ -1753,10 +1757,13 @@ impl Bindings {
17531757
) -> Result<Bindings, ()> {
17541758
ensure_libclang_is_loaded();
17551759

1760+
#[cfg(feature = "runtime")]
17561761
debug!(
17571762
"Generating bindings, libclang at {}",
17581763
clang_sys::get_library().unwrap().path().display()
17591764
);
1765+
#[cfg(not(feature = "runtime"))]
1766+
debug!("Generating bindings, libclang linked");
17601767

17611768
options.build();
17621769

@@ -2113,10 +2120,7 @@ pub struct ClangVersion {
21132120

21142121
/// Get the major and the minor semver numbers of Clang's version
21152122
pub fn clang_version() -> ClangVersion {
2116-
if !clang_sys::is_loaded() {
2117-
// TODO(emilio): Return meaningful error (breaking).
2118-
clang_sys::load().expect("Unable to find libclang");
2119-
}
2123+
ensure_libclang_is_loaded();
21202124

21212125
let raw_v: String = clang::extract_clang_version();
21222126
let split_v: Option<Vec<&str>> = raw_v

0 commit comments

Comments
 (0)