| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::{Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#}; |
| 4 | |
| 5 | use globrust-analyzer cargo glob 0.3.4 crate/::Patternrust-analyzer cargo glob 0.3.4 Pattern#; |
| 6 | |
| 7 | use superrust-analyzer cargo clang-sys 1.8.1 crate/::commonrust-analyzer cargo clang-sys 1.8.1 common/; |
| 8 | |
| 9 | //================================================ |
| 10 | // Searching |
| 11 | //================================================ |
| 12 | |
| 13 | /// Clang static libraries required to link to `libclang` 3.5 and later. |
| 14 | const CLANG_LIBRARIESrust-analyzer cargo clang-sys 1.8.1 `r#static`/CLANG_LIBRARIES.: &[&str] = &[ |
| 15 | "clang", |
| 16 | "clangAST", |
| 17 | "clangAnalysis", |
| 18 | "clangBasic", |
| 19 | "clangDriver", |
| 20 | "clangEdit", |
| 21 | "clangFrontend", |
| 22 | "clangIndex", |
| 23 | "clangLex", |
| 24 | "clangParse", |
| 25 | "clangRewrite", |
| 26 | "clangSema", |
| 27 | "clangSerialization", |
| 28 | ]; |
| 29 | |
| 30 | /// Gets the name of an LLVM or Clang static library from a path. |
| 31 | fn get_library_namerust-analyzer cargo clang-sys 1.8.1 `r#static`/get_library_name().(path: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> { |
| 32 | path.file_stemrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]file_stem().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|p| { |
| 33 | let string = p.to_string_lossyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_string_lossy().(); |
| 34 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(name) = string.strip_prefixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]strip_prefix().("lib") { |
| 35 | name.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().() |
| 36 | } else { |
| 37 | string.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().() |
| 38 | } |
| 39 | }) |
| 40 | } |
| 41 | |
| 42 | /// Gets the LLVM static libraries required to link to `libclang`. |
| 43 | fn get_llvm_librariesrust-analyzer cargo clang-sys 1.8.1 `r#static`/get_llvm_libraries().() -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> { |
| 44 | commonrust-analyzer cargo clang-sys 1.8.1 common/::run_llvm_configrust-analyzer cargo clang-sys 1.8.1 common/run_llvm_config().(&["--libs"]) |
| 45 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().() |
| 46 | .split_whitespacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_whitespace().() |
| 47 | .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|p| { |
| 48 | // Depending on the version of `llvm-config` in use, listed |
| 49 | // libraries may be in one of two forms, a full path to the library |
| 50 | // or simply prefixed with `-l`. |
| 51 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(path) = p.strip_prefixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]strip_prefix().("-l") { |
| 52 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(path.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()) |
| 53 | } else { |
| 54 | get_library_namerust-analyzer cargo clang-sys 1.8.1 `r#static`/get_library_name().(Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().(p)) |
| 55 | } |
| 56 | }) |
| 57 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().() |
| 58 | } |
| 59 | |
| 60 | /// Gets the Clang static libraries required to link to `libclang`. |
| 61 | fn get_clang_librariesrust-analyzer cargo clang-sys 1.8.1 `r#static`/get_clang_libraries().<P: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>>(directory: P) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> { |
| 62 | // Escape the directory in case it contains characters that have special |
| 63 | // meaning in glob patterns (e.g., `[` or `]`). |
| 64 | let directory = Patternrust-analyzer cargo glob 0.3.4 Pattern#::escaperust-analyzer cargo glob 0.3.4 impl#[Pattern]escape().(directory.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().to_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_str().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()); |
| 65 | let directory = Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().(&directory); |
| 66 | |
| 67 | let pattern = directory.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("libclang*.a").to_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_str().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(); |
| 68 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(libraries) = globrust-analyzer cargo glob 0.3.4 crate/::globrust-analyzer cargo glob 0.3.4 glob().(&pattern) { |
| 69 | libraries |
| 70 | .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|l| l.okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().().and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|l| get_library_namerust-analyzer cargo clang-sys 1.8.1 `r#static`/get_library_name().(&l))) |
| 71 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().() |
| 72 | } else { |
| 73 | CLANG_LIBRARIESrust-analyzer cargo clang-sys 1.8.1 `r#static`/CLANG_LIBRARIES..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|l| (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().l).to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().() |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /// Finds a directory containing LLVM and Clang static libraries and returns the |
| 78 | /// path to that directory. |
| 79 | fn findrust-analyzer cargo clang-sys 1.8.1 `r#static`/find().() -> PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf# { |
| 80 | let name = if target_osrust-analyzer cargo clang-sys 1.8.1 macros/target_os!!("windows") { |
| 81 | "libclang.lib" |
| 82 | } else { |
| 83 | "libclang.a" |
| 84 | }; |
| 85 | |
| 86 | let files = commonrust-analyzer cargo clang-sys 1.8.1 common/::search_libclang_directoriesrust-analyzer cargo clang-sys 1.8.1 common/search_libclang_directories().(&[name.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()], "LIBCLANG_STATIC_PATH"); |
| 87 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((directory, _)) = files.into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().().nextrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/into_iter/impl#[`IntoIter<T, A>`][Iterator]next().() { |
| 88 | directory |
| 89 | } else { |
| 90 | panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!( |
| 91 | "could not find the required `{name}` static library, see the \ |
| 92 | README for more information on how to link to `libclang` statically: \ |
| 93 | https://github.com/KyleMayes/clang-sys?tab=readme-ov-file#static" |
| 94 | ); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | //================================================ |
| 99 | // Linking |
| 100 | //================================================ |
| 101 | |
| 102 | /// Finds and links to `libclang` static libraries. |
| 103 | pub fn linkrust-analyzer cargo clang-sys 1.8.1 `r#static`/link().() { |
| 104 | let cep = commonrust-analyzer cargo clang-sys 1.8.1 common/::CommandErrorPrinterrust-analyzer cargo clang-sys 1.8.1 common/CommandErrorPrinter#::defaultrust-analyzer cargo clang-sys 1.8.1 common/impl#[CommandErrorPrinter][Default]default().(); |
| 105 | |
| 106 | let directory = findrust-analyzer cargo clang-sys 1.8.1 `r#static`/find().(); |
| 107 | |
| 108 | // Specify required Clang static libraries. |
| 109 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("cargo:rustc-link-search=native={}", directory.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().()); |
| 110 | for library in get_clang_librariesrust-analyzer cargo clang-sys 1.8.1 `r#static`/get_clang_libraries().(directory) { |
| 111 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("cargo:rustc-link-lib=static={}", library); |
| 112 | } |
| 113 | |
| 114 | // Determine the shared mode used by LLVM. |
| 115 | let mode = commonrust-analyzer cargo clang-sys 1.8.1 common/::run_llvm_configrust-analyzer cargo clang-sys 1.8.1 common/run_llvm_config().(&["--shared-mode"]).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|m| m.trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().().to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()); |
| 116 | let prefix = if mode.map_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map_or().(false, |m| m rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq().==rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq()."static") { |
| 117 | "static=" |
| 118 | } else { |
| 119 | "" |
| 120 | }; |
| 121 | |
| 122 | // Specify required LLVM static libraries. |
| 123 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!( |
| 124 | "cargo:rustc-link-search=native={}", |
| 125 | commonrust-analyzer cargo clang-sys 1.8.1 common/::run_llvm_configrust-analyzer cargo clang-sys 1.8.1 common/run_llvm_config().(&["--libdir"]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().trim_endrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_end().() |
| 126 | ); |
| 127 | for library in get_llvm_librariesrust-analyzer cargo clang-sys 1.8.1 `r#static`/get_llvm_libraries().() { |
| 128 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("cargo:rustc-link-lib={}{}", prefix, library); |
| 129 | } |
| 130 | |
| 131 | // Specify required system libraries. |
| 132 | // MSVC doesn't need this, as it tracks dependencies inside `.lib` files. |
| 133 | if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_os = "freebsd") { |
| 134 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("cargo:rustc-flags=-l ffi -l ncursesw -l c++ -l z"); |
| 135 | } else if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(any(target_os = "haiku", target_os = "linux")) { |
| 136 | if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(feature = "libcpp") { |
| 137 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("cargo:rustc-flags=-l c++"); |
| 138 | } else { |
| 139 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("cargo:rustc-flags=-l ffi -l ncursesw -l stdc++ -l z"); |
| 140 | } |
| 141 | } else if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_os = "macos") { |
| 142 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("cargo:rustc-flags=-l ffi -l ncurses -l c++ -l z"); |
| 143 | } |
| 144 | |
| 145 | cep.discardrust-analyzer cargo clang-sys 1.8.1 common/impl#[CommandErrorPrinter]discard().(); |
| 146 | } |