From 962261ba12fbeacd58a1faab54f450e23aae5b24 Mon Sep 17 00:00:00 2001 From: Yuyi-Oak <1722157266@qq.com> Date: Thu, 16 Jul 2026 07:46:37 -0700 Subject: [PATCH] =?UTF-8?q?fix(ffi):=20=E7=BB=9F=E4=B8=80=20cgo=20?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=E4=B8=8E=20release=20=E6=9E=84=E5=BB=BA=20pr?= =?UTF-8?q?ofile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit internal/ffi/ffi.go 的 cgo LDFLAGS 硬编码 -L target/debug,而 build-rust 用 --release(产物落 target/release),干净环境下 make build/test-go/ci 必然 报 cannot find -lbat_ffi。 - LDFLAGS 指向 target/release,并加 -Wl,-rpath 让产物运行时无需 LD_LIBRARY_PATH 即可定位 libbat_ffi.so;删除指向不存在目录的无效 CFLAGS -I(C 声明本就内联)。 - 新增 build-ffi target(cargo build --release -p bat-ffi),build-go 与 test-go 依赖它,确保链接前 release FFI 库已存在。 验证:make build-go 成功产出并可运行 bin/bat;make test-go 链接并通过; go vet(check-go)通过。 对应 issue #18 维护清单 1-6。 Co-Authored-By: Claude Fable 5 --- Makefile | 10 +++++++--- internal/ffi/ffi.go | 6 ++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index d1dcf74..b76d169 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: help build test clean check fmt lint install dev docker-build docker-up docker-down official-smoke +.PHONY: help build build-ffi test clean check fmt lint install dev docker-build docker-up docker-down official-smoke # 默认目标 .DEFAULT_GOAL := help @@ -24,7 +24,11 @@ build-rust: ## 构建 Rust 组件 @echo "$(BLUE)Building Rust workspace...$(NC)" cargo build --release --workspace -build-go: ## 构建 Go 组件 +build-ffi: ## 构建 bat-ffi release 库(cgo 链接依赖) + @echo "$(BLUE)Building bat-ffi (release)...$(NC)" + cargo build --release -p bat-ffi + +build-go: build-ffi ## 构建 Go 组件 @echo "$(BLUE)Building Go CLI...$(NC)" @if [ -f cmd/bat/main.go ]; then \ go build -o bin/bat ./cmd/bat; \ @@ -50,7 +54,7 @@ test-rust: ## 运行 Rust 测试 @echo "$(BLUE)Running Rust tests...$(NC)" cargo test --workspace -test-go: ## 运行 Go 测试 +test-go: build-ffi ## 运行 Go 测试 @echo "$(BLUE)Running Go tests...$(NC)" @if [ -n "$$(go list ./... 2>/dev/null)" ]; then \ go test -v ./...; \ diff --git a/internal/ffi/ffi.go b/internal/ffi/ffi.go index f530b65..e299367 100644 --- a/internal/ffi/ffi.go +++ b/internal/ffi/ffi.go @@ -6,8 +6,10 @@ package ffi /* -#cgo CFLAGS: -I${SRCDIR}/../../target/bat-ffi -#cgo LDFLAGS: -L${SRCDIR}/../../target/debug -lbat_ffi +// Links against the release bat-ffi library built by `cargo build --release -p bat-ffi` +// (see the `build-ffi` Make target). The rpath lets the resulting binary locate +// libbat_ffi.so at runtime without setting LD_LIBRARY_PATH during local development. +#cgo LDFLAGS: -L${SRCDIR}/../../target/release -lbat_ffi -Wl,-rpath,${SRCDIR}/../../target/release #include typedef char* ccharp;