Skip to content

Commit

Permalink
Use compile time detection of what features are available
Browse files Browse the repository at this point in the history
Previously features were gated on the botan3 feature (which is removed here).
This didn't work well in practice as it made it difficult to add support for
features added in Botan3 after the 3.0 release.

Add declarations in botan-sys for functions added to ffi.h since 3.0.0, gated on
the detected library version. Also add interfaces for some of the new
functionality to the high level Rust crate.
  • Loading branch information
randombit committed Mar 2, 2025
1 parent 41a0d9c commit 46e9412
Show file tree
Hide file tree
Showing 34 changed files with 707 additions and 237 deletions.
7 changes: 2 additions & 5 deletions .ci/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ def compute_features(features, for_who):
if 'vendored' in features:
feat.append('vendored')

if 'git' in features or 'botan3' in features:
feat.append('botan3')

if for_who == 'lib' and 'no-std' not in features:
feat.append('std')

Expand Down Expand Up @@ -64,7 +61,7 @@ def main(args = None):
if "SCCACHE_MAXSIZE" not in os.environ:
os.environ["SCCACHE_MAXSIZE"] = "2G"

KNOWN_FEATURES = ['vendored', 'git', 'no-std', 'botan3']
KNOWN_FEATURES = ['vendored', 'git', 'no-std']

features = [] if len(args) == 1 else args[1].split(',')

Expand All @@ -89,7 +86,7 @@ def main(args = None):
os.environ["DYLD_LIBRARY_PATH"] = "/usr/local/lib"

homebrew_dir = "/opt/homebrew/lib"
if 'botan3' in features and os.access(homebrew_dir, os.R_OK):
if os.access(homebrew_dir, os.R_OK):
os.environ["RUSTFLAGS"] = "-D warnings -L/opt/homebrew/lib"
os.environ["RUSTDOCFLAGS"] = "-D warnings -L/opt/homebrew/lib"
os.environ["DYLD_LIBRARY_PATH"] = homebrew_dir
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
runs-on: ubuntu-24.04

steps:
- run: sudo apt-get -qq install libbotan-2-dev
- uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly
Expand Down Expand Up @@ -84,11 +85,9 @@ jobs:
matrix:
include:
- toolchain: stable
features: botan3
- toolchain: stable
features: vendored
- toolchain: nightly
features: botan3

steps:
- run: brew install ccache botan
Expand Down
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,15 @@ The following features are supported:
is still required.
* `vendored`: Build a copy of the C++ library directly, without
relying on a system installed version.
* `botan3`: Enable support for using APIs added in Botan 3.
This enables several new features, and more efficient operation.
This feature is implicitly enabled if you use `vendored`.
* `static`: Enable static linking for a non-vendored, externally
provided Botan dependency.
* `pkg-config`: Enable finding a non-vendored, externally provided
Botan with pkg-config. Can be used in combination with `static`.

The crate detects at build time what features are exported from the C interface,
and adjusts itself accordingly. These feature sets can be checked using
`#[cfg(botan_ffi_YYYYMMDD)]` where YYYYMMDD can be any of

* 20230403: Botan 3.0
* 20240408: Botan 3.4
* 20250506: Botan 3.8
2 changes: 1 addition & 1 deletion botan-src/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "botan-src"
version = "0.30701.1"
version = "0.30701.2"
authors = ["Rodolphe Breard <[email protected]>", "Jack Lloyd <[email protected]>"]
description = "Sources of Botan cryptography library"
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion botan-src/examples/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn main() {
let (lib_dir, include_dir) = botan_src::build();
println!("Library directory: {lib_dir}");
println!("Include directory: {include_dir}");
println!("Include directory: {}", include_dir.display());
}
8 changes: 5 additions & 3 deletions botan-src/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::process::Command;
const BUILD_ERROR_MSG: &str = "Unable to build botan.";
const SRC_DIR_ERROR_MSG: &str = "Unable to find the source directory.";
const SRC_DIR: &str = "botan";
const INCLUDE_DIR: &str = "build/include/botan";
const INCLUDE_DIR: &str = "build/include/public";

macro_rules! pathbuf_to_string {
($s: ident) => {
Expand Down Expand Up @@ -115,14 +115,16 @@ fn make(build_dir: &str) {
}
}

pub fn build() -> (String, String) {
pub fn build() -> (String, std::path::PathBuf) {
let src_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(SRC_DIR);
let build_dir = env::var_os("OUT_DIR").map_or(src_dir.to_owned(), PathBuf::from);
let build_dir = build_dir.join("botan");
let include_dir = build_dir.join(INCLUDE_DIR);
let build_dir = pathbuf_to_string!(build_dir);
let orig_dir = env::current_dir().expect(SRC_DIR_ERROR_MSG);
env::set_current_dir(&src_dir).expect(SRC_DIR_ERROR_MSG);
configure(&build_dir);
make(&build_dir);
(build_dir, pathbuf_to_string!(include_dir))
env::set_current_dir(&orig_dir).expect("Unable to restore cwd");
(build_dir, include_dir)
}
12 changes: 6 additions & 6 deletions botan-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "botan-sys"
version = "0.11.1"
version = "0.12.0"
authors = ["Jack Lloyd <[email protected]>"]
links = "botan-3"
build = "build.rs"
links = "botan"
build = "build/main.rs"
description = "FFI wrapper for Botan cryptography library"
license = "MIT"
homepage = "https://botan.randombit.net/"
Expand All @@ -16,11 +16,11 @@ rust-version = "1.64"

[features]
default = []
vendored = ["botan-src", "botan3"]
botan3 = []
vendored = ["botan-src"]
static = []
pkg-config = ["dep:pkg-config"]

[build-dependencies]
botan-src = { version = "0.30701.1", optional = true, path = "../botan-src" }
botan-src = { version = "0.30701.2", optional = true, path = "../botan-src" }
pkg-config = { version = "0.3.30", optional = true }
cc = "1"
19 changes: 18 additions & 1 deletion botan-sys/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
# botan-sys

This crate contains the FFI declarations for calling the C API included in the
[Botan](https://botan.randombit.net/) cryptography library.
[Botan](https://botan.randombit.net/) cryptography library as well as the rules
for linking to it.

A high level Rust interface built on these declarations is included in the
[botan](https://crates.io/crates/botan) crate.

## Features

* `vendored`: Build against the `botan-src` crate
* `static`: Statically link the library. This is always used if `vendored` is set
* `pkg-config`: Use `pkg-config` instead of probing to find the library

## Environment Variables

The following environment variables are used to guide features

* `BOTAN_INCLUDE_DIR` the base path to where the relevant library includes are
found. For example if the headers are in `/opt/foo/botan-3/botan`, this
variable should be set to `/opt/foo`. If not set, tries a few common
locations. This variable is ignored if the `pkg-config` or `vendored`
features are used.
80 changes: 0 additions & 80 deletions botan-sys/build.rs

This file was deleted.

Loading

0 comments on commit 46e9412

Please sign in to comment.