Skip to content

Makefile buff-ification #3700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
chore: buff makefile pt. 1
  • Loading branch information
deansheather committed Aug 26, 2022
commit 73e4db46ada03f7f173c0f21b6b4e372c5fa53e2
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ site/**/*.typegen.ts
site/build-storybook.log

# Build
build/
dist/
site/out/

Expand Down
184 changes: 141 additions & 43 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,149 @@ SHELL := bash
# See https://stackoverflow.com/questions/25752543/make-delete-on-error-for-directory-targets
.DELETE_ON_ERROR:

INSTALL_DIR=$(shell go env GOPATH)/bin
GOOS=$(shell go env GOOS)
GOARCH=$(shell go env GOARCH)
VERSION=$(shell ./scripts/version.sh)

bin: $(shell find . -not -path './vendor/*' -type f -name '*.go') go.mod go.sum $(shell find ./examples/templates)
@echo "== This builds slim binaries for command-line usage."
@echo "== Use \"make build\" to embed the site."

mkdir -p ./dist
rm -rf ./dist/coder-slim_*
rm -f ./site/out/bin/coder*
./scripts/build_go_slim.sh \
--compress 6 \
--version "$(VERSION)" \
--output ./dist/ \
linux:amd64,armv7,arm64 \
windows:amd64,arm64 \
darwin:amd64,arm64
.PHONY: bin

build: site/out/index.html $(shell find . -not -path './vendor/*' -type f -name '*.go') go.mod go.sum $(shell find ./examples/templates)
rm -rf ./dist
mkdir -p ./dist
rm -f ./site/out/bin/coder*

# build slim artifacts and copy them to the site output directory
./scripts/build_go_slim.sh \
INSTALL_DIR = $(shell go env GOPATH)/bin
GOOS = $(shell go env GOOS)
GOARCH = $(shell go env GOARCH)
VERSION = $(shell ./scripts/version.sh)

# All ${OS}_${ARCH} combos we build for. Windows binaries have the .exe suffix.
OS_ARCHES = \
linux_amd64 linux_arm64 linux_armv7 \
darwin_amd64 darwin_arm64 \
windows_amd64.exe windows_arm64.exe

# Archive formats and their corresponding ${OS}_${ARCH} combos.
ARCHIVE_TAR_GZ = linux_amd64 linux_arm64 linux_armv7
ARCHIVE_ZIP = \
darwin darwin_arm64 \
windows_amd64 windows_arm64

# All ${OS}_${ARCH} combos we build packages for.
PACKAGE_OS_ARCHES = linux_amd64 linux_armv7 linux_arm64

# All package formats we build.
PACKAGE_FORMATS = apk deb rpm

bin build-slim: $(addprefix build/coder-slim_$(VERSION)_,$(OS_ARCHES))
.PHONY: bin build-slim

build build-full: $(addprefix build/coder_$(VERSION)_,$(OS_ARCHES))
.PHONY: build build-full

# Redirect from version-less targets to the versioned ones. This is kinda gross
# since it's make shelling out to make, but it's the easiest way less we write
# out every target manually.
$(addprefix build/coder_,$(OS_ARCHES)): site/out/index.html build-slim
@target="coder_$(VERSION)_$(@:build/coder_%=%)"
@$(MAKE) \
--no-print-directory \
--assume-old site/out/index.html \
--assume-old build-slim \
"build/$$target"
@rm -f "$@"
@ln -s "$$target" "$@"
.PHONY: $(addprefix build/coder_,$(OS_ARCHES))

$(addprefix build/coder-slim_,$(OS_ARCHES)):
@target="coder-slim_$(VERSION)_$(@:build/coder-slim_%=%)"
@echo $(MAKE) \
--no-print-directory \
"build/$$target"
@rm -f "$@"
@ln -s "$$target" "$@"
.PHONY: $(addprefix build/coder-slim_,$(OS_ARCHES))

# "full" binaries always depend on all "slim" binaries.
$(addprefix build/coder_$(VERSION)_,$(OS_ARCHES)): site/out/index.html build-slim

# This task handles all builds, for both "full" and "slim" binaries. It parses
# the target name to get the metadata for the build, so it must be specified in
# this format:
# build/coder(-slim)?_$version_$os_$arch(.exe)?
$(addprefix build/coder_$(VERSION)_,$(OS_ARCHES)) $(addprefix build/coder-slim_$(VERSION)_,$(OS_ARCHES)): \
go.mod go.sum \
$(shell find . -not -path './vendor/*' -type f -name '*.go') \
$(shell find ./examples/templates)

@mkdir -p build
@mode=$$([[ "$@" = build/coder-slim* ]] && echo "slim" || echo "full")
@os=$$(echo $@ | cut -d_ -f3)
@arch=$$(echo $@ | cut -d_ -f4)
@if [ "$$mode" != "full" ] && [ "$$mode" != "slim" ]; then
@echo "Invalid build mode: $$mode"
@exit 1
@fi
@if [[ "$$os" == "windows" ]] && [[ "$$arch" == *.exe ]]; then
@arch=$${arch%.exe}
@fi

@build_args=( \
--os "$$os" \
--arch "$$arch" \
--version "$(VERSION)" \
--compress 6 \
--output ./dist/ \
linux:amd64,armv7,arm64 \
windows:amd64,arm64 \
darwin:amd64,arm64

# build not-so-slim artifacts with the default name format
./scripts/build_go_matrix.sh \
--output "$@" \
)
@if [ "$$mode" == "slim" ]; then
@build_args+=(--slim)
@fi

./scripts/build_go.sh "$${build_args[@]}"

# This task builds all archives. It parses the target name to get the metadata
# for the build, so it must be specified in this format:
# build/coder_${version}_${os}_${arch}.${format}
#
# The following OS/arch/format combinations are supported:
# .tar.gz: linux_amd64, linux_arm64, linux_armv7
# .zip: darwin_amd64, darwin_arm64, windows_amd64, windows_arm64
#
# This depends on build-full because it's difficult to do dynamic dependencies.
$(foreach os_arch, $(ARCHIVE_TAR_GZ), build/coder_$(VERSION)_$(os_arch).tar.gz) \
$(foreach os_arch, $(ARCHIVE_ZIP), build/coder_$(VERSION)_$(os_arch).zip): \
build-full

@mkdir -p build
@os=$$(echo $@ | cut -d_ -f3)
@arch_format=$$(echo $@ | cut -d_ -f4)
@arch=$$(echo $$arch_format | cut -d. -f1)
@format=$${arch_format#*.}

@bin_ext=""
@if [[ "$$os" == "windows" ]]; then
@bin_ext=".exe"
@fi

./script/archive.sh \
--format "$$format" \
--output "$@" \
"build/coder_$(VERSION)_$${os}_$${arch}$${bin_ext}"

# This task builds all packages. It parses the target name to get the metadata
# for the build, so it must be specified in this format:
# build/coder_${version}_${os}_${arch}.${format}
#
# Supports apk, deb, rpm for all linux targets.
#
# This depends on build-full because it's difficult to do dynamic dependencies.
$(foreach os_arch, $(PACKAGE_OS_ARCHES), $(addprefix build/coder_$(VERSION)_$(os_arch).,$(PACKAGE_FORMATS))): \
build-full

@mkdir -p build
@os=$$(echo $@ | cut -d_ -f3)
@arch_format=$$(echo $@ | cut -d_ -f4)
@arch=$$(echo $$arch_format | cut -d. -f1)
@format=$${arch_format#*.}

./scripts/package.sh \
--arch "$$arch" \
--format "$$format" \
--version "$(VERSION)" \
--output ./dist/ \
--archive \
--package-linux \
linux:amd64,armv7,arm64 \
windows:amd64,arm64 \
darwin:amd64,arm64
.PHONY: build
--output "$@" \
"build/coder_$(VERSION)_$${os}_$${arch}"

clean:
rm -rf build
.PHONY: clean

# Runs migrations to output a dump of the database.
coderd/database/dump.sql: coderd/database/dump/main.go $(wildcard coderd/database/migrations/*.sql)
Expand Down
4 changes: 2 additions & 2 deletions scripts/archive.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# utility and then notarized using the `gon` utility, which may take a while.
# $AC_APPLICATION_IDENTITY must be set and the signing certificate must be
# imported for this to work. Also, the input binary must already be signed with
# the `codesign` tool.=
# the `codesign` tool.
#
# If the --agpl parameter is specified, only includes AGPL license.
#
Expand All @@ -26,7 +26,7 @@ source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"

format=""
output_path=""
sign_darwin=0
sign_darwin="${CODER_SIGN_DARWIN:-0}"
agpl="${CODER_BUILD_AGPL:-0}"

args="$(getopt -o "" -l format:,output:,sign-darwin,agpl -- "$@")"
Expand Down
3 changes: 2 additions & 1 deletion scripts/build_go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ version=""
os="${GOOS:-linux}"
arch="${GOARCH:-amd64}"
slim="${CODER_SLIM_BUILD:-0}"
sign_darwin=0
sign_darwin="${CODER_SIGN_DARWIN:-0}"
output_path=""
agpl="${CODER_BUILD_AGPL:-0}"

Expand All @@ -53,6 +53,7 @@ while true; do
shift 2
;;
--output)
mkdir -p "$(dirname "$2")"
output_path="$(realpath "$2")"
shift 2
;;
Expand Down
44 changes: 26 additions & 18 deletions scripts/package.sh
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
#!/usr/bin/env bash

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

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

version=""
arch=""
format=""
output_path=""
version=""

args="$(getopt -o "" -l arch:,version: -- "$@")"
args="$(getopt -o "" -l arch:,format:,output:,version: -- "$@")"
eval set -- "$args"
while true; do
case "$1" in
--arch)
arch="$2"
shift 2
;;
--format)
format="$2"
shift 2
;;
--output)
mkdir -p "$(dirname "$2")"
output_path="$(realpath "$2")"
shift 2
;;
--version)
version="$2"
shift 2
Expand All @@ -41,6 +49,12 @@ done
if [[ "$arch" == "" ]]; then
error "--arch is a required parameter"
fi
if [[ "$format" != "apk" ]] && [[ "$format" != "deb" ]] && [[ "$format" != "rpm" ]]; then
error "--format is a required parameter and must be one of 'apk', 'deb', or 'rpm'"
fi
if [[ "$output_path" == "" ]]; then
error "--output is a required parameter"
fi

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

cd "$temp_dir"

formats=(apk deb rpm)
for format in "${formats[@]}"; do
output_path="$input_file.$format"
log "--- Building $format package ($output_path)"

pushd "$temp_dir"
GOARCH="$arch" CODER_VERSION="$version" nfpm package \
-f nfpm.yaml \
-p "$format" \
-t "$output_path"
done
-t "$output_path" \
1>&2
popd

cdroot
rm -rf "$temp_dir"