| Chih-Hung Hsieh | fab4380 | 2020-04-07 14:24:01 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | extern crate glob; |
| 16 | |
| 17 | use std::path::{Path, PathBuf}; |
| 18 | |
| Joel Galenson | eabe835 | 2021-09-22 10:52:39 -0700 | [diff] [blame] | 19 | use glob::Pattern; |
| 20 | |
| Chih-Hung Hsieh | fab4380 | 2020-04-07 14:24:01 -0700 | [diff] [blame] | 21 | use common; |
| 22 | |
| 23 | /// Returns the name of an LLVM or Clang library from a path to such a library. |
| 24 | fn get_library_name(path: &Path) -> Option<String> { |
| 25 | path.file_stem().map(|p| { |
| 26 | let string = p.to_string_lossy(); |
| Joel Galenson | eabe835 | 2021-09-22 10:52:39 -0700 | [diff] [blame] | 27 | if let Some(name) = string.strip_prefix("lib") { |
| 28 | name.to_owned() |
| Chih-Hung Hsieh | fab4380 | 2020-04-07 14:24:01 -0700 | [diff] [blame] | 29 | } else { |
| 30 | string.to_string() |
| 31 | } |
| 32 | }) |
| 33 | } |
| 34 | |
| 35 | /// Returns the LLVM libraries required to link to `libclang` statically. |
| 36 | fn 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 Galenson | eabe835 | 2021-09-22 10:52:39 -0700 | [diff] [blame] | 44 | if let Some(path) = p.strip_prefix("-l") { |
| 45 | Some(path.into()) |
| Chih-Hung Hsieh | fab4380 | 2020-04-07 14:24:01 -0700 | [diff] [blame] | 46 | } 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. |
| 54 | const 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. |
| 71 | fn get_clang_libraries<P: AsRef<Path>>(directory: P) -> Vec<String> { |
| Joel Galenson | eabe835 | 2021-09-22 10:52:39 -0700 | [diff] [blame] | 72 | // 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 Hsieh | fab4380 | 2020-04-07 14:24:01 -0700 | [diff] [blame] | 78 | 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. |
| 88 | fn 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 Huang | 8b9513e | 2020-07-13 22:05:39 -0700 | [diff] [blame] | 96 | if let Some((directory, _)) = files.into_iter().next() { |
| Chih-Hung Hsieh | fab4380 | 2020-04-07 14:24:01 -0700 | [diff] [blame] | 97 | directory |
| 98 | } else { |
| 99 | panic!("could not find any static libraries"); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /// Find and link to `libclang` statically. |
| 104 | pub fn link() { |
| Haibo Huang | 8b9513e | 2020-07-13 22:05:39 -0700 | [diff] [blame] | 105 | let cep = common::CommandErrorPrinter::default(); |
| 106 | |
| Chih-Hung Hsieh | fab4380 | 2020-04-07 14:24:01 -0700 | [diff] [blame] | 107 | 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 Huang | 8b9513e | 2020-07-13 22:05:39 -0700 | [diff] [blame] | 141 | |
| 142 | cep.discard(); |
| Chih-Hung Hsieh | fab4380 | 2020-04-07 14:24:01 -0700 | [diff] [blame] | 143 | } |