Skip to content

Commit de5c638

Browse files
committed
chore: support building Coder Desktop .dylib
1 parent cb1a006 commit de5c638

File tree

4 files changed

+269
-67
lines changed

4 files changed

+269
-67
lines changed

.github/workflows/ci.yaml

Lines changed: 115 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -806,18 +806,60 @@ jobs:
806806
807807
echo "Required checks have passed"
808808
809+
# Builds the dylibs and upload it as an artifact so it can be embedded in the main build
810+
build-dylib:
811+
needs: changes
812+
if: github.ref == 'refs/heads/main' && needs.changes.outputs.docs-only == 'false' && !github.event.pull_request.head.repo.fork
813+
runs-on: ${{ github.repository_owner == 'coder' && 'depot-macos-latest' || 'macos-latest' }}
814+
steps:
815+
- name: Harden Runner
816+
uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1
817+
with:
818+
egress-policy: audit
819+
820+
- name: Checkout
821+
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
822+
with:
823+
fetch-depth: 0
824+
825+
- name: Setup Node
826+
uses: ./.github/actions/setup-node
827+
828+
- name: Setup Go
829+
uses: ./.github/actions/setup-go
830+
831+
- name: Build dylibs
832+
run: |
833+
set -euxo pipefail
834+
go mod download
835+
836+
make gen/mark-fresh
837+
make build/coder-dylib
838+
839+
- name: Upload build artifacts
840+
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3
841+
with:
842+
name: dylibs
843+
path: |
844+
./build/*.h
845+
./build/*.dylib
846+
retention-days: 7
847+
809848
build:
810849
# This builds and publishes ghcr.io/coder/coder-preview:main for each commit
811850
# to main branch.
812-
needs: changes
813-
if: github.ref == 'refs/heads/main' && needs.changes.outputs.docs-only == 'false' && !github.event.pull_request.head.repo.fork
851+
needs:
852+
- changes
853+
- build-dylib
854+
# TODO: Uncomment
855+
# if: github.ref == 'refs/heads/main' && needs.changes.outputs.docs-only == 'false' && !github.event.pull_request.head.repo.fork
814856
runs-on: ${{ github.repository_owner == 'coder' && 'depot-ubuntu-22.04-8' || 'ubuntu-latest' }}
815-
permissions:
816-
packages: write # Needed to push images to ghcr.io
817-
env:
818-
DOCKER_CLI_EXPERIMENTAL: "enabled"
819-
outputs:
820-
IMAGE: ghcr.io/coder/coder-preview:${{ steps.build-docker.outputs.tag }}
857+
# permissions:
858+
# packages: write # Needed to push images to ghcr.io
859+
# env:
860+
# DOCKER_CLI_EXPERIMENTAL: "enabled"
861+
# outputs:
862+
# IMAGE: ghcr.io/coder/coder-preview:${{ steps.build-docker.outputs.tag }}
821863
steps:
822864
- name: Harden Runner
823865
uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1
@@ -848,6 +890,16 @@ jobs:
848890
- name: Install zstd
849891
run: sudo apt-get install -y zstd
850892

893+
- name: Download dylibs
894+
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
895+
with:
896+
name: dylibs
897+
path: ./build
898+
- run: |
899+
mv ./build/*amd64.dylib ./site/out/bin/coder-amd64.dylib
900+
mv ./build/*arm64.dylib ./site/out/bin/coder-arm64.dylib
901+
mv ./build/*.h ./site/out/bin/coder-dylib.h
902+
851903
- name: Build
852904
run: |
853905
set -euxo pipefail
@@ -863,60 +915,61 @@ jobs:
863915
build/coder_"$version"_windows_amd64.zip \
864916
build/coder_"$version"_linux_amd64.{tar.gz,deb}
865917
866-
- name: Build Linux Docker images
867-
id: build-docker
868-
env:
869-
CODER_IMAGE_BASE: ghcr.io/coder/coder-preview
870-
CODER_IMAGE_TAG_PREFIX: main
871-
DOCKER_CLI_EXPERIMENTAL: "enabled"
872-
run: |
873-
set -euxo pipefail
874-
875-
# build Docker images for each architecture
876-
version="$(./scripts/version.sh)"
877-
tag="main-$(echo "$version" | sed 's/+/-/g')"
878-
echo "tag=$tag" >> $GITHUB_OUTPUT
879-
880-
# build images for each architecture
881-
# note: omitting the -j argument to avoid race conditions when pushing
882-
make build/coder_"$version"_linux_{amd64,arm64,armv7}.tag
883-
884-
# only push if we are on main branch
885-
if [ "${{ github.ref }}" == "refs/heads/main" ]; then
886-
# build and push multi-arch manifest, this depends on the other images
887-
# being pushed so will automatically push them
888-
# note: omitting the -j argument to avoid race conditions when pushing
889-
make push/build/coder_"$version"_linux_{amd64,arm64,armv7}.tag
890-
891-
# Define specific tags
892-
tags=("$tag" "main" "latest")
893-
894-
# Create and push a multi-arch manifest for each tag
895-
# we are adding `latest` tag and keeping `main` for backward
896-
# compatibality
897-
for t in "${tags[@]}"; do
898-
./scripts/build_docker_multiarch.sh \
899-
--push \
900-
--target "ghcr.io/coder/coder-preview:$t" \
901-
--version $version \
902-
$(cat build/coder_"$version"_linux_{amd64,arm64,armv7}.tag)
903-
done
904-
fi
905-
906-
- name: Prune old images
907-
if: github.ref == 'refs/heads/main'
908-
uses: vlaurin/action-ghcr-prune@0cf7d39f88546edd31965acba78cdcb0be14d641 # v0.6.0
909-
with:
910-
token: ${{ secrets.GITHUB_TOKEN }}
911-
organization: coder
912-
container: coder-preview
913-
keep-younger-than: 7 # days
914-
keep-tags: latest
915-
keep-tags-regexes: ^pr
916-
prune-tags-regexes: |
917-
^main-
918-
^v
919-
prune-untagged: true
918+
# TODO: Uncomment
919+
# - name: Build Linux Docker images
920+
# id: build-docker
921+
# env:
922+
# CODER_IMAGE_BASE: ghcr.io/coder/coder-preview
923+
# CODER_IMAGE_TAG_PREFIX: main
924+
# DOCKER_CLI_EXPERIMENTAL: "enabled"
925+
# run: |
926+
# set -euxo pipefail
927+
928+
# # build Docker images for each architecture
929+
# version="$(./scripts/version.sh)"
930+
# tag="main-$(echo "$version" | sed 's/+/-/g')"
931+
# echo "tag=$tag" >> $GITHUB_OUTPUT
932+
933+
# # build images for each architecture
934+
# # note: omitting the -j argument to avoid race conditions when pushing
935+
# make build/coder_"$version"_linux_{amd64,arm64,armv7}.tag
936+
937+
# # only push if we are on main branch
938+
# if [ "${{ github.ref }}" == "refs/heads/main" ]; then
939+
# # build and push multi-arch manifest, this depends on the other images
940+
# # being pushed so will automatically push them
941+
# # note: omitting the -j argument to avoid race conditions when pushing
942+
# make push/build/coder_"$version"_linux_{amd64,arm64,armv7}.tag
943+
944+
# # Define specific tags
945+
# tags=("$tag" "main" "latest")
946+
947+
# # Create and push a multi-arch manifest for each tag
948+
# # we are adding `latest` tag and keeping `main` for backward
949+
# # compatibality
950+
# for t in "${tags[@]}"; do
951+
# ./scripts/build_docker_multiarch.sh \
952+
# --push \
953+
# --target "ghcr.io/coder/coder-preview:$t" \
954+
# --version $version \
955+
# $(cat build/coder_"$version"_linux_{amd64,arm64,armv7}.tag)
956+
# done
957+
# fi
958+
959+
# - name: Prune old images
960+
# if: github.ref == 'refs/heads/main'
961+
# uses: vlaurin/action-ghcr-prune@0cf7d39f88546edd31965acba78cdcb0be14d641 # v0.6.0
962+
# with:
963+
# token: ${{ secrets.GITHUB_TOKEN }}
964+
# organization: coder
965+
# container: coder-preview
966+
# keep-younger-than: 7 # days
967+
# keep-tags: latest
968+
# keep-tags-regexes: ^pr
969+
# prune-tags-regexes: |
970+
# ^main-
971+
# ^v
972+
# prune-untagged: true
920973

921974
- name: Upload build artifacts
922975
if: github.ref == 'refs/heads/main'

Makefile

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,12 @@ PACKAGE_OS_ARCHES := linux_amd64 linux_armv7 linux_arm64
7979
# All architectures we build Docker images for (Linux only).
8080
DOCKER_ARCHES := amd64 arm64 armv7
8181

82+
# All ${OS}_${ARCH} combos we build the desktop dylib for.
83+
DYLIB_ARCHES := darwin_amd64.dylib darwin_arm64.dylib
84+
8285
# Computed variables based on the above.
8386
CODER_SLIM_BINARIES := $(addprefix build/coder-slim_$(VERSION)_,$(OS_ARCHES))
87+
CODER_DYLIBS := $(addprefix build/coder-desktop_$(VERSION)_,$(DYLIB_ARCHES))
8488
CODER_FAT_BINARIES := $(addprefix build/coder_$(VERSION)_,$(OS_ARCHES))
8589
CODER_ALL_BINARIES := $(CODER_SLIM_BINARIES) $(CODER_FAT_BINARIES)
8690
CODER_TAR_GZ_ARCHIVES := $(foreach os_arch, $(ARCHIVE_TAR_GZ), build/coder_$(VERSION)_$(os_arch).tar.gz)
@@ -128,12 +132,12 @@ release: $(CODER_FAT_BINARIES) $(CODER_ALL_ARCHIVES) $(CODER_ALL_PACKAGES) $(COD
128132
build/coder-slim_$(VERSION)_checksums.sha1: site/out/bin/coder.sha1
129133
cp "$<" "$@"
130134

131-
site/out/bin/coder.sha1: $(CODER_SLIM_BINARIES)
135+
site/out/bin/coder.sha1: $(CODER_SLIM_BINARIES) $(CODER_DYLIBS)
132136
pushd ./site/out/bin
133137
openssl dgst -r -sha1 coder-* | tee coder.sha1
134138
popd
135139

136-
build/coder-slim_$(VERSION).tar: build/coder-slim_$(VERSION)_checksums.sha1 $(CODER_SLIM_BINARIES)
140+
build/coder-slim_$(VERSION).tar: build/coder-slim_$(VERSION)_checksums.sha1 $(CODER_SLIM_BINARIES) $(CODER_DYLIBS)
137141
pushd ./site/out/bin
138142
tar cf "../../../build/$(@F)" coder-*
139143
popd
@@ -238,6 +242,25 @@ $(CODER_ALL_BINARIES): go.mod go.sum \
238242
cp "$@" "./site/out/bin/coder-$$os-$$arch$$dot_ext"
239243
fi
240244

245+
# This task builds Coder Desktop dylibs
246+
$(CODER_DYLIBS): go.mod go.sum $(GO_SRC_FILES)
247+
@if [ "$(shell uname)" = "Darwin" ]; then
248+
$(get-mode-os-arch-ext)
249+
./scripts/build_go.sh \
250+
--os "$$os" \
251+
--arch "$$arch" \
252+
--version "$(VERSION)" \
253+
--output "$@" \
254+
--dylib
255+
256+
cp "$@" "./site/out/bin/coder-desktop-$$os-$$arch.dylib"
257+
else
258+
echo "Skipping dylib build on non-Darwin OS"
259+
fi
260+
261+
# This task builds both dylibs
262+
build/coder-dylib: $(CODER_DYLIBS)
263+
241264
# This task builds all archives. It parses the target name to get the metadata
242265
# for the build, so it must be specified in this format:
243266
# build/coder_${version}_${os}_${arch}.${format}

scripts/build_go.sh

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# This script builds a single Go binary of Coder with the given parameters.
44
#
5-
# Usage: ./build_go.sh [--version 1.2.3-devel+abcdef] [--os linux] [--arch amd64] [--output path/to/output] [--slim] [--agpl] [--boringcrypto]
5+
# Usage: ./build_go.sh [--version 1.2.3-devel+abcdef] [--os linux] [--arch amd64] [--output path/to/output] [--slim] [--agpl] [--boringcrypto] [--dylib]
66
#
77
# Defaults to linux:amd64 with slim disabled, but can be controlled with GOOS,
88
# GOARCH and CODER_SLIM_BUILD=1. If no version is specified, defaults to the
@@ -25,6 +25,9 @@
2525
#
2626
# If the --boringcrypto parameter is specified, builds use boringcrypto instead of
2727
# the standard go crypto libraries.
28+
#
29+
# If the --dylib parameter is specified, the Coder Desktop `.dylib` is built
30+
# instead of the standard binary. This is only supported on macOS arm64 & amd64.
2831

2932
set -euo pipefail
3033
# shellcheck source=scripts/lib.sh
@@ -40,8 +43,9 @@ output_path=""
4043
agpl="${CODER_BUILD_AGPL:-0}"
4144
boringcrypto=${CODER_BUILD_BORINGCRYPTO:-0}
4245
debug=0
46+
dylib=0
4347

44-
args="$(getopt -o "" -l version:,os:,arch:,output:,slim,agpl,sign-darwin,boringcrypto,debug -- "$@")"
48+
args="$(getopt -o "" -l version:,os:,arch:,output:,slim,agpl,sign-darwin,boringcrypto,dylib,debug -- "$@")"
4549
eval set -- "$args"
4650
while true; do
4751
case "$1" in
@@ -78,6 +82,10 @@ while true; do
7882
boringcrypto=1
7983
shift
8084
;;
85+
--dylib)
86+
dylib=1
87+
shift
88+
;;
8189
--debug)
8290
debug=1
8391
shift
@@ -168,13 +176,25 @@ if [[ "$agpl" == 1 ]]; then
168176
fi
169177

170178
cgo=0
179+
sdk=""
180+
if [[ "$dylib" == 1 ]]; then
181+
if [[ "$os" != "darwin" ]]; then
182+
error "dylib builds are not supported on $os"
183+
fi
184+
cgo=1
185+
cmd_path="./vpn/dylib/lib.go"
186+
build_args+=("-buildmode=c-shared")
187+
sdk="$(xcrun --sdk macosx --show-sdk-path)"
188+
fi
189+
171190
goexp=""
172191
if [[ "$boringcrypto" == 1 ]]; then
173192
cgo=1
174193
goexp="boringcrypto"
175194
fi
176195

177-
GOEXPERIMENT="$goexp" CGO_ENABLED="$cgo" GOOS="$os" GOARCH="$arch" GOARM="$arm_version" go build \
196+
GOEXPERIMENT="$goexp" CGO_ENABLED="$cgo" GOOS="$os" GOARCH="$arch" GOARM="$arm_version" SDKROOT="$sdk" \
197+
go build \
178198
"${build_args[@]}" \
179199
"$cmd_path" 1>&2
180200

0 commit comments

Comments
 (0)