blob: bfaaff607cb63b49362ebad6c6da16c55c362b23 [file] [log] [blame]
David LeGare82e2b172022-03-01 18:53:05 +00001// SPDX-License-Identifier: Apache-2.0
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -07002
David LeGare82e2b172022-03-01 18:53:05 +00003//! Finds `libclang` static or shared libraries and links to them.
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -07004//!
5//! # Environment Variables
6//!
7//! This build script can make use of several environment variables to help it
David LeGare82e2b172022-03-01 18:53:05 +00008//! find the required static or shared libraries.
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -07009//!
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"]
David LeGare82e2b172022-03-01 18:53:05 +000027pub mod r#static;
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -070028
David LeGare82e2b172022-03-01 18:53:05 +000029/// Copies a file.
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -070030#[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
David LeGare82e2b172022-03-01 18:53:05 +000046/// 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 Hsiehfab43802020-04-07 14:24:01 -070048#[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
David LeGare82e2b172022-03-01 18:53:05 +000061/// Finds and links to the required libraries dynamically or statically.
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -070062#[cfg(not(feature = "runtime"))]
63fn main() {
64 if cfg!(feature = "static") {
David LeGare82e2b172022-03-01 18:53:05 +000065 r#static::link();
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -070066 } 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}