fix: 修复darwin的cgo编译问题

This commit is contained in:
spiritlhl
2025-07-17 06:37:02 +00:00
parent e5129ab244
commit 50f6ef1f60
+23 -11
View File
@@ -158,18 +158,20 @@ jobs:
- goos: darwin
goarch: amd64
cgo_enabled: "1"
cc: clang
cc: "clang --target=x86_64-apple-darwin"
cflags: "-O2 -arch x86_64 -mmacosx-version-min=10.12"
ldflags: "-s -w"
runner: macos-latest
runner: ubuntu-latest
requires_osxcross: true
- goos: darwin
goarch: arm64
cgo_enabled: "1"
cc: clang
cc: "clang --target=aarch64-apple-darwin"
cflags: "-O2 -arch arm64 -mmacosx-version-min=11.0"
ldflags: "-s -w"
runner: macos-latest
runner: ubuntu-latest
requires_osxcross: true
- goos: linux
goarch: arm
@@ -238,7 +240,9 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GHT }}
# 只在非macOS环境安装交叉编译工具
- name: Install cross-compilation tools
if: matrix.runner != 'macos-latest'
run: |
sudo apt-get update || true
case "${{ matrix.goos }}-${{ matrix.goarch }}" in
@@ -256,12 +260,21 @@ jobs:
sudo apt-get install -y build-essential gcc-powerpc64le-linux-gnu ;;
windows-amd64|windows-386)
sudo apt-get install -y build-essential gcc-mingw-w64-x86-64 gcc-mingw-w64-i686 ;;
darwin-amd64|darwin-arm64)
echo "No need to do" ;;
*)
sudo apt-get install -y build-essential || true ;;
esac
# 需要支持CGO的Darwin交叉编译,需要安装osxcross
- name: Install osxcross for Darwin
if: matrix.requires_osxcross == true
run: |
git clone https://github.com/tpoechtrager/osxcross.git
cd osxcross
wget -nc https://github.com/joseluisq/macosx-sdks/releases/download/12.3/MacOSX12.3.sdk.tar.xz
mv MacOSX12.3.sdk.tar.xz tarballs/
UNATTENDED=yes OSX_VERSION_MIN=10.12 ./build.sh
export PATH="$PWD/target/bin:$PATH"
- name: Get latest tag
id: tag
run: |
@@ -280,7 +293,7 @@ jobs:
if [[ -n "${{ matrix.gomips }}" ]]; then
export GOMIPS=${{ matrix.gomips }}
fi
if [[ "${{ matrix.cgo_enabled }}" == "1" ]]; then
if [[ "${{ matrix.cgo_enabled }}" == "1" && "${{ matrix.runner }}" != "macos-latest" ]]; then
export CC="${{ matrix.cc }}"
export CGO_CFLAGS="${{ matrix.cflags }}"
if [[ "${{ matrix.goos }}" == "windows" ]]; then
@@ -296,14 +309,13 @@ jobs:
LDFLAGS="-s -w -X main.version=${{ steps.tag.outputs.version }} -X main.arch=${{ matrix.goarch }}"
if [[ "${{ matrix.cgo_enabled }}" == "1" ]]; then
LDFLAGS="${LDFLAGS} -checklinkname=0 ${{ matrix.ldflags }}"
echo "Building $BINARY_NAME for ${{ matrix.goos }}/${{ matrix.goarch }}"
echo "Building $BINARY_NAME for ${{ matrix.goos }}/${{ matrix.goarch }} with CGO"
CGO_ENABLED=1 go build -o bin/$BINARY_NAME -v -ldflags="$LDFLAGS" -trimpath ./
else
LDFLAGS="${LDFLAGS} -checklinkname=0 ${{ matrix.ldflags }}"
echo "Building $BINARY_NAME for ${{ matrix.goos }}/${{ matrix.goarch }}"
echo "Building $BINARY_NAME for ${{ matrix.goos }}/${{ matrix.goarch }} without CGO"
CGO_ENABLED=0 go build -o bin/$BINARY_NAME -v -ldflags="$LDFLAGS" -trimpath ./
fi
if [[ -f "bin/$BINARY_NAME" ]]; then
echo "Successfully built bin/$BINARY_NAME"
ls -la bin/$BINARY_NAME
@@ -434,4 +446,4 @@ jobs:
echo "goecs.sh not found, skipping version update"
fi
env:
GITHUB_TOKEN: ${{ secrets.GHT }}
GITHUB_TOKEN: ${{ secrets.GHT }}