David LeGare | 82e2b17 | 2022-03-01 18:53:05 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
Chih-Hung Hsieh | fab4380 | 2020-04-07 14:24:01 -0700 | [diff] [blame] | 2 | |
David LeGare | 82e2b17 | 2022-03-01 18:53:05 +0000 | [diff] [blame] | 3 | //! Finds `libclang` static or shared libraries and links to them. |
Chih-Hung Hsieh | fab4380 | 2020-04-07 14:24:01 -0700 | [diff] [blame] | 4 | //! |
| 5 | //! # Environment Variables |
| 6 | //! |
| 7 | //! This build script can make use of several environment variables to help it |
David LeGare | 82e2b17 | 2022-03-01 18:53:05 +0000 | [diff] [blame] | 8 | //! find the required static or shared libraries. |
Chih-Hung Hsieh | fab4380 | 2020-04-07 14:24:01 -0700 | [diff] [blame] | 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 | #[path = "build/common.rs"] |
| 23 | pub mod common; |
| 24 | #[path = "build/dynamic.rs"] |
| 25 | pub mod dynamic; |
| 26 | #[path = "build/static.rs"] |
David LeGare | 82e2b17 | 2022-03-01 18:53:05 +0000 | [diff] [blame] | 27 | pub mod r#static; |
Chih-Hung Hsieh | fab4380 | 2020-04-07 14:24:01 -0700 | [diff] [blame] | 28 | |
David LeGare | 82e2b17 | 2022-03-01 18:53:05 +0000 | [diff] [blame] | 29 | /// Copies a file. |
Chih-Hung Hsieh | fab4380 | 2020-04-07 14:24:01 -0700 | [diff] [blame] | 30 | #[cfg(feature = "runtime")] |
| 31 | fn 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 | |
David LeGare | 82e2b17 | 2022-03-01 18:53:05 +0000 | [diff] [blame] | 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. |
Chih-Hung Hsieh | fab4380 | 2020-04-07 14:24:01 -0700 | [diff] [blame] | 48 | #[cfg(feature = "runtime")] |
| 49 | fn 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 | |
David LeGare | 82e2b17 | 2022-03-01 18:53:05 +0000 | [diff] [blame] | 61 | /// Finds and links to the required libraries dynamically or statically. |
Chih-Hung Hsieh | fab4380 | 2020-04-07 14:24:01 -0700 | [diff] [blame] | 62 | #[cfg(not(feature = "runtime"))] |
| 63 | fn main() { |
| 64 | if cfg!(feature = "static") { |
David LeGare | 82e2b17 | 2022-03-01 18:53:05 +0000 | [diff] [blame] | 65 | r#static::link(); |
Chih-Hung Hsieh | fab4380 | 2020-04-07 14:24:01 -0700 | [diff] [blame] | 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 | } |