Makefile: introduce infrastructure to build internal Rust library

Introduce infrastructure to build the internal Rust library. This mirrors the infrastructure we have added to Meson in the preceding commit. Developers can enable the infrastructure by passing the new `WITH_RUST` build toggle. Inspired-by: Ezekiel Newren <ezekielnewren@gmail.com> Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Oct 2, 2025 at 09:29 UTC e30c081c6af4963418184dbcd5df37322032f9dc
3 files changed +40
.gitignore
+2
@@ -1,4 +1,6 @@
1 /fuzz_corpora
2 +/target/
3 +/Cargo.lock
4 /GIT-BUILD-DIR
5 /GIT-BUILD-OPTIONS
6 /GIT-CFLAGS
Makefile
+37
@@ -483,6 +483,14 @@ include shared.mak
483 # Define LIBPCREDIR=/foo/bar if your PCRE header and library files are
484 # in /foo/bar/include and /foo/bar/lib directories.
485 #
486 +# == Optional Rust support ==
487 +#
488 +# Define WITH_RUST if you want to include features and subsystems written in
489 +# Rust into Git. For now, Rust is still an optional feature of the build
490 +# process. With Git 3.0 though, Rust will always be enabled.
491 +#
492 +# Building Rust code requires Cargo.
493 +#
494 # == SHA-1 and SHA-256 defines ==
495 #
496 # === SHA-1 backend ===
@@ -683,6 +691,7 @@ OBJECTS =
691 OTHER_PROGRAMS =
692 PROGRAM_OBJS =
693 PROGRAMS =
694 +RUST_SOURCES =
695 EXCLUDED_PROGRAMS =
696 SCRIPT_PERL =
697 SCRIPT_PYTHON =
@@ -918,6 +927,11 @@ TEST_SHELL_PATH = $(SHELL_PATH)
927 LIB_FILE = libgit.a
928 XDIFF_LIB = xdiff/lib.a
929 REFTABLE_LIB = reftable/libreftable.a
930 +ifdef DEBUG
931 +RUST_LIB = target/debug/libgitcore.a
932 +else
933 +RUST_LIB = target/release/libgitcore.a
934 +endif
935
936 # xdiff and reftable libs may in turn depend on what is in libgit.a
937 GITLIBS = common-main.o $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB) $(LIB_FILE)
@@ -943,6 +957,15 @@ BASIC_LDFLAGS =
957 ARFLAGS = rcs
958 PTHREAD_CFLAGS =
959
960 +# Rust flags
961 +CARGO_ARGS =
962 +ifndef V
963 +CARGO_ARGS += --quiet
964 +endif
965 +ifndef DEBUG
966 +CARGO_ARGS += --release
967 +endif
968 +
969 # For the 'sparse' target
970 SPARSE_FLAGS ?= -std=gnu99 -D__STDC_NO_VLA__
971 SP_EXTRA_FLAGS =
@@ -1475,6 +1498,8 @@ CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o
1498
1499 UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o
1500
1501 +RUST_SOURCES += src/lib.rs
1502 +
1503 GIT-VERSION-FILE: FORCE
1504 @OLD=$$(cat $@ 2>/dev/null || :) && \
1505 $(call version_gen,"$(shell pwd)",GIT-VERSION-FILE.in,$@) && \
@@ -1504,6 +1529,11 @@ endif
1529 ALL_CFLAGS = $(DEVELOPER_CFLAGS) $(CPPFLAGS) $(CFLAGS) $(CFLAGS_APPEND)
1530 ALL_LDFLAGS = $(LDFLAGS) $(LDFLAGS_APPEND)
1531
1532 +ifdef WITH_RUST
1533 +BASIC_CFLAGS += -DWITH_RUST
1534 +GITLIBS += $(RUST_LIB)
1535 +endif
1536 +
1537 ifdef SANITIZE
1538 SANITIZERS := $(foreach flag,$(subst $(comma),$(space),$(SANITIZE)),$(flag))
1539 BASIC_CFLAGS += -fsanitize=$(SANITIZE) -fno-sanitize-recover=$(SANITIZE)
@@ -2918,6 +2948,12 @@ scalar$X: scalar.o GIT-LDFLAGS $(GITLIBS)
2948 $(LIB_FILE): $(LIB_OBJS)
2949 $(QUIET_AR)$(RM) $@ && $(AR) $(ARFLAGS) $@ $^
2950
2951 +$(RUST_LIB): Cargo.toml $(RUST_SOURCES)
2952 + $(QUIET_CARGO)cargo build $(CARGO_ARGS)
2953 +
2954 +.PHONY: rust
2955 +rust: $(RUST_LIB)
2956 +
2957 $(XDIFF_LIB): $(XDIFF_OBJS)
2958 $(QUIET_AR)$(RM) $@ && $(AR) $(ARFLAGS) $@ $^
2959
@@ -3768,6 +3804,7 @@ clean: profile-clean coverage-clean cocciclean
3804 $(RM) $(FUZZ_PROGRAMS)
3805 $(RM) $(SP_OBJ)
3806 $(RM) $(HCC)
3807 + $(RM) -r Cargo.lock target/
3808 $(RM) version-def.h
3809 $(RM) -r $(dep_dirs) $(compdb_dir) compile_commands.json
3810 $(RM) $(test_bindir_programs)
shared.mak
+1
@@ -56,6 +56,7 @@ ifndef V
56 QUIET_MKDIR_P_PARENT = @echo ' ' MKDIR -p $(@D);
57
58 ## Used in "Makefile"
59 + QUIET_CARGO = @echo ' ' CARGO $@;
60 QUIET_CC = @echo ' ' CC $@;
61 QUIET_AR = @echo ' ' AR $@;
62 QUIET_LINK = @echo ' ' LINK $@;