boxdd-sys 0.2.1

Low-level FFI bindings for Box2D built from upstream via submodule
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
use std::env;
use std::fs;
use std::path::{Path, PathBuf};

// From-source-only build for Box2D C API.
// - Prefers pregenerated bindings to avoid libclang on CI (e.g. macOS).
// - Always compiles vendored C sources with `cc` for native targets.
// - WASM builds are opt-in via EMSDK/WASI or `BOXDD_SYS_WASM_CC=1`.

fn parse_bool_env(key: &str) -> bool {
    match env::var(key) {
        Ok(v) => matches!(
            v.as_str(),
            "1" | "true" | "yes" | "on" | "TRUE" | "YES" | "ON"
        ),
        Err(_) => false,
    }
}

fn main() {
    // Re-run triggers
    println!("cargo:rustc-check-cfg=cfg(has_pregenerated)");
    println!("cargo:rustc-check-cfg=cfg(has_wasm_pregenerated)");
    println!("cargo:rerun-if-changed=build.rs");
    println!("cargo:rerun-if-changed=third-party/box2d/include/box2d/box2d.h");
    println!("cargo:rerun-if-changed=third-party/box2d");
    println!("cargo:rerun-if-env-changed=BOXDD_SYS_SKIP_CC");
    println!("cargo:rerun-if-env-changed=BOXDD_SYS_WASM_CC");
    println!("cargo:rerun-if-env-changed=BOX2D_LIB_DIR");
    println!("cargo:rerun-if-env-changed=BOXDD_SYS_LINK_KIND");
    println!("cargo:rerun-if-env-changed=BOXDD_SYS_FORCE_BINDGEN");
    println!("cargo:rerun-if-env-changed=BOXDD_SYS_STRICT_FEATURES");
    println!("cargo:rerun-if-env-changed=EMSDK");
    println!("cargo:rerun-if-env-changed=WASI_SDK_PATH");
    println!("cargo:rerun-if-env-changed=DOCS_RS");
    println!("cargo:rerun-if-env-changed=CARGO_CFG_DOCSRS");

    let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
    let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
    let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default();
    let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
    let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
    let profile = env::var("PROFILE").unwrap_or_else(|_| "release".into());
    let is_debug = profile == "debug";

    // Detect pregenerated bindings and docs.rs
    let pregenerated = manifest_dir.join("src").join("bindings_pregenerated.rs");
    let wasm_pregenerated = manifest_dir
        .join("src")
        .join("wasm_bindings_pregenerated.rs");
    let has_pregenerated = pregenerated.exists();
    let has_wasm_pregenerated = wasm_pregenerated.exists();
    if has_pregenerated {
        println!("cargo:rustc-cfg=has_pregenerated");
    }
    if target_arch == "wasm32" && has_wasm_pregenerated {
        println!("cargo:rustc-cfg=has_wasm_pregenerated");
    }
    let is_docsrs = env::var("DOCS_RS").is_ok() || env::var("CARGO_CFG_DOCSRS").is_ok();

    // If wasm and have wasm-specific pregenerated, copy to OUT_DIR
    let mut used_wasm_pregenerated = false;
    if target_arch == "wasm32"
        && has_wasm_pregenerated
        && let Ok(s) = std::fs::read_to_string(&wasm_pregenerated)
    {
        let _ = std::fs::write(out_dir.join("bindings.rs"), s);
        used_wasm_pregenerated = true;
        println!(
            "cargo:warning=Using wasm pregenerated bindings: {}",
            wasm_pregenerated.display()
        );
    }

    // Run bindgen only if needed or explicitly requested.
    // Note: bindgen is an optional build dependency behind the `bindgen` feature to avoid requiring libclang
    // for normal builds that use pregenerated bindings.
    let force_bindgen = parse_bool_env("BOXDD_SYS_FORCE_BINDGEN");
    if force_bindgen || (!has_pregenerated && !is_docsrs && !used_wasm_pregenerated) {
        #[cfg(feature = "bindgen")]
        generate_bindings(&manifest_dir, &out_dir);
        #[cfg(not(feature = "bindgen"))]
        {
            if force_bindgen {
                panic!(
                    "BOXDD_SYS_FORCE_BINDGEN=1 but the `boxdd-sys` crate was built without the `bindgen` feature"
                );
            }
            panic!(
                "pregenerated bindings are missing and the `boxdd-sys` crate was built without the `bindgen` feature"
            );
        }
    }

    // docs.rs: do not attempt to build/link native C sources.
    // Rustdoc only needs the Rust bindings to compile; the final link step happens in binaries/tests.
    if is_docsrs {
        println!("cargo:warning=DOCS_RS detected: skipping native Box2D C build");
        return;
    }

    // Build C sources unless explicitly skipped
    if parse_bool_env("BOXDD_SYS_SKIP_CC") {
        println!("cargo:warning=Skipping native C build due to BOXDD_SYS_SKIP_CC");
        return;
    }

    if target_arch == "wasm32" {
        if target_env == "emscripten" {
            build_with_cc_wasm(&manifest_dir, &target_env, is_debug);
        } else if target_os == "wasi" {
            if env::var("WASI_SDK_PATH").is_ok() || parse_bool_env("BOXDD_SYS_WASM_CC") {
                build_with_cc_wasi(&manifest_dir, is_debug);
            } else {
                println!(
                    "cargo:warning=WASI target detected but no WASI_SDK_PATH; skipping native C build"
                );
            }
        } else if parse_bool_env("BOXDD_SYS_WASM_CC") {
            build_with_cc_wasm(&manifest_dir, &target_env, is_debug);
        } else {
            println!(
                "cargo:warning=WASM (unknown) skeleton: skipping native C build (set BOXDD_SYS_WASM_CC=1 to enable)"
            );
        }
    } else {
        // Try system link first (env or pkg-config), then fallback to source build
        if try_link_system(&target_arch) {
            return;
        }
        build_box2d_from_source(&manifest_dir, &target_env, &target_os, is_debug);
    }
}

#[cfg(feature = "bindgen")]
fn generate_bindings(manifest_dir: &Path, out_dir: &Path) {
    let header = manifest_dir
        .join("third-party")
        .join("box2d")
        .join("include")
        .join("box2d")
        .join("box2d.h");
    let include_dir = header.parent().unwrap().to_path_buf();
    let bindings = bindgen::Builder::default()
        .header(header.to_string_lossy())
        .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
        .clang_args(["-x", "c", "-std=c17"]) // upstream uses C17
        .clang_arg(format!("-I{}", include_dir.display()))
        .allowlist_function("b2.*")
        .allowlist_type("b2.*")
        .allowlist_var("B2_.*")
        .layout_tests(false)
        .generate()
        .expect("Failed to generate box2d bindings");
    let out = out_dir.join("bindings.rs");
    bindings
        .write_to_file(&out)
        .expect("Couldn't write bindings!");
}

#[cfg(not(feature = "bindgen"))]
#[allow(dead_code)]
fn generate_bindings(_manifest_dir: &Path, _out_dir: &Path) {
    unreachable!("generate_bindings is only available with the `bindgen` feature enabled");
}

fn build_box2d_from_source(manifest_dir: &Path, target_env: &str, target_os: &str, is_debug: bool) {
    let third_party = manifest_dir.join("third-party");
    let box2d_root = third_party.join("box2d");
    let box2d_include = box2d_root.join("include");
    let box2d_src = box2d_root.join("src");
    if !box2d_include.exists() || !box2d_src.exists() {
        println!(
            "cargo:warning=Box2D submodule not found at {}; run: git submodule update --init --recursive",
            box2d_root.display()
        );
    }

    let mut build = cc::Build::new();
    build.include(&box2d_include);

    let mut files: Vec<PathBuf> = Vec::new();
    collect_c_files(&box2d_src, &mut files);
    for f in files {
        build.file(f);
    }

    if target_env == "msvc" {
        let use_static_crt = env::var("CARGO_CFG_TARGET_FEATURE")
            .unwrap_or_default()
            .split(',')
            .any(|f| f == "crt-static");
        build.static_crt(use_static_crt);
        if use_static_crt {
            build.flag("/MT");
        } else {
            build.flag("/MD");
        }
        if is_debug {
            build.debug(true);
            build.opt_level(0);
        } else {
            build.debug(false);
            build.opt_level(2);
        }
        build.flag_if_supported("/std:c17");
        build.flag_if_supported("/std:c11");
        if cfg!(feature = "validate") {
            build.define("BOX2D_VALIDATE", None);
        }
        let is_x86_64 = env::var("CARGO_CFG_TARGET_ARCH").ok().as_deref() == Some("x86_64");
        if cfg!(feature = "disable-simd") {
            build.define("BOX2D_DISABLE_SIMD", None);
        } else if cfg!(feature = "simd-avx2") && is_x86_64 {
            build.define("BOX2D_AVX2", None);
            build.flag_if_supported("/arch:AVX2");
        }
    } else {
        build.flag_if_supported("-std=c17");
        if target_os == "linux"
            || target_os == "macos"
            || env::var("CARGO_CFG_TARGET_ENV").ok().as_deref() == Some("gnu")
        {
            build.flag_if_supported("-ffp-contract=off");
        }
        if cfg!(feature = "validate") {
            build.define("BOX2D_VALIDATE", None);
        }
        let is_x86_64 = env::var("CARGO_CFG_TARGET_ARCH").ok().as_deref() == Some("x86_64");
        if cfg!(feature = "disable-simd") {
            build.define("BOX2D_DISABLE_SIMD", None);
        } else if cfg!(feature = "simd-avx2") && is_x86_64 {
            build.define("BOX2D_AVX2", None);
            build.flag_if_supported("-mavx2");
        }
        if target_os == "linux" {
            build.define("_POSIX_C_SOURCE", Some("199309L"));
            build.flag_if_supported("-pthread");
            println!("cargo:rustc-link-lib=pthread");
        }
    }

    build.compile("box2d");
}

fn link_kind_from_env() -> Option<&'static str> {
    match env::var("BOXDD_SYS_LINK_KIND").ok().as_deref() {
        Some("static") | Some("STATIC") => Some("static"),
        Some("dylib") | Some("DYLIB") | Some("shared") | Some("SHARED") => Some("dylib"),
        _ => None,
    }
}

fn warn_or_error_system_ignores_features() {
    let simd = cfg!(feature = "simd-avx2");
    let nosimd = cfg!(feature = "disable-simd");
    let validate = cfg!(feature = "validate");
    if simd || nosimd || validate {
        if parse_bool_env("BOXDD_SYS_STRICT_FEATURES") {
            panic!(
                "System library mode ignores crate features (simd-avx2/disable-simd/validate). Use source build instead or unset BOXDD_SYS_STRICT_FEATURES."
            );
        } else {
            println!(
                "cargo:warning=System library mode ignores crate features: {}{}{}",
                if simd { "simd-avx2 " } else { "" },
                if nosimd { "disable-simd " } else { "" },
                if validate { "validate" } else { "" },
            );
        }
    }
}

fn try_link_system(_target_arch: &str) -> bool {
    // Env-provided lib dir
    if let Ok(dir) = env::var("BOX2D_LIB_DIR") {
        println!("cargo:rustc-link-search=native={}", dir);
        if let Some(kind) = link_kind_from_env() {
            println!("cargo:rustc-link-lib={}=box2d", kind);
        } else {
            println!("cargo:rustc-link-lib=box2d");
        }
        warn_or_error_system_ignores_features();
        return true;
    }
    // pkg-config path when enabled
    #[cfg(feature = "pkg-config")]
    {
        if pkg_config::Config::new()
            .cargo_metadata(true)
            .probe("box2d")
            .is_ok()
        {
            warn_or_error_system_ignores_features();
            return true;
        }
    }
    false
}

fn collect_c_files(dir: &Path, out: &mut Vec<PathBuf>) {
    if let Ok(rd) = fs::read_dir(dir) {
        for entry in rd.flatten() {
            let path = entry.path();
            if path.is_dir() {
                collect_c_files(&path, out);
            } else if let Some(ext) = path.extension()
                && ext == "c"
            {
                out.push(path);
            }
        }
    }
}

fn build_with_cc_wasm(manifest_dir: &Path, target_env: &str, is_debug: bool) {
    let third_party = manifest_dir.join("third-party");
    let box2d_root = third_party.join("box2d");
    let box2d_include = box2d_root.join("include");
    let box2d_src = box2d_root.join("src");

    let mut build = cc::Build::new();
    build.include(&box2d_include);

    let mut files: Vec<PathBuf> = Vec::new();
    collect_c_files(&box2d_src, &mut files);
    for f in files {
        build.file(f);
    }

    if target_env == "emscripten" {
        if let Ok(emsdk) = env::var("EMSDK") {
            let mut clang = PathBuf::from(&emsdk);
            clang.push("upstream");
            clang.push("bin");
            clang.push(if cfg!(windows) { "clang.exe" } else { "clang" });
            if clang.exists() {
                build.compiler(clang);
            }
            let mut sysroot = PathBuf::from(&emsdk);
            sysroot.push("upstream");
            sysroot.push("emscripten");
            sysroot.push("cache");
            sysroot.push("sysroot");
            if sysroot.exists() {
                build.flag(format!("--sysroot={}", sysroot.display()));
            }
        }
        build.flag("-target");
        build.flag("wasm32-unknown-emscripten");
    } else {
        build.flag("-target");
        build.flag("wasm32-unknown-unknown");
    }

    build.flag_if_supported("-ffp-contract=off");
    if is_debug {
        build.debug(true);
        build.opt_level(0);
    } else {
        build.debug(false);
        build.opt_level(2);
    }
    build.flag_if_supported("-std=c17");
    build.static_flag(true);
    build.compile("box2d");
}

fn build_with_cc_wasi(manifest_dir: &Path, is_debug: bool) {
    let third_party = manifest_dir.join("third-party");
    let box2d_root = third_party.join("box2d");
    let box2d_include = box2d_root.join("include");
    let box2d_src = box2d_root.join("src");

    let mut build = cc::Build::new();
    build.include(&box2d_include);

    let mut files: Vec<PathBuf> = Vec::new();
    collect_c_files(&box2d_src, &mut files);
    for f in files {
        build.file(f);
    }

    if let Ok(wasi_sdk) = env::var("WASI_SDK_PATH") {
        let mut clang = PathBuf::from(&wasi_sdk);
        clang.push("bin");
        clang.push(if cfg!(windows) { "clang.exe" } else { "clang" });
        if clang.exists() {
            build.compiler(clang);
        }
        let mut sysroot = PathBuf::from(&wasi_sdk);
        sysroot.push("share");
        sysroot.push("wasi-sysroot");
        if sysroot.exists() {
            build.flag(format!("--sysroot={}", sysroot.display()));
        }
    }

    build.flag("-target");
    build.flag("wasm32-wasip1");
    build.flag_if_supported("-ffp-contract=off");
    if is_debug {
        build.debug(true);
        build.opt_level(0);
    } else {
        build.debug(false);
        build.opt_level(2);
    }
    build.flag_if_supported("-std=c17");
    build.static_flag(true);
    build.compile("box2d");
}