Skip to content

Commit

Permalink
improve cross compile
Browse files Browse the repository at this point in the history
- move cross compile from manual command (in Makefile) to github actions
- add more cross compile targets and now there are targets for:
  - Linux: x86 32bit, 64bit, ARMv6, ARMv7, ARM64
  - MacOS: x86 64bit
  - Windows: x86 64bit
  • Loading branch information
waltzofpearls committed Jan 25, 2023
1 parent 90fee4d commit f85cde8
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 50 deletions.
122 changes: 122 additions & 0 deletions .github/workflows/build_and_release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
on:
push:
tags:
- "v*.*.*" # push events to matching v*.*.*, i.e. v0.1.8, v20.15.10

jobs:
build-linux:
runs-on: ubuntu-20.04
strategy:
matrix:
include:
- { arch: "x86_64", libc: "musl" }
- { arch: "i686", libc: "musl" }
- { arch: "aarch64", libc: "musl" }
- { arch: "armv7", libc: "musleabihf" }
- { arch: "arm", libc: "musleabihf" }
steps:
- uses: actions/checkout@v3
- name: Pull Docker image
run: docker pull messense/rust-musl-cross:${{ matrix.arch }}-${{ matrix.libc }}
- name: Build in Docker
run: |
docker run --rm -i \
-v "$(pwd)":/home/rust/src messense/rust-musl-cross:${{ matrix.arch }}-${{ matrix.libc }} \
cargo build --release
- name: Strip binary
run: |
docker run --rm -i \
-v "$(pwd)":/home/rust/src messense/rust-musl-cross:${{ matrix.arch }}-${{ matrix.libc }} \
musl-strip -s /home/rust/src/target/${{ matrix.arch }}-unknown-linux-${{ matrix.libc }}/release/belt
- name: Make package
run: make package arch=${{ matrix.arch }} libc=${{ matrix.libc }}
- uses: actions/upload-artifact@v3
with:
name: "linux-${{ matrix.arch }}-${{ matrix.libc }}"
path: "target/package/*-*.*.*-${{ matrix.arch }}-unknown-linux-${{ matrix.libc }}.tar.gz"
retention-days: 5

build-macos:
runs-on: macos-11
steps:
- uses: actions/checkout@v3
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
- name: Build
run: cargo build --release --target x86_64-apple-darwin
- name: Make package
run: make package
- uses: actions/upload-artifact@v3
with:
name: macos-x86_64
path: target/package/*-*.*.*-macos-x86_64.zip
retention-days: 5

build-windows:
runs-on: windows-2022
steps:
- uses: actions/checkout@v3
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
- name: Build
run: cargo build --release --target x86_64-pc-windows-msvc
- name: Make package
run: make package
- uses: actions/upload-artifact@v3
with:
name: windows-x86_64-msvc
path: target/package/*-*-windows-x86_64-msvc.zip
retention-days: 5

release:
needs: [ build-linux, build-macos, build-windows ]
name: Release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Download all artifacts
uses: actions/download-artifact@v3
- name: Move to files around
run: |
mkdir -p target/package
[ -d macos-x86_64 ] && mv macos-x86_64/* target/package || true
[ -d windows-x86_64-msvc ] && mv windows-x86_64-msvc/* target/package || true
[ -d linux-x86_64-musl ] && mv linux-x86_64-musl/* target/package || true
[ -d linux-i686-musl ] && mv linux-i686-musl/* target/package || true
[ -d linux-aarch64-musl ] && mv linux-aarch64-musl/* target/package || true
[ -d linux-armv7-musleabihf ] && mv linux-armv7-musleabihf/* target/package || true
[ -d linux-arm-musleabihf ] && mv linux-arm-musleabihf/* target/package || true
- name: Create checksum file
run: shasum -a 256 target/package/*-*.*.*-*.{tar.gz,zip} > target/package/checksums.txt
- name: List files
run: ls -ahl target/package
- name: Release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
generate_release_notes: true
fail_on_unmatched_files: true
files: |
target/package/*-*.*.*-aarch64-unknown-linux-musl.tar.gz
target/package/*-*.*.*-arm-unknown-linux-musleabihf.tar.gz
target/package/*-*.*.*-armv7-unknown-linux-musleabihf.tar.gz
target/package/*-*.*.*-i686-unknown-linux-musl.tar.gz
target/package/*-*.*.*-macos-x86_64.zip
target/package/*-*.*.*-windows-x86_64-msvc.zip
target/package/*-*.*.*-x86_64-unknown-linux-musl.tar.gz
target/package/checksums.txt
README.md
LICENSE
cargo-publish:
needs: release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Cargo publish
if: startsWith(github.ref, 'refs/tags/')
run: make publish
32 changes: 8 additions & 24 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,39 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
with:
command: check
args: --all --all-targets --all-features
- run: cargo check --all --all-targets --all-features

test:
name: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
with:
command: test
args: --workspace --all-features
- run: cargo test --workspace --all-features

fmt:
name: fmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
override: true
- run: rustup component add rustfmt
- uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check
- run: cargo fmt --all -- --check

clippy:
name: clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
override: true
- run: rustup component add clippy
- uses: actions-rs/cargo@v1
with:
command: clippy
args: --workspace --tests --all-features -- -D warnings
- run: cargo clippy --workspace --tests --all-features -- -D warnings
42 changes: 17 additions & 25 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,25 @@ bench:

APP = belt
VERSION := $(shell cargo metadata -q | jq -r '.packages[] | select(.name == "$(APP)") | .version')
MACOS_X86_64 := target/package/$(APP)-$(VERSION)-x86_64-apple-darwin.tar.gz
LINUX_DYN_X86_64 := target/package/$(APP)-$(VERSION)-x86_64-unknown-linux-gnu.tar.gz
LINUX_STAT_X86_64 := target/package/$(APP)-$(VERSION)-x86_64-unknown-linux-musl.tar.gz
LINUX_ARMV7 := target/package/$(APP)-$(VERSION)-armv7-unknown-linux-gnueabihf.tar.gz
UNAME_S := $(shell uname -s)

.PHONY: release
release: cross
@echo "[release] Cleaning up before packaging..."
.PHONY: package
package:
ifdef OS # windows
mkdir -p target/package
rm -f $(MACOS_X86_64) $(LINUX_X86_64) $(LINUX_ARMV7)
@echo "[release] Creating package for MacOS x86_64..."
tar -cvzf $(MACOS_X86_64) \
-C $$PWD/target/release $(APP)
@echo "[release] Creating package for Linux (dynamic) x86_64..."
tar -cvzf $(LINUX_DYN_X86_64) \
-C $$PWD/target/x86_64-unknown-linux-gnu/release $(APP)
@echo "[release] Creating package for Linux (static) x86_64..."
tar -cvzf $(LINUX_STAT_X86_64) \
-C $$PWD/target/x86_64-unknown-linux-musl/release $(APP)
@echo "[release] Creating package for Linux armv7..."
tar -cvzf $(LINUX_ARMV7) \
-C $$PWD/target/armv7-unknown-linux-gnueabihf/release $(APP)
@make checksum

.PHONY: checksum
checksum:
shasum -a 256 target/package/$(APP)-$(VERSION)-*.tar.gz > target/package/$(APP)-$(VERSION)-checksums.txt
tar -a -cvf target/package/$(APP)-$(VERSION)-windows-x86_64-msvc.zip \
-C $$PWD/target/x86_64-pc-windows-msvc/release $(APP).exe \
-C $$PWD LICENSE README.md
else ifeq ($(UNAME_S),Darwin) # macOS
mkdir -p target/package
zip -j target/package/$(APP)-$(VERSION)-macos-x86_64.zip \
target/x86_64-apple-darwin/release/$(APP) LICENSE README.md
else ifeq ($(UNAME_S),Linux) # linux
sudo mkdir -p target/package
sudo tar -z -cvf target/package/$(APP)-$(VERSION)-$(arch)-unknown-linux-$(libc).tar.gz \
-C $$PWD/target/$(arch)-unknown-linux-$(libc)/release $(APP) \
-C $$PWD LICENSE README.md
endif

version:
@grep -rn --color \
Expand Down
2 changes: 1 addition & 1 deletion cross.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.50.0-buster
FROM --platform=linux/amd64 rust:1.66-slim-bullseye

ENV CROSS_DOCKER_IN_DOCKER=true
RUN cargo install cross
Expand Down

0 comments on commit f85cde8

Please sign in to comment.