Skip to content

Commit

Permalink
Portable executables (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
MOZGIII committed Mar 16, 2023
1 parent 4faf36f commit 62f030f
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 63 deletions.
26 changes: 19 additions & 7 deletions .github/actions/plan/plan.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const allPlatforms = {
env: {},
cacheKey: "ubuntu-amd64",
isBroken: false,
buildTarget: "x86_64-unknown-linux-gnu.2.17",
buildTargetDir: "x86_64-unknown-linux-gnu",
extraTargetsToInstall: [], // native
},
windows: {
name: "Windows",
Expand All @@ -22,6 +25,9 @@ const allPlatforms = {
},
cacheKey: "windows-amd64",
isBroken: false,
buildTarget: null, // native
buildTargetDir: null, // native
extraTargetsToInstall: [], // native
},
macos: {
name: "macOS (amd64)",
Expand All @@ -31,6 +37,9 @@ const allPlatforms = {
env: {},
cacheKey: "macos-amd64",
isBroken: false,
buildTarget: null, // native
buildTargetDir: null, // native
extraTargetsToInstall: [], // native
},
macos_aarch64: {
name: "macOS (aarch64)",
Expand All @@ -40,6 +49,9 @@ const allPlatforms = {
env: {},
cacheKey: "macos-aarch64",
isBroken: false,
buildTarget: null, // native
buildTargetDir: null, // native
extraTargetsToInstall: [], // native
},
};

Expand All @@ -55,10 +67,10 @@ const codeModes = {
cargoCommand: "test",
cargoCacheKey: "test",
},
build: {
name: "build",
cargoCommand: "build",
cargoCacheKey: "build",
zigbuild: {
name: "zigbuild",
cargoCommand: "zigbuild",
cargoCacheKey: "zigbuild",
},
fmt: {
name: "fmt",
Expand All @@ -78,10 +90,10 @@ const codeModes = {

const buildModes = {
build: {
name: "build",
cargoCommand: "build",
name: "zigbuild",
cargoCommand: "zigbuild",
cargoArgs: "--release",
cargoCacheKey: "release-build",
cargoCacheKey: "release-zigbuild",
},
};

Expand Down
36 changes: 3 additions & 33 deletions .github/scripts/build_env/macos.sh
Original file line number Diff line number Diff line change
@@ -1,39 +1,9 @@
#!/bin/bash
set -euo pipefail

# Undo the Rosetta hack if needed.
PROC_TRANSLATED="$(sysctl -n sysctl.proc_translated || true)"
CURRENT_ARCH="$(uname -m)"
if [[ "$PROC_TRANSLATED" == "1" && "$CURRENT_ARCH" == "x86_64" ]]; then
if [[ "${MACOS_ROSETTA_HACK_UNDONE:-}" == "true" ]]; then
printf "Seems like we've fallen into a loop of rosetta hack undoing, crashing the run.\n" >&2
exit 1
fi

export MACOS_ROSETTA_HACK_UNDONE=true
# Here we assume the native arch is arm64...
set -x
exec arch -arm64 "$0" "$@"
fi

brew install \
coreutils

if [[ "$(uname -m)" == "x86_64" ]]; then
echo "Intel-specific setup..."

brew install \
michaeleisel/zld/zld

ZLD_PATH="$(which zld)"

"$ZLD_PATH" -v

cat >>./.cargo/config.toml <<EOF
[target.x86_64-apple-darwin]
linker = "clang"
rustflags = ["-C", "link-arg=-fuse-ld=${ZLD_PATH}"]
EOF

cat ./.cargo/config.toml
fi
.github/scripts/zig.sh
.github/scripts/cargo-zigbuild.sh
.github/scripts/use-zig.sh
24 changes: 3 additions & 21 deletions .github/scripts/build_env/ubuntu.sh
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
#!/bin/bash
set -euo pipefail

MOLD_GITHUB_REPO="rui314/mold"
MOLD_VERSION="1.2.1"
MOLD_INSTALL_PATH="${HOME}/.local"

mkdir -p "$MOLD_INSTALL_PATH"
wget \
-O- \
"https://github.com/$MOLD_GITHUB_REPO/releases/download/v${MOLD_VERSION}/mold-${MOLD_VERSION}-$(uname -m)-linux.tar.gz" |
tar -C "$MOLD_INSTALL_PATH" --strip-components=1 -xzf -

MOLD_PATH="${MOLD_INSTALL_PATH}/bin/mold"

"$MOLD_PATH" --version

cat >>./.cargo/config.toml <<EOF
[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = ["-C", "link-arg=-fuse-ld=${MOLD_PATH}"]
EOF

cat ./.cargo/config.toml
.github/scripts/zig.sh
.github/scripts/cargo-zigbuild.sh
.github/scripts/use-zig.sh
6 changes: 6 additions & 0 deletions .github/scripts/build_env/windows.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
set -euo pipefail

.github/scripts/zig.sh
.github/scripts/cargo-zigbuild.sh
.github/scripts/use-zig.sh
41 changes: 41 additions & 0 deletions .github/scripts/cargo-zigbuild.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash
set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")/../.."

URL="$(build-utils/cargo-zigbuild-download-url)"
TARGET_FILE="$(basename "$URL")"

printf "Downloading %s from %s\n" "$TARGET_FILE" "$URL"
curl -sSL "$URL" -o "$TARGET_FILE"

maybe_sudo() {
if [[ "${USE_SUDO:-}" == true ]]; then
sudo "$@"
else
"$@"
fi
}

extract() {
maybe_sudo mkdir -p "$INSTALL_PATH"
if [[ "$TARGET_FILE" == *.zip ]]; then
maybe_sudo unzip -o -d "$INSTALL_PATH" "$TARGET_FILE"
else
maybe_sudo tar -C "$INSTALL_PATH" -xvf "$TARGET_FILE"
fi
}

case "$(uname -s)" in
"Win"* | "MINGW"*)
INSTALL_PATH="$HOME/.cargo/bin"
extract
;;
*)
INSTALL_PATH="/usr/local/bin"
USE_SUDO=true extract
;;
esac

rm "$TARGET_FILE"

"$INSTALL_PATH/cargo-zigbuild" --version
27 changes: 27 additions & 0 deletions .github/scripts/use-zig.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
set -euo pipefail

setenv() {
local KEY="$1"
local VAL="$2"
printf "%s=%s\n" "$KEY" "$VAL" >"$GITHUB_ENV"
}

case "$(uname -s)" in
"Linux")
LINKER="ld.lld" # ELF
;;
"Darwin")
LINKER="ld64.lld" # Mach-O
;;
*)
# Windows
LINKER="lld-link" # COFF
;;
esac

setenv CC "zig cc"
setenv CXX "zig c++"
setenv AR "zig ar"
setenv RANLIB "zig ranlib"
setenv LD "zig $LINKER"
47 changes: 47 additions & 0 deletions .github/scripts/zig.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash
set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")/../.."

URL="$(build-utils/zig-download-url)"
TARGET_FILE="$(basename "$URL")"

printf "Downloading %s from %s\n" "$TARGET_FILE" "$URL"
curl -sSL "$URL" -o "$TARGET_FILE"

maybe_sudo() {
if [[ "${USE_SUDO:-}" == true ]]; then
sudo "$@"
else
"$@"
fi
}

extract() {
if [[ "$TARGET_FILE" == *.zip ]]; then
EXTRACT_DIR="$(mktemp -d)"
unzip -o -d "$EXTRACT_DIR" "$TARGET_FILE"
maybe_sudo mkdir -p "$(dirname "$INSTALL_PATH")"
maybe_sudo mv "$EXTRACT_DIR/$(basename "$TARGET_FILE" .zip)" "$INSTALL_PATH"
else
maybe_sudo mkdir -p "$INSTALL_PATH"
maybe_sudo tar -C "$INSTALL_PATH" --strip-components=1 -xf "$TARGET_FILE"
fi
}

case "$(uname -s)" in
"Win"* | "MINGW"*)
INSTALL_PATH="C:/zig"
extract
;;
*)
INSTALL_PATH="/usr/local/zig"
USE_SUDO=true extract
;;
esac

rm "$TARGET_FILE"

"$INSTALL_PATH/zig" version

# Add to PATH.
printf "%s\n" "$INSTALL_PATH" >>"$GITHUB_PATH"
13 changes: 11 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ jobs:
run: rustup show
timeout-minutes: 10

- name: Install additional rust toolchains
run: rustup target add ${{ join(matrix.plan.platform.extraTargetsToInstall, ' ') }}
if: ${{ join(matrix.plan.platform.extraTargetsToInstall, ' ') != '' }}
timeout-minutes: 10

- name: Prepare the build environment
run: ${{ matrix.plan.platform.buildEnvScript }}

Expand All @@ -108,13 +113,17 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: ${{ matrix.plan.mode.cargoCommand }}
args: ${{ matrix.plan.mode.cargoArgs }}
args: >
${{ matrix.plan.mode.cargoArgs }}
${{ matrix.plan.platform.buildTarget && format('--target {0}', matrix.plan.platform.buildTarget) || '' }}
- name: Archive the binaries for release
env:
TARGET_DIR: ${{ matrix.plan.platform.buildTargetDir && format('{0}/release', matrix.plan.platform.buildTargetDir) || 'release' }}
run: |
./utils/build/package \
target/build-package \
target/release \
"target/$TARGET_DIR" \
""
- name: Upload release archive
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ jobs:
run: rustup show
timeout-minutes: 10

- name: Install additional rust toolchains
run: rustup target add ${{ join(matrix.plan.platform.extraTargetsToInstall, ' ') }}
if: ${{ join(matrix.plan.platform.extraTargetsToInstall, ' ') != '' }}
timeout-minutes: 10

- name: Prepare the build environment
run: ${{ matrix.plan.platform.buildEnvScript }}

Expand Down
27 changes: 27 additions & 0 deletions build-utils/cargo-zigbuild-download-url
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
set -euo pipefail

VERSION="0.16.3"
BASE_URL="https://github.com/rust-cross/cargo-zigbuild/releases"

DESCRIPTOR="$(uname -s)-$(uname -m)"

case "$DESCRIPTOR" in
"Linux-x86_64")
PACKAGE="x86_64-unknown-linux-musl.tar.gz"
;;
"Darwin-x86_64" | "Darwin-arm64")
PACKAGE="apple-darwin.tar.gz"
;;
"Win"* | "MINGW"*)
PACKAGE="windows-x64.zip"
;;
*)
printf "Unsupported system: %s\n" "$DESCRIPTOR" >&2
exit 1
;;
esac

URL="$BASE_URL/download/v$VERSION/cargo-zigbuild-v$VERSION.$PACKAGE"

printf "%s\n" "$URL"
36 changes: 36 additions & 0 deletions build-utils/zig-download-url
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash
set -euo pipefail

ZIG_VERSION="0.10.1"
ZIG_BASE_URL="https://ziglang.org/download"

SYSTEM="$(uname -s)"
PLATFORM="$(uname -m)"
EXT=".tar.xz"

case "$SYSTEM" in
"Linux")
SYSTEM="linux"
;;
"Darwin")
SYSTEM="macos"
;;
"Win"* | "MINGW"*)
SYSTEM="windows"
EXT=".zip"
;;
*)
printf "Unsupported system: %s\n" "$SYSTEM" >&2
exit 1
;;
esac

case "$PLATFORM" in
"arm64")
PLATFORM="aarch64"
;;
esac

URL="${ZIG_BASE_URL}/${ZIG_VERSION}/zig-${SYSTEM}-${PLATFORM}-${ZIG_VERSION}${EXT}"

printf "%s\n" "$URL"

0 comments on commit 62f030f

Please sign in to comment.