Skip to content

Commit 73e4db4

Browse files
committed
chore: buff makefile pt. 1
1 parent 14a9576 commit 73e4db4

File tree

5 files changed

+172
-64
lines changed

5 files changed

+172
-64
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ site/**/*.typegen.ts
3030
site/build-storybook.log
3131

3232
# Build
33+
build/
3334
dist/
3435
site/out/
3536

Makefile

Lines changed: 141 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,51 +9,149 @@ SHELL := bash
99
# See https://stackoverflow.com/questions/25752543/make-delete-on-error-for-directory-targets
1010
.DELETE_ON_ERROR:
1111

12-
INSTALL_DIR=$(shell go env GOPATH)/bin
13-
GOOS=$(shell go env GOOS)
14-
GOARCH=$(shell go env GOARCH)
15-
VERSION=$(shell ./scripts/version.sh)
16-
17-
bin: $(shell find . -not -path './vendor/*' -type f -name '*.go') go.mod go.sum $(shell find ./examples/templates)
18-
@echo "== This builds slim binaries for command-line usage."
19-
@echo "== Use \"make build\" to embed the site."
20-
21-
mkdir -p ./dist
22-
rm -rf ./dist/coder-slim_*
23-
rm -f ./site/out/bin/coder*
24-
./scripts/build_go_slim.sh \
25-
--compress 6 \
26-
--version "$(VERSION)" \
27-
--output ./dist/ \
28-
linux:amd64,armv7,arm64 \
29-
windows:amd64,arm64 \
30-
darwin:amd64,arm64
31-
.PHONY: bin
32-
33-
build: site/out/index.html $(shell find . -not -path './vendor/*' -type f -name '*.go') go.mod go.sum $(shell find ./examples/templates)
34-
rm -rf ./dist
35-
mkdir -p ./dist
36-
rm -f ./site/out/bin/coder*
37-
38-
# build slim artifacts and copy them to the site output directory
39-
./scripts/build_go_slim.sh \
12+
INSTALL_DIR = $(shell go env GOPATH)/bin
13+
GOOS = $(shell go env GOOS)
14+
GOARCH = $(shell go env GOARCH)
15+
VERSION = $(shell ./scripts/version.sh)
16+
17+
# All ${OS}_${ARCH} combos we build for. Windows binaries have the .exe suffix.
18+
OS_ARCHES = \
19+
linux_amd64 linux_arm64 linux_armv7 \
20+
darwin_amd64 darwin_arm64 \
21+
windows_amd64.exe windows_arm64.exe
22+
23+
# Archive formats and their corresponding ${OS}_${ARCH} combos.
24+
ARCHIVE_TAR_GZ = linux_amd64 linux_arm64 linux_armv7
25+
ARCHIVE_ZIP = \
26+
darwin darwin_arm64 \
27+
windows_amd64 windows_arm64
28+
29+
# All ${OS}_${ARCH} combos we build packages for.
30+
PACKAGE_OS_ARCHES = linux_amd64 linux_armv7 linux_arm64
31+
32+
# All package formats we build.
33+
PACKAGE_FORMATS = apk deb rpm
34+
35+
bin build-slim: $(addprefix build/coder-slim_$(VERSION)_,$(OS_ARCHES))
36+
.PHONY: bin build-slim
37+
38+
build build-full: $(addprefix build/coder_$(VERSION)_,$(OS_ARCHES))
39+
.PHONY: build build-full
40+
41+
# Redirect from version-less targets to the versioned ones. This is kinda gross
42+
# since it's make shelling out to make, but it's the easiest way less we write
43+
# out every target manually.
44+
$(addprefix build/coder_,$(OS_ARCHES)): site/out/index.html build-slim
45+
@target="coder_$(VERSION)_$(@:build/coder_%=%)"
46+
@$(MAKE) \
47+
--no-print-directory \
48+
--assume-old site/out/index.html \
49+
--assume-old build-slim \
50+
"build/$$target"
51+
@rm -f "$@"
52+
@ln -s "$$target" "$@"
53+
.PHONY: $(addprefix build/coder_,$(OS_ARCHES))
54+
55+
$(addprefix build/coder-slim_,$(OS_ARCHES)):
56+
@target="coder-slim_$(VERSION)_$(@:build/coder-slim_%=%)"
57+
@echo $(MAKE) \
58+
--no-print-directory \
59+
"build/$$target"
60+
@rm -f "$@"
61+
@ln -s "$$target" "$@"
62+
.PHONY: $(addprefix build/coder-slim_,$(OS_ARCHES))
63+
64+
# "full" binaries always depend on all "slim" binaries.
65+
$(addprefix build/coder_$(VERSION)_,$(OS_ARCHES)): site/out/index.html build-slim
66+
67+
# This task handles all builds, for both "full" and "slim" binaries. It parses
68+
# the target name to get the metadata for the build, so it must be specified in
69+
# this format:
70+
# build/coder(-slim)?_$version_$os_$arch(.exe)?
71+
$(addprefix build/coder_$(VERSION)_,$(OS_ARCHES)) $(addprefix build/coder-slim_$(VERSION)_,$(OS_ARCHES)): \
72+
go.mod go.sum \
73+
$(shell find . -not -path './vendor/*' -type f -name '*.go') \
74+
$(shell find ./examples/templates)
75+
76+
@mkdir -p build
77+
@mode=$$([[ "$@" = build/coder-slim* ]] && echo "slim" || echo "full")
78+
@os=$$(echo $@ | cut -d_ -f3)
79+
@arch=$$(echo $@ | cut -d_ -f4)
80+
@if [ "$$mode" != "full" ] && [ "$$mode" != "slim" ]; then
81+
@echo "Invalid build mode: $$mode"
82+
@exit 1
83+
@fi
84+
@if [[ "$$os" == "windows" ]] && [[ "$$arch" == *.exe ]]; then
85+
@arch=$${arch%.exe}
86+
@fi
87+
88+
@build_args=( \
89+
--os "$$os" \
90+
--arch "$$arch" \
4091
--version "$(VERSION)" \
41-
--compress 6 \
42-
--output ./dist/ \
43-
linux:amd64,armv7,arm64 \
44-
windows:amd64,arm64 \
45-
darwin:amd64,arm64
46-
47-
# build not-so-slim artifacts with the default name format
48-
./scripts/build_go_matrix.sh \
92+
--output "$@" \
93+
)
94+
@if [ "$$mode" == "slim" ]; then
95+
@build_args+=(--slim)
96+
@fi
97+
98+
./scripts/build_go.sh "$${build_args[@]}"
99+
100+
# This task builds all archives. It parses the target name to get the metadata
101+
# for the build, so it must be specified in this format:
102+
# build/coder_${version}_${os}_${arch}.${format}
103+
#
104+
# The following OS/arch/format combinations are supported:
105+
# .tar.gz: linux_amd64, linux_arm64, linux_armv7
106+
# .zip: darwin_amd64, darwin_arm64, windows_amd64, windows_arm64
107+
#
108+
# This depends on build-full because it's difficult to do dynamic dependencies.
109+
$(foreach os_arch, $(ARCHIVE_TAR_GZ), build/coder_$(VERSION)_$(os_arch).tar.gz) \
110+
$(foreach os_arch, $(ARCHIVE_ZIP), build/coder_$(VERSION)_$(os_arch).zip): \
111+
build-full
112+
113+
@mkdir -p build
114+
@os=$$(echo $@ | cut -d_ -f3)
115+
@arch_format=$$(echo $@ | cut -d_ -f4)
116+
@arch=$$(echo $$arch_format | cut -d. -f1)
117+
@format=$${arch_format#*.}
118+
119+
@bin_ext=""
120+
@if [[ "$$os" == "windows" ]]; then
121+
@bin_ext=".exe"
122+
@fi
123+
124+
./script/archive.sh \
125+
--format "$$format" \
126+
--output "$@" \
127+
"build/coder_$(VERSION)_$${os}_$${arch}$${bin_ext}"
128+
129+
# This task builds all packages. It parses the target name to get the metadata
130+
# for the build, so it must be specified in this format:
131+
# build/coder_${version}_${os}_${arch}.${format}
132+
#
133+
# Supports apk, deb, rpm for all linux targets.
134+
#
135+
# This depends on build-full because it's difficult to do dynamic dependencies.
136+
$(foreach os_arch, $(PACKAGE_OS_ARCHES), $(addprefix build/coder_$(VERSION)_$(os_arch).,$(PACKAGE_FORMATS))): \
137+
build-full
138+
139+
@mkdir -p build
140+
@os=$$(echo $@ | cut -d_ -f3)
141+
@arch_format=$$(echo $@ | cut -d_ -f4)
142+
@arch=$$(echo $$arch_format | cut -d. -f1)
143+
@format=$${arch_format#*.}
144+
145+
./scripts/package.sh \
146+
--arch "$$arch" \
147+
--format "$$format" \
49148
--version "$(VERSION)" \
50-
--output ./dist/ \
51-
--archive \
52-
--package-linux \
53-
linux:amd64,armv7,arm64 \
54-
windows:amd64,arm64 \
55-
darwin:amd64,arm64
56-
.PHONY: build
149+
--output "$@" \
150+
"build/coder_$(VERSION)_$${os}_$${arch}"
151+
152+
clean:
153+
rm -rf build
154+
.PHONY: clean
57155

58156
# Runs migrations to output a dump of the database.
59157
coderd/database/dump.sql: coderd/database/dump/main.go $(wildcard coderd/database/migrations/*.sql)

scripts/archive.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# utility and then notarized using the `gon` utility, which may take a while.
1515
# $AC_APPLICATION_IDENTITY must be set and the signing certificate must be
1616
# imported for this to work. Also, the input binary must already be signed with
17-
# the `codesign` tool.=
17+
# the `codesign` tool.
1818
#
1919
# If the --agpl parameter is specified, only includes AGPL license.
2020
#
@@ -26,7 +26,7 @@ source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
2626

2727
format=""
2828
output_path=""
29-
sign_darwin=0
29+
sign_darwin="${CODER_SIGN_DARWIN:-0}"
3030
agpl="${CODER_BUILD_AGPL:-0}"
3131

3232
args="$(getopt -o "" -l format:,output:,sign-darwin,agpl -- "$@")"

scripts/build_go.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ version=""
3232
os="${GOOS:-linux}"
3333
arch="${GOARCH:-amd64}"
3434
slim="${CODER_SLIM_BUILD:-0}"
35-
sign_darwin=0
35+
sign_darwin="${CODER_SIGN_DARWIN:-0}"
3636
output_path=""
3737
agpl="${CODER_BUILD_AGPL:-0}"
3838

@@ -53,6 +53,7 @@ while true; do
5353
shift 2
5454
;;
5555
--output)
56+
mkdir -p "$(dirname "$2")"
5657
output_path="$(realpath "$2")"
5758
shift 2
5859
;;

scripts/package.sh

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
11
#!/usr/bin/env bash
22

3-
# This script creates Linux packages for the given binary. It will output a
4-
# .rpm, .deb and .apk file in the same directory as the input file with the same
5-
# filename (except the package format suffix).
3+
# This script creates a Linux package for the given binary.
64
#
7-
# ./package.sh --arch amd64 [--version 1.2.3] path/to/coder
5+
# ./package.sh --arch amd64 --format "(apk|deb|rpm)" --output "path/to/coder.apk" [--version 1.2.3] path/to/coder
86
#
9-
# The --arch parameter is required. If no version is specified, defaults to the
10-
# version from ./version.sh.
7+
# If no version is specified, defaults to the version from ./version.sh.
118

129
set -euo pipefail
1310
# shellcheck source=scripts/lib.sh
1411
source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
1512

16-
version=""
1713
arch=""
14+
format=""
15+
output_path=""
16+
version=""
1817

19-
args="$(getopt -o "" -l arch:,version: -- "$@")"
18+
args="$(getopt -o "" -l arch:,format:,output:,version: -- "$@")"
2019
eval set -- "$args"
2120
while true; do
2221
case "$1" in
2322
--arch)
2423
arch="$2"
2524
shift 2
2625
;;
26+
--format)
27+
format="$2"
28+
shift 2
29+
;;
30+
--output)
31+
mkdir -p "$(dirname "$2")"
32+
output_path="$(realpath "$2")"
33+
shift 2
34+
;;
2735
--version)
2836
version="$2"
2937
shift 2
@@ -41,6 +49,12 @@ done
4149
if [[ "$arch" == "" ]]; then
4250
error "--arch is a required parameter"
4351
fi
52+
if [[ "$format" != "apk" ]] && [[ "$format" != "deb" ]] && [[ "$format" != "rpm" ]]; then
53+
error "--format is a required parameter and must be one of 'apk', 'deb', or 'rpm'"
54+
fi
55+
if [[ "$output_path" == "" ]]; then
56+
error "--output is a required parameter"
57+
fi
4458

4559
if [[ "$#" != 1 ]]; then
4660
error "Exactly one argument must be provided to this script, $# were supplied"
@@ -74,18 +88,12 @@ ln -P "$(realpath coder.service)" "$temp_dir/"
7488
ln -P "$(realpath preinstall.sh)" "$temp_dir/"
7589
ln -P "$(realpath scripts/nfpm.yaml)" "$temp_dir/"
7690

77-
cd "$temp_dir"
78-
79-
formats=(apk deb rpm)
80-
for format in "${formats[@]}"; do
81-
output_path="$input_file.$format"
82-
log "--- Building $format package ($output_path)"
83-
91+
pushd "$temp_dir"
8492
GOARCH="$arch" CODER_VERSION="$version" nfpm package \
8593
-f nfpm.yaml \
8694
-p "$format" \
87-
-t "$output_path"
88-
done
95+
-t "$output_path" \
96+
1>&2
97+
popd
8998

90-
cdroot
9199
rm -rf "$temp_dir"

0 commit comments

Comments
 (0)