boxddd-sys 0.4.0

Low-level FFI bindings for Box3D built from vendored upstream sources
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
447
448
449
450
451
452
453
454
455
use std::env;
use std::fs;
use std::path::{Path, PathBuf};

fn write_if_changed(path: &Path, content: &[u8]) {
    if matches!(fs::read(path), Ok(existing) if existing == content) {
        return;
    }
    fs::write(path, content).unwrap_or_else(|error| {
        panic!("failed to write {}: {error}", path.display());
    });
}

#[allow(dead_code)]
mod upstream_contract {
    include!("src/upstream_contract.rs");
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum WasmMode {
    CompileOnly,
    Source,
    Provider,
}

#[derive(Debug)]
struct BuildConfig {
    manifest_dir: PathBuf,
    #[cfg_attr(not(feature = "bindgen"), allow(dead_code))]
    out_dir: PathBuf,
    target_env: String,
    target_os: String,
    profile: String,
    is_docsrs: bool,
    skip_cc: bool,
    force_bindgen: bool,
    wasm_mode: Option<WasmMode>,
}

impl BuildConfig {
    fn from_env() -> Self {
        let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
        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 is_docsrs = env::var("DOCS_RS").is_ok() || env::var("CARGO_CFG_DOCSRS").is_ok();
        let skip_cc = parse_bool_env("BOXDDD_SYS_SKIP_CC");
        let force_bindgen = parse_bool_env("BOXDDD_SYS_FORCE_BINDGEN");
        let wasm_mode = (target_arch == "wasm32").then(|| {
            env::var("BOXDDD_SYS_WASM_MODE")
                .ok()
                .map(|value| parse_wasm_mode(&value))
                .unwrap_or_else(|| default_wasm_mode(&target_os))
        });

        Self {
            manifest_dir: PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()),
            out_dir: PathBuf::from(env::var("OUT_DIR").unwrap()),
            target_env,
            target_os,
            profile: env::var("PROFILE").unwrap_or_else(|_| "release".into()),
            is_docsrs,
            skip_cc,
            force_bindgen,
            wasm_mode,
        }
    }

    fn is_debug(&self) -> bool {
        self.profile == "debug"
    }

    fn pregenerated_bindings(&self) -> PathBuf {
        if cfg!(feature = "double-precision") {
            self.manifest_dir
                .join("src")
                .join("bindings_pregenerated_double.rs")
        } else {
            self.manifest_dir
                .join("src")
                .join("bindings_pregenerated.rs")
        }
    }
}

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 parse_wasm_mode(value: &str) -> WasmMode {
    match value {
        "compile-only" => WasmMode::CompileOnly,
        "source" => WasmMode::Source,
        "provider" => WasmMode::Provider,
        other => panic!(
            "unsupported BOXDDD_SYS_WASM_MODE={other:?}; expected compile-only, source, or provider"
        ),
    }
}

fn default_wasm_mode(target_os: &str) -> WasmMode {
    if target_os == "wasi" {
        WasmMode::Source
    } else {
        WasmMode::CompileOnly
    }
}

fn main() {
    println!("cargo:rustc-check-cfg=cfg(has_pregenerated)");
    println!("cargo:rustc-check-cfg=cfg(force_bindgen)");
    println!("cargo:rustc-check-cfg=cfg(boxddd_sys_wasm_provider)");
    println!("cargo:rerun-if-changed=build.rs");
    println!("cargo:rerun-if-changed=box3d-upstream.toml");
    println!("cargo:rerun-if-changed=patches");
    println!("cargo:rerun-if-changed=src/upstream_contract.rs");
    println!("cargo:rerun-if-changed=third-party/box3d/include/box3d/box3d.h");
    println!("cargo:rerun-if-changed=third-party/box3d");
    println!("cargo:rerun-if-env-changed=BOXDDD_SYS_SKIP_CC");
    println!("cargo:rerun-if-env-changed=BOXDDD_SYS_FORCE_BINDGEN");
    println!("cargo:rerun-if-env-changed=BOXDDD_SYS_WASM_MODE");
    println!("cargo:rerun-if-env-changed=BOXDDD_SYS_LINK_LIB");
    println!("cargo:rerun-if-env-changed=BOXDDD_SYS_LINK_SEARCH");
    println!("cargo:rerun-if-env-changed=WASI_SYSROOT");
    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 config = BuildConfig::from_env();
    let pregenerated = config.pregenerated_bindings();
    let has_pregenerated = pregenerated.exists();
    validate_build_config(&config);
    if config.force_bindgen {
        println!("cargo:rustc-cfg=force_bindgen");
    } else if has_pregenerated {
        println!("cargo:rustc-cfg=has_pregenerated");
    }
    if config.wasm_mode == Some(WasmMode::Provider) {
        if cfg!(feature = "double-precision")
            && !upstream_contract::PROVIDER_SUPPORTS_DOUBLE_PRECISION
        {
            panic!(
                "BOXDDD_SYS_WASM_MODE=provider does not support double-precision in provider ABI revision {}",
                upstream_contract::PROVIDER_BRIDGE_REVISION
            );
        }
        println!("cargo:rustc-cfg=boxddd_sys_wasm_provider");
        if config.force_bindgen {
            panic!(
                "BOXDDD_SYS_WASM_MODE=provider cannot be combined with BOXDDD_SYS_FORCE_BINDGEN=1 yet"
            );
        }
        if !has_pregenerated {
            panic!("BOXDDD_SYS_WASM_MODE=provider requires checked-in pregenerated bindings");
        }
        generate_wasm_provider_bindings(&pregenerated, &config.out_dir);
    }

    if config.force_bindgen || (!has_pregenerated && !config.is_docsrs) {
        #[cfg(feature = "bindgen")]
        generate_bindings(&config.manifest_dir, &config.out_dir);
        #[cfg(not(feature = "bindgen"))]
        {
            if config.force_bindgen {
                panic!("BOXDDD_SYS_FORCE_BINDGEN=1 requires the `bindgen` feature");
            }
            panic!(
                "pregenerated Box3D bindings are missing for the selected ABI mode; enable `bindgen` or refresh checked-in bindings"
            );
        }
    }

    if config.is_docsrs || config.skip_cc {
        if config.is_docsrs {
            println!("cargo:warning=DOCS_RS detected: skipping native Box3D C build");
        } else {
            println!("cargo:warning=Skipping native Box3D C build due to BOXDDD_SYS_SKIP_CC");
        }
        return;
    }

    if handle_wasm_build(&config) {
        return;
    }

    if !cfg!(feature = "build-from-source") {
        emit_external_link_directives();
        println!(
            "cargo:warning=build-from-source disabled: not compiling vendored Box3D C sources"
        );
        return;
    }

    build_box3d_from_source(&config);
}

fn validate_build_config(config: &BuildConfig) {
    if config.skip_cc && config.wasm_mode == Some(WasmMode::Source) {
        panic!(
            "BOXDDD_SYS_SKIP_CC=1 cannot be combined with BOXDDD_SYS_WASM_MODE=source; \
             source mode is the runtime-capable WASM path and must compile Box3D C sources"
        );
    }
}

fn handle_wasm_build(config: &BuildConfig) -> bool {
    let Some(mode) = config.wasm_mode else {
        return false;
    };

    match mode {
        WasmMode::CompileOnly => {
            println!(
                "cargo:warning=boxddd-sys is using compile-only WASM mode; Box3D C sources are not linked"
            );
            true
        }
        WasmMode::Provider => {
            println!(
                "cargo:warning=boxddd-sys WASM provider mode is opt-in scaffold only; Box3D symbols are expected from the provider module"
            );
            true
        }
        WasmMode::Source => {
            if config.target_os != "wasi" {
                panic!(
                    "BOXDDD_SYS_WASM_MODE=source currently supports wasm32-wasip1 only; use provider mode for wasm32-unknown-unknown browser builds"
                );
            }
            if !cfg!(feature = "build-from-source") {
                panic!(
                    "BOXDDD_SYS_WASM_MODE=source requires the default `build-from-source` feature"
                );
            }
            build_box3d_from_source(config);
            true
        }
    }
}

fn emit_external_link_directives() {
    if let Ok(path) = env::var("BOXDDD_SYS_LINK_SEARCH")
        && !path.is_empty()
    {
        println!("cargo:rustc-link-search=native={path}");
    }

    let lib = env::var("BOXDDD_SYS_LINK_LIB").unwrap_or_else(|_| "box3d".into());
    if !lib.is_empty() {
        println!("cargo:rustc-link-lib={lib}");
    }
}

fn generate_wasm_provider_bindings(pregenerated: &Path, out_dir: &Path) {
    let import_module = upstream_contract::PROVIDER_MODULE;
    let source = fs::read_to_string(pregenerated).unwrap_or_else(|err| {
        panic!(
            "failed to read pregenerated bindings at {}: {err}",
            pregenerated.display()
        )
    });
    let rewritten = source.replace(
        "unsafe extern \"C\" {",
        &format!("#[link(wasm_import_module = \"{import_module}\")]\nunsafe extern \"C\" {{"),
    );
    if rewritten == source {
        panic!(
            "failed to generate WASM provider bindings from {}; no extern blocks were found",
            pregenerated.display()
        );
    }
    let bridge = format!(
        r#"
#[link(wasm_import_module = "{import_module}")]
unsafe extern "C" {{
    pub fn boxddd_provider_abi_revision() -> u32;
    pub fn boxddd_provider_install_default_pre_solve(world_id: b3WorldId);
    pub fn boxddd_provider_debug_install_world_def(def: *mut b3WorldDef, token: u32);
    pub fn boxddd_provider_debug_init_draw(draw: *mut b3DebugDraw, token: u32);
    pub fn boxddd_provider_debug_take_error(token: u32) -> i32;
    pub fn boxddd_provider_query_take_error(token: u32) -> i32;
    pub fn boxddd_provider_world_overlap_aabb(
        world_id: b3WorldId,
        aabb: b3AABB,
        filter: b3QueryFilter,
        token: u32,
    ) -> b3TreeStats;
    pub fn boxddd_provider_world_cast_ray(
        world_id: b3WorldId,
        origin: b3Pos,
        translation: b3Vec3,
        filter: b3QueryFilter,
        token: u32,
    ) -> b3TreeStats;
}}
"#
    );
    let output = out_dir.join("wasm_provider_bindings.rs");
    write_if_changed(&output, format!("{rewritten}{bridge}").as_bytes());
}

#[cfg(feature = "bindgen")]
fn generate_bindings(manifest_dir: &Path, out_dir: &Path) {
    let include_root = manifest_dir
        .join("third-party")
        .join("box3d")
        .join("include");
    let header = include_root.join("box3d").join("box3d.h");
    let mut builder = bindgen::Builder::default()
        .header(header.to_string_lossy())
        .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
        .formatter(bindgen::Formatter::Prettyplease)
        .clang_args(["-x", "c", "-std=c17"])
        .clang_arg(format!("-I{}", include_root.display()))
        .clang_args(double_precision_clang_args())
        .allowlist_function("b3.*")
        .allowlist_type("b3.*")
        .allowlist_var("B3_.*")
        .blocklist_item("^B3_DEFAULT_CATEGORY_BITS$")
        .blocklist_item("^B3_DEFAULT_MASK_BITS$")
        .raw_line("pub const B3_DEFAULT_CATEGORY_BITS: u64 = u64::MAX;")
        .raw_line("pub const B3_DEFAULT_MASK_BITS: u64 = u64::MAX;")
        .layout_tests(false);

    if cfg!(feature = "double-precision") {
        builder = builder
            .blocklist_function("^b3CreateWorldDoublePrecision$")
            .raw_line(
                "unsafe extern \"C\" {\n    pub fn b3CreateWorldDoublePrecision(\n        def: *const b3WorldDef,\n    ) -> b3WorldId;\n}",
            );
    }

    let bindings = builder
        .generate()
        .expect("failed to generate Box3D bindings");

    bindings
        .write_to_file(out_dir.join("bindings.rs"))
        .expect("failed to write Box3D bindings");
}

#[cfg(feature = "bindgen")]
fn double_precision_clang_args() -> Vec<&'static str> {
    if cfg!(feature = "double-precision") {
        vec!["-DBOX3D_DOUBLE_PRECISION"]
    } else {
        Vec::new()
    }
}

#[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 add_msvc_c_standard_flag(build: &mut cc::Build) {
    match build.is_flag_supported("/std:c17") {
        Ok(true) => {
            build.flag("/std:c17");
        }
        Ok(false) | Err(_) => {
            build.flag_if_supported("/std:c11");
        }
    }
}

fn build_box3d_from_source(config: &BuildConfig) {
    let box3d_root = config.manifest_dir.join("third-party").join("box3d");
    let box3d_include = box3d_root.join("include");
    let box3d_src = box3d_root.join("src");

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

    for relative in upstream_contract::BOX3D_C_SOURCES {
        let file = box3d_root.join(relative);
        if !file.is_file() {
            panic!(
                "manifest-declared Box3D source is missing: {}",
                file.display()
            );
        }
        build.file(file);
    }

    if config.target_env == "msvc" {
        let use_static_crt = env::var("CARGO_CFG_TARGET_FEATURE")
            .unwrap_or_default()
            .split(',')
            .any(|feature| feature == "crt-static");
        build.static_crt(use_static_crt);
        build.debug(config.is_debug());
        build.opt_level(if config.is_debug() { 0 } else { 2 });
        add_msvc_c_standard_flag(&mut build);
    } else {
        build.flag_if_supported("-std=c17");
        build.flag_if_supported("-ffp-contract=off");
        build.debug(config.is_debug());
        build.opt_level(if config.is_debug() { 0 } else { 2 });
        if config.target_os == "linux" {
            build.define("_POSIX_C_SOURCE", Some("199309L"));
            build.flag_if_supported("-pthread");
            println!("cargo:rustc-link-lib=pthread");
            println!("cargo:rustc-link-lib=m");
        }
        if config.wasm_mode == Some(WasmMode::Source) {
            configure_wasi_sysroot(&mut build);
        }
    }

    if cfg!(feature = "disable-simd") {
        build.define("BOX3D_DISABLE_SIMD", None);
    }
    if cfg!(feature = "validate") {
        build.define("BOX3D_VALIDATE", None);
    }
    if cfg!(feature = "double-precision") {
        build.define("BOX3D_DOUBLE_PRECISION", None);
    }

    build.compile("box3d");
}

fn configure_wasi_sysroot(build: &mut cc::Build) {
    let sysroot = env::var_os("WASI_SYSROOT")
        .map(PathBuf::from)
        .or_else(|| env::var_os("WASI_SDK_PATH").map(|path| PathBuf::from(path).join("share/wasi-sysroot")))
        .unwrap_or_else(|| {
            panic!(
                "wasm32-wasip1 source builds require WASI_SYSROOT or WASI_SDK_PATH so clang can find WASI libc headers"
            )
        });

    let has_libc_headers = sysroot.join("include").join("math.h").exists()
        || sysroot
            .join("include")
            .join("wasm32-wasi")
            .join("math.h")
            .exists();
    if !has_libc_headers {
        panic!(
            "WASI sysroot at {} does not contain WASI libc headers",
            sysroot.display()
        );
    }

    build.flag(format!("--sysroot={}", sysroot.display()));
}