i2pd-sys 0.0.5

Raw FFI bindings to a minimal C shim over libi2pd (PurpleI2P/i2pd).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! Builds the committed `vendor/` snapshots -- Boost and i2pd via CMake, zlib and the C++ shim
//! via `cc` -- then runs bindgen against `shim/shim.h`.
//!
//! Nothing shells out to `configure`/`b2`/`bootstrap.sh`, so the build survives `cargo package`.
//! OpenSSL is not vendored; i2pd links AWS-LC via the `aws-lc-*-sys` crates, with
//! `openssl_stub_lib` covering the one place CMake insists on a library path.

use std::env;
use std::path::{Path, PathBuf};
use std::process::Command;

fn main() {
    let fips = env::var("CARGO_FEATURE_FIPS").is_ok();
    let aws_lc = env::var("CARGO_FEATURE_AWS_LC").is_ok();
    let transit = env::var("CARGO_FEATURE_TRANSIT").is_ok();
    if !fips && !aws_lc {
        panic!(
            "no crypto backend feature selected -- enable exactly one of the `aws-lc` (default) \
             or `fips` features."
        );
    }

    let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR"));
    let vendor_dir = manifest_dir.join("vendor");
    let i2pd_dir = vendor_dir.join("i2pd-src");
    let boost_dir = vendor_dir.join("boost-src");
    let zlib_dir = vendor_dir.join("zlib-src");

    let target = env::var("TARGET").expect("TARGET");
    let host = env::var("HOST").expect("HOST");
    let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR"));

    let compilers = if target == host {
        None // ambient cc/c++, including native-musl-on-musl (e.g. Alpine)
    } else {
        Some(resolve_cross_compilers(&target))
    };

    let zlib_lib = build_zlib(&zlib_dir, &out_dir, compilers.as_ref());

    let boost_install = build_boost(&boost_dir, &out_dir, compilers.as_ref());

    let crypto = CryptoBackend::discover(fips);
    let openssl_include = PathBuf::from(&crypto.include);
    let openssl_crypto_stub = openssl_stub_lib(&out_dir, "crypto", compilers.as_ref());
    let openssl_ssl_stub = openssl_stub_lib(&out_dir, "ssl", compilers.as_ref());

    let i2pd_install = build_i2pd(
        &i2pd_dir,
        &out_dir,
        &boost_install,
        &openssl_include,
        &openssl_crypto_stub,
        &openssl_ssl_stub,
        &zlib_dir,
        &zlib_lib,
        transit,
        compilers.as_ref(),
    );

    println!("cargo:rustc-link-search=native={}", i2pd_install.join("lib").display());
    println!("cargo:rustc-link-search=native={}", boost_install.join("lib").display());
    println!("cargo:rustc-link-search=native={}", zlib_lib.parent().unwrap().display());

    let aws_lc_crypto = &crypto.libcrypto;
    let aws_lc_ssl = crypto.libssl.as_deref();
    let mut shim = cc::Build::new();
    shim.cpp(true).std("c++17").warnings(false);
    if let Some(c) = &compilers {
        shim.compiler(&c.cxx);
    }
    for flag in HARDENING_CFLAGS {
        shim.flag_if_supported(flag);
    }
    if !transit {
        shim.define("I2PD_SYS_NO_TRANSIT", None);
    }
    shim.include(i2pd_dir.join("libi2pd"))
        .include(boost_install.join("include"))
        .include(&openssl_include)
        .include(zlib_dir)
        .file("shim/shim.cpp")
        .compile("tachyon_i2pd_shim");
    println!("cargo:rustc-link-search=native={}", out_dir.display());

    for lib in ["tachyon_i2pd_shim", "i2pd"] {
        println!("cargo:rustc-link-lib=static:+verbatim=lib{lib}.a");
    }
    for lib in ["filesystem", "program_options", "atomic", "container"] {
        println!("cargo:rustc-link-lib=static:+verbatim=libboost_{lib}.a");
    }
    println!("cargo:rustc-link-lib=static:+verbatim=libz.a");
    if let Some(aws_lc_ssl) = aws_lc_ssl {
        println!("cargo:rustc-link-lib=static:+verbatim=lib{aws_lc_ssl}.a");
    }
    println!("cargo:rustc-link-lib=static:+verbatim=lib{aws_lc_crypto}.a");
    // No libstdc++ entry needed: cc::Build already emits it for the C++ shim.
    bindgen::Builder::default()
        .header("shim/shim.h")
        .allowlist_function("i2pd_.*")
        .allowlist_type("I2pd.*")
        // Without this libclang parses for the host, so a cross build derives `size_t` from the
        // wrong data model: silently wrong FFI signatures rather than a build error.
        .clang_arg(format!("--target={target}"))
        .generate()
        .expect("bindgen failed to generate shim bindings")
        .write_to_file(out_dir.join("bindings.rs"))
        .expect("failed to write bindgen bindings.rs");

    for path in [
        "build.rs",
        "shim/shim.h",
        "shim/shim.cpp",
        "vendor/i2pd-src/SNAPSHOT_COMMIT.txt",
        "vendor/boost-src/SNAPSHOT_TAG.txt",
        "vendor/zlib-src/SNAPSHOT_VERSION.txt",
    ] {
        println!("cargo:rerun-if-changed={path}");
    }
    for var in ["MUSL_CROSS_TOOLCHAIN", "CC", "CXX", "AR", "CMAKE", "CMAKE_GENERATOR", "RUSTC_WRAPPER"] {
        println!("cargo:rerun-if-env-changed={var}");
    }
}

/// The `cargo:include=` / `cargo:libcrypto=` / `cargo:libssl=` metadata published by whichever of
/// `aws-lc-sys` / `aws-lc-fips-sys` this build selected.
struct CryptoBackend {
    include: String,
    libcrypto: String,
    libssl: Option<String>,
}

impl CryptoBackend {
    /// Both crates bake their version into their `links` name (`DEP_AWS_LC_0_43_0_INCLUDE`
    /// today), so this matches on prefix to survive `cargo update`. `DEP_AWS_LC_FIPS_*` also
    /// starts with `DEP_AWS_LC_`, so the non-FIPS lookup must exclude it or a build with both
    /// dependencies compiled in picks the wrong backend.
    fn discover(fips: bool) -> Self {
        const FIPS_PREFIX: &str = "DEP_AWS_LC_FIPS_";
        const PREFIX: &str = "DEP_AWS_LC_";
        let (krate, matches): (_, fn(&str) -> bool) = if fips {
            ("aws-lc-fips-sys", |k| k.starts_with(FIPS_PREFIX))
        } else {
            ("aws-lc-sys", |k| k.starts_with(PREFIX) && !k.starts_with(FIPS_PREFIX))
        };

        let get = |suffix: &str| {
            env::vars().find(|(k, _)| matches(k) && k.ends_with(suffix)).map(|(k, v)| {
                println!("cargo:rerun-if-env-changed={k}");
                v
            })
        };

        let include = get("_INCLUDE").unwrap_or_else(|| {
            panic!(
                "{krate} published no DEP_*_INCLUDE metadata -- is it actually in the dependency \
                 graph for the selected feature set?"
            )
        });
        let libcrypto =
            get("_LIBCRYPTO").unwrap_or_else(|| panic!("{krate} published no DEP_*_LIBCRYPTO metadata"));
        Self { include, libcrypto, libssl: get("_LIBSSL") }
    }
}

struct CrossCompilers {
    cc: PathBuf,
    cxx: PathBuf,
    ar: Option<PathBuf>,
}

/// Locates a musl C++ cross toolchain: `CC_<target>`/`CXX_<target>`, then `MUSL_CROSS_TOOLCHAIN`,
/// then `<target>-g++` on `$PATH`. Never builds one -- a 20-40 minute surprise on a first build.
fn resolve_cross_compilers(target: &str) -> CrossCompilers {
    let env_key = |prefix: &str| format!("{prefix}_{}", target.replace('-', "_"));

    if let (Ok(cc), Ok(cxx)) = (env::var(env_key("CC")), env::var(env_key("CXX"))) {
        return CrossCompilers {
            cc: cc.into(),
            cxx: cxx.into(),
            ar: env::var(env_key("AR")).ok().map(PathBuf::from),
        };
    }

    if let Ok(toolchain_dir) = env::var("MUSL_CROSS_TOOLCHAIN") {
        let bin = PathBuf::from(&toolchain_dir).join("bin");
        let cc = bin.join(format!("{target}-gcc"));
        let cxx = bin.join(format!("{target}-g++"));
        if cxx.exists() {
            let ar = bin.join(format!("{target}-ar"));
            return CrossCompilers { cc, cxx, ar: ar.exists().then_some(ar) };
        }
    }

    let path_cc = format!("{target}-gcc");
    let path_cxx = format!("{target}-g++");
    if which(&path_cxx) {
        return CrossCompilers {
            cc: path_cc.into(),
            cxx: path_cxx.into(),
            ar: which(&format!("{target}-ar")).then(|| format!("{target}-ar").into()),
        };
    }

    panic!(
        "cross-compiling to `{target}` needs a real musl C++ toolchain (binutils+gcc+musl+\
         libstdc++) -- no C++ compiler shipped by default on a glibc host can target musl. \
         Point `MUSL_CROSS_TOOLCHAIN` at a `musl-cross-make` install (containing \
         bin/{target}-g++), or set `CC_{t}`/`CXX_{t}` directly, or put `{target}-g++` on \
         $PATH. See i2pd-sys/README.md for how to build one with musl-cross-make. (Building \
         natively *inside* a musl system, e.g. an Alpine container, needs none of this --\
         only true cross-compilation from a non-musl host does.)",
        t = target.replace('-', "_"),
    );
}

fn which(cmd: &str) -> bool {
    Command::new(cmd)
        .arg("--version")
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()
        .is_ok_and(|s| s.success())
}

/// Applied to every C/C++ translation unit: zlib decompresses attacker-controlled data and i2pd
/// parses the I2P wire protocol from arbitrary peers. Not left to i2pd's own `WITH_HARDENING`,
/// which its CMakeLists.txt gates on `CMAKE_CXX_COMPILER_ID STREQUAL "GNU"` and so silently does
/// nothing under Clang. `-U_FORTIFY_SOURCE` leads because Debian and Fedora predefine it, and
/// redefining it to another value warns (or errors under `-Werror`).
const HARDENING_CFLAGS: [&str; 3] = ["-U_FORTIFY_SOURCE", "-D_FORTIFY_SOURCE=2", "-fstack-protector-strong"];

fn build_zlib(zlib_dir: &Path, out_dir: &Path, compilers: Option<&CrossCompilers>) -> PathBuf {
    let sources = [
        "adler32.c",
        "compress.c",
        "crc32.c",
        "deflate.c",
        "gzclose.c",
        "gzlib.c",
        "gzread.c",
        "gzwrite.c",
        "infback.c",
        "inflate.c",
        "inftrees.c",
        "inffast.c",
        "trees.c",
        "uncompr.c",
        "zutil.c",
    ];
    let mut build = cc::Build::new();
    // `./configure` normally probes these; true on every POSIX target this crate supports.
    build.warnings(false).include(zlib_dir).define("HAVE_UNISTD_H", None).define("HAVE_STDARG_H", None);
    if let Some(c) = compilers {
        build.compiler(&c.cc);
    }
    for flag in HARDENING_CFLAGS {
        build.flag_if_supported(flag);
    }
    for src in sources {
        build.file(zlib_dir.join(src));
    }
    build.compile("z");
    out_dir.join("libz.a")
}

/// The explicit `out_dir` matters: cmake-rs derives its build tree from `OUT_DIR` alone, so the
/// default puts Boost and i2pd in the same `$OUT_DIR/build`, each reconfiguring over the other's
/// CMakeCache and silently rebuilding both on every `cargo build`.
fn build_boost(boost_dir: &Path, out_dir: &Path, compilers: Option<&CrossCompilers>) -> PathBuf {
    let mut cfg = cmake::Config::new(boost_dir);
    cfg.out_dir(out_dir.join("boost"));
    cfg.define(
        // `dynamic_bitset` went with libi2pd_client/Torrents.h, its only consumer; `date_time`
        // with Boost.Asio's deadline_timer sub-target, since i2pd uses steady_timer exclusively.
        "BOOST_INCLUDE_LIBRARIES",
        "filesystem;program_options;atomic;system;asio;property_tree",
    )
    .define("CMAKE_BUILD_TYPE", "Release")
    .define("BUILD_TESTING", "OFF")
    .build_target("install");
    // Measured as noise against the Boost 1.86 subset and rejected; the 1.91 bump's much larger
    // dependency closure made Boost the largest phase, where the pair takes it 30.2s -> 14.3s.
    define_unless_env(&mut cfg, "CMAKE_C_FLAGS_RELEASE", "-O2 -DNDEBUG");
    define_unless_env(&mut cfg, "CMAKE_CXX_FLAGS_RELEASE", "-O2 -DNDEBUG");
    define_unless_env(&mut cfg, "CMAKE_UNITY_BUILD", "ON");
    define_unless_env(&mut cfg, "CMAKE_UNITY_BUILD_BATCH_SIZE", "8");
    apply_cross(&mut cfg, compilers);
    native_build_speedups(&mut cfg, &out_dir.join("boost"));
    cfg.build()
}

/// An empty archive to satisfy `find_package(OpenSSL REQUIRED)`, which hard-checks that
/// `OPENSSL_{CRYPTO,SSL}_LIBRARY` exist on disk. Nothing links them -- i2pd builds only static
/// libraries here, and archiving resolves no symbols. AWS-LC is resolved at the final binary.
fn openssl_stub_lib(out_dir: &Path, name: &str, compilers: Option<&CrossCompilers>) -> PathBuf {
    let stub_dir = out_dir.join("openssl-stub");
    std::fs::create_dir_all(&stub_dir).expect("create openssl-stub dir");
    let stub_c = stub_dir.join(format!("{name}_stub.c"));
    std::fs::write(&stub_c, "// empty -- see openssl_stub_lib in build.rs\n").expect("write stub source");

    let mut build = cc::Build::new();
    build.warnings(false);
    if let Some(c) = compilers {
        build.compiler(&c.cc);
    }
    build.file(&stub_c).compile(&format!("tachyon_openssl_stub_{name}"));
    out_dir.join(format!("libtachyon_openssl_stub_{name}.a"))
}

#[allow(clippy::too_many_arguments)]
fn build_i2pd(
    i2pd_dir: &Path,
    out_dir: &Path,
    boost_install: &Path,
    openssl_include: &Path,
    openssl_crypto_stub: &Path,
    openssl_ssl_stub: &Path,
    zlib_dir: &Path,
    zlib_lib: &Path,
    transit: bool,
    compilers: Option<&CrossCompilers>,
) -> PathBuf {
    let mut cfg = cmake::Config::new(i2pd_dir.join("build"));
    cfg.out_dir(out_dir.join("i2pd"));
    cfg.define("WITH_LIBRARY", "ON")
        .define("WITH_BINARY", "OFF")
        .define("WITH_UPNP", "OFF")
        // Real under GCC, a no-op under Clang; HARDENING_CFLAGS is what covers both.
        .define("WITH_HARDENING", "ON")
        .define("BUILD_TESTING", "OFF")
        .define("CMAKE_BUILD_TYPE", "Release")
        .define("Boost_NO_SYSTEM_PATHS", "ON")
        .define("Boost_USE_STATIC_LIBS", "ON")
        .define("Boost_USE_STATIC_RUNTIME", "ON")
        .define("BOOST_ROOT", boost_install)
        .define("OPENSSL_INCLUDE_DIR", openssl_include)
        .define("OPENSSL_CRYPTO_LIBRARY", openssl_crypto_stub)
        .define("OPENSSL_SSL_LIBRARY", openssl_ssl_stub)
        .define("OPENSSL_VERSION", "1.1.1")
        .define("ZLIB_INCLUDE_DIR", zlib_dir)
        .define("ZLIB_LIBRARY", zlib_lib)
        .build_target("install");
    for flag in HARDENING_CFLAGS {
        cfg.cflag(flag).cxxflag(flag);
    }
    if !transit {
        cfg.cflag("-DI2PD_SYS_NO_TRANSIT").cxxflag("-DI2PD_SYS_NO_TRANSIT");
    }

    // it would re-enable every assert in Boost and i2pd.
    define_unless_env(&mut cfg, "CMAKE_C_FLAGS_RELEASE", "-O2 -DNDEBUG");
    define_unless_env(&mut cfg, "CMAKE_CXX_FLAGS_RELEASE", "-O2 -DNDEBUG");

    define_unless_env(&mut cfg, "CMAKE_UNITY_BUILD", "ON");
    define_unless_env(&mut cfg, "CMAKE_UNITY_BUILD_BATCH_SIZE", "8");
    define_unless_env(&mut cfg, "I2PD_SYS_PCH", "ON");

    apply_cross(&mut cfg, compilers);
    native_build_speedups(&mut cfg, &out_dir.join("i2pd"));
    cfg.build()
}

/// Sets a CMake variable unless the environment names it, keeping every build-speed choice a
/// default rather than a mandate: `CMAKE_UNITY_BUILD=OFF cargo build` bisects without an edit.
fn define_unless_env(cfg: &mut cmake::Config, key: &str, value: &str) {
    println!("cargo:rerun-if-env-changed={key}");
    match env::var(key) {
        Ok(from_env) => cfg.define(key, from_env),
        Err(_) => cfg.define(key, value),
    };
}

/// Ninja and a compiler cache for both CMake builds. Neither Boost nor i2pd is code a consumer
/// ever edits, so both cache almost perfectly. Ninja schedules a wide flat graph like i2pd's
/// better than recursive make, but cmake-rs forwards Cargo's jobserver only to make, so Ninja is
/// capped at `NUM_JOBS` explicitly -- otherwise it uses CPUs+2 and ignores `cargo build -j`.
///
/// Opt-out: an explicit `CMAKE_GENERATOR` or `CMAKE_CXX_COMPILER_LAUNCHER` wins.
fn native_build_speedups(cfg: &mut cmake::Config, cmake_out_dir: &Path) {
    let generator = match cmake_generator_override() {
        Some(explicit) => explicit,
        None if which("ninja") => {
            cfg.generator("Ninja");
            if let Ok(jobs) = env::var("NUM_JOBS") {
                cfg.build_arg(format!("-j{jobs}"));
            }
            "Ninja".to_string()
        }
        None => "Unix Makefiles".to_string(),
    };
    discard_tree_on_generator_change(cmake_out_dir, &generator);

    if env::var_os("CMAKE_CXX_COMPILER_LAUNCHER").is_some() {
        return;
    }
    if let Some(cache) = ["sccache", "ccache"].into_iter().find(|c| which(c)) {
        cfg.define("CMAKE_C_COMPILER_LAUNCHER", cache).define("CMAKE_CXX_COMPILER_LAUNCHER", cache);
        if env::var_os("CCACHE_SLOPPINESS").is_none() {
            cfg.env("CCACHE_SLOPPINESS", "pch_defines,time_macros");
        }
        println!("cargo:rerun-if-env-changed=CCACHE_SLOPPINESS");
    }
}

/// Whichever `CMAKE_GENERATOR` cmake-rs picks up, probed in its own order
/// (`Config::getenv_target_os`). Reading the plain variable alone misses the target-suffixed
/// forms, which `discard_tree_on_generator_change` would then read as a change, deleting a
/// perfectly good build tree on every build.
fn cmake_generator_override() -> Option<String> {
    let target = env::var("TARGET").unwrap_or_default();
    let host = env::var("HOST").unwrap_or_default();
    let kind = if host == target { "HOST" } else { "TARGET" };
    let keys = [
        format!("CMAKE_GENERATOR_{target}"),
        format!("CMAKE_GENERATOR_{}", target.replace('-', "_")),
        format!("{kind}_CMAKE_GENERATOR"),
        "CMAKE_GENERATOR".to_string(),
    ];
    for key in &keys {
        println!("cargo:rerun-if-env-changed={key}");
    }
    keys.into_iter().find_map(|key| env::var(key).ok())
}

/// CMake refuses to reconfigure a tree with a different generator than created it, so a `target/`
/// from before `ninja` was installed would hard-error. One rebuild beats a broken build.
fn discard_tree_on_generator_change(cmake_out_dir: &Path, generator: &str) {
    let cache = cmake_out_dir.join("build/CMakeCache.txt");
    let Ok(contents) = std::fs::read_to_string(&cache) else { return };
    let configured =
        contents.lines().find_map(|l| l.strip_prefix("CMAKE_GENERATOR:INTERNAL=")).unwrap_or_default();
    if configured != generator {
        let _ = std::fs::remove_dir_all(cmake_out_dir.join("build"));
    }
}

fn apply_cross(cfg: &mut cmake::Config, compilers: Option<&CrossCompilers>) {
    let Some(c) = compilers else { return };
    cfg.define("CMAKE_C_COMPILER", &c.cc)
        .define("CMAKE_CXX_COMPILER", &c.cxx)
        .define("CMAKE_EXE_LINKER_FLAGS", "-static");
    if let Some(ar) = &c.ar {
        cfg.define("CMAKE_AR", ar);
    }
}