Skip to content

Commit

Permalink
.github, config: cross-compile an arm64 asset
Browse files Browse the repository at this point in the history
And install Zig via script, rather than snap. One benefit is that it
allows us to pin a Zig version.

The script is adapted from one I added to the Exercism Zig repo [1].

[1] exercism/zig@9dfb95f031b1
  • Loading branch information
ee7 committed Aug 14, 2023
1 parent c4cf6be commit 0fd7591
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 26 deletions.
18 changes: 18 additions & 0 deletions .github/bin/cross-compile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env sh

nimble --accept install --depsOnly

artifacts_dir='artifacts'
mkdir -p "${artifacts_dir}"

target='aarch64-linux-musl'
arch='arm64'
os='linux'
nimble --verbose build --cpu:"${arch}" --os:"${os}" -d:release -d:zig -d:target:"${target}"

binary_name='configlet'
build_tag="${GITHUB_REF_NAME}"
artifact_file="${artifacts_dir}/${binary_name}_${build_tag}_${os}_${arch}.tar.gz"
tar -cvzf "${artifact_file}" "${binary_name}"

gh release upload "${build_tag}" "${artifacts_dir}/${artifact_file}"
12 changes: 0 additions & 12 deletions .github/bin/linux-install-build-tools

This file was deleted.

2 changes: 2 additions & 0 deletions .github/bin/linux-install-musl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env sh
sudo apt-get install musl-dev musl-tools
45 changes: 45 additions & 0 deletions .github/bin/linux-install-zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env bash
set -eo pipefail

version='0.11.0' # 2023-08-04
release_name="zig-linux-x86_64-${version}"
archive="${release_name}.tar.xz"
url="https://ziglang.org/download/${version}/${archive}"

curlopts=(
--silent
--show-error
--fail
--location
--retry 3
)

# Download the release archive.
echo "Downloading Zig release archive..." >&2
curl "${curlopts[@]}" --output "${archive}" "${url}"

# Check that the archive has the expected hash.
echo "Verifying archive..." >&2
archive_sha256='2d00e789fec4f71790a6e7bf83ff91d564943c5ee843c5fd966efc474b423047'
echo "${archive_sha256} ${archive}" | sha256sum -c -

# Extract the archive, then remove it.
echo "Extracting archive..." >&2
tar xJf "${archive}"
rm "${archive}"

# Add zig directory to `GITHUB_PATH`.
zig_dir="$(pwd)/${release_name}"
echo "${zig_dir}" >> "${GITHUB_PATH}"

# Install `zigcc`, which is just a wrapper for `zig cc`. We need this because
# the value of e.g. `--clang.exe` cannot contain a space (Nim requires the value
# to be an executable, not a command).
zigcc_path="${zig_dir}/zigcc"
printf '#!/usr/bin/env sh\nzig cc $@\n' > "${zigcc_path}"
chmod +x "${zigcc_path}"

# Print the versions of Zig and Clang.
"${zig_dir}"/zig version
"${zig_dir}"/zig cc --version
echo "Successfully installed Zig ${version}."
31 changes: 28 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ jobs:
with:
fetch-depth: 0 # Allows using `git log` to set initial release notes.

- name: On Linux, install Zig
- name: On Linux, install musl
if: matrix.os == 'linux'
run: ./.github/bin/linux-install-build-tools
run: ./.github/bin/linux-install-musl

- name: Install Nim
uses: iffy/install-nim@ac410af52523f06e0fa037ee81d06ead7b95692c
Expand All @@ -62,9 +62,34 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

checksums:
cross-compile:
needs: [build]
runs-on: ubuntu-22.04
name: cross-compile
permissions:
contents: write
steps:
- name: Checkout code
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9

- name: Install Nim
uses: iffy/install-nim@ac410af52523f06e0fa037ee81d06ead7b95692c
with:
version: "binary:2.0.0"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Install Zig
run: ./.github/bin/linux-install-zig

- name: Cross-compile
run: ./.github/bin/cross-compile
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

checksums:
needs: [cross-compile]
runs-on: ubuntu-22.04
name: Upload signatures and checksums
permissions:
contents: write
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ jobs:
- name: Checkout code
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9

- name: On Linux, install Zig
- name: On Linux, install musl
if: matrix.os == 'linux'
run: ./.github/bin/linux-install-build-tools
run: ./.github/bin/linux-install-musl

- name: Install Nim
uses: iffy/install-nim@ac410af52523f06e0fa037ee81d06ead7b95692c
Expand Down
21 changes: 12 additions & 9 deletions config.nims
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ switch("mm", "refc")
patchFile("stdlib", "json", "src/patched_stdlib/json")
patchFile("stdlib", "parsejson", "src/patched_stdlib/parsejson")

if defined(zig) and findExe("zigcc").len > 0:
switch("cc", "clang")
# We can't write `zig cc` below, because the value cannot contain a space.
switch("clang.exe", "zigcc")
switch("clang.linkerexe", "zigcc")
const target {.strdefine.} = ""
if target.len > 0:
switch("passC", "-target " & target)
switch("passL", "-target " & target)

if defined(release):
switch("opt", "size")
switch("passC", "-flto")
Expand All @@ -21,15 +31,8 @@ if defined(release):
switch("passL", "-s")
switch("passL", "-static")

if defined(linux):
if findExe("zigcc").len > 0 and defined(amd64):
switch("cc", "clang")
# We can't write `zig cc` below, because the value cannot contain a space.
switch("clang.exe", "zigcc")
switch("clang.linkerexe", "zigcc")
switch("passC", "-target x86_64-linux-musl")
switch("passL", "-target x86_64-linux-musl")
elif defined(gcc):
if defined(linux) and not defined(zig):
if defined(gcc):
switch("gcc.exe", "musl-gcc")
switch("gcc.linkerexe", "musl-gcc")
elif defined(clang):
Expand Down

0 comments on commit 0fd7591

Please sign in to comment.