blob: 66da27445727447f9c9f6fd73c2ca5d86c1648a7 [file] [log] [blame]
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -07001// Copyright 2018 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
15extern crate glob;
16
17use std::path::{Path, PathBuf};
18
Joel Galensoneabe8352021-09-22 10:52:39 -070019use glob::Pattern;
20
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -070021use common;
22
23/// Returns the name of an LLVM or Clang library from a path to such a library.
24fn get_library_name(path: &Path) -> Option<String> {
25 path.file_stem().map(|p| {
26 let string = p.to_string_lossy();
Joel Galensoneabe8352021-09-22 10:52:39 -070027 if let Some(name) = string.strip_prefix("lib") {
28 name.to_owned()
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -070029 } else {
30 string.to_string()
31 }
32 })
33}
34
35/// Returns the LLVM libraries required to link to `libclang` statically.
36fn get_llvm_libraries() -> Vec<String> {
37 common::run_llvm_config(&["--libs"])
38 .unwrap()
39 .split_whitespace()
40 .filter_map(|p| {
41 // Depending on the version of `llvm-config` in use, listed
42 // libraries may be in one of two forms, a full path to the library
43 // or simply prefixed with `-l`.
Joel Galensoneabe8352021-09-22 10:52:39 -070044 if let Some(path) = p.strip_prefix("-l") {
45 Some(path.into())
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -070046 } else {
47 get_library_name(Path::new(p))
48 }
49 })
50 .collect()
51}
52
53/// Clang libraries required to link to `libclang` 3.5 and later statically.
54const CLANG_LIBRARIES: &[&str] = &[
55 "clang",
56 "clangAST",
57 "clangAnalysis",
58 "clangBasic",
59 "clangDriver",
60 "clangEdit",
61 "clangFrontend",
62 "clangIndex",
63 "clangLex",
64 "clangParse",
65 "clangRewrite",
66 "clangSema",
67 "clangSerialization",
68];
69
70/// Returns the Clang libraries required to link to `libclang` statically.
71fn get_clang_libraries<P: AsRef<Path>>(directory: P) -> Vec<String> {
Joel Galensoneabe8352021-09-22 10:52:39 -070072 // Escape the directory in case it contains characters that have special
73 // meaning in glob patterns (e.g., `[` or `]`).
74 let directory = Pattern::escape(directory.as_ref().to_str().unwrap());
75 let directory = Path::new(&directory);
76
77 let pattern = directory.join("libclang*.a").to_str().unwrap().to_owned();
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -070078 if let Ok(libraries) = glob::glob(&pattern) {
79 libraries
80 .filter_map(|l| l.ok().and_then(|l| get_library_name(&l)))
81 .collect()
82 } else {
83 CLANG_LIBRARIES.iter().map(|l| (*l).to_string()).collect()
84 }
85}
86
87/// Returns a directory containing `libclang` static libraries.
88fn find() -> PathBuf {
89 let name = if cfg!(target_os = "windows") {
90 "libclang.lib"
91 } else {
92 "libclang.a"
93 };
94
95 let files = common::search_libclang_directories(&[name.into()], "LIBCLANG_STATIC_PATH");
Haibo Huang8b9513e2020-07-13 22:05:39 -070096 if let Some((directory, _)) = files.into_iter().next() {
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -070097 directory
98 } else {
99 panic!("could not find any static libraries");
100 }
101}
102
103/// Find and link to `libclang` statically.
104pub fn link() {
Haibo Huang8b9513e2020-07-13 22:05:39 -0700105 let cep = common::CommandErrorPrinter::default();
106
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -0700107 let directory = find();
108
109 // Specify required Clang static libraries.
110 println!("cargo:rustc-link-search=native={}", directory.display());
111 for library in get_clang_libraries(directory) {
112 println!("cargo:rustc-link-lib=static={}", library);
113 }
114
115 // Determine the shared mode used by LLVM.
116 let mode = common::run_llvm_config(&["--shared-mode"]).map(|m| m.trim().to_owned());
117 let prefix = if mode.map_or(false, |m| m == "static") {
118 "static="
119 } else {
120 ""
121 };
122
123 // Specify required LLVM static libraries.
124 println!(
125 "cargo:rustc-link-search=native={}",
126 common::run_llvm_config(&["--libdir"]).unwrap().trim_end()
127 );
128 for library in get_llvm_libraries() {
129 println!("cargo:rustc-link-lib={}{}", prefix, library);
130 }
131
132 // Specify required system libraries.
133 // MSVC doesn't need this, as it tracks dependencies inside `.lib` files.
134 if cfg!(target_os = "freebsd") {
135 println!("cargo:rustc-flags=-l ffi -l ncursesw -l c++ -l z");
136 } else if cfg!(target_os = "linux") {
137 println!("cargo:rustc-flags=-l ffi -l ncursesw -l stdc++ -l z");
138 } else if cfg!(target_os = "macos") {
139 println!("cargo:rustc-flags=-l ffi -l ncurses -l c++ -l z");
140 }
Haibo Huang8b9513e2020-07-13 22:05:39 -0700141
142 cep.discard();
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -0700143}