0%

github action 支持 macOS arm 架构了

前言

之前苦于github action不支持macOS arm架构,导致我为了打gopped macos包煞费苦心,我在 linux 上用xgo交叉编译出来macos arm64的动态库,然后在macOS x64上再打包成macos amd64的动态库,最后把两个架构的动态库合并成一个universal binary包,来同时支持IntelApple Silicon架构,如今github action终于支持macOS arm架构了,这下可以省去很多麻烦了。

开始改造

gopeed 目前的构建依赖图是这样的:

可以看到需要先编译出macos arm64的动态库,之前这里是用xgo交叉编译的,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
build-macos-arm64-lib:
if: ${{ github.event.inputs.platform == 'all' || github.event.inputs.platform == 'macos' }}
runs-on: ubuntu-latest
needs: [get-release]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}
- name: Build
env:
VERSION: ${{ needs.get-release.outputs.version }}
run: |
go install src.techknowlogick.com/xgo@latest
xgo -go go-$GO_VERSION.x --targets=darwin/arm64 -tags="nosqlite" -ldflags="-w -s -X github.com/GopeedLab/gopeed/pkg/base.Version=$VERSION" -buildmode=c-shared -pkg=bind/desktop -out=libgopeed .
mv libgopeed-*.dylib libgopeed.dylib
- uses: actions/upload-artifact@v3
with:
name: macos-arm64-lib
path: libgopeed.dylib

现在有了macOS arm架构的支持,我们可以直接用macos-latest-arm来构建macos arm64的动态库,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
build-macos-arm64-lib:
if: ${{ github.event.inputs.platform == 'all' || github.event.inputs.platform == 'macos' }}
runs-on: macos-latest
needs: [get-release]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}
- name: Build
env:
VERSION: ${{ needs.get-release.outputs.version }}
run: |
go build -tags nosqlite -ldflags="-w -s -X github.com/GopeedLab/gopeed/pkg/base.Version=$VERSION" -buildmode=c-shared -o libgopeed.dylib github.com/GopeedLab/gopeed/bind/desktop
- uses: actions/upload-artifact@v3
with:
name: macos-arm64-lib
path: libgopeed.dylib

这样一下简单了很多,而且节省了大量的构建时间,因为xgo这货要拉个很大的镜像,然后再编译,速度真的很慢,目前基本上是从 3 分钟缩短到了 30 秒,非常不错。


我是MonkeyWie,欢迎扫码👇👇关注!不定期在公众号中分享JAVAGolang前端dockerk8s等干货知识。

如果觉得本文对您有帮助,可以请我喝一杯咖啡☕