blob: 83a81857ea0cc3c2796108a8ec6437174460bee7 [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
19use common;
20
21/// Returns the name of an LLVM or Clang library from a path to such a library.
22fn get_library_name(path: &Path) -> Option<String> {
23 path.file_stem().map(|p| {
24 let string = p.to_string_lossy();
25 if string.starts_with("lib") {
26 string[3..].to_owned()
27 } else {
28 string.to_string()
29 }
30 })
31}
32
33/// Returns the LLVM libraries required to link to `libclang` statically.
34fn get_llvm_libraries() -> Vec<String> {
35 common::run_llvm_config(&["--libs"])
36 .unwrap()
37 .split_whitespace()
38 .filter_map(|p| {
39 // Depending on the version of `llvm-config` in use, listed
40 // libraries may be in one of two forms, a full path to the library
41 // or simply prefixed with `-l`.
42 if p.starts_with("-l") {
43 Some(p[2..].into())
44 } else {
45 get_library_name(Path::new(p))
46 }
47 })
48 .collect()
49}
50
51/// Clang libraries required to link to `libclang` 3.5 and later statically.
52const CLANG_LIBRARIES: &[&str] = &[
53 "clang",
54 "clangAST",
55 "clangAnalysis",
56 "clangBasic",
57 "clangDriver",
58 "clangEdit",
59 "clangFrontend",
60 "clangIndex",
61 "clangLex",
62 "clangParse",
63 "clangRewrite",
64 "clangSema",
65 "clangSerialization",
66];
67
68/// Returns the Clang libraries required to link to `libclang` statically.
69fn get_clang_libraries<P: AsRef<Path>>(directory: P) -> Vec<String> {
70 let pattern = directory
71 .as_ref()
72 .join("libclang*.a")
73 .to_string_lossy()
74 .to_string();
75 if let Ok(libraries) = glob::glob(&pattern) {
76 libraries
77 .filter_map(|l| l.ok().and_then(|l| get_library_name(&l)))
78 .collect()
79 } else {
80 CLANG_LIBRARIES.iter().map(|l| (*l).to_string()).collect()
81 }
82}
83
84/// Returns a directory containing `libclang` static libraries.
85fn find() -> PathBuf {
86 let name = if cfg!(target_os = "windows") {
87 "libclang.lib"
88 } else {
89 "libclang.a"
90 };
91
92 let files = common::search_libclang_directories(&[name.into()], "LIBCLANG_STATIC_PATH");
Haibo Huang8b9513e2020-07-13 22:05:39 -070093 if let Some((directory, _)) = files.into_iter().next() {
Chih-Hung Hsiehfab43802020-04-07 14:24:01 -070094 directory
95 } else {
96 panic!("could not find any static libraries");
97 }
98}
99
100/// Find and link to `libclang` statically.
101pub 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");
133 } else if cfg!(target_os = "linux") {
134 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}