Truly independent web browser
/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
//! Writes LibJS/Embedding/ABI.h, the C declarations of the embedding ABI, with cbindgen from src/embedding alone.
use std::fs;
use std::io;
use std::path::Path;
/// The C types that LibJS/HostObjectABI.h defines, which the generated header includes rather than declares again.
const TYPES_FROM_HOST_OBJECT_ABI_HEADER: &[&str] = &[
"JSValue",
"JSPropertyKey",
"JSCompletion",
"JSObject",
"JSVM",
"JSModule",
"JSPromiseCapability",
"JSGetCacheMetadata",
"JSSetCacheMetadata",
"JSPropertyDescriptor",
"JSValueSink",
"JSStringSink",
"JSHostObjectHooks",
"JSHostFunctionHooks",
"JSHostArrayHooks",
"JSResolvedBinding",
"JSHostModuleHooks",
"JSHostClass",
];
fn configuration() -> cbindgen::Config {
let mut configuration = cbindgen::Config {
language: cbindgen::Language::C,
cpp_compat: true,
header: Some(
"/*\n * Copyright (c) 2026-present, the Ladybird developers.\n *\n * SPDX-License-Identifier: BSD-2-Clause\n */"
.to_string(),
),
include_guard: Some("LIBJS_EMBEDDING_ABI_H".to_string()),
autogen_warning: Some(
"// Generated by the build script of libjs_embedding_headers from the runtime's src/embedding -- DO NOT EDIT".to_string(),
),
no_includes: true,
// LibGC/CAPI.h declares the LibGC types the ABI passes, such as the GCVisitor through which an embedder
// traces what it holds.
sys_includes: vec!["LibGC/CAPI.h".to_string(), "LibJS/HostObjectABI.h".to_string()],
documentation_style: cbindgen::DocumentationStyle::C99,
line_length: 120,
tab_width: 4,
usize_is_size_t: true,
..Default::default()
};
configuration.export.exclude = TYPES_FROM_HOST_OBJECT_ABI_HEADER
.iter()
.map(|name| name.to_string())
.collect();
configuration.defines = CFG_PREDICATES_AS_C_MACROS
.iter()
.map(|(predicate, macro_name)| (predicate.to_string(), macro_name.to_string()))
.collect();
configuration
}
/// The predefined macros that stand for the cfg predicates of items in src/embedding, which cbindgen turns into #if
/// conditions, as JSNativeFunction depends on how the target returns a C++ ThrowCompletionOr<Value>.
const CFG_PREDICATES_AS_C_MACROS: &[(&str, &str)] = &[
("target_arch = x86_64", "__x86_64__"),
("target_vendor = apple", "__APPLE__"),
("target_os = windows", "_WIN32"),
];
/// Parses the embedding module, whose mod.rs leads cbindgen to every file under it, and writes the header to `output`
/// if it changed.
pub fn generate(embedding_module: &Path, output: &Path) {
match cbindgen::Builder::new()
.with_config(configuration())
.with_src(embedding_module)
.generate()
{
Ok(bindings) => {
bindings.write_to_file(output);
}
// The build script does not fail here, so that rustc reports a syntax error it shares with cbindgen in its own
// words. Removing the header of the previous build keeps it from being copied out in place of the new one when
// only cbindgen rejects the code.
Err(error @ cbindgen::Error::ParseSyntaxError { .. }) => {
println!("cargo:warning=cbindgen could not parse src/embedding, so it wrote no ABI.h: {error}");
if let Err(remove_error) = fs::remove_file(output)
&& remove_error.kind() != io::ErrorKind::NotFound
{
panic!("remove the outdated {}: {remove_error}", output.display());
}
}
Err(error) => panic!("cbindgen failed to generate {}: {error}", output.display()),
}
}