Skip to content

Commit 95e8a9a

Browse files
pks-tgitster
authored andcommitted
rust: generate bindings via cbindgen
When compiling Git with Rust enabled we replace our C implementation of the varint encoding with a Rust implementation. A prerequisite for doing so is of course that the interfaces for both implementations are exactly the same. If they aren't, then we risk subtle runtime errors. We don't really have a way to detect such interface mismatches though: the code will happily compile if we change either of the implementations without adjusting the other implementation in the same spirit. The risk of divergence is low right now as we only replace a single subsystem. But it is expected that we'll grow more reimplementations over time, so it is bound to increase. A related issue is that we don't have an easy way to implement features exclusively in Rust and make them available to our C library. Again, we don't have such features yet, but there are work-in-progress patch series that will eventually add them. Both of these issues can be addressed by generating C bindings via the cbindgen(1) tool: given a Rust crate, it extracts all functions marked with `extern "C"` and creates a C declaration for them. These are then written into a header file that we can include. Set up this infrastructure in both our Makefile and in Meson. To demonstrate its use, the generated "c-bindings.h" header is included in "varint.c". If we now adapt "varint.rs" to have a different function signature than the C code we'll now get a compiler error: In file included from ../varint.c:10: ./c-bindings.h:10:10: error: conflicting types for 'decode_varint' 10 | uint32_t decode_varint(const uint8_t **bufp); | ^ ../varint.h:5:10: note: previous declaration is here 5 | uint64_t decode_varint(const unsigned char **); An initial version instead included the bindings in "varint.h". But that would cause us to recompile all dependents of "varint.h" every time the signatures of exported Rust functions change. So instead, we now include it in "varint.c" and compile that file unconditionally again. Adapt our CI to install cbindgen(1) accordingly. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent edc56a8 commit 95e8a9a

7 files changed

Lines changed: 63 additions & 10 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@
197197
/gitweb/gitweb.cgi
198198
/gitweb/static/gitweb.js
199199
/gitweb/static/gitweb.min.*
200+
/c-bindings.h
200201
/config-list.h
201202
/command-list.h
202203
/hook-list.h

Makefile

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,9 +1327,7 @@ LIB_OBJS += urlmatch.o
13271327
LIB_OBJS += usage.o
13281328
LIB_OBJS += userdiff.o
13291329
LIB_OBJS += utf8.o
1330-
ifndef WITH_RUST
13311330
LIB_OBJS += varint.o
1332-
endif
13331331
LIB_OBJS += version.o
13341332
LIB_OBJS += versioncmp.o
13351333
LIB_OBJS += walker.o
@@ -1563,6 +1561,14 @@ ALL_LDFLAGS = $(LDFLAGS) $(LDFLAGS_APPEND)
15631561
ifdef WITH_RUST
15641562
BASIC_CFLAGS += -DWITH_RUST
15651563
GITLIBS += $(RUST_LIB)
1564+
1565+
C_BINDINGS = c-bindings.h
1566+
1567+
GENERATED_H += $(C_BINDINGS)
1568+
1569+
$(C_BINDINGS): cbindgen.toml $(RUST_SOURCES)
1570+
$(QUIET_CBINDGEN)cbindgen --output $@
1571+
15661572
ifeq ($(uname_S),Windows)
15671573
EXTLIBS += -luserenv
15681574
endif
@@ -2620,6 +2626,8 @@ PAGER_ENV_CQ_SQ = $(subst ','\'',$(PAGER_ENV_CQ))
26202626
pager.sp pager.s pager.o: EXTRA_CPPFLAGS = \
26212627
-DPAGER_ENV='$(PAGER_ENV_CQ_SQ)'
26222628

2629+
varint.sp varint.s varint.o: $(C_BINDINGS)
2630+
26232631
version-def.h: version-def.h.in GIT-VERSION-GEN GIT-VERSION-FILE GIT-USER-AGENT
26242632
$(QUIET_GEN)$(call version_gen,"$(shell pwd)",$<,$@)
26252633

@@ -3807,7 +3815,7 @@ clean: profile-clean coverage-clean cocciclean
38073815
$(RM) $(FUZZ_PROGRAMS)
38083816
$(RM) $(SP_OBJ)
38093817
$(RM) $(HCC)
3810-
$(RM) -r Cargo.lock target/
3818+
$(RM) -r Cargo.lock target/ $(C_BINDINGS)
38113819
$(RM) version-def.h
38123820
$(RM) -r $(dep_dirs) $(compdb_dir) compile_commands.json
38133821
$(RM) $(test_bindir_programs)

cbindgen.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
language = "C"
2+
3+
# Write a warning into the generated file.
4+
autogen_warning = "/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */"
5+
6+
# Don't include standard C headers. These are managed by "git-compat-util.h".
7+
no_includes = true
8+
9+
# Use plain structs instead of using typedefs.
10+
style = "tag"
11+
12+
# Match our coding style more closely.
13+
tab_width = 8

ci/install-dependencies.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,18 @@ alpine-*)
3232
bash cvs gnupg perl-cgi perl-dbd-sqlite perl-io-tty >/dev/null
3333
;;
3434
fedora-*|almalinux-*)
35+
RUST_DEPS="cargo cbindgen"
3536
case "$jobname" in
37+
almalinux-8)
38+
# AlmaLinux 8 does not have cbindgen, it was only added in version 9.
39+
RUST_DEPS=;;
3640
*-meson)
3741
MESON_DEPS="meson ninja";;
3842
esac
43+
3944
dnf -yq update >/dev/null &&
40-
dnf -yq install shadow-utils sudo make pkg-config gcc findutils diffutils perl python3 gawk gettext zlib-devel expat-devel openssl-devel curl-devel pcre2-devel $MESON_DEPS cargo >/dev/null
45+
dnf -yq install shadow-utils sudo make pkg-config gcc findutils diffutils perl python3 gawk gettext \
46+
zlib-devel expat-devel openssl-devel curl-devel pcre2-devel $MESON_DEPS $RUST_DEPS >/dev/null
4147
;;
4248
ubuntu-*|i386/debian-*|debian-*)
4349
# Required so that apt doesn't wait for user input on certain packages.
@@ -64,7 +70,7 @@ ubuntu-*|i386/debian-*|debian-*)
6470
make libssl-dev libcurl4-openssl-dev libexpat-dev wget sudo default-jre \
6571
tcl tk gettext zlib1g-dev perl-modules liberror-perl libauthen-sasl-perl \
6672
libemail-valid-perl libio-pty-perl libio-socket-ssl-perl libnet-smtp-ssl-perl libdbd-sqlite3-perl libcgi-pm-perl \
67-
libsecret-1-dev libpcre2-dev meson ninja-build pkg-config cargo \
73+
libsecret-1-dev libpcre2-dev meson ninja-build pkg-config cargo cbindgen \
6874
${CC_PACKAGE:-${CC:-gcc}} $PYTHON_PACKAGE
6975

7076
# Starting with Ubuntu 25.10, sudo can now be provided via either

meson.build

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ libgit_sources = [
523523
'usage.c',
524524
'userdiff.c',
525525
'utf8.c',
526+
'varint.c',
526527
'version.c',
527528
'versioncmp.c',
528529
'walker.c',
@@ -1704,18 +1705,32 @@ version_def_h = custom_target(
17041705
libgit_sources += version_def_h
17051706

17061707
cargo = find_program('cargo', dirs: program_path, native: true, required: get_option('rust'))
1707-
rust_option = get_option('rust').disable_auto_if(not cargo.found())
1708+
cbindgen = find_program('cbindgen', dirs: program_path, native: true, required: get_option('rust'))
1709+
1710+
rust_option = get_option('rust').disable_auto_if(not cargo.found() or not cbindgen.found())
17081711
if rust_option.allowed()
17091712
subdir('src')
17101713
libgit_c_args += '-DWITH_RUST'
17111714

17121715
if host_machine.system() == 'windows'
17131716
libgit_dependencies += compiler.find_library('userenv')
17141717
endif
1715-
else
1716-
libgit_sources += [
1717-
'varint.c',
1718-
]
1718+
1719+
cbindgen_input = [ 'cbindgen.toml' ]
1720+
foreach source : libgit_rs_sources
1721+
cbindgen_input += 'src' / source
1722+
endforeach
1723+
1724+
libgit_sources += custom_target('c-bindings.h',
1725+
input: cbindgen_input,
1726+
output: 'c-bindings.h',
1727+
command: [
1728+
cbindgen,
1729+
'--output',
1730+
'@OUTPUT@',
1731+
meson.current_source_dir(),
1732+
],
1733+
)
17191734
endif
17201735

17211736
libgit = declare_dependency(

shared.mak

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ ifndef V
5757

5858
## Used in "Makefile"
5959
QUIET_CARGO = @echo ' ' CARGO $@;
60+
QUIET_CBINDGEN = @echo ' ' CBINDGEN $@;
6061
QUIET_CC = @echo ' ' CC $@;
6162
QUIET_AR = @echo ' ' AR $@;
6263
QUIET_LINK = @echo ' ' LINK $@;

varint.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
#include "git-compat-util.h"
22
#include "varint.h"
33

4+
/*
5+
* When building with Rust we don't compile the C code, but we only verify
6+
* whether the function signatures of our C bindings match the ones we have
7+
* declared in "varint.h".
8+
*/
9+
#ifdef WITH_RUST
10+
# include "c-bindings.h"
11+
#else
412
uint64_t decode_varint(const unsigned char **bufp)
513
{
614
const unsigned char *buf = *bufp;
@@ -28,3 +36,4 @@ uint8_t encode_varint(uint64_t value, unsigned char *buf)
2836
memcpy(buf, varint + pos, sizeof(varint) - pos);
2937
return sizeof(varint) - pos;
3038
}
39+
#endif

0 commit comments

Comments
 (0)