Skip to contentSkip to conversation

rust: respect CARGO_BUILD_TARGET when locating build output

PATCH Superseded James Le Cuirot 2 participants 5 messages Sep 8 – Sep 10

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: respect CARGO_BUILD_TARGET when locating build output

When cross-compiling, Cargo always writes to a target-tuple subdirectory determined by CARGO_BUILD_TARGET, even when it matches the native tuple. The script looked in $BUILD_DIR/$BUILD_TYPE directly, so it failed to find (and copy) the freshly built library.

Respect CARGO_BUILD_TARGET in the output path so the correct artifact is located.

---
 src/cargo-meson.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/cargo-meson.sh +2 -2
index 75f3cd1..3a558aa 100755
@@ -38,7 +38,7 @@ then
 	exit $RET
 fi
 
-if ! cmp "$BUILD_DIR/$BUILD_TYPE/$LIBNAME" "$BUILD_DIR/libgitcore.a" >/dev/null 2>&1
+if ! cmp "$BUILD_DIR/${CARGO_BUILD_TARGET-}/$BUILD_TYPE/$LIBNAME" "$BUILD_DIR/libgitcore.a" >/dev/null 2>&1
 then
-	cp "$BUILD_DIR/$BUILD_TYPE/$LIBNAME" "$BUILD_DIR/libgitcore.a"
+	cp "$BUILD_DIR/${CARGO_BUILD_TARGET-}/$BUILD_TYPE/$LIBNAME" "$BUILD_DIR/libgitcore.a"
 fi
Signature · 1 line
2.55.0
#1 lore raw
Quoted 9 lines · James Le Cuirot

Subject: Re: [PATCH] rust: respect CARGO_BUILD_TARGET when locating build output

When cross-compiling, Cargo always writes to a target-tuple subdirectory determined by CARGO_BUILD_TARGET, even when it matches the native tuple. The script looked in $BUILD_DIR/$BUILD_TYPE directly, so it failed to find (and copy) the freshly built library.

Respect CARGO_BUILD_TARGET in the output path so the correct artifact is located.

Nowhere in the above description I see mention of meson, but the patch is only to cargo-meson that is referenced by src/meson.build which invites a few questions:

* Does "make" work fine without any change similar to this?

* Shouldn't the commit title say "meson" somewhere if this change is only for "meson" driven build?

Thanks.

Quoted 20 lines
---
 src/cargo-meson.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/cargo-meson.sh +2 -2
index 75f3cd1..3a558aa 100755
@@ -38,7 +38,7 @@ then
 	exit $RET
 fi
 
-if ! cmp "$BUILD_DIR/$BUILD_TYPE/$LIBNAME" "$BUILD_DIR/libgitcore.a" >/dev/null 2>&1
+if ! cmp "$BUILD_DIR/${CARGO_BUILD_TARGET-}/$BUILD_TYPE/$LIBNAME" "$BUILD_DIR/libgitcore.a" >/dev/null 2>&1
 then
-	cp "$BUILD_DIR/$BUILD_TYPE/$LIBNAME" "$BUILD_DIR/libgitcore.a"
+	cp "$BUILD_DIR/${CARGO_BUILD_TARGET-}/$BUILD_TYPE/$LIBNAME" "$BUILD_DIR/libgitcore.a"
 fi
#2 lore raw

PATCH v2 1/1 rust: respect CARGO_BUILD_TARGET when locating build output

When cross-compiling, Cargo always writes to a target-tuple subdirectory determined by CARGO_BUILD_TARGET, even when it matches the native tuple. The build looked in $BUILD_DIR/$BUILD_TYPE directly, so it failed to locate the freshly built library.

Respect CARGO_BUILD_TARGET in the output path so the correct artifact is located.

---

Quoted 8 lines

Nowhere in the above description I see mention of meson, but the patch is only to cargo-meson that is referenced by src/meson.build which invites a few questions:

* Does "make" work fine without any change similar to this?

* Shouldn't the commit title say "meson" somewhere if this change is only for "meson" driven build?

Good point. I hadn't realised that Make is still supported. I have now amended the Makefile and tested both the native and cross cases.

 Makefile           | 4 ++--
 src/cargo-meson.sh | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
Makefile +2 -2
index 1cec251..5cd5889 100644
@@ -940,9 +940,9 @@ TEST_SHELL_PATH = $(SHELL_PATH)
 LIB_FILE = libgit.a
 
 ifdef DEBUG
-RUST_TARGET_DIR = target/debug
+RUST_TARGET_DIR = target/$(CARGO_BUILD_TARGET)/debug
 else
-RUST_TARGET_DIR = target/release
+RUST_TARGET_DIR = target/$(CARGO_BUILD_TARGET)/release
 endif
 
 ifeq ($(uname_S),Windows)
src/cargo-meson.sh +2 -2
index 75f3cd1..3a558aa 100755
@@ -38,7 +38,7 @@ then
 	exit $RET
 fi
 
-if ! cmp "$BUILD_DIR/$BUILD_TYPE/$LIBNAME" "$BUILD_DIR/libgitcore.a" >/dev/null 2>&1
+if ! cmp "$BUILD_DIR/${CARGO_BUILD_TARGET-}/$BUILD_TYPE/$LIBNAME" "$BUILD_DIR/libgitcore.a" >/dev/null 2>&1
 then
-	cp "$BUILD_DIR/$BUILD_TYPE/$LIBNAME" "$BUILD_DIR/libgitcore.a"
+	cp "$BUILD_DIR/${CARGO_BUILD_TARGET-}/$BUILD_TYPE/$LIBNAME" "$BUILD_DIR/libgitcore.a"
 fi

--
2.55.0

#3 lore raw
Quoted 32 lines · James Le Cuirot

When cross-compiling, Cargo always writes to a target-tuple subdirectory determined by CARGO_BUILD_TARGET, even when it matches the native tuple. The build looked in $BUILD_DIR/$BUILD_TYPE directly, so it failed to locate the freshly built library.

Respect CARGO_BUILD_TARGET in the output path so the correct artifact is located.

---

Quoted 8 lines

Nowhere in the above description I see mention of meson, but the patch is only to cargo-meson that is referenced by src/meson.build which invites a few questions:

* Does "make" work fine without any change similar to this?

* Shouldn't the commit title say "meson" somewhere if this change is only for "meson" driven build?

Good point. I hadn't realised that Make is still supported. I have now amended the Makefile and tested both the native and cross cases.

 Makefile           | 4 ++--
 src/cargo-meson.sh | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
Makefile +0 -0
index 1cec251..5cd5889 100644

Makefile TEST_SHELL_PATH = $(SHELL_PATH) · line 946

Quoted 9 lines
Makefile +2 -2
@@ -940,9 +940,9 @@ TEST_SHELL_PATH = $(SHELL_PATH)
 LIB_FILE = libgit.a
 
 ifdef DEBUG
-RUST_TARGET_DIR = target/debug
+RUST_TARGET_DIR = target/$(CARGO_BUILD_TARGET)/debug
 else
-RUST_TARGET_DIR = target/release
+RUST_TARGET_DIR = target/$(CARGO_BUILD_TARGET)/release
 endif

This seems to be based on a bit stale codebase. Specifically you would want to build on top of post-924dfced6b (Makefile: support universal macOS builds via RUST_TARGETS, 2026-07-08) version of Makefile.

src/cargo-meson.sh then · line 41

Quoted 12 lines
Makefile +0 -0
 ifeq ($(uname_S),Windows)
src/cargo-meson.sh +1 -1
index 75f3cd1..3a558aa 100755
@@ -38,7 +38,7 @@ then
 	exit $RET
 fi
 
-if ! cmp "$BUILD_DIR/$BUILD_TYPE/$LIBNAME" "$BUILD_DIR/libgitcore.a" >/dev/null 2>&1
+if ! cmp "$BUILD_DIR/${CARGO_BUILD_TARGET-}/$BUILD_TYPE/$LIBNAME" "$BUILD_DIR/libgitcore.a" >/dev/null 2>&1

When CARGO_BUILD_TARGET is not defined, this leaves double slashes in the resujlting pathname, which may not be incorrect per-se, but still is not what you meant to say, which is probably a lot closer to

    ${CARGO_BUILD_TARGET+$CARGO_BUILD_TARGET/}
Quoted 6 lines
src/cargo-meson.sh +1 -2
 then
-	cp "$BUILD_DIR/$BUILD_TYPE/$LIBNAME" "$BUILD_DIR/libgitcore.a"
+	cp "$BUILD_DIR/${CARGO_BUILD_TARGET-}/$BUILD_TYPE/$LIBNAME" "$BUILD_DIR/libgitcore.a"
 fi
--

2.55.0

#4 lore raw

PATCH v3 1/1 rust: respect CARGO_BUILD_TARGET when locating build output

When cross-compiling, Cargo always writes to a target-tuple subdirectory determined by CARGO_BUILD_TARGET, even when it matches the native tuple. The build looked in $BUILD_DIR/$BUILD_TYPE directly, so it failed to locate the freshly built library.

Respect CARGO_BUILD_TARGET in the output path so the correct artifact is located.

---

Quoted 4 lines

This seems to be based on a bit stale codebase. Specifically you would want to build on top of post-924dfced6b (Makefile: support universal macOS builds via RUST_TARGETS, 2026-07-08) version of Makefile.

Apologies, I had used the maint branch. Now rebased on master.

Quoted 6 lines

When CARGO_BUILD_TARGET is not defined, this leaves double slashes in the resulting pathname, which may not be incorrect per-se, but still is not what you meant to say, which is probably a lot closer to

    ${CARGO_BUILD_TARGET+$CARGO_BUILD_TARGET/}

I believed this would not cause an issue, so I intentionally went with simplicity over correctness. It's not a big difference though, so this revision goes with correctness.

 Makefile           | 2 +-
 src/cargo-meson.sh | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
Makefile +1 -1
index d4b7759..f0ca2e4 100644
@@ -959,7 +959,7 @@ RUST_LIB_NAME = gitcore.lib
 else
 RUST_LIB_NAME = libgitcore.a
 endif
-RUST_LIB = target/$(RUST_BUILD_CONFIG)/$(RUST_LIB_NAME)
+RUST_LIB = target/$(if $(CARGO_BUILD_TARGET),$(CARGO_BUILD_TARGET)/)$(RUST_BUILD_CONFIG)/$(RUST_LIB_NAME)
 endif
 
 GITLIBS = common-main.o $(LIB_FILE)
src/cargo-meson.sh +2 -2
index 75f3cd1..83c7e7b 100755
@@ -38,7 +38,7 @@ then
 	exit $RET
 fi
 
-if ! cmp "$BUILD_DIR/$BUILD_TYPE/$LIBNAME" "$BUILD_DIR/libgitcore.a" >/dev/null 2>&1
+if ! cmp "$BUILD_DIR/${CARGO_BUILD_TARGET:+$CARGO_BUILD_TARGET/}$BUILD_TYPE/$LIBNAME" "$BUILD_DIR/libgitcore.a" >/dev/null 2>&1
 then
-	cp "$BUILD_DIR/$BUILD_TYPE/$LIBNAME" "$BUILD_DIR/libgitcore.a"
+	cp "$BUILD_DIR/${CARGO_BUILD_TARGET:+$CARGO_BUILD_TARGET/}$BUILD_TYPE/$LIBNAME" "$BUILD_DIR/libgitcore.a"
 fi

--
2.55.0