Skip to content

Commit 3fc4ec2

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

File tree

4 files changed

+270
-67
lines changed

4 files changed

+270
-67
lines changed

.github/workflows/ci.yaml

Lines changed: 116 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -806,18 +806,61 @@ 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+
# TODO: Uncomment
813+
# if: github.ref == 'refs/heads/main' && needs.changes.outputs.docs-only == 'false' && !github.event.pull_request.head.repo.fork
814+
runs-on: ${{ github.repository_owner == 'coder' && 'depot-macos-latest' || 'macos-latest' }}
815+
steps:
816+
- name: Harden Runner
817+
uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1
818+
with:
819+
egress-policy: audit
820+
821+
- name: Checkout
822+
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
823+
with:
824+
fetch-depth: 0
825+
826+
- name: Setup Node
827+
uses: ./.github/actions/setup-node
828+
829+
- name: Setup Go
830+
uses: ./.github/actions/setup-go
831+
832+
- name: Build dylibs
833+
run: |
834+
set -euxo pipefail
835+
go mod download
836+
837+
make gen/mark-fresh
838+
make build/coder-dylib
839+
840+
- name: Upload build artifacts
841+
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3
842+
with:
843+
name: dylibs
844+
path: |
845+
./build/*.h
846+
./build/*.dylib
847+
retention-days: 7
848+
809849
build:
810850
# This builds and publishes ghcr.io/coder/coder-preview:main for each commit
811851
# 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
852+
needs:
853+
- changes
854+
- build-dylib
855+
# TODO: Uncomment
856+
# if: github.ref == 'refs/heads/main' && needs.changes.outputs.docs-only == 'false' && !github.event.pull_request.head.repo.fork
814857
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 }}
858+
# permissions:
859+
# packages: write # Needed to push images to ghcr.io
860+
# env:
861+
# DOCKER_CLI_EXPERIMENTAL: "enabled"
862+
# outputs:
863+
# IMAGE: ghcr.io/coder/coder-preview:${{ steps.build-docker.outputs.tag }}
821864
steps:
822865
- name: Harden Runner
823866
uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1
@@ -848,6 +891,16 @@ jobs:
848891
- name: Install zstd
849892
run: sudo apt-get install -y zstd
850893

894+
- name: Download dylibs
895+
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
896+
with:
897+
name: dylibs
898+
path: ./build
899+
- run: |
900+
mv ./build/*amd64.dylib ./site/out/bin/coder-amd64.dylib
901+
mv ./build/*arm64.dylib ./site/out/bin/coder-arm64.dylib
902+
mv ./build/*.h ./site/out/bin/coder-dylib.h
903+
851904
- name: Build
852905
run: |
853906
set -euxo pipefail
@@ -863,60 +916,61 @@ jobs:
863916
build/coder_"$version"_windows_amd64.zip \
864917
build/coder_"$version"_linux_amd64.{tar.gz,deb}
865918
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
919+
# TODO: Uncomment
920+
# - name: Build Linux Docker images
921+
# id: build-docker
922+
# env:
923+
# CODER_IMAGE_BASE: ghcr.io/coder/coder-preview
924+
# CODER_IMAGE_TAG_PREFIX: main
925+
# DOCKER_CLI_EXPERIMENTAL: "enabled"
926+
# run: |
927+
# set -euxo pipefail
928+
929+
# # build Docker images for each architecture
930+
# version="$(./scripts/version.sh)"
931+
# tag="main-$(echo "$version" | sed 's/+/-/g')"
932+
# echo "tag=$tag" >> $GITHUB_OUTPUT
933+
934+
# # build images for each architecture
935+
# # note: omitting the -j argument to avoid race conditions when pushing
936+
# make build/coder_"$version"_linux_{amd64,arm64,armv7}.tag
937+
938+
# # only push if we are on main branch
939+
# if [ "${{ github.ref }}" == "refs/heads/main" ]; then
940+
# # build and push multi-arch manifest, this depends on the other images
941+
# # being pushed so will automatically push them
942+
# # note: omitting the -j argument to avoid race conditions when pushing
943+
# make push/build/coder_"$version"_linux_{amd64,arm64,armv7}.tag
944+
945+
# # Define specific tags
946+
# tags=("$tag" "main" "latest")
947+
948+
# # Create and push a multi-arch manifest for each tag
949+
# # we are adding `latest` tag and keeping `main` for backward
950+
# # compatibality
951+
# for t in "${tags[@]}"; do
952+
# ./scripts/build_docker_multiarch.sh \
953+
# --push \
954+
# --target "ghcr.io/coder/coder-preview:$t" \
955+
# --version $version \
956+
# $(cat build/coder_"$version"_linux_{amd64,arm64,armv7}.tag)
957+
# done
958+
# fi
959+
960+
# - name: Prune old images
961+
# if: github.ref == 'refs/heads/main'
962+
# uses: vlaurin/action-ghcr-prune@0cf7d39f88546edd31965acba78cdcb0be14d641 # v0.6.0
963+
# with:
964+
# token: ${{ secrets.GITHUB_TOKEN }}
965+
# organization: coder
966+
# container: coder-preview
967+
# keep-younger-than: 7 # days
968+
# keep-tags: latest
969+
# keep-tags-regexes: ^pr
970+
# prune-tags-regexes: |
971+
# ^main-
972+
# ^v
973+
# prune-untagged: true
920974

921975
- name: Upload build artifacts
922976
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)