fix(ffi): 统一 cgo 链接与 release 构建 profile

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 <noreply@anthropic.com>
This commit is contained in:
2026-07-16 07:46:37 -07:00
co-authored by Claude Fable 5
parent 1c4debbaa7
commit 962261ba12
2 changed files with 11 additions and 5 deletions
+7 -3
View File
@@ -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 .DEFAULT_GOAL := help
@@ -24,7 +24,11 @@ build-rust: ## 构建 Rust 组件
@echo "$(BLUE)Building Rust workspace...$(NC)" @echo "$(BLUE)Building Rust workspace...$(NC)"
cargo build --release --workspace 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)" @echo "$(BLUE)Building Go CLI...$(NC)"
@if [ -f cmd/bat/main.go ]; then \ @if [ -f cmd/bat/main.go ]; then \
go build -o bin/bat ./cmd/bat; \ go build -o bin/bat ./cmd/bat; \
@@ -50,7 +54,7 @@ test-rust: ## 运行 Rust 测试
@echo "$(BLUE)Running Rust tests...$(NC)" @echo "$(BLUE)Running Rust tests...$(NC)"
cargo test --workspace cargo test --workspace
test-go: ## 运行 Go 测试 test-go: build-ffi ## 运行 Go 测试
@echo "$(BLUE)Running Go tests...$(NC)" @echo "$(BLUE)Running Go tests...$(NC)"
@if [ -n "$$(go list ./... 2>/dev/null)" ]; then \ @if [ -n "$$(go list ./... 2>/dev/null)" ]; then \
go test -v ./...; \ go test -v ./...; \
+4 -2
View File
@@ -6,8 +6,10 @@
package ffi package ffi
/* /*
#cgo CFLAGS: -I${SRCDIR}/../../target/bat-ffi // Links against the release bat-ffi library built by `cargo build --release -p bat-ffi`
#cgo LDFLAGS: -L${SRCDIR}/../../target/debug -lbat_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 <stdlib.h> #include <stdlib.h>
typedef char* ccharp; typedef char* ccharp;