| 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 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#; |
| 19 | |
| 20 | #[macro_use] |
| 21 | #[path = "build/macros.rs"] |
| 22 | pub mod macrosrust-analyzer cargo clang-sys 1.8.1 macros/; |
| 23 | |
| 24 | #[path = "build/common.rs"] |
| 25 | pub mod commonrust-analyzer cargo clang-sys 1.8.1 common/; |
| 26 | #[path = "build/dynamic.rs"] |
| 27 | pub mod dynamicrust-analyzer cargo clang-sys 1.8.1 dynamic/; |
| 28 | #[path = "build/static.rs"] |
| 29 | pub mod r#staticrust-analyzer cargo clang-sys 1.8.1 `r#static`/; |
| 30 | |
| 31 | /// Copies a file. |
| 32 | #[cfg(feature = "runtime")] |
| 33 | fn copyrust-analyzer cargo clang-sys 1.8.1 copy().(source: &str, destination: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#) { |
| 34 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#; |
| 35 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#}; |
| 36 | |
| 37 | let mut string = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().(); |
| 38 | Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(source) |
| 39 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() |
| 40 | .read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File][Read]read_to_string().(&mut string) |
| 41 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 42 | Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::createrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]create().(destination) |
| 43 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() |
| 44 | .write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(string.as_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_bytes().()) |
| 45 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 46 | } |
| 47 | |
| 48 | /// Copies the code used to find and link to `libclang` shared libraries into |
| 49 | /// the build output directory so that it may be used when linking at runtime. |
| 50 | #[cfg(feature = "runtime")] |
| 51 | fn mainrust-analyzer cargo clang-sys 1.8.1 main().() { |
| 52 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/; |
| 53 | |
| 54 | if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(feature = "static") { |
| 55 | panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("`runtime` and `static` features can't be combined"); |
| 56 | } |
| 57 | |
| 58 | let out = envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("OUT_DIR").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 59 | copyrust-analyzer cargo clang-sys 1.8.1 copy().("build/macros.rs", &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().(&out).joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("macros.rs")); |
| 60 | copyrust-analyzer cargo clang-sys 1.8.1 copy().("build/common.rs", &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().(&out).joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("common.rs")); |
| 61 | copyrust-analyzer cargo clang-sys 1.8.1 copy().("build/dynamic.rs", &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().(&out).joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("dynamic.rs")); |
| 62 | } |
| 63 | |
| 64 | /// Finds and links to the required libraries dynamically or statically. |
| 65 | #[cfg(not(feature = "runtime"))] |
| 66 | fn main() { |
| 67 | if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(feature = "static") { |
| 68 | r#staticrust-analyzer cargo clang-sys 1.8.1 `r#static`/::linkrust-analyzer cargo clang-sys 1.8.1 `r#static`/link().(); |
| 69 | } else { |
| 70 | dynamicrust-analyzer cargo clang-sys 1.8.1 dynamic/::link(); |
| 71 | } |
| 72 | |
| 73 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(output) = commonrust-analyzer cargo clang-sys 1.8.1 common/::run_llvm_configrust-analyzer cargo clang-sys 1.8.1 common/run_llvm_config().(&["--includedir"]) { |
| 74 | let directory = Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::new(output.trim_end()); |
| 75 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("cargo:include={}", directory.display()); |
| 76 | } |
| 77 | } |