Skip to main content

revive_build_utils/
lib.rs

1//! The compiler build utilities library.
2
3/// The revive LLVM host dependency directory prefix environment variable.
4pub const REVIVE_LLVM_HOST_PREFIX: &str = "LLVM_SYS_211_PREFIX";
5
6/// The revive LLVM target dependency directory prefix environment variable.
7pub const REVIVE_LLVM_TARGET_PREFIX: &str = "REVIVE_LLVM_TARGET_PREFIX";
8
9/// The revive LLVM host tool help link.
10pub const REVIVE_LLVM_BUILDER_HELP_LINK: &str =
11    "https://github.com/paritytech/revive?tab=readme-ov-file#building-from-source";
12
13/// Constructs a path to the LLVM tool `name`.
14///
15/// Respects the [`REVIVE_LLVM_HOST_PREFIX`] environment variable.
16pub fn llvm_host_tool(name: &str) -> std::path::PathBuf {
17    std::env::var_os(REVIVE_LLVM_HOST_PREFIX)
18        .map(Into::<std::path::PathBuf>::into)
19        .unwrap_or_else(|| {
20            panic!("install LLVM using the revive-llvm builder and export '{REVIVE_LLVM_HOST_PREFIX}'; see also: {REVIVE_LLVM_BUILDER_HELP_LINK}")
21        })
22        .join("bin")
23        .join(name)
24}
25
26/// Returns the LLVM lib dir.
27///
28/// Respects the [`REVIVE_LLVM_HOST_PREFIX`] environment variable.
29pub fn llvm_lib_dir() -> std::path::PathBuf {
30    std::path::PathBuf::from(llvm_config("--libdir").trim())
31}
32
33/// Returns the LLVM CXX compiler flags.
34///
35/// Respects the [`REVIVE_LLVM_HOST_PREFIX`] environment variable.
36pub fn llvm_cxx_flags() -> String {
37    llvm_config("--cxxflags")
38}
39
40/// Execute the `llvm-config` utility respecting the [`REVIVE_LLVM_HOST_PREFIX`] environment variable.
41fn llvm_config(arg: &str) -> String {
42    let llvm_config = llvm_host_tool("llvm-config");
43    let output = std::process::Command::new(&llvm_config)
44        .arg(arg)
45        .output()
46        .unwrap_or_else(|error| panic!("`{} {arg}` failed: {error}", llvm_config.display()));
47
48    String::from_utf8(output.stdout)
49        .unwrap_or_else(|_| panic!("output of `{} {arg}` should be utf8", llvm_config.display()))
50}