Skip to content

Commit 2a1f4be

Browse files
committed
xdiff: introduce rust
Upcoming patches will accelerate and simplify xdiff, while also porting parts of it to Rust. In preparation, add some stubs and setup the Rust build. For now, it is easier to let cargo build rust and have make or meson merely link against the static library that cargo builds. In line with ongoing libification efforts, use multiple crates to allow more modularity on the Rust side. xdiff is the crate that this series will focus on, but we also introduce the interop crate for future patch series. In order to facilitate interoperability between C and Rust, introduce C definitions for Rust primitive types in git-compat-util.h. Signed-off-by: Ezekiel Newren <[email protected]>
1 parent 16bd9f2 commit 2a1f4be

File tree

9 files changed

+117
-1
lines changed

9 files changed

+117
-1
lines changed

Makefile

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,11 @@ TEST_SHELL_PATH = $(SHELL_PATH)
919919

920920
LIB_FILE = libgit.a
921921
XDIFF_LIB = xdiff/lib.a
922+
ifeq ($(DEBUG), 1)
923+
RUST_LIB = rust/target/debug/libxdiff.a
924+
else
925+
RUST_LIB = rust/target/release/libxdiff.a
926+
endif
922927
REFTABLE_LIB = reftable/libreftable.a
923928

924929
GENERATED_H += command-list.h
@@ -1392,6 +1397,8 @@ UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/lib-reftable.o
13921397
GITLIBS = common-main.o $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB) $(LIB_FILE)
13931398
EXTLIBS =
13941399

1400+
GITLIBS += $(RUST_LIB)
1401+
13951402
GIT_USER_AGENT = git/$(GIT_VERSION)
13961403

13971404
ifeq ($(wildcard sha1collisiondetection/lib/sha1.h),sha1collisiondetection/lib/sha1.h)
@@ -2925,6 +2932,14 @@ $(LIB_FILE): $(LIB_OBJS)
29252932
$(XDIFF_LIB): $(XDIFF_OBJS)
29262933
$(QUIET_AR)$(RM) $@ && $(AR) $(ARFLAGS) $@ $^
29272934

2935+
.PHONY: $(RUST_LIB)
2936+
$(RUST_LIB):
2937+
ifeq ($(DEBUG), 1)
2938+
cd rust && RUSTFLAGS="-Aunused_imports -Adead_code" cargo build --verbose
2939+
else
2940+
cd rust && RUSTFLAGS="-Aunused_imports -Adead_code" cargo build --verbose --release
2941+
endif
2942+
29282943
$(REFTABLE_LIB): $(REFTABLE_OBJS)
29292944
$(QUIET_AR)$(RM) $@ && $(AR) $(ARFLAGS) $@ $^
29302945

@@ -3756,7 +3771,10 @@ cocciclean:
37563771
$(RM) -r .build/contrib/coccinelle
37573772
$(RM) contrib/coccinelle/*.cocci.patch
37583773

3759-
clean: profile-clean coverage-clean cocciclean
3774+
rustclean:
3775+
cd rust && cargo clean
3776+
3777+
clean: profile-clean coverage-clean cocciclean rustclean
37603778
$(RM) -r .build $(UNIT_TEST_BIN)
37613779
$(RM) GIT-TEST-SUITES
37623780
$(RM) po/git.pot po/git-core.pot

git-compat-util.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,23 @@ static inline int is_xplatform_dir_sep(int c)
196196
#include "compat/msvc.h"
197197
#endif
198198

199+
/* rust types */
200+
typedef uint8_t u8;
201+
typedef uint16_t u16;
202+
typedef uint32_t u32;
203+
typedef uint64_t u64;
204+
205+
typedef int8_t i8;
206+
typedef int16_t i16;
207+
typedef int32_t i32;
208+
typedef int64_t i64;
209+
210+
typedef float f32;
211+
typedef double f64;
212+
213+
typedef size_t usize;
214+
typedef ptrdiff_t isize;
215+
199216
/* used on Mac OS X */
200217
#ifdef PRECOMPOSE_UNICODE
201218
#include "compat/precompose_utf8.h"

meson.build

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,36 @@ version_gen_environment.set('GIT_DATE', get_option('build_date'))
267267
version_gen_environment.set('GIT_USER_AGENT', get_option('user_agent'))
268268
version_gen_environment.set('GIT_VERSION', get_option('version'))
269269

270+
if get_option('optimization') in ['2', '3', 's', 'z']
271+
rust_target = 'release'
272+
rust_args = ['--release']
273+
rustflags = '-Aunused_imports -Adead_code'
274+
else
275+
rust_target = 'debug'
276+
rust_args = []
277+
rustflags = '-Aunused_imports -Adead_code -C debuginfo=2 -C opt-level=1 -C force-frame-pointers=yes'
278+
endif
279+
280+
281+
rust_leaf = custom_target('rust_leaf',
282+
output: 'libxdiff.a',
283+
build_by_default: true,
284+
build_always_stale: true,
285+
command: ['cargo', 'build',
286+
'--manifest-path', meson.project_source_root() / 'rust/Cargo.toml'
287+
] + rust_args,
288+
env: {
289+
'RUSTFLAGS': rustflags,
290+
},
291+
install: false,
292+
)
293+
294+
rust_xdiff_dep = declare_dependency(
295+
link_args: ['-L' + meson.project_source_root() / 'rust/target' / rust_target, '-lxdiff'],
296+
# include_directories: include_directories('xdiff/include'), # Adjust if you expose headers
297+
)
298+
299+
270300
compiler = meson.get_compiler('c')
271301

272302
libgit_sources = [
@@ -1677,6 +1707,8 @@ version_def_h = custom_target(
16771707
)
16781708
libgit_sources += version_def_h
16791709

1710+
libgit_dependencies += rust_xdiff_dep
1711+
16801712
libgit = declare_dependency(
16811713
link_with: static_library('git',
16821714
sources: libgit_sources,

rust/Cargo.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[workspace]
2+
members = [
3+
"xdiff",
4+
"interop",
5+
]
6+
resolver = "2"

rust/interop/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "interop"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
name = "interop"
8+
path = "src/lib.rs"
9+
## staticlib to generate xdiff.a for use by gcc
10+
## cdylib (optional) to generate xdiff.so for use by gcc
11+
## rlib is required by the rust unit tests
12+
crate-type = ["staticlib", "rlib"]
13+
14+
[dependencies]

rust/interop/src/lib.rs

Whitespace-only changes.

rust/xdiff/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "xdiff"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
name = "xdiff"
8+
path = "src/lib.rs"
9+
## staticlib to generate xdiff.a for use by gcc
10+
## cdylib (optional) to generate xdiff.so for use by gcc
11+
## rlib is required by the rust unit tests
12+
crate-type = ["staticlib", "rlib"]
13+
14+
[dependencies]
15+
interop = { path = "../interop" }

rust/xdiff/src/lib.rs

Whitespace-only changes.

0 commit comments

Comments
 (0)