Skip to content

Commit

Permalink
Merge pull request #2 from makotom/20210728-android-windows-aarch64
Browse files Browse the repository at this point in the history
Build native executables for Android and Windows on AArch64
  • Loading branch information
makotom authored Jul 27, 2021
2 parents be082fb + e3ba79f commit d57c26e
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 63 deletions.
152 changes: 111 additions & 41 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,60 @@ version: 2.1

commands:
smoke-test-common-steps:
parameters:
goos:
type: string
goarch:
type: string
steps:
- attach_workspace:
at: .
- when:
condition:
or:
- equal:
- << parameters.goos >>
- linux
- equal:
- << parameters.goos >>
- android
steps:
- run:
name: Extract tarball
command: |
. BUILD_NAME
tar -xf cfspeed-${BUILD_NAME}-<< parameters.goos >>-<< parameters.goarch >>.tar.gz
- when:
condition:
equal:
- << parameters.goos >>
- windows
steps:
- run:
name: Extract zip
command: |
$ProgressPreference = "SilentlyContinue"
$BuildName = $(& bash -c '. BUILD_NAME; echo $BUILD_NAME')
Expand-Archive cfspeed-${BuildName}-<< parameters.goos >>-<< parameters.goarch >>.zip .
- unless:
condition:
or:
- equal:
- << parameters.goos >>
- linux
- equal:
- << parameters.goos >>
- android
- equal:
- << parameters.goos >>
- windows
steps:
- run:
name: Extract zip
command: |
. BUILD_NAME
unzip cfspeed-${BUILD_NAME}-<< parameters.goos >>-<< parameters.goarch >>.zip
- run:
name: Show version
command: ./cfspeed --version
Expand All @@ -11,30 +64,47 @@ commands:
command: ./cfspeed

jobs:
determine-build-name:
docker:
- image: debian:buster-slim
steps:
- run:
name: Determine build name
command: |
echo export BUILD_NAME=\"0.1.${CIRCLE_BUILD_NUM}-${CIRCLE_SHA1:0:7}\" | tee -a BUILD_NAME
- persist_to_workspace:
root: .
paths:
- BUILD_NAME

build:
docker:
- image: golang
- image: golang:1.17-rc
parameters:
goos:
type: string
goarch:
type: string
steps:
- checkout
- run:
name: Install dependencies
command: |
apt update
apt install -y zip
- attach_workspace:
at: dist
- run:
name: Determine build name
command: |
mkdir -p dist
echo export BUILD_NAME=\"0.1.${CIRCLE_BUILD_NUM}-${CIRCLE_SHA1:0:7}\" | tee -a dist/BUILD_NAME
- run:
name: Build all
name: Build
command: |
. dist/BUILD_NAME
./build-and-pack-all.sh
GOOS_LIST_OVERRIDE='("<< parameters.goos >>")' GOARCH_LIST_OVERRIDE='("<< parameters.goarch >>")' ./build-and-pack-all.sh
- persist_to_workspace:
root: dist
paths:
- .
- << parameters.goos >>/<< parameters.goarch >>
- "*.tar.gz"
- "*.zip"
- store_artifacts:
path: dist
destination: /
Expand All @@ -51,50 +121,36 @@ jobs:
docker:
- image: archlinux
steps:
- attach_workspace:
at: .
- run:
name: Extract tarball
command: tar -xf cfspeed-*-linux-amd64.tar.gz
- smoke-test-common-steps
- smoke-test-common-steps:
goos: linux
goarch: amd64

smoke-test-linux-arm64:
machine:
image: ubuntu-2004:202101-01
resource_class: arm.medium
steps:
- attach_workspace:
at: .
- run:
name: Extract tarball
command: tar -xf cfspeed-*-linux-arm64.tar.gz
- smoke-test-common-steps
- smoke-test-common-steps:
goos: linux
goarch: arm64

smoke-test-windows-amd64:
machine:
image: windows-server-2019-vs2019:stable
resource_class: windows.medium
shell: powershell.exe
steps:
- attach_workspace:
at: .
- run:
name: Extract zip
command: |
$ProgressPreference = "SilentlyContinue"
Expand-Archive cfspeed-*-windows-amd64.zip .
- smoke-test-common-steps
- smoke-test-common-steps:
goos: windows
goarch: amd64

smoke-test-darwin-amd64:
macos:
xcode: 12.4.0
steps:
- attach_workspace:
at: .
- run:
name: Extract zip
command: unzip cfspeed-*-darwin-amd64.zip
- smoke-test-common-steps
- smoke-test-common-steps:
goos: darwin
goarch: amd64

release:
docker:
Expand All @@ -112,25 +168,39 @@ jobs:
name: Create a release
command: |
. BUILD_NAME
gh release create -R ${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME} -t "${BUILD_NAME}" -n "" "${BUILD_NAME}" *.tar.* *.zip
gh release create -R ${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME} -t "${BUILD_NAME}" -n "" "${BUILD_NAME}" *.tar.gz *.zip
workflows:
build-test-release:
jobs:
- build
- determine-build-name
- build:
name: build-<< matrix.goos >>-<< matrix.goarch >>
matrix:
parameters:
goos:
- linux
- windows
- darwin
- android
goarch:
- amd64
- arm64
requires:
- determine-build-name
- go-test
- smoke-test-linux-amd64:
requires:
- build
- build-linux-amd64
- smoke-test-linux-arm64:
requires:
- build
- build-linux-arm64
- smoke-test-windows-amd64:
requires:
- build
- build-windows-amd64
- smoke-test-darwin-amd64:
requires:
- build
- build-darwin-amd64
- release:
requires:
- build
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ go build -ldflags "-X main.BuildName=dev -X main.BuildAnnotation=$(date --iso-86

Note that the shell script needs Zip, tar and gzip.

## Notes

- On Debian/Ubuntu, you will need to install `ca-certificates`. Otherwise errors regarding TLS would be raised.

## TODO

- Go tests
- Smoke tests for AArch64 environments
- Support for Windows on AArch64 (along with Go 1.17)
- Research on TLS certificate issues with Debian-based Linux distros (and write an advisory)

## Dear Cloudflare

Expand Down
88 changes: 68 additions & 20 deletions build-and-pack-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,86 @@

set -euo pipefail

OS_LIST=("linux" "windows" "darwin")
ARCH_LIST=("amd64" "arm64")
GOOS_LIST=("linux" "windows" "darwin" "android")
GOARCH_LIST=("amd64" "arm64")
BUILD_NAME=${BUILD_NAME:-"git"}
BUILD_ANNOTATION="$(date --iso-8601=seconds)"

for os in "${OS_LIST[@]}"
do
if [ "${os}" == "windows" ]
if [ "${GOOS_LIST_OVERRIDE:-}" ]
then
eval GOOS_LIST="${GOOS_LIST_OVERRIDE}"
fi
if [ "${GOARCH_LIST_OVERRIDE:-}" ]
then
eval GOARCH_LIST="${GOARCH_LIST_OVERRIDE}"
fi

build_gc() {
goos=$1
goarch=$2

GOOS="${goos}" GOARCH="${goarch}" go build -ldflags "-X main.BuildName=${BUILD_NAME} -X main.BuildAnnotation=${BUILD_ANNOTATION}" -o "dist/${goos}/${goarch}/cfspeed" .

if [ "${goos}" == "windows" ]
then
mv "dist/${goos}/${goarch}/cfspeed" "dist/${goos}/${goarch}/cfspeed.exe"
fi
}

build_android() {
goos=$1
goarch=$2

if [ ! -d android-ndk-r21e ]
then
# https://developer.android.com/ndk/downloads
curl -JOL https://dl.google.com/android/repository/android-ndk-r21e-linux-x86_64.zip
echo 'c3ebc83c96a4d7f539bd72c241b2be9dcd29bda9 android-ndk-r21e-linux-x86_64.zip' | sha1sum -c
unzip android-ndk-r21e-linux-x86_64.zip
fi

if [ "${goarch}" == "arm64" ]
then
ext=".exe"
arch_clang="aarch64"
elif [ "${goarch}" == "amd64" ]
then
arch_clang="x86_64"
fi

CC="$(pwd)/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64/bin/${arch_clang}-linux-android30-clang" \
CXX="$(pwd)/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64/bin/${arch_clang}-linux-android30-clang++" \
GOOS="${goos}" GOARCH="${goarch}" CGO_ENABLED=1 go build -ldflags "-X main.BuildName=${BUILD_NAME} -X main.BuildAnnotation=${BUILD_ANNOTATION}" -o "dist/${goos}/${goarch}/cfspeed" .
}

package() {
goos=$1
goarch=$2

pushd "dist/${goos}/${goarch}"
if [ "${goos}" == "linux" ] || [ "${goos}" == "android" ]
then
tar -czf "../../cfspeed-${BUILD_NAME}-${goos}-${goarch}.tar.gz" .
else
ext=""
zip -r "../../cfspeed-${BUILD_NAME}-${goos}-${goarch}.zip" .
fi
popd
}

for arch in "${ARCH_LIST[@]}"
for goos in "${GOOS_LIST[@]}"
do
for goarch in "${GOARCH_LIST[@]}"
do
if [ "${os}" == "windows" ] && [ "${arch}" == "arm64" ]
then
continue
fi
echo "${goos}/${goarch}"

echo "${os}/${arch}"
mkdir -p "dist/${os}/${arch}"
GOOS=$os GOARCH=$arch go build -ldflags "-X main.BuildName=${BUILD_NAME} -X main.BuildAnnotation=${BUILD_ANNOTATION}" -o "dist/${os}/${arch}/cfspeed${ext}" .
mkdir -p "dist/${goos}/${goarch}"

pushd "dist/${os}/${arch}"
if [ "${os}" == "linux" ]
if [ "${goos}" == "android" ]
then
tar -czf "../../cfspeed-${BUILD_NAME}-${os}-${arch}.tar.gz" "cfspeed${ext}"
build_android "${goos}" "${goarch}"
else
zip -r "../../cfspeed-${BUILD_NAME}-${os}-${arch}.zip" "cfspeed${ext}"
build_gc "${goos}" "${goarch}"
fi
popd

package "${goos}" "${goarch}"
done
done

0 comments on commit d57c26e

Please sign in to comment.