Skip to main content

botan_src/
lib.rs

1use std::env;
2use std::fs;
3use std::io;
4use std::path::{Path, PathBuf};
5use std::process::Command;
6
7const BUILD_ERROR_MSG: &str = "Unable to build botan.";
8const SRC_DIR_ERROR_MSG: &str = "Unable to find the source directory.";
9const INCLUDE_DIR: &str = "build/include/public";
10
11// Pinned upstream release. Single source of truth lives in release.toml;
12// build.rs parses that file and re-exports it via `cargo:rustc-env`.
13pub const BOTAN_VERSION: &str = env!("BOTAN_VERSION");
14pub const BOTAN_TARBALL_SHA256: &str = env!("BOTAN_TARBALL_SHA256");
15pub const BOTAN_TARBALL_URL: &str = env!("BOTAN_TARBALL_URL");
16
17macro_rules! pathbuf_to_string {
18    ($s: ident) => {
19        $s.to_str().expect(BUILD_ERROR_MSG).to_string()
20    };
21}
22
23fn env_name_for(opt: &'static str) -> String {
24    assert!(opt[0..2] == *"--");
25    let to_var = opt[2..].to_uppercase().replace('-', "_");
26    format!("BOTAN_CONFIGURE_{to_var}")
27}
28
29fn configure(build_dir: &str) {
30    let mut configure = Command::new("python3");
31    configure.arg("configure.py");
32    configure.arg(format!("--with-build-dir={build_dir}"));
33    configure.arg("--build-targets=static");
34    configure.arg("--without-documentation");
35    configure.arg("--no-install-python-module");
36    configure.arg("--distribution-info=https://crates.io/crates/botan-src");
37
38    configure.arg(format!(
39        "--cpu={}",
40        env::var("CARGO_CFG_TARGET_ARCH").unwrap()
41    ));
42    configure.arg(format!("--os={}", env::var("CARGO_CFG_TARGET_OS").unwrap()));
43
44    #[cfg(debug_assertions)]
45    configure.arg("--with-debug-info");
46
47    // On Windows we require the amalgamation, to work around the fact that
48    // otherwise the linker command lines become too long for Windows
49    #[cfg(target_os = "windows")]
50    configure.arg("--amalgamation");
51
52    let args = [
53        "--compiler-cache",
54        "--cc",
55        "--cc-bin",
56        "--cc-abi-flags",
57        "--cxxflags",
58        "--extra-cxxflags",
59        "--ldflags",
60        "--ar-command",
61        "--ar-options",
62        "--msvc-runtime",
63        "--system-cert-bundle",
64        "--module-policy",
65        "--enable-modules",
66        "--disable-modules",
67    ];
68
69    let flags = [
70        "--optimize-for-size",
71        "--amalgamation",
72        "--with-commoncrypto",
73        "--with-sqlite3",
74    ];
75
76    for arg_name in &args {
77        let env_name = env_name_for(arg_name);
78        if let Ok(arg_val) = env::var(env_name) {
79            let arg = format!("{arg_name}={arg_val}");
80            configure.arg(arg);
81        }
82    }
83
84    for flag_name in &flags {
85        let env_name = env_name_for(flag_name);
86        if env::var(env_name).is_ok() {
87            configure.arg(flag_name);
88        }
89    }
90
91    let status = configure
92        .spawn()
93        .expect(BUILD_ERROR_MSG)
94        .wait()
95        .expect(BUILD_ERROR_MSG);
96    if !status.success() {
97        panic!("configure terminated unsuccessfully");
98    }
99}
100
101fn make(build_dir: &str) {
102    let mut cmd = Command::new("make");
103    // Set MAKEFLAGS to the content of CARGO_MAKEFLAGS
104    // to give jobserver (parallel builds) support to the
105    // spawned sub-make.
106    if let Ok(val) = env::var("CARGO_MAKEFLAGS") {
107        cmd.env("MAKEFLAGS", val);
108    } else {
109        eprintln!("Can't set MAKEFLAGS as CARGO_MAKEFLAGS couldn't be read");
110    }
111
112    let status = cmd
113        .arg("-f")
114        .arg(format!("{build_dir}/Makefile"))
115        .arg("libs")
116        .spawn()
117        .expect(BUILD_ERROR_MSG)
118        .wait()
119        .expect(BUILD_ERROR_MSG);
120    if !status.success() {
121        panic!("make terminated unsuccessfully");
122    }
123}
124
125fn bundled_tarball_path() -> PathBuf {
126    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
127        .join("vendor")
128        .join(format!("Botan-{BOTAN_VERSION}.tar.xz"))
129}
130
131fn verify_sha256(path: &Path) {
132    use sha2::{Digest, Sha256};
133    let bytes = fs::read(path).expect("read tarball");
134    let actual = format!("{:x}", Sha256::digest(&bytes));
135    if actual != BOTAN_TARBALL_SHA256 {
136        panic!(
137            "Botan tarball at {} has unexpected sha256 (expected {}, got {})",
138            path.display(),
139            BOTAN_TARBALL_SHA256,
140            actual,
141        );
142    }
143}
144
145fn extract_tarball(tarball: &Path, dest: &Path) {
146    let file = fs::File::open(tarball).expect("open tarball");
147    let mut reader = io::BufReader::new(file);
148    let mut decompressed = Vec::new();
149    lzma_rs::xz_decompress(&mut reader, &mut decompressed).expect("xz decompress");
150    let mut archive = tar::Archive::new(io::Cursor::new(decompressed));
151    archive.unpack(dest).expect("untar");
152}
153
154// After unpacking, find the single top-level directory the tarball
155// produced. Bundled Botan releases use `Botan-X.Y.Z`, but a developer's
156// custom tarball (BOTAN_SRC_TARBALL) might use anything.
157fn find_extracted_root(extract_root: &Path) -> PathBuf {
158    let mut dirs = fs::read_dir(extract_root)
159        .expect("read extract root")
160        .filter_map(Result::ok)
161        .map(|e| e.path())
162        .filter(|p| p.is_dir());
163    let first = dirs
164        .next()
165        .expect("tarball produced no top-level directory");
166    if dirs.next().is_some() {
167        panic!("tarball must contain exactly one top-level directory");
168    }
169    first
170}
171
172/// Returns the directory containing Botan sources to build against.
173///
174/// Resolution order, highest priority first:
175/// - `BOTAN_SRC_DIR` — use this directory as the source tree directly
176///   (no extraction, no checksum). Useful for testing a local git
177///   checkout or fork.
178/// - `BOTAN_SRC_TARBALL` — extract this `.tar.xz` instead of the bundled
179///   one. No checksum: the caller is responsible for what they hand us.
180/// - otherwise: extract the bundled `vendor/Botan-X.Y.Z.tar.xz`,
181///   verifying it matches the pinned SHA-256.
182fn ensure_source(out_dir: &Path) -> PathBuf {
183    println!("cargo:rerun-if-env-changed=BOTAN_SRC_DIR");
184    println!("cargo:rerun-if-env-changed=BOTAN_SRC_TARBALL");
185
186    if let Some(custom_dir) = env::var_os("BOTAN_SRC_DIR") {
187        let path = PathBuf::from(custom_dir);
188        if !path.join("configure.py").is_file() {
189            panic!(
190                "BOTAN_SRC_DIR={} does not contain configure.py",
191                path.display()
192            );
193        }
194        return path;
195    }
196
197    let custom_tarball = env::var_os("BOTAN_SRC_TARBALL").map(PathBuf::from);
198    let tarball = custom_tarball.clone().unwrap_or_else(bundled_tarball_path);
199    let stamp_marker = match &custom_tarball {
200        Some(p) => format!("custom:{}", p.display()),
201        None => format!("bundled:{BOTAN_TARBALL_SHA256}"),
202    };
203
204    let extract_root = out_dir.join("botan-src");
205    let stamp = extract_root.join(".extracted");
206    let already_extracted = fs::read_to_string(&stamp)
207        .map(|s| s.trim() == stamp_marker)
208        .unwrap_or(false);
209    if !already_extracted {
210        if !tarball.exists() {
211            panic!("Botan source tarball missing at {}", tarball.display());
212        }
213        if custom_tarball.is_none() {
214            verify_sha256(&tarball);
215        }
216        let _ = fs::remove_dir_all(&extract_root);
217        fs::create_dir_all(&extract_root).expect("mkdir extract root");
218        extract_tarball(&tarball, &extract_root);
219        fs::write(&stamp, &stamp_marker).expect("write stamp");
220    }
221    find_extracted_root(&extract_root)
222}
223
224pub fn build() -> (String, std::path::PathBuf) {
225    let out_dir = env::var_os("OUT_DIR")
226        .map(PathBuf::from)
227        .expect("OUT_DIR is set when invoked from a build script");
228    let src_dir = ensure_source(&out_dir);
229    let build_dir = out_dir.join("botan-build");
230    let include_dir = build_dir.join(INCLUDE_DIR);
231    let build_dir = pathbuf_to_string!(build_dir);
232    let orig_dir = env::current_dir().expect(SRC_DIR_ERROR_MSG);
233    env::set_current_dir(&src_dir).expect(SRC_DIR_ERROR_MSG);
234    configure(&build_dir);
235    make(&build_dir);
236    env::set_current_dir(&orig_dir).expect("Unable to restore cwd");
237    (build_dir, include_dir)
238}