Skip to main content

reifydb_build/
lib.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3#![cfg_attr(not(debug_assertions), deny(clippy::disallowed_methods))]
4#![cfg_attr(debug_assertions, warn(clippy::disallowed_methods))]
5#![cfg_attr(not(debug_assertions), deny(warnings))]
6#![allow(clippy::tabs_in_doc_comments)]
7
8//! Build utilities for ReifyDB target detection.
9//!
10//! This crate provides a single function to emit the `reifydb_target` cfg
11//! based on the compilation target. Add this crate as a build-dependency
12//! and call `emit_target_cfg()` from your build.rs.
13//!
14//! # Supported Targets
15//! - `native` - Default for non-WASM targets
16//! - `wasm` - For wasm32-unknown-unknown (JS-WASM) targets
17//! - `wasi` - For wasm32-wasip1 (WASI) targets
18//! - `dst` - (Future) Deterministic software testing
19
20use std::env;
21
22/// Emit the `reifydb_target` cfg based on the current compilation target.
23///
24/// Call this from your crate's `build.rs`:
25/// ```ignore
26/// fn main() {
27///     reifydb_build::emit_target_cfg();
28/// }
29/// ```
30///
31/// Then use in code:
32/// ```ignore
33/// #[cfg(reifydb_target = "native")]
34/// fn native_only() { }
35///
36/// #[cfg(reifydb_target = "wasm")]
37/// fn wasm_only() { }
38///
39/// #[cfg(reifydb_target = "wasi")]
40/// fn wasi_only() { }
41/// ```
42pub fn emit_target_cfg() {
43	let target = env::var("TARGET").unwrap_or_default();
44
45	let (reifydb_target, single_threaded) = if target.contains("wasm32") && target.contains("wasi") {
46		("wasi", true)
47	} else if target.contains("wasm32") {
48		("wasm", true)
49	} else {
50		// Default to native for all non-wasm targets
51		// Future: could check for DST-specific target/env var here
52		("native", false)
53	};
54
55	// Emit the check-cfg directive to tell the compiler about our custom cfgs
56	println!("cargo::rustc-check-cfg=cfg(reifydb_target, values(\"native\", \"wasm\", \"wasi\", \"dst\"))");
57	println!("cargo::rustc-check-cfg=cfg(reifydb_single_threaded)");
58	println!("cargo:rustc-cfg=reifydb_target=\"{}\"", reifydb_target);
59	if single_threaded {
60		println!("cargo:rustc-cfg=reifydb_single_threaded");
61	}
62	println!("cargo:rerun-if-changed=build.rs");
63}