| 1 |
// SPDX-License-Identifier: Apache-2.0 |
| 2 |
|
| 3 |
//! Finds `libclang` static or shared libraries and links to them. |
| 4 |
//! |
| 5 |
//! # Environment Variables |
| 6 |
//! |
| 7 |
//! This build script can make use of several environment variables to help it |
| 8 |
//! find the required static or shared libraries. |
| 9 |
//! |
| 10 |
//! * `LLVM_CONFIG_PATH` - provides a path to an `llvm-config` executable |
| 11 |
//! * `LIBCLANG_PATH` - provides a path to a directory containing a `libclang` |
| 12 |
//! shared library or a path to a specific `libclang` shared library |
| 13 |
//! * `LIBCLANG_STATIC_PATH` - provides a path to a directory containing LLVM |
| 14 |
//! and Clang static libraries |
| 15 |
|
| 16 |
#![allow(unused_attributes)] |
| 17 |
|
| 18 |
extern crate glob; |
| 19 |
|
| 20 |
use std::path::Path; |
| 21 |
|
| 22 |
#[macro_use] |
| 23 |
#[path = "build/macros.rs"] |
| 24 |
pub mod macros; |
| 25 |
|
| 26 |
#[path = "build/common.rs"] |
| 27 |
pub mod common; |
| 28 |
#[path = "build/dynamic.rs"] |
| 29 |
pub mod dynamic; |
| 30 |
#[path = "build/static.rs"] |
| 31 |
pub mod r#static; |
| 32 |
|
| 33 |
/// Copies a file. |
| 34 |
#[cfg(feature = "runtime")] |
| 35 |
fn copy(source: &str, destination: &Path) { |
| 36 |
use std::fs::File; |
| 37 |
use std::io::{Read, Write}; |
| 38 |
|
| 39 |
let mut string = String::new(); |
| 40 |
File::open(source) |
| 41 |
.unwrap() |
| 42 |
.read_to_string(&mut string) |
| 43 |
.unwrap(); |
| 44 |
File::create(destination) |
| 45 |
.unwrap() |
| 46 |
.write_all(string.as_bytes()) |
| 47 |
.unwrap(); |
| 48 |
} |
| 49 |
|
| 50 |
/// Copies the code used to find and link to `libclang` shared libraries into |
| 51 |
/// the build output directory so that it may be used when linking at runtime. |
| 52 |
#[cfg(feature = "runtime")] |
| 53 |
fn main() { |
| 54 |
use std::env; |
| 55 |
|
| 56 |
if cfg!(feature = "static") { |
| 57 |
panic!("`runtime` and `static` features can't be combined"); |
| 58 |
} |
| 59 |
|
| 60 |
let out = env::var("OUT_DIR").unwrap(); |
| 61 |
copy("build/macros.rs", &Path::new(&out).join("macros.rs")); |
| 62 |
copy("build/common.rs", &Path::new(&out).join("common.rs")); |
| 63 |
copy("build/dynamic.rs", &Path::new(&out).join("dynamic.rs")); |
| 64 |
} |
| 65 |
|
| 66 |
/// Finds and links to the required libraries dynamically or statically. |
| 67 |
#[cfg(not(feature = "runtime"))] |
| 68 |
fn main() { |
| 69 |
if cfg!(feature = "static") { |
| 70 |
r#static::link(); |
| 71 |
} else { |
| 72 |
dynamic::link(); |
| 73 |
} |
| 74 |
|
| 75 |
if let Some(output) = common::run_llvm_config(&["--includedir"]) { |
| 76 |
let directory = Path::new(output.trim_end()); |
| 77 |
println!("cargo:include={}", directory.display()); |
| 78 |
} |
| 79 |
} |