From: "Ezekiel Newren via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Ezekiel Newren <ezekielnewren@gmail.com>,
Ezekiel Newren <ezekielnewren@gmail.com>
Subject: [PATCH v2 14/18] build-helper: cbindgen, let crates generate a header file
Date: Wed, 17 Sep 2025 01:16:34 +0000 [thread overview]
Message-ID: <fa334405686a329dd1508bf8d8cbfa12dc5dc7bb.1758071798.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.2043.v2.git.git.1758071798.gitgitgadget@gmail.com>
From: Ezekiel Newren <ezekielnewren@gmail.com>
Signed-off-by: Ezekiel Newren <ezekielnewren@gmail.com>
---
rust/build-helper/Cargo.toml | 5 ++++
rust/build-helper/src/lib.rs | 44 +++++++++++++++++++++++++++++++++++-
rust/cbindgen-template.toml | 16 +++++++++++++
3 files changed, 64 insertions(+), 1 deletion(-)
create mode 100644 rust/cbindgen-template.toml
diff --git a/rust/build-helper/Cargo.toml b/rust/build-helper/Cargo.toml
index 8939b4b876..2e42bb5405 100644
--- a/rust/build-helper/Cargo.toml
+++ b/rust/build-helper/Cargo.toml
@@ -4,3 +4,8 @@ version = "0.1.0"
edition = "2021"
[dependencies]
+cbindgen = "0.24.0"
+textwrap = "=0.16.1"
+once_cell = "=1.20.3"
+unicode-width = "=0.1.13"
+
diff --git a/rust/build-helper/src/lib.rs b/rust/build-helper/src/lib.rs
index 70e95d16f5..640bc19734 100644
--- a/rust/build-helper/src/lib.rs
+++ b/rust/build-helper/src/lib.rs
@@ -1,5 +1,7 @@
use std::collections::HashMap;
+use std::io::Write;
use std::path::PathBuf;
+use cbindgen::Config;
fn parse_bool_from_str(value: &str) -> bool {
@@ -23,12 +25,20 @@ fn parse_bool_from_option(value: Option<&String>, default: bool) -> bool {
/// To run tests set GIT_BUILD_DIR and run `USE_LINKING=true cargo test`
pub struct BuildHelper {
crate_env: HashMap<String, String>,
+ generate_header: bool,
+ file_out: PathBuf,
+ config: Config,
}
impl BuildHelper {
pub fn new(crate_env: HashMap<String, String>) -> Self {
- let it = Self {crate_env};
+ let mut it = Self {
+ crate_env,
+ generate_header: false,
+ file_out: PathBuf::default(),
+ config: Config::default(),
+ };
let dir_crate = it.dir_crate();
let dir_workspace = dir_crate.parent().unwrap();
@@ -38,6 +48,12 @@ impl BuildHelper {
std::fs::create_dir(dir_interop.clone()).unwrap();
}
+ let file_cbindgen = dir_workspace.join("cbindgen-template.toml");
+ it.file_out = dir_interop.join(format!("{}.h", it.crate_name()));
+
+ it.config = Config::from_file(file_cbindgen.display().to_string().as_str()).unwrap();
+ it.config.include_guard = Some(format!("{}_H", it.crate_name().to_uppercase()));
+
it
}
@@ -49,6 +65,16 @@ impl BuildHelper {
PathBuf::from(self.crate_env["CARGO_MANIFEST_DIR"].clone())
}
+ pub fn generate_header<F>(mut self, editor: F) -> Self
+ where
+ F: Fn(&mut Config)
+ {
+ self.generate_header = true;
+ editor(&mut self.config);
+
+ self
+ }
+
pub fn build(self) {
let use_linking = parse_bool_from_option(self.crate_env.get("USE_LINKING"), self.crate_env.get("CARGO_TARGET_DIR").is_none());
let dir_crate = self.dir_crate();
@@ -78,6 +104,22 @@ impl BuildHelper {
} else {
println!("cargo:warning={} is not linking against C objects, `USE_LINKING=true cargo test`", self.crate_env["CARGO_PKG_NAME"]);
}
+
+ if self.generate_header {
+ let mut buffer = Vec::<u8>::new();
+ cbindgen::Builder::new()
+ .with_crate(dir_crate.clone())
+ .with_config(self.config)
+ .with_std_types(true)
+ .generate()
+ .expect("Unable to generate bindings")
+ .write(&mut buffer);
+
+ let mut fd = std::fs::File::create(self.file_out).unwrap();
+ fd.write(buffer.as_slice()).unwrap();
+ } else {
+ let _ = std::fs::remove_file(self.file_out);
+ }
}
}
diff --git a/rust/cbindgen-template.toml b/rust/cbindgen-template.toml
new file mode 100644
index 0000000000..cab83218d0
--- /dev/null
+++ b/rust/cbindgen-template.toml
@@ -0,0 +1,16 @@
+## compat/posix.h includes stdbool.h where git-compat-util.h does not
+## this is mandatory for correct bool C <-> Rust interop. Though the
+## 'includes' (and all other variables in this file) can be
+## overridden in build.rs.
+sys_includes = ["compat/posix.h"]
+
+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 = 4
+
+[parse]
+parse_deps = false
--
gitgitgadget
next prev parent reply other threads:[~2025-09-17 1:16 UTC|newest]
Thread overview: 100+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-29 19:42 [PATCH 00/15] Introduce rust: In xdiff Ezekiel Newren via GitGitGadget
2025-08-29 19:42 ` [PATCH 01/15] doc: add a policy for using Rust brian m. carlson via GitGitGadget
2025-08-29 20:00 ` brian m. carlson
2025-08-29 20:11 ` Ezekiel Newren
2025-09-02 16:39 ` brian m. carlson
2025-09-02 18:39 ` Ezekiel Newren
2025-09-04 22:55 ` Ezekiel Newren
2025-08-29 19:42 ` [PATCH 02/15] xdiff: introduce rust Ezekiel Newren via GitGitGadget
2025-08-29 19:42 ` [PATCH 03/15] github workflows: install rust Ezekiel Newren via GitGitGadget
2025-08-29 19:42 ` [PATCH 04/15] win+Meson: do allow linking with the Rust-built xdiff Johannes Schindelin via GitGitGadget
2025-08-29 19:42 ` [PATCH 05/15] github workflows: upload Cargo.lock Ezekiel Newren via GitGitGadget
2025-08-29 19:42 ` [PATCH 06/15] ivec: create a vector type that is interoperable between C and Rust Ezekiel Newren via GitGitGadget
2025-08-29 19:42 ` [PATCH 07/15] xdiff/xprepare: remove superfluous forward declarations Ezekiel Newren via GitGitGadget
2025-08-29 19:42 ` [PATCH 08/15] xdiff: delete unnecessary fields from xrecord_t and xdfile_t Ezekiel Newren via GitGitGadget
2025-08-29 19:42 ` [PATCH 09/15] xdiff: make fields of xrecord_t Rust friendly Ezekiel Newren via GitGitGadget
2025-08-29 19:42 ` [PATCH 10/15] xdiff: use one definition for freeing xdfile_t Ezekiel Newren via GitGitGadget
2025-08-29 19:42 ` [PATCH 11/15] xdiff: replace chastore with an ivec in xdfile_t Ezekiel Newren via GitGitGadget
2025-08-29 19:42 ` [PATCH 12/15] xdiff: delete nrec field from xdfile_t Ezekiel Newren via GitGitGadget
2025-08-29 19:42 ` [PATCH 13/15] xdiff: delete recs " Ezekiel Newren via GitGitGadget
2025-08-29 19:42 ` [PATCH 14/15] xdiff: make xdfile_t more rust friendly Ezekiel Newren via GitGitGadget
2025-08-29 19:42 ` [PATCH 15/15] xdiff: implement xdl_trim_ends() in Rust Ezekiel Newren via GitGitGadget
2025-09-17 1:16 ` [PATCH v2 00/18] Introduce rust: In xdiff Ezekiel Newren via GitGitGadget
2025-09-17 1:16 ` [PATCH v2 01/18] cleanup: rename variables that collide with Rust primitive type names Ezekiel Newren via GitGitGadget
2025-09-17 7:42 ` Eric Sunshine
2025-09-17 14:32 ` Junio C Hamano
2025-09-19 19:36 ` Ezekiel Newren
2025-09-17 1:16 ` [PATCH v2 02/18] make: add -fPIE flag Ezekiel Newren via GitGitGadget
2025-09-17 7:44 ` Eric Sunshine
2025-09-19 19:48 ` Ezekiel Newren
2025-09-19 20:07 ` Junio C Hamano
2025-09-19 21:52 ` Ezekiel Newren
2025-09-19 23:43 ` Junio C Hamano
2025-09-19 23:59 ` Collin Funk
2025-09-20 16:44 ` Junio C Hamano
2025-09-21 1:14 ` Ramsay Jones
2025-09-17 1:16 ` [PATCH v2 03/18] make: merge xdiff lib into libgit.a Ezekiel Newren via GitGitGadget
2025-09-17 7:46 ` Eric Sunshine
2025-09-19 19:54 ` Ezekiel Newren
2025-09-17 1:16 ` [PATCH v2 04/18] make: merge reftable " Ezekiel Newren via GitGitGadget
2025-09-17 7:46 ` Eric Sunshine
2025-09-19 19:02 ` Junio C Hamano
2025-09-19 20:00 ` Ezekiel Newren
2025-09-19 20:14 ` Junio C Hamano
2025-09-19 23:02 ` Ezekiel Newren
2025-09-17 1:16 ` [PATCH v2 05/18] doc: add a policy for using Rust brian m. carlson via GitGitGadget
2025-09-17 1:16 ` [PATCH v2 06/18] BreakingChanges: announce Rust becoming mandatory Patrick Steinhardt via GitGitGadget
2025-09-17 1:16 ` [PATCH v2 07/18] build: introduce rust Ezekiel Newren via GitGitGadget
2025-09-17 8:26 ` Eric Sunshine
2025-09-17 14:54 ` Junio C Hamano
2025-09-18 7:06 ` Eric Sunshine
2025-09-19 20:11 ` Ezekiel Newren
2025-09-19 20:24 ` Eric Sunshine
2025-09-19 21:28 ` Ezekiel Newren
2025-09-17 1:16 ` [PATCH v2 08/18] help: report on whether or not Rust is enabled Patrick Steinhardt via GitGitGadget
2025-09-17 1:16 ` [PATCH v2 09/18] github workflows: install rust Ezekiel Newren via GitGitGadget
2025-09-17 8:01 ` Eric Sunshine
2025-09-17 1:16 ` [PATCH v2 10/18] win+Meson: do allow linking with the Rust-built xdiff Johannes Schindelin via GitGitGadget
2025-09-17 1:16 ` [PATCH v2 11/18] github workflows: upload Cargo.lock Ezekiel Newren via GitGitGadget
2025-09-17 1:16 ` [PATCH v2 12/18] build: new crate, build-helper Ezekiel Newren via GitGitGadget
2025-09-17 8:58 ` Eric Sunshine
2025-09-17 1:16 ` [PATCH v2 13/18] build-helper: link against libgit.a and any other required C libraries Ezekiel Newren via GitGitGadget
2025-09-17 8:51 ` Eric Sunshine
2025-09-17 23:07 ` D. Ben Knoble
2025-09-17 23:31 ` Eric Sunshine
2025-09-19 20:25 ` Ezekiel Newren
2025-09-17 1:16 ` Ezekiel Newren via GitGitGadget [this message]
2025-09-17 9:08 ` [PATCH v2 14/18] build-helper: cbindgen, let crates generate a header file Eric Sunshine
2025-09-19 20:34 ` Ezekiel Newren
2025-09-17 1:16 ` [PATCH v2 15/18] varint: use explicit width for integers Patrick Steinhardt via GitGitGadget
2025-09-17 1:16 ` [PATCH v2 16/18] build: new crate, misc Ezekiel Newren via GitGitGadget
2025-09-17 9:16 ` Eric Sunshine
2025-09-19 20:42 ` Ezekiel Newren
2025-09-19 20:50 ` Eric Sunshine
2025-09-19 21:54 ` Ezekiel Newren
2025-09-17 1:16 ` [PATCH v2 17/18] misc: use BuildHelper Ezekiel Newren via GitGitGadget
2025-09-17 1:16 ` [PATCH v2 18/18] misc::varint: reimplement as test balloon for Rust Patrick Steinhardt via GitGitGadget
2025-09-17 5:58 ` [PATCH v2 00/18] Introduce rust: In xdiff Patrick Steinhardt
2025-09-19 20:57 ` Ezekiel Newren
2025-09-22 13:01 ` Patrick Steinhardt
2025-09-22 15:31 ` Ezekiel Newren
2025-09-22 16:08 ` Patrick Steinhardt
2025-09-22 16:47 ` Junio C Hamano
2025-09-22 17:23 ` Ezekiel Newren
2025-09-22 17:32 ` Ezekiel Newren
2025-09-22 18:17 ` Junio C Hamano
2025-09-22 18:33 ` Ezekiel Newren
2025-09-22 18:41 ` Junio C Hamano
2025-09-22 18:12 ` Junio C Hamano
2025-09-17 17:07 ` Junio C Hamano
2025-09-17 20:44 ` Junio C Hamano
2025-09-17 21:34 ` Elijah Newren
2025-09-17 22:48 ` Junio C Hamano
2025-09-22 13:01 ` Patrick Steinhardt
2025-09-22 15:18 ` Ezekiel Newren
2025-09-22 16:15 ` Patrick Steinhardt
2025-09-22 16:27 ` Ezekiel Newren
2025-09-23 5:11 ` Patrick Steinhardt
2025-09-23 16:32 ` Ezekiel Newren
2025-09-23 18:05 ` Ezekiel Newren
2025-09-23 21:04 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=fa334405686a329dd1508bf8d8cbfa12dc5dc7bb.1758071798.git.gitgitgadget@gmail.com \
--to=gitgitgadget@gmail.com \
--cc=ezekielnewren@gmail.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).