Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions build/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ thread_local! {
/// Adds an error encountered by the build script while executing a command.
fn add_command_error(name: &str, path: &str, arguments: &[&str], message: String) {
COMMAND_ERRORS.with(|e| {
e.borrow_mut()
.entry(name.into())
.or_default()
.push(format!(
"couldn't execute `{} {}` (path={}) ({})",
name,
arguments.join(" "),
path,
message,
))
e.borrow_mut().entry(name.into()).or_default().push(format!(
"couldn't execute `{} {}` (path={}) ({})",
name,
arguments.join(" "),
path,
message,
))
});
}

Expand Down Expand Up @@ -190,14 +187,14 @@ const DIRECTORIES_WINDOWS: &[(&str, bool)] = &[
("C:\\LLVM\\lib", true),
// LLVM + Clang can be installed as a component of Visual Studio.
// https://github.com/KyleMayes/clang-sys/issues/121
("C:\\Program Files*\\Microsoft Visual Studio\\*\\VC\\Tools\\Llvm\\**\\lib", true),
(
"C:\\Program Files*\\Microsoft Visual Studio\\*\\VC\\Tools\\Llvm\\**\\lib",
true,
),
];

/// `libclang` directory patterns for illumos
const DIRECTORIES_ILLUMOS: &[&str] = &[
"/opt/ooce/llvm-*/lib",
"/opt/ooce/clang-*/lib",
];
const DIRECTORIES_ILLUMOS: &[&str] = &["/opt/ooce/llvm-*/lib", "/opt/ooce/clang-*/lib"];

//================================================
// Searching
Expand Down Expand Up @@ -318,11 +315,15 @@ pub fn search_libclang_directories(filenames: &[String], variable: &str) -> Vec<
DIRECTORIES_MACOS.into()
} else if target_os!("windows") {
let msvc = target_env!("msvc");

DIRECTORIES_WINDOWS
.iter()
.filter(|d| d.1 || !msvc)
.map(|d| d.0)
.collect()
} else if target_os!("cygwin") {
// For Cygwin/MSYS environments, the filesystem layout is Unix-like.
DIRECTORIES_LINUX.into()
} else if target_os!("illumos") {
DIRECTORIES_ILLUMOS.into()
} else {
Expand All @@ -334,7 +335,11 @@ pub fn search_libclang_directories(filenames: &[String], variable: &str) -> Vec<
let directories = if test!() {
directories
.iter()
.map(|d| d.strip_prefix('/').or_else(|| d.strip_prefix("C:\\")).unwrap_or(d))
.map(|d| {
d.strip_prefix('/')
.or_else(|| d.strip_prefix("C:\\"))
.unwrap_or(d)
})
.collect::<Vec<_>>()
} else {
directories
Expand Down
13 changes: 10 additions & 3 deletions build/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ fn search_libclang_directories(runtime: bool) -> Result<Vec<(PathBuf, String, Ve
}
}

if target_os!("freebsd") || target_os!("haiku") || target_os!("netbsd") || target_os!("openbsd") {
if target_os!("freebsd") || target_os!("haiku") || target_os!("netbsd") || target_os!("openbsd")
{
// Some BSD distributions don't create a `libclang.so` symlink either,
// but use a different naming scheme for versioned files (e.g.,
// `libclang.so.7.0`).
Expand All @@ -151,8 +152,10 @@ fn search_libclang_directories(runtime: bool) -> Result<Vec<(PathBuf, String, Ve
// The official LLVM build uses `libclang.dll` on Windows instead of
// `clang.dll`. However, unofficial builds such as MinGW use `clang.dll`.
files.push("libclang.dll".into());
} else if target_os!("cygwin") {
files.push("msys-clang-*.dll".into());
files.push("cygclang-*.dll".into());
}

// Find and validate `libclang` shared libraries and collect the versions.
let mut valid = vec![];
let mut invalid = vec![];
Expand Down Expand Up @@ -264,7 +267,11 @@ pub fn link() {

// Strip extensions and trailing version numbers (e.g., the `.so.7.0` in
// `libclang.so.7.0`) and also `.dll` for MinGW / MSYS.
let name = match name.find(".dylib").or_else(|| name.find(".so")).or_else(|| name.find(".dll")) {
let name = match name
.find(".dylib")
.or_else(|| name.find(".so"))
.or_else(|| name.find(".dll"))
{
Some(index) => &name[0..index],
None => name,
};
Expand Down
4 changes: 3 additions & 1 deletion build/macros.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// SPDX-License-Identifier: Apache-2.0

macro_rules! test {
() => (cfg!(test) && ::std::env::var("_CLANG_SYS_TEST").is_ok());
() => {
cfg!(test) && ::std::env::var("_CLANG_SYS_TEST").is_ok()
};
}

macro_rules! target_os {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,7 @@ cenum! {
/// Only produced by `libclang` 11.0 and later.
const CXType_Atomic = 177,
/// Only produced by `libclang` 15.0 and later.
const CXType_BTFTagAttributed = 178,
const CXType_BTFTagAttributed = 178,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ https://docs.rs/clang-sys/latest/clang_sys/{0}/index.html

Instructions for installing `libclang` can be found here:
https://rust-lang.github.io/rust-bindgen/requirements.html
"#,
"#,
stringify!($name),
library
.version()
Expand Down
30 changes: 21 additions & 9 deletions tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ struct RunCommandMock {
responses: HashMap<Vec<String>, String>,
}


#[derive(Copy, Clone, Debug)]
enum Arch {
ARM64,
Expand Down Expand Up @@ -77,7 +76,10 @@ impl Env {
env: None,
vars: HashMap::new(),
cwd: env::current_dir().unwrap(),
tmp: tempfile::Builder::new().prefix("clang_sys_test").tempdir().unwrap(),
tmp: tempfile::Builder::new()
.prefix("clang_sys_test")
.tempdir()
.unwrap(),
files: vec![],
commands: Default::default(),
}
Expand All @@ -96,7 +98,8 @@ impl Env {

fn var(mut self, name: &str, value: Option<&str>) -> Self {
let previous = env::var(name).ok();
self.vars.insert(name.into(), (value.map(|v| v.into()), previous));
self.vars
.insert(name.into(), (value.map(|v| v.into()), previous));
self
}

Expand Down Expand Up @@ -141,7 +144,11 @@ impl Env {

let mut key = vec![command];
key.extend(args);
self.commands.lock().unwrap().responses.insert(key, response.into());
self.commands
.lock()
.unwrap()
.responses
.insert(key, response.into());

self
}
Expand Down Expand Up @@ -173,7 +180,9 @@ impl Env {
let args = args.iter().map(|a| a.to_string()).collect::<Vec<_>>();

let mut commands = commands.lock().unwrap();
commands.invocations.push((command.clone(), path, args.clone()));
commands
.invocations
.push((command.clone(), path, args.clone()));

let mut key = vec![command];
key.extend(args);
Expand Down Expand Up @@ -209,7 +218,7 @@ impl Drop for Env {
#[test]
fn test_all() {
// Run tests serially since they alter the environment.

test_linux_directory_preference();
test_linux_version_preference();
test_linux_directory_and_version_preference();
Expand All @@ -228,7 +237,10 @@ macro_rules! assert_error {
($result:expr, $contents:expr $(,)?) => {
if let Err(error) = $result {
if !error.contains($contents) {
panic!("expected error to contain {:?}, received: {error:?}", $contents);
panic!(
"expected error to contain {:?}, received: {error:?}",
$contents
);
}
} else {
panic!("expected error, received: {:?}", $result);
Expand Down Expand Up @@ -336,7 +348,7 @@ fn test_windows_arm64_on_x86_64() {
.enable();

assert_error!(
dynamic::find(true),
dynamic::find(true),
"invalid: [(Program Files\\LLVM\\bin\\libclang.dll: invalid DLL (ARM64)",
);
}
Expand All @@ -350,7 +362,7 @@ fn test_windows_x86_64_on_arm64() {
.enable();

assert_error!(
dynamic::find(true),
dynamic::find(true),
"invalid: [(Program Files\\LLVM\\bin\\libclang.dll: invalid DLL (x86-64)",
);
}