xref: /third_party/rust/crates/clang-sys/build.rs (revision 6cdb10c1)
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
18extern crate glob;
19
20use std::path::Path;
21
22#[path = "build/common.rs"]
23pub mod common;
24#[path = "build/dynamic.rs"]
25pub mod dynamic;
26#[path = "build/static.rs"]
27pub mod r#static;
28
29/// Copies a file.
30#[cfg(feature = "runtime")]
31fn copy(source: &str, destination: &Path) {
32    use std::fs::File;
33    use std::io::{Read, Write};
34
35    let mut string = String::new();
36    File::open(source)
37        .unwrap()
38        .read_to_string(&mut string)
39        .unwrap();
40    File::create(destination)
41        .unwrap()
42        .write_all(string.as_bytes())
43        .unwrap();
44}
45
46/// Copies the code used to find and link to `libclang` shared libraries into
47/// the build output directory so that it may be used when linking at runtime.
48#[cfg(feature = "runtime")]
49fn main() {
50    use std::env;
51
52    if cfg!(feature = "static") {
53        panic!("`runtime` and `static` features can't be combined");
54    }
55
56    let out = env::var("OUT_DIR").unwrap();
57    copy("build/common.rs", &Path::new(&out).join("common.rs"));
58    copy("build/dynamic.rs", &Path::new(&out).join("dynamic.rs"));
59}
60
61/// Finds and links to the required libraries dynamically or statically.
62#[cfg(not(feature = "runtime"))]
63fn main() {
64    if cfg!(feature = "static") {
65        // r#static::link();
66    } else {
67        dynamic::link();
68    }
69
70    if let Some(output) = common::run_llvm_config(&["--includedir"]) {
71        let directory = Path::new(output.trim_end());
72        println!("cargo:include={}", directory.display());
73    }
74}
75