Debian Codegraph @

build.rs

raw symbols SCIP
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 std::path::Path;
19
20 #[macro_use]
21 #[path = "build/macros.rs"]
22 pub mod macros;
23
24 #[path = "build/common.rs"]
25 pub mod common;
26 #[path = "build/dynamic.rs"]
27 pub mod dynamic;
28 #[path = "build/static.rs"]
29 pub mod r#static;
30
31 /// Copies a file.
32 #[cfg(feature = "runtime")]
33 fn copy(source: &str, destination: &Path) {
34 use std::fs::File;
35 use std::io::{Read, Write};
36
37 let mut string = String::new();
38 File::open(source)
39 .unwrap()
40 .read_to_string(&mut string)
41 .unwrap();
42 File::create(destination)
43 .unwrap()
44 .write_all(string.as_bytes())
45 .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 main() {
52 use std::env;
53
54 if cfg!(feature = "static") {
55 panic!("`runtime` and `static` features can't be combined");
56 }
57
58 let out = env::var("OUT_DIR").unwrap();
59 copy("build/macros.rs", &Path::new(&out).join("macros.rs"));
60 copy("build/common.rs", &Path::new(&out).join("common.rs"));
61 copy("build/dynamic.rs", &Path::new(&out).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 cfg!(feature = "static") {
68 r#static::link();
69 } else {
70 dynamic::link();
71 }
72
73 if let Some(output) = common::run_llvm_config(&["--includedir"]) {
74 let directory = Path::new(output.trim_end());
75 println!("cargo:include={}", directory.display());
76 }
77 }