1 // Copyright 2016 Kyle Mayes
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 //! Finds `libclang` static or dynamic libraries and links to them.
16 //!
17 //! # Environment Variables
18 //!
19 //! This build script can make use of several environment variables to help it
20 //! find the required static or dynamic libraries.
21 //!
22 //! * `LLVM_CONFIG_PATH` - provides a path to an `llvm-config` executable
23 //! * `LIBCLANG_PATH` - provides a path to a directory containing a `libclang`
24 //!    shared library or a path to a specific `libclang` shared library
25 //! * `LIBCLANG_STATIC_PATH` - provides a path to a directory containing LLVM
26 //!    and Clang static libraries
27 
28 #![allow(unused_attributes)]
29 
30 extern crate glob;
31 
32 use std::path::Path;
33 
34 #[path = "build/common.rs"]
35 pub mod common;
36 #[path = "build/dynamic.rs"]
37 pub mod dynamic;
38 #[path = "build/static.rs"]
39 pub mod static_;
40 
41 /// Copy the file from the supplied source to the supplied destination.
42 #[cfg(feature = "runtime")]
copy(source: &str, destination: &Path)43 fn copy(source: &str, destination: &Path) {
44     use std::fs::File;
45     use std::io::{Read, Write};
46 
47     let mut string = String::new();
48     File::open(source)
49         .unwrap()
50         .read_to_string(&mut string)
51         .unwrap();
52     File::create(destination)
53         .unwrap()
54         .write_all(string.as_bytes())
55         .unwrap();
56 }
57 
58 /// Generates the finding and linking code so that it may be used at runtime.
59 #[cfg(feature = "runtime")]
main()60 fn main() {
61     use std::env;
62 
63     if cfg!(feature = "static") {
64         panic!("`runtime` and `static` features can't be combined");
65     }
66 
67     let out = env::var("OUT_DIR").unwrap();
68     copy("build/common.rs", &Path::new(&out).join("common.rs"));
69     copy("build/dynamic.rs", &Path::new(&out).join("dynamic.rs"));
70 }
71 
72 /// Finds and links to the required libraries.
73 #[cfg(not(feature = "runtime"))]
main()74 fn main() {
75     if cfg!(feature = "static") {
76         static_::link();
77     } else {
78         dynamic::link();
79     }
80 
81     if let Some(output) = common::run_llvm_config(&["--includedir"]) {
82         let directory = Path::new(output.trim_end());
83         println!("cargo:include={}", directory.display());
84     }
85 }
86