Skip to content

Commit

Permalink
Merge pull request #2 from nyoungstudios/feat/build-on-arm
Browse files Browse the repository at this point in the history
feat: builds arm executables
  • Loading branch information
nyoungstudios authored Sep 17, 2022
2 parents f2bd716 + 696a0e8 commit b9f7192
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 21 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/build_alfa_cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ on:
jobs:
tag:
runs-on: ubuntu-latest
timeout-minutes: 15

outputs:
tag: ${{ steps.create-tag.outputs.tag }}
Expand All @@ -29,6 +30,7 @@ jobs:
release:
needs: tag
runs-on: ${{ matrix.os }}
timeout-minutes: 15

strategy:
matrix:
Expand Down Expand Up @@ -59,3 +61,60 @@ jobs:
file: alfa_*
tag: ${{ needs.tag.outputs.tag }}
file_glob: true

release-on-arm:
needs: tag
runs-on: ubuntu-latest
timeout-minutes: 15

steps:
- uses: actions/checkout@v2
with:
fetch-depth: '0'
- name: Setup arm emulation with qemu
run: |
sudo apt-get update -q -y
sudo apt-get -qq install -y qemu qemu-user-static
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes --credential yes
- name: Create docker command script
run: |
SCRIPT=$(cat << EOF
#!/bin/sh
set -eu
# installs dependencies
dart pub get
# compile executable (make isn't part of this docker image)
dart compile exe bin/alfa.dart -o alfa
# renames executable
./rename.sh
EOF
)
echo "$SCRIPT" > run.sh
chmod +x run.sh
- name: Cat docker command script
run: cat run.sh

- name: Build arm executable with docker
run: |
docker run \
--rm \
-t \
--workdir "${GITHUB_WORKSPACE}" \
-v "${GITHUB_WORKSPACE}:${GITHUB_WORKSPACE}" \
--platform linux/arm64 \
dart:stable \
"${GITHUB_WORKSPACE}/run.sh"
- name: Upload binaries to release
uses: svenstaro/upload-release-action@v2
with:
file: alfa_*
tag: ${{ needs.tag.outputs.tag }}
file_glob: true
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ pubspec.lock

# alfa executables
/alfa
/alfa_linux
/alfa_macos
/alfa_linux*
/alfa_macos*
5 changes: 3 additions & 2 deletions bin/alfa.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ void main(List<String> args) async {
// gets environment variables
String user = Platform.environment['SUDO_USER'];
String alfaUser = Platform.environment['ALFA_USER'];
String alfaArch = Platform.environment['ALFA_ARCH'];
if (user == 'root' && alfaUser != '') {
user = alfaUser;
}

// gets operating system
// valid options (linux, macos)
var osName = Platform.operatingSystem;
print("Running alfa on ${osName}");
print("Running alfa on ${osName} ${alfaArch}");

// loads config file which maps the install keys to the tags
var configFile = await TomlDocument.load(argResults['config']);
Expand Down Expand Up @@ -189,7 +190,7 @@ void main(List<String> args) async {
arguments = ['-u', user];
}

arguments.addAll(['--preserve-env=ALFA_USER', '--', '/bin/bash']);
arguments.addAll(['--preserve-env=ALFA_USER,ALFA_ARCH', '--', '/bin/bash']);
}

arguments.addAll(['-euc', command]);
Expand Down
36 changes: 36 additions & 0 deletions codemagic.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
workflows:
release:
name: Release
instance_type: mac_mini_m1
max_build_duration: 15
environment:
flutter: stable

groups:
- github

triggering:
events:
- tag
tag_patterns:
- pattern: 'v*'

scripts:
- name: Add Dart SDK to PATH
script: |
echo PATH="$PATH":"$FLUTTER_ROOT/.pub-cache/bin" >> $CM_ENV
echo PATH="$PATH":"$FLUTTER_ROOT/bin" >> $CM_ENV
- name: Install dependencies
script: dart pub get
- name: Compile executable
script: make
- name: Renames executable
script: ./rename.sh

artifacts:
- alfa_macos_arm64

publishing:
scripts:
- name: Publish to GitHub
script: gh release upload "${CM_TAG}" alfa_macos_arm64
5 changes: 3 additions & 2 deletions functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# rather "install_brew".
# To access the list of "options" in the config.toml file you pass to the installer, use the "$@" variable.
# To access the user that called the installer, do not user the environment variable "$SUDO_USER", but rather use "$ALFA_USER".
# To access the uname -m output (system architecture), you can use the environment variable "$ALFA_ARCH"

create_git_config() {
# sets up git config name and email
Expand Down Expand Up @@ -217,14 +218,14 @@ install_anaconda3_common() {
install_anaconda3_macos() {
# installs anaconda3
echo "Installing Anaconda3..."
curl "${1:-https://repo.anaconda.com/archive/Anaconda3-2022.05-MacOSX-x86_64.sh}" -o ~/anaconda3.sh
curl "${1:-https://repo.anaconda.com/archive/Anaconda3-2022.05-MacOSX-${ALFA_ARCH}.sh}" -o ~/anaconda3.sh
install_anaconda3_common
}

install_anaconda3_linux() {
# installs anaconda3
echo "Installing Anaconda3..."
curl "${1:-https://repo.anaconda.com/archive/Anaconda3-2022.05-Linux-x86_64.sh}" -o ~/anaconda3.sh
curl "${1:-https://repo.anaconda.com/archive/Anaconda3-2022.05-Linux-${ALFA_ARCH}.sh}" -o ~/anaconda3.sh
install_anaconda3_common
}

Expand Down
20 changes: 12 additions & 8 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ fi

alfaCommand=""

# sets the alfa command variable based on the operating system
unameResults="$(uname -s)"
if [[ "Linux" == *"$unameResults"* ]]; then
alfaCommand="alfa_linux"
elif [[ "Darwin" == *"$unameResults"* ]]; then
alfaCommand="alfa_macos"
# renames the alfa executable based on the operating system and architecture
unameSystem="$(uname -s)"
unameMachine="$(uname -m)"

if [[ "Linux" == "$unameSystem" && ( "x86_64" == "$unameMachine" || "aarch64" == "$unameMachine" ) ]]; then
alfaCommand="alfa_linux_$unameMachine"
elif [[ "Darwin" == "$unameSystem" && ( "x86_64" == "$unameMachine" || "arm64" == "$unameMachine" ) ]]; then
alfaCommand="alfa_macos_$unameMachine"
else
echo "alfa cannot run on ${unameResults}. It can only run on Linux and macOS."
echo "alfa cannot run on ${unameSystem} ${unameMachine}. It can only run on Linux and macOS."
exit 1
fi

export ALFA_ARCH="$unameMachine"

# runs the alfa command depending upon if sudo exists
if ! command -v "sudo" > /dev/null 2>&1; then
# sudo command doesn't exist
Expand All @@ -37,7 +41,7 @@ else
while :; do sudo -v; sleep 59; done &
loopPid="$!"

export ALFA_USER="${SUDO_USER:-${USER:-}}"; sudo --preserve-env=ALFA_USER ./$alfaCommand "$@"
export ALFA_USER="${SUDO_USER:-${USER:-}}"; sudo --preserve-env=ALFA_USER,ALFA_ARCH ./$alfaCommand "$@"

trap 'trap - SIGTERM && kill $(pgrep -P $loopPid) $loopPid' SIGINT SIGTERM EXIT

Expand Down
16 changes: 9 additions & 7 deletions rename.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ if [[ ! -f "alfa" ]]; then
exit 1
fi

# renames the alfa executable based on the operating system
unameResults="$(uname -s)"
if [[ "Linux" == *"$unameResults"* ]]; then
alfaCommand="alfa_linux"
elif [[ "Darwin" == *"$unameResults"* ]]; then
alfaCommand="alfa_macos"
# renames the alfa executable based on the operating system and architecture
unameSystem="$(uname -s)"
unameMachine="$(uname -m)"

if [[ "Linux" == "$unameSystem" && ( "x86_64" == "$unameMachine" || "aarch64" == "$unameMachine" ) ]]; then
alfaCommand="alfa_linux_$unameMachine"
elif [[ "Darwin" == "$unameSystem" && ( "x86_64" == "$unameMachine" || "arm64" == "$unameMachine" ) ]]; then
alfaCommand="alfa_macos_$unameMachine"
else
echo "alfa cannot run on ${unameResults}. It can only run on Linux and macOS."
echo "alfa cannot run on ${unameSystem} ${unameMachine}. It can only run on Linux and macOS."
exit 1
fi

Expand Down

0 comments on commit b9f7192

Please sign in to comment.