Skip to content

Build dd Static Binaries #11

Build dd Static Binaries

Build dd Static Binaries #11

Workflow file for this run

name: Build dd Static Binaries
on:
workflow_dispatch:
jobs:
build:
strategy:
matrix:
include:
# Linux builds - 主要架构
- os: ubuntu-latest
target: linux-amd64
goos: linux
goarch: amd64
- os: ubuntu-latest
target: linux-386
goos: linux
goarch: 386
- os: ubuntu-latest
target: linux-arm64
goos: linux
goarch: arm64
- os: ubuntu-latest
target: linux-armv7
goos: linux
goarch: arm
goarm: 7
# Linux builds - 特殊架构
- os: ubuntu-latest
target: linux-riscv64
goos: linux
goarch: riscv64
- os: ubuntu-latest
target: linux-mips64
goos: linux
goarch: mips64
- os: ubuntu-latest
target: linux-mips64le
goos: linux
goarch: mips64le
- os: ubuntu-latest
target: linux-mips
goos: linux
goarch: mips
- os: ubuntu-latest
target: linux-mipsle
goos: linux
goarch: mipsle
- os: ubuntu-latest
target: linux-ppc64
goos: linux
goarch: ppc64
- os: ubuntu-latest
target: linux-ppc64le
goos: linux
goarch: ppc64le
# macOS builds
- os: macos-latest
target: darwin-amd64
goos: darwin
goarch: amd64
- os: macos-latest
target: darwin-arm64
goos: darwin
goarch: arm64
# FreeBSD builds
- os: ubuntu-latest
target: freebsd-amd64
goos: freebsd
goarch: amd64
- os: ubuntu-latest
target: freebsd-arm64
goos: freebsd
goarch: arm64
# OpenBSD builds
- os: ubuntu-latest
target: openbsd-amd64
goos: openbsd
goarch: amd64
- os: ubuntu-latest
target: openbsd-arm64
goos: openbsd
goarch: arm64
fail-fast: false # 确保即使某个任务失败,其他任务仍然继续
runs-on: ${{ matrix.os }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup coreutils build environment (Linux)
if: runner.os == 'Linux'
run: |
sudo apt-get update
sudo apt-get install -y build-essential autoconf automake libtool gettext texinfo bison
# 安装交叉编译工具链
if [[ "${{ matrix.target }}" != "linux-amd64" ]]; then
echo "Installing cross-compilation toolchains for ${{ matrix.target }}"
if [[ "${{ matrix.target }}" == "linux-386" ]]; then
sudo apt-get install -y gcc-multilib
elif [[ "${{ matrix.target }}" == "linux-arm64" ]]; then
sudo apt-get install -y gcc-aarch64-linux-gnu
elif [[ "${{ matrix.target }}" == "linux-armv7" ]]; then
sudo apt-get install -y gcc-arm-linux-gnueabihf
elif [[ "${{ matrix.target }}" == "linux-riscv64" ]]; then
sudo apt-get install -y gcc-riscv64-linux-gnu
elif [[ "${{ matrix.target }}" == "linux-mips64" ]]; then
sudo apt-get install -y gcc-mips64-linux-gnuabi64
elif [[ "${{ matrix.target }}" == "linux-mips64le" ]]; then
sudo apt-get install -y gcc-mips64el-linux-gnuabi64
elif [[ "${{ matrix.target }}" == "linux-mips" ]]; then
sudo apt-get install -y gcc-mips-linux-gnu
elif [[ "${{ matrix.target }}" == "linux-mipsle" ]]; then
sudo apt-get install -y gcc-mipsel-linux-gnu
elif [[ "${{ matrix.target }}" == "linux-ppc64" ]]; then
sudo apt-get install -y gcc-powerpc64-linux-gnu
elif [[ "${{ matrix.target }}" == "linux-ppc64le" ]]; then
sudo apt-get install -y gcc-powerpc64le-linux-gnu
fi
fi
- name: Setup coreutils build environment (macOS)
if: runner.os == 'macOS'
run: |
brew install autoconf automake libtool gettext texinfo bison
brew link gettext --force
- name: Download and extract coreutils source
run: |
curl -L -o coreutils.tar.gz https://ftp.gnu.org/gnu/coreutils/coreutils-9.4.tar.gz
tar -xzf coreutils.tar.gz
mv coreutils-9.4 coreutils-src
shell: bash
- name: Build dd static binary (Linux - amd64)
if: matrix.target == 'linux-amd64'
run: |
cd coreutils-src
export FORCE_UNSAFE_CONFIGURE=1
export CFLAGS="-O2 -march=x86-64 -mtune=generic"
./configure --disable-native
# 确保完整运行 make 以生成 configmake.h
make CFLAGS="$CFLAGS"
# 修改 Makefile 添加静态链接
sed -i "s/^LDFLAGS =/LDFLAGS = -static/g" ./Makefile
# 重新编译 dd
make src/dd CFLAGS="$CFLAGS" LDFLAGS="$LDFLAGS"
strip src/dd
cp src/dd ../bin/dd-${{ matrix.goos }}-${{ matrix.goarch }}
ls -lah src/dd
file src/dd
continue-on-error: true
- name: Build dd static binary (Linux - 386)
if: matrix.target == 'linux-386'
run: |
cd coreutils-src
export FORCE_UNSAFE_CONFIGURE=1
# 添加 TIME_T_32_BIT_OK=yes 来解决 time_t 问题
TIME_T_32_BIT_OK=yes CFLAGS="-m32" LDFLAGS="-m32" ./configure --disable-native
# 确保完整运行 make 以生成 configmake.h
make
# 修改 Makefile 添加静态链接
sed -i "s/^LDFLAGS = -m32/LDFLAGS = -m32 -static/g" ./Makefile
# 重新编译 dd
make src/dd
strip src/dd
cp src/dd ../bin/dd-${{ matrix.goos }}-${{ matrix.goarch }}
ls -lah src/dd
file src/dd
continue-on-error: true
- name: Build dd static binary (Linux - ARM64)
if: matrix.target == 'linux-arm64'
run: |
cd coreutils-src
export FORCE_UNSAFE_CONFIGURE=1
CC=aarch64-linux-gnu-gcc ./configure --host=aarch64-linux-gnu --disable-native
# 确保完整运行 make 以生成 configmake.h
make
# 修改 Makefile 添加静态链接
sed -i "s/^LDFLAGS =/LDFLAGS = -static/g" ./Makefile
# 重新编译 dd
make src/dd
aarch64-linux-gnu-strip src/dd
cp src/dd ../bin/dd-${{ matrix.goos }}-${{ matrix.goarch }}
ls -lah src/dd
file src/dd
continue-on-error: true
- name: Build dd static binary (Linux - ARMv7)
if: matrix.target == 'linux-armv7'
run: |
cd coreutils-src
export FORCE_UNSAFE_CONFIGURE=1
CC=arm-linux-gnueabihf-gcc ./configure --host=arm-linux-gnueabihf --disable-native
# 确保完整运行 make 以生成 configmake.h
make
# 修改 Makefile 添加静态链接
sed -i "s/^LDFLAGS =/LDFLAGS = -static/g" ./Makefile
# 重新编译 dd
make src/dd
arm-linux-gnueabihf-strip src/dd
cp src/dd ../bin/dd-${{ matrix.goos }}-${{ matrix.goarch }}v${{ matrix.goarm }}
ls -lah src/dd
file src/dd
continue-on-error: true
- name: Build dd static binary (Linux - Special Architectures)
if: startsWith(matrix.target, 'linux-') && !contains(matrix.target, 'amd64') && !contains(matrix.target, '386') && !contains(matrix.target, 'arm')
run: |
cd coreutils-src
export FORCE_UNSAFE_CONFIGURE=1
# 选择合适的交叉编译器和主机类型
if [[ "${{ matrix.target }}" == "linux-riscv64" ]]; then
CROSS_CC=riscv64-linux-gnu-gcc
HOST=riscv64-linux-gnu
STRIP=riscv64-linux-gnu-strip
elif [[ "${{ matrix.target }}" == "linux-mips64" ]]; then
CROSS_CC=mips64-linux-gnuabi64-gcc
HOST=mips64-linux-gnuabi64
STRIP=mips64-linux-gnuabi64-strip
elif [[ "${{ matrix.target }}" == "linux-mips64le" ]]; then
CROSS_CC=mips64el-linux-gnuabi64-gcc
HOST=mips64el-linux-gnuabi64
STRIP=mips64el-linux-gnuabi64-strip
elif [[ "${{ matrix.target }}" == "linux-mips" ]]; then
CROSS_CC=mips-linux-gnu-gcc
HOST=mips-linux-gnu
STRIP=mips-linux-gnu-strip
elif [[ "${{ matrix.target }}" == "linux-mipsle" ]]; then
CROSS_CC=mipsel-linux-gnu-gcc
HOST=mipsel-linux-gnu
STRIP=mipsel-linux-gnu-strip
elif [[ "${{ matrix.target }}" == "linux-ppc64" ]]; then
CROSS_CC=powerpc64-linux-gnu-gcc
HOST=powerpc64-linux-gnu
STRIP=powerpc64-linux-gnu-strip
elif [[ "${{ matrix.target }}" == "linux-ppc64le" ]]; then
CROSS_CC=powerpc64le-linux-gnu-gcc
HOST=powerpc64le-linux-gnu
STRIP=powerpc64le-linux-gnu-strip
fi
CC=${CROSS_CC} ./configure --host=${HOST} --disable-native
# 确保完整运行 make 以生成 configmake.h
make
# 修改 Makefile 添加静态链接
sed -i "s/^LDFLAGS =/LDFLAGS = -static/g" ./Makefile
# 重新编译 dd
make src/dd
${STRIP} src/dd
cp src/dd ../bin/dd-${{ matrix.goos }}-${{ matrix.goarch }}
ls -lah src/dd
file src/dd
continue-on-error: true
- name: Build dd binary (macOS)
if: startsWith(matrix.target, 'darwin-')
run: |
cd coreutils-src
export FORCE_UNSAFE_CONFIGURE=1
./configure --disable-native
# 确保完整运行 make 以生成 configmake.h
make
# macOS 不支持完全静态链接,使用标准编译
strip src/dd
cp src/dd ../bin/dd-${{ matrix.goos }}-${{ matrix.goarch }}
ls -lah src/dd
file src/dd
continue-on-error: true
- name: Build dd static binary (FreeBSD/OpenBSD)
if: startsWith(matrix.target, 'freebsd-') || startsWith(matrix.target, 'openbsd-')
run: |
cd coreutils-src
export FORCE_UNSAFE_CONFIGURE=1
# 安装必要的BSD交叉编译工具
echo "Installing cross-compilation toolchains for ${{ matrix.target }}"
sudo apt-get update
sudo apt-get install -y clang lld llvm
# 配置编译环境变量
if [[ "${{ matrix.target }}" == *"-amd64" ]]; then
ARCH_FLAGS="-m64"
elif [[ "${{ matrix.target }}" == *"-arm64" ]]; then
ARCH_FLAGS="--target=aarch64-unknown-${MATRIX_GOOS}"
fi
# 配置BSD特定编译选项
if [[ "${{ matrix.target }}" == "freebsd-"* ]]; then
OS_CFLAGS="-I/usr/include -I/usr/local/include -D__FreeBSD__"
elif [[ "${{ matrix.target }}" == "openbsd-"* ]]; then
OS_CFLAGS="-I/usr/include -I/usr/local/include -D__OpenBSD__"
fi
# 使用clang进行交叉编译
CC="clang" \
CFLAGS="$ARCH_FLAGS $OS_CFLAGS -static" \
LDFLAGS="$ARCH_FLAGS -static" \
./configure --host=${{ matrix.goarch }}-unknown-${{ matrix.goos }} --disable-native
# 确保完整运行 make 以生成 configmake.h
make
# 修改 Makefile 添加静态链接
sed -i "s/^LDFLAGS =/LDFLAGS = -static/g" ./Makefile
# 重新编译 dd
make src/dd
strip src/dd || true
cp src/dd ../bin/dd-${{ matrix.goos }}-${{ matrix.goarch }}
ls -lah src/dd
file src/dd
continue-on-error: true
- name: Upload binary artifact
uses: actions/upload-artifact@v4
with:
name: dd-${{ matrix.target }}
path: bin/dd-${{ matrix.goos }}-${{ matrix.goarch }}${{ matrix.goarm && 'v'}}${{ matrix.goarm }}
continue-on-error: true
collect:
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Download all artifacts
uses: actions/download-artifact@v4
with:
path: artifacts/
- name: Organize binaries
run: |
# 确保bin目录存在
mkdir -p bin/
# 从所有单独的构建工件中复制文件到bin目录
find artifacts/ -type f -not -path "*/bin-directory/*" -exec cp {} bin/ \;
# 给所有二进制文件添加执行权限
chmod +x bin/*
# 列出所有收集到的二进制文件
echo "Successfully built binaries:"
ls -la bin/
- name: Upload combined bin directory
uses: actions/upload-artifact@v4
with:
name: dd-binaries
path: bin/
- name: Commit binaries to repository
run: |
git config --global user.name "GitHub Actions"
git config --global user.email "[email protected]"
git add bin/
git commit -m "fix: Update dd static binaries [skip ci]" || echo "No changes to commit"
git push
continue-on-error: true