Subject: [PATCH 06/13] cargo: create crate generate-headers

From: "Ezekiel Newren via GitGitGadget" <[email-masked]>
Date: Thu, 27 Nov 2025 01:10:28 +0000

From: Ezekiel Newren <[email-masked]>

Signed-off-by: Ezekiel Newren <[email-masked]>
---
 Cargo.toml                        |  1 +
 rust/cbindgen-template.toml       | 13 +++++++++
 rust/generate-headers/Cargo.toml  | 12 +++++++++
 rust/generate-headers/src/main.rs | 44 +++++++++++++++++++++++++++++++
 4 files changed, 70 insertions(+)
 create mode 100644 rust/cbindgen-template.toml
 create mode 100644 rust/generate-headers/Cargo.toml
 create mode 100644 rust/generate-headers/src/main.rs

diff --git a/Cargo.toml b/Cargo.toml
index 02273f5142..b7b5890934 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,5 +1,6 @@
 [workspace]
 members = [
+    "rust/generate-headers",
     "rust/gitcore",
 ]
 resolver = "2"
diff --git a/rust/cbindgen-template.toml b/rust/cbindgen-template.toml
new file mode 100644
index 0000000000..13c192d60e
--- /dev/null
+++ b/rust/cbindgen-template.toml
@@ -0,0 +1,13 @@
+## don't include any system files, not even git-compat-util.h
+# sys_includes = []
+
+autogen_warning = "/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */"
+
+language = "C"
+no_includes = true
+usize_is_size_t = true
+style = "tag"
+tab_width = 8
+
+[parse]
+parse_deps = false
diff --git a/rust/generate-headers/Cargo.toml b/rust/generate-headers/Cargo.toml
new file mode 100644
index 0000000000..9216d83d75
--- /dev/null
+++ b/rust/generate-headers/Cargo.toml
@@ -0,0 +1,12 @@
+[package]
+name = "generate-headers"
+version = "0.1.0"
+edition = "2018"
+
+[dependencies]
+cbindgen = "0.24.0"
+textwrap = "=0.16.1"
+once_cell = "=1.20.3"
+unicode-width = "=0.1.13"
+quote = "=1.0.41"
+syn = "=2.0.106"
diff --git a/rust/generate-headers/src/main.rs b/rust/generate-headers/src/main.rs
new file mode 100644
index 0000000000..da68c8ae3d
--- /dev/null
+++ b/rust/generate-headers/src/main.rs
@@ -0,0 +1,44 @@
+use cbindgen;
+
+use std::io::Write;
+use std::path::PathBuf;
+use cbindgen::Config;
+
+fn generate_header<F>(crate_name: String, editor: F)
+where
+    F: Fn(&mut Config)
+{
+    let dir_workspace = PathBuf::default();
+    let dir_rust = dir_workspace.join("rust");
+    let dir_crate = dir_rust.join(crate_name.clone());
+    let dir_generated = dir_workspace.join("generated");
+    if !dir_generated.exists() {
+        std::fs::create_dir(dir_generated.clone()).unwrap();
+    }
+
+    let file_cbindgen = dir_rust.join("cbindgen-template.toml");
+    let file_out = dir_generated.join(format!("{}.h", crate_name.clone()));
+
+    let mut config = Config::from_file(file_cbindgen.display().to_string().as_str()).unwrap();
+    config.include_guard = Some(format!("{}_H", crate_name.to_uppercase()));
+
+    editor(&mut config);
+
+    let mut buffer = Vec::<u8>::new();
+    cbindgen::Builder::new()
+        .with_crate(dir_crate.clone())
+        .with_config(config)
+        .with_std_types(true)
+        .generate()
+        .expect("Unable to generate bindings")
+        .write(&mut buffer);
+
+    let mut fd = std::fs::File::create(file_out).unwrap();
+    fd.write(buffer.as_slice()).unwrap();
+}
+
+fn main() {
+    // cargo run -p generate-headers
+
+    generate_header(String::from("gitcore"), |_|{});
+}
-- 
gitgitgadget