Skip to content

Commit 4885076

Browse files
committed
chore: update makefile to use new build scripts
1 parent 9e279a9 commit 4885076

File tree

5 files changed

+175
-27
lines changed

5 files changed

+175
-27
lines changed

Makefile

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,53 @@
11
.DEFAULT_GOAL := build
22

3+
# use a single bash shell for each job, and immediately exit on failure
4+
SHELL := bash
5+
.SHELLFLAGS = -ceu
6+
.ONESHELL:
7+
8+
# This doesn't work on directories.
9+
# See https://stackoverflow.com/questions/25752543/make-delete-on-error-for-directory-targets
10+
.DELETE_ON_ERROR:
11+
312
INSTALL_DIR=$(shell go env GOPATH)/bin
413
GOOS=$(shell go env GOOS)
514
GOARCH=$(shell go env GOARCH)
15+
VERSION=$(shell ./scripts/version.sh)
616

717
bin: $(shell find . -not -path './vendor/*' -type f -name '*.go') go.mod go.sum $(shell find ./examples/templates)
8-
@echo "== This builds binaries for command-line usage."
18+
@echo "== This builds slim binaries for command-line usage."
919
@echo "== Use \"make build\" to embed the site."
10-
goreleaser build --snapshot --rm-dist --single-target
1120

12-
build: dist/artifacts.json
21+
rm -rf ./dist/coder-slim_*
22+
./scripts/build_go_slim.sh \
23+
--version "$(VERSION)" \
24+
--output ./dist/ \
25+
linux:amd64,armv7,arm64 \
26+
windows:amd64,arm64 \
27+
darwin:amd64,arm64
28+
.PHONY: bin
29+
30+
build: site/out/index.html $(shell find . -not -path './vendor/*' -type f -name '*.go') go.mod go.sum $(shell find ./examples/templates)
31+
rm -rf ./dist
32+
mkdir -p ./dist
33+
34+
# build slim artifacts and copy them to the site output directory
35+
./scripts/build_go_slim.sh \
36+
--version "$(VERSION)" \
37+
--output ./dist/ \
38+
linux:amd64,armv7,arm64 \
39+
windows:amd64,arm64 \
40+
darwin:amd64,arm64
41+
42+
# build not-so-slim artifacts with the default name format
43+
./scripts/build_go_matrix.sh \
44+
--version "$(VERSION)" \
45+
--output ./dist/ \
46+
--archive \
47+
--package-linux \
48+
linux:amd64,armv7,arm64 \
49+
windows:amd64,arm64 \
50+
darwin:amd64,arm64
1351
.PHONY: build
1452

1553
# Runs migrations to output a dump of the database.
@@ -24,9 +62,6 @@ dev:
2462
./scripts/develop.sh
2563
.PHONY: dev
2664

27-
dist/artifacts.json: site/out/index.html $(shell find . -not -path './vendor/*' -type f -name '*.go') go.mod go.sum $(shell find ./examples/templates)
28-
goreleaser release --snapshot --rm-dist --skip-sign
29-
3065
fmt/prettier:
3166
@echo "--- prettier"
3267
# Avoid writing files in CI to reduce file write activity
@@ -49,20 +84,34 @@ ifdef CI
4984
else
5085
shfmt -w $(shell shfmt -f .)
5186
endif
87+
.PHONY: fmt/shfmt
5288

5389
fmt: fmt/prettier fmt/terraform fmt/shfmt
5490
.PHONY: fmt
5591

5692
gen: coderd/database/querier.go peerbroker/proto/peerbroker.pb.go provisionersdk/proto/provisioner.pb.go provisionerd/proto/provisionerd.pb.go site/src/api/typesGenerated.ts
93+
.PHONY: gen
94+
95+
install: site/out/index.html $(shell find . -not -path './vendor/*' -type f -name '*.go') go.mod go.sum $(shell find ./examples/templates)
96+
@output_file="$(INSTALL_DIR)/coder"
5797

58-
install: build
59-
mkdir -p $(INSTALL_DIR)
60-
@echo "--- Copying from bin to $(INSTALL_DIR)"
61-
cp -r ./dist/coder-$(GOOS)_$(GOOS)_$(GOARCH)*/* $(INSTALL_DIR)
62-
@echo "-- CLI available at $(shell ls $(INSTALL_DIR)/coder*)"
98+
@if [[ "$(GOOS)" == "windows" ]]; then
99+
@output_file="$${output_file}.exe"
100+
@fi
101+
102+
@echo "-- Building CLI for $(GOOS) $(GOARCH) at $$output_file"
103+
104+
./scripts/build_go.sh \
105+
--version "$(VERSION)" \
106+
--output "$$output_file" \
107+
--os "$(GOOS)" \
108+
--arch "$(GOARCH)"
109+
110+
@echo
63111
.PHONY: install
64112

65113
lint: lint/shellcheck lint/go
114+
.PHONY: lint
66115

67116
lint/go:
68117
golangci-lint run
@@ -72,6 +121,7 @@ lint/go:
72121
lint/shellcheck: $(shell shfmt -f .)
73122
@echo "--- shellcheck"
74123
shellcheck --external-sources $(shell shfmt -f .)
124+
.PHONY: lint/shellcheck
75125

76126
peerbroker/proto/peerbroker.pb.go: peerbroker/proto/peerbroker.proto
77127
protoc \
@@ -108,19 +158,17 @@ site/src/api/typesGenerated.ts: scripts/apitypings/main.go $(shell find codersdk
108158
go run scripts/apitypings/main.go > site/src/api/typesGenerated.ts
109159
cd site && yarn run format:types
110160

111-
.PHONY: test
112161
test: test-clean
113162
gotestsum -- -v -short ./...
163+
.PHONY: test
114164

115-
.PHONY: test-postgres
116165
test-postgres: test-clean
117166
DB=ci gotestsum --junitfile="gotests.xml" --packages="./..." -- \
118167
-covermode=atomic -coverprofile="gotests.coverage" -timeout=5m \
119168
-coverpkg=./...,github.com/coder/coder/codersdk \
120169
-count=1 -parallel=1 -race -failfast
170+
.PHONY: test-postgres
121171

122-
123-
.PHONY: test-postgres-docker
124172
test-postgres-docker:
125173
docker run \
126174
--env POSTGRES_PASSWORD=postgres \
@@ -134,7 +182,8 @@ test-postgres-docker:
134182
postgres:11 \
135183
-c shared_buffers=1GB \
136184
-c max_connections=1000
185+
.PHONY: test-postgres-docker
137186

138-
.PHONY: test-clean
139187
test-clean:
140188
go clean -testcache
189+
.PHONY: test-clean

scripts/build_go.sh

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
# GOARCH and CODER_SLIM_BUILD=1. If no version is specified, defaults to the
99
# version from ./version.sh.
1010
#
11+
# GOARM can be controlled by suffixing any arm architecture (i.e. arm or arm64)
12+
# with "vX" (e.g. "v7", "v8").
13+
#
1114
# Unless overridden via --output, the built binary will be dropped in
1215
# "$repo_root/dist/coder_$version_$os_$arch" (with a ".exe" suffix for windows
1316
# builds) and the absolute path to the binary will be printed to stdout on
@@ -95,8 +98,18 @@ if [[ "$output_path" == "" ]]; then
9598
fi
9699
build_args+=(-o "$output_path")
97100

98-
# TODO: GOARM
99-
CGO_ENABLED=0 GOOS="$os" GOARCH="$arch" go build \
101+
# Determine GOARM.
102+
arm_version=""
103+
if [[ "$arch" == "arm" ]]; then
104+
arm_version="7"
105+
elif [[ "$arch" == "armv"* ]] || [[ "$arch" == "arm64v"* ]]; then
106+
arm_version="${arch//*v/}"
107+
108+
# Remove the v* suffix.
109+
arch="${arch//v*/}"
110+
fi
111+
112+
CGO_ENABLED=0 GOOS="$os" GOARCH="$arch" GOARM="$arm_version" go build \
100113
"${build_args[@]}" \
101114
./cmd/coder 1>&2
102115

scripts/build_go_matrix.sh

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ while true; do
5151
shift 2
5252
;;
5353
--output)
54-
# realpath fails if the dir doesn't exist.
55-
mkdir -p "$(dirname "$2")"
56-
output_path="$(realpath "$2")"
54+
output_path="$2"
5755
shift 2
5856
;;
5957
--slim)
@@ -93,14 +91,16 @@ if [[ "$output_path" == "" ]]; then
9391
mkdir -p dist
9492
output_path="$(realpath "dist/coder_{version}_{os}_{arch}")"
9593
elif [[ "$output_path" == */ ]]; then
96-
output_path="${output_path}coder_{version_{os}_{arch}"
97-
else
98-
# Verify that it contains {os} and {arch} at least.
99-
if [[ "$output_path" != *"{os}"* ]] || [[ "$output_path" != *"{arch}"* ]]; then
100-
error "Templated output path '$output_path' must contain {os} and {arch}"
101-
fi
94+
output_path="${output_path}coder_{version}_{os}_{arch}"
95+
elif [[ "$output_path" != *"{os}"* ]] || [[ "$output_path" != *"{arch}"* ]]; then
96+
# If the output path isn't a directory (ends with /) then it must have
97+
# template variables.
98+
error "Templated output path '$output_path' must contain {os} and {arch}"
10299
fi
103100

101+
mkdir -p "$(dirname "$output_path")"
102+
output_path="$(realpath "$output_path")"
103+
104104
# Remove the "v" prefix.
105105
version="${version#v}"
106106
if [[ "$version" == "" ]]; then

scripts/build_go_slim.sh

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env bash
2+
3+
# This script builds multiple "slim" Go binaries for Coder with the given OS and
4+
# architecture combinations. This wraps ./build_go_matrix.sh.
5+
#
6+
# Usage: ./build_go_slim.sh [--version 1.2.3+devel.abcdef] [--output dist/] os1:arch1,arch2 os2:arch1 os1:arch3
7+
#
8+
# If no OS:arch combinations are provided, nothing will happen and no error will
9+
# be returned. If no version is specified, defaults to the version from
10+
# ./version.sh
11+
#
12+
# The --output parameter differs from ./build_go_matrix.sh, in that it does not
13+
# accept variables such as `{os}` and `{arch}` and only accepts a directory
14+
# ending with `/`.
15+
#
16+
# The built binaries are additionally copied to the site output directory so
17+
# they can be packaged into non-slim binaries correctly.
18+
19+
set -euo pipefail
20+
# shellcheck source=scripts/lib.sh
21+
source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
22+
23+
version=""
24+
output_path=""
25+
26+
args="$(getopt -o "" -l version:,output: -- "$@")"
27+
eval set -- "$args"
28+
while true; do
29+
case "$1" in
30+
--version)
31+
version="$2"
32+
shift 2
33+
;;
34+
--output)
35+
output_path="$2"
36+
shift 2
37+
;;
38+
--)
39+
shift
40+
break
41+
;;
42+
*)
43+
error "Unrecognized option: $1"
44+
;;
45+
esac
46+
done
47+
48+
# Remove the "v" prefix.
49+
version="${version#v}"
50+
if [[ "$version" == "" ]]; then
51+
version="$(execrelative ./version.sh)"
52+
fi
53+
54+
# Verify the output path.
55+
if [[ "$output_path" == "" ]]; then
56+
# Input paths are relative, so we don't cdroot at the top, but for this case
57+
# we want it to be relative to the root.
58+
cdroot
59+
mkdir -p dist
60+
output_path="$(realpath "dist/coder-slim_{version}_{os}_{arch}")"
61+
elif [[ "$output_path" != */ ]] || [[ "$output_path" == *"{"* ]]; then
62+
error "The output path '$output_path' cannot contain variables and must end with a slash"
63+
else
64+
mkdir -p "$output_path"
65+
output_path="$(realpath "${output_path}coder-slim_{version}_{os}_{arch}")"
66+
fi
67+
68+
./scripts/build_go_matrix.sh \
69+
--version "$version" \
70+
--output "$output_path" \
71+
--slim \
72+
"$@"
73+
74+
cdroot
75+
dest_dir="./site/out/bin"
76+
mkdir -p "$dest_dir"
77+
dest_dir="$(realpath "$dest_dir")"
78+
79+
# Copy the binaries to the site directory.
80+
cd "$(dirname "$output_path")"
81+
for f in ./coder-slim_*; do
82+
f="${f#./}"
83+
dest="$dest_dir/${f//-slim_$version/}"
84+
cp "$f" "$dest"
85+
done

scripts/lib.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env bash
22

33
# This script is meant to be sourced by other scripts. To source this script:
4+
# # shellcheck source=scripts/lib.sh
45
# source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
56

67
set -euo pipefail

0 commit comments

Comments
 (0)