Skip to content

Commit

Permalink
Improve clang executable search (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleMayes committed May 29, 2024
1 parent 4d2cd0f commit e46f0bf
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## [1.8.2] - UNRELEASED

### Changed
- Improved `Clang::find` to first check directories related to the runtime-loaded `libclang` instance (if any)

### Fixed
- Fixed linking to `libclang` on Windows with MSYS2
- Fixed `Clang::find` to support both the `-target` and `--target` arguments
Expand Down
4 changes: 2 additions & 2 deletions src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ macro_rules! link {
/// A dynamically loaded instance of the `libclang` library.
#[derive(Debug)]
pub struct SharedLibrary {
library: libloading::Library,
path: PathBuf,
pub(crate) library: libloading::Library,
pub(crate) path: PathBuf,
pub functions: Functions,
}

Expand Down
22 changes: 17 additions & 5 deletions src/support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ impl Clang {
/// Returns a `clang` executable if one can be found.
///
/// If the `CLANG_PATH` environment variable is set, that is the instance of
/// `clang` used. Otherwise, a series of directories are searched. First, if
/// a path is supplied, that is the first directory searched. Then, the
/// directory returned by `llvm-config --bindir` is searched. On macOS
/// systems, `xcodebuild -find clang` will next be queried. Last, the
/// directories in the system's `PATH` are searched.
/// `clang` used. Otherwise, these directories are searched in order:
///
/// 1. The supplied path (if provided)
/// 2. Sibling directories for the runtime-loaded `libclang` instance (if any)
/// 3. The directory returned by `llvm-config --bindir`
/// 4. The directory returned by `xcodebuild -find clang` (on macOS)
/// 5. The directories in the system's `PATH` environment variable
///
/// ## Cross-compilation
///
Expand Down Expand Up @@ -83,6 +85,16 @@ impl Clang {
paths.push(path.into());
}

#[cfg(feature = "runtime")]
if let Some(library) = crate::get_library() {
if let Some(directory) = library.path().parent() {
paths.push(directory.into());
if let Some(parent) = directory.parent() {
paths.push(parent.join("bin"));
}
}
}

if let Ok(path) = run_llvm_config(&["--bindir"]) {
if let Some(line) = path.lines().next() {
paths.push(line.into());
Expand Down
11 changes: 11 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,14 @@ fn test_support_target() {
let clang = support::Clang::find(None, args).unwrap();
println!("{:?}", clang);
}

#[cfg(feature = "runtime")]
#[test]
fn test_support_runtime() {
load().unwrap();
let library = get_library().unwrap();
let clang = support::Clang::find(None, &[]).unwrap();
println!("Library path: {}", library.path().display());
println!("Clang path: {}", clang.path.display());
unload().unwrap();
}

0 comments on commit e46f0bf

Please sign in to comment.