如何在Windows上进行python项目(fastapi框架)交叉编译,编译linux-amd64,linux-arm64,mac多平台二进制文件?
我试过在github action编译,但是无法编译linux-ARM64的软件包,只能编译linux-amd64的。
我的需求:
1,github action如何编译linux-arm64的二进制程序?
2,在Windows上安装docker desktop软件,如何在这个软件内进行编译多平台程序?【生产环境代码不变上传github】
以下是github action编译的arm64,但是运行发现仍然是amd64的:
[root@lnas552f ~]# ./fastapi_app
-sh: ./fastapi_app: cannot execute binary file: Exec format error
[root@lnas552f ~]# file ./fastapi_app
./fastapi_app: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=aa5cd8892ec75e94945edf5de21c0673ae51ac17, for GNU/Linux 2.6.32, stripped
我的action workflow.yml如下:
name: Build and Release ARM64 Binary
on:
push:
tags:
- v*
jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Python 3.11
uses: actions/setup-python@v3
with:
python-version: "3.11"
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y gcc-aarch64-linux-gnu
pip install fastapi pyinstaller pywebview uvicorn
- name: Build ARM64 executable
env:
CFLAGS: "-march=armv8-a"
CC: aarch64-linux-gnu-gcc # 指定交叉编译器
run: |
pyinstaller --onefile --name fastapi_app app.py
file ./dist/fastapi_app # 确认文件类型
- name: Create release if needed
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: "Release ${{ github.ref }}"
body: "Release for Linux ARM64"
draft: false
prerelease: false
- name: Upload ARM64 binary to release
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./dist/fastapi_app
asset_name: fastapi_app-arm64
asset_content_type: application/octet-stream
编译多平台二进制文件通常有两种方案:
github actions将在年底推出面向公共项目的 aarch64 的 runner 实例,到时候方案二会更方便 (目前只有组织用户和企业用户可用,如果你是这两种用户,那么你现在就可以采用这种方案,而不需要下面的qemu方案)。不过目前你可以考虑采用
qemu
模拟 CPU 指令的方案实现类多平台编译的效果。正好,docker就支持 qemu。利用 docker 的 cross platform 特性 (基于 Qemu 实现)就可以轻松实现一套编译脚本支持多平台构建了,一个示例可以参考我的github action,利用此特性构建多平台安装包: https://github.com/abcfy2/docker-postgresql-dev-packages