blob: 6af914f011770c7528a66489600881b35e03e901 [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
3extern crate glob;
4
5use std::path::{Path, PathBuf};
6
Joel Galensoneabe8352021-09-22 10:52:39 -07007use glob::Pattern;
8
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -07009use common;
10
David LeGare82e2b172022-03-01 18:53:05 +000011//================================================
12// Searching
13//================================================
14
15/// Clang static libraries required to link to `libclang` 3.5 and later.
16const CLANG_LIBRARIES: &[&str] = &[
17 "clang",
18 "clangAST",
19 "clangAnalysis",
20 "clangBasic",
21 "clangDriver",
22 "clangEdit",
23 "clangFrontend",
24 "clangIndex",
25 "clangLex",
26 "clangParse",
27 "clangRewrite",
28 "clangSema",
29 "clangSerialization",
30];
31
32/// Gets the name of an LLVM or Clang static library from a path.
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -070033fn get_library_name(path: &Path) -> Option<String> {
34 path.file_stem().map(|p| {
35 let string = p.to_string_lossy();
Joel Galensoneabe8352021-09-22 10:52:39 -070036 if let Some(name) = string.strip_prefix("lib") {
37 name.to_owned()
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -070038 } else {
39 string.to_string()
40 }
41 })
42}
43
David LeGare82e2b172022-03-01 18:53:05 +000044/// Gets the LLVM static libraries required to link to `libclang`.
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -070045fn get_llvm_libraries() -> Vec<String> {
46 common::run_llvm_config(&["--libs"])
47 .unwrap()
48 .split_whitespace()
49 .filter_map(|p| {
50 // Depending on the version of `llvm-config` in use, listed
51 // libraries may be in one of two forms, a full path to the library
52 // or simply prefixed with `-l`.
Joel Galensoneabe8352021-09-22 10:52:39 -070053 if let Some(path) = p.strip_prefix("-l") {
54 Some(path.into())
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -070055 } else {
56 get_library_name(Path::new(p))
57 }
58 })
59 .collect()
60}
61
David LeGare82e2b172022-03-01 18:53:05 +000062/// Gets the Clang static libraries required to link to `libclang`.
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -070063fn get_clang_libraries<P: AsRef<Path>>(directory: P) -> Vec<String> {
Joel Galensoneabe8352021-09-22 10:52:39 -070064 // Escape the directory in case it contains characters that have special
65 // meaning in glob patterns (e.g., `[` or `]`).
66 let directory = Pattern::escape(directory.as_ref().to_str().unwrap());
67 let directory = Path::new(&directory);
68
69 let pattern = directory.join("libclang*.a").to_str().unwrap().to_owned();
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -070070 if let Ok(libraries) = glob::glob(&pattern) {
71 libraries
72 .filter_map(|l| l.ok().and_then(|l| get_library_name(&l)))
73 .collect()
74 } else {
75 CLANG_LIBRARIES.iter().map(|l| (*l).to_string()).collect()
76 }
77}
78
David LeGare82e2b172022-03-01 18:53:05 +000079/// Finds a directory containing LLVM and Clang static libraries and returns the
80/// path to that directory.
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -070081fn find() -> PathBuf {
82 let name = if cfg!(target_os = "windows") {
83 "libclang.lib"
84 } else {
85 "libclang.a"
86 };
87
88 let files = common::search_libclang_directories(&[name.into()], "LIBCLANG_STATIC_PATH");
Haibo Huang8b9513e2020-07-13 22:05:39 -070089 if let Some((directory, _)) = files.into_iter().next() {
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -070090 directory
91 } else {
92 panic!("could not find any static libraries");
93 }
94}
95
David LeGare82e2b172022-03-01 18:53:05 +000096//================================================
97// Linking
98//================================================
99
100/// Finds and links to `libclang` static libraries.
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -0700101pub fn link() {
Haibo Huang8b9513e2020-07-13 22:05:39 -0700102 let cep = common::CommandErrorPrinter::default();
103
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -0700104 let directory = find();
105
106 // Specify required Clang static libraries.
107 println!("cargo:rustc-link-search=native={}", directory.display());
108 for library in get_clang_libraries(directory) {
109 println!("cargo:rustc-link-lib=static={}", library);
110 }
111
112 // Determine the shared mode used by LLVM.
113 let mode = common::run_llvm_config(&["--shared-mode"]).map(|m| m.trim().to_owned());
114 let prefix = if mode.map_or(false, |m| m == "static") {
115 "static="
116 } else {
117 ""
118 };
119
120 // Specify required LLVM static libraries.
121 println!(
122 "cargo:rustc-link-search=native={}",
123 common::run_llvm_config(&["--libdir"]).unwrap().trim_end()
124 );
125 for library in get_llvm_libraries() {
126 println!("cargo:rustc-link-lib={}{}", prefix, library);
127 }
128
129 // Specify required system libraries.
130 // MSVC doesn't need this, as it tracks dependencies inside `.lib` files.
131 if cfg!(target_os = "freebsd") {
132 println!("cargo:rustc-flags=-l ffi -l ncursesw -l c++ -l z");
David LeGare82e2b172022-03-01 18:53:05 +0000133 } else if cfg!(any(target_os = "haiku", target_os = "linux")) {
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -0700134 println!("cargo:rustc-flags=-l ffi -l ncursesw -l stdc++ -l z");
135 } else if cfg!(target_os = "macos") {
136 println!("cargo:rustc-flags=-l ffi -l ncurses -l c++ -l z");
137 }
Haibo Huang8b9513e2020-07-13 22:05:39 -0700138
139 cep.discard();
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -0700140}