Commit c29b2094 authored by Yann Hodique's avatar Yann Hodique Committed by GitHub

make -> just migration recipe (#12883)

* build: add just modules

A lot of our build recipes share similar structures. Move some of
these to modules so we can control them centrally.

* build: add Makefile deprecation helper

This allows us to implement deprecated Makefiles as:

  DEPRECATED_TARGETS := ...
  include ../just/deprecate.mk

* build(docker): add just modules to docker context

We need to make sure our build framework is included in the docker build
context.
Also make sure just is part of the builder image.

* build(op-batcher): migrate build to just
parent 79306cb2
set shell := ["bash", "-c"]
PARALLEL := num_cpus()
MAP_JUST := "/usr/bin/env -S parallel --shebang --jobs " + PARALLEL + " --colsep ' ' -r " + just_executable()
ifeq (, $(shell which tput))
# CI environment typically does not support tput.
banner-style = $1
else
# print in bold red to bring attention.
banner-style = $(shell tput bold)$(shell tput setaf 1)$1$(shell tput sgr0)
endif
# Variable assignments can affect the semantic of the make targets.
# Typical use-case: setting VERSION in a release build, since CI
# doesn't preserve the git environment.
#
# We need to translate:
# "make target VAR=val" to "just VAR=val target"
#
# MAKEFLAGS is a string of the form:
# "abc --foo --bar=baz -- VAR1=val1 VAR2=val2", namely:
# - abc is the concatnation of all short flags
# - --foo and --bar=baz are long options,
# - -- is the separator between flags and variable assignments,
# - VAR1=val1 and VAR2=val2 are variable assignments
#
# Goal: ignore all CLI flags, keep only variable assignments.
#
# First remove the short flags at the beginning, or the first long-flag,
# or if there is no flag at all, the -- separator (which then makes the
# next step a noop). If there's no flag and no variable assignment, the
# result is empty anyway, so the wordlist call is safe (everything is a noop).
tmp-flags = $(wordlist 2,$(words $(MAKEFLAGS)),$(MAKEFLAGS))
# Then remove all long options, including the -- separator, if needed. That
# leaves only variable assignments.
just-flags = $(patsubst --%,,$(tmp-flags))
define make-deprecated-target
$1:
@echo
@printf %s\\n '$(call banner-style,"make $1 $(just-flags)" is deprecated. Please use "just $(just-flags) $1" instead.)'
@echo
just $(just-flags) $1
endef
$(foreach element,$(DEPRECATED_TARGETS),$(eval $(call make-deprecated-target,$(element))))
.PHONY:
$(DEPRECATED_TARGETS)
import 'default.just'
# Set default values for git info
GITCOMMIT := env('GITCOMMIT', `git rev-parse HEAD 2> /dev/null || true`)
GITDATE := env('GITDATE', `git show -s --format='%ct' 2> /dev/null|| true`)
_PROJECT := shell("basename $1", justfile_directory())
_ALL_TAGS := shell("git tag --points-at $1 2> /dev/null || true", GITCOMMIT)
_PROJECT_TAGS := shell("echo $1 | grep ^$2/ | sed s:$2/:: | sort -V", _ALL_TAGS, _PROJECT)
_PREFERRED_TAG := shell("echo $1 | grep -v -- '-rc' | tail -n 1", _PROJECT_TAGS)
_LAST_TAG := shell("echo $1 | tail -n 1", _PROJECT_TAGS)
# Find version tag, prioritizing non-rc release tags
VERSION := shell('if [ -z "$1" ]; then
if [ -z "$2" ]; then
echo "untagged"
else
echo "$2"
fi
else
echo $1
fi', _PREFERRED_TAG, _LAST_TAG)
import 'git.just'
_EXTRALDFLAGS := if os() == "macos" { "-ldflags=-extldflags=-Wl,-ld_classic" } else { "" }
# We use both GOOS/GOARCH and TARGETOS/TARGETARCH to set the build targets.
# From the usage patterns, it looks like TARGETOS/TARGETARCH should take
# precedence if set, and default to GOOS/GOARCH if not set.
# TODO: should we just remove TARGETOS/TARGETARCH altogether eventually?
GOOS := env('GOOS', `go env GOOS`)
GOARCH := env('GOARCH', `go env GOARCH`)
TARGETOS := env('TARGETOS', GOOS)
TARGETARCH := env('TARGETARCH', GOARCH)
GORACE := "0"
_GORACE_FLAG := if GORACE == "1" { "-race " } else { "" }
[private]
go_build BIN PKG *FLAGS:
env GO111MODULE=on GOOS={{TARGETOS}} GOARCH={{TARGETARCH}} CGO_ENABLED=0 go build -v {{_GORACE_FLAG}} {{FLAGS}} -o {{BIN}} {{PKG}}
[private]
go_test SELECTOR *FLAGS:
go test -v {{_GORACE_FLAG}} {{FLAGS}} {{SELECTOR}}
[private]
go_fuzz FUZZ TIME='10s' PKG='': (go_test PKG _EXTRALDFLAGS "-fuzztime" TIME "-fuzz" FUZZ "-run" "NOTAREALTEST")
GITCOMMIT ?= $(shell git rev-parse HEAD)
GITDATE ?= $(shell git show -s --format='%ct')
# Find the github tag that points to this commit. If none are found, set the version string to "untagged"
# Prioritizes release tag, if one exists, over tags suffixed with "-rc"
VERSION ?= $(shell tags=$$(git tag --points-at $(GITCOMMIT) | grep '^op-batcher/' | sed 's/op-batcher\///' | sort -V); \
preferred_tag=$$(echo "$$tags" | grep -v -- '-rc' | tail -n 1); \
if [ -z "$$preferred_tag" ]; then \
if [ -z "$$tags" ]; then \
echo "untagged"; \
else \
echo "$$tags" | tail -n 1; \
fi \
else \
echo $$preferred_tag; \
fi)
DEPRECATED_TARGETS := op-batcher clean test fuzz
LDFLAGSSTRING +=-X main.GitCommit=$(GITCOMMIT)
LDFLAGSSTRING +=-X main.GitDate=$(GITDATE)
LDFLAGSSTRING +=-X main.Version=$(VERSION)
LDFLAGS := -ldflags "$(LDFLAGSSTRING)"
# Use the old Apple linker to workaround broken xcode - https://github.com/golang/go/issues/65169
ifeq ($(shell uname),Darwin)
FUZZLDFLAGS := -ldflags=-extldflags=-Wl,-ld_classic
endif
op-batcher:
env GO111MODULE=on GOOS=$(TARGETOS) GOARCH=$(TARGETARCH) CGO_ENABLED=0 go build -v $(LDFLAGS) -o ./bin/op-batcher ./cmd
clean:
rm bin/op-batcher
test:
go test -v ./...
fuzz:
printf "%s\n" \
"go test $(FUZZLDFLAGS) -run NOTAREALTEST -v -fuzztime 10s -fuzz FuzzChannelConfig_CheckTimeout ./batcher" \
"go test $(FUZZLDFLAGS) -run NOTAREALTEST -v -fuzztime 10s -fuzz FuzzDurationZero ./batcher" \
"go test $(FUZZLDFLAGS) -run NOTAREALTEST -v -fuzztime 10s -fuzz FuzzDurationTimeoutMaxChannelDuration ./batcher" \
"go test $(FUZZLDFLAGS) -run NOTAREALTEST -v -fuzztime 10s -fuzz FuzzDurationTimeoutZeroMaxChannelDuration ./batcher" \
"go test $(FUZZLDFLAGS) -run NOTAREALTEST -v -fuzztime 10s -fuzz FuzzChannelCloseTimeout ./batcher" \
"go test $(FUZZLDFLAGS) -run NOTAREALTEST -v -fuzztime 10s -fuzz FuzzChannelZeroCloseTimeout ./batcher" \
"go test $(FUZZLDFLAGS) -run NOTAREALTEST -v -fuzztime 10s -fuzz FuzzSeqWindowClose ./batcher" \
"go test $(FUZZLDFLAGS) -run NOTAREALTEST -v -fuzztime 10s -fuzz FuzzSeqWindowZeroTimeoutClose ./batcher" \
| parallel -j 8 {}
.PHONY: \
op-batcher \
clean \
test \
fuzz
include ../just/deprecated.mk
import '../just/go.just'
# Build ldflags string
_LDFLAGSSTRING := "'" + trim(
"-X main.GitCommit=" + GITCOMMIT + " " + \
"-X main.GitDate=" + GITDATE + " " + \
"-X main.Version=" + VERSION + " " + \
"") + "'"
BINARY := "./bin/op-batcher"
# Build op-batcher binary
op-batcher: (go_build BINARY "./cmd" "-ldflags" _LDFLAGSSTRING)
# Clean build artifacts
clean:
rm -f {{BINARY}}
# Run tests
test: (go_test "./...")
[private]
batcher_fuzz_task FUZZ TIME='10s': (go_fuzz FUZZ TIME "./batcher")
# Run fuzzing tests
fuzz:
#!{{MAP_JUST}} batcher_fuzz_task
FuzzChannelConfig_CheckTimeout
FuzzDurationZero
FuzzDurationTimeoutMaxChannelDuration
FuzzDurationTimeoutZeroMaxChannelDuration
FuzzChannelCloseTimeout
FuzzChannelZeroCloseTimeout
FuzzSeqWindowClose
FuzzSeqWindowZeroTimeoutClose
......@@ -12,7 +12,12 @@ ARG TARGET_BASE_IMAGE=alpine:3.20
# We may be cross-building for another platform. Specify which platform we need as builder.
FROM --platform=$BUILDPLATFORM golang:1.22.7-alpine3.20 AS builder
RUN apk add --no-cache make gcc musl-dev linux-headers git jq bash
RUN apk add --no-cache curl tar gzip make gcc musl-dev linux-headers git jq bash
# install versioned toolchain
COPY ./versions.json .
RUN curl -L https://github.com/casey/just/releases/download/$(jq -r .just < versions.json)/just-$(jq -r .just < versions.json)-x86_64-unknown-linux-musl.tar.gz | \
tar xz -C /usr/local/bin just
# We copy the go.mod/sum first, so the `go mod download` does not have to re-run if dependencies do not change.
COPY ./go.mod /app/go.mod
......
......@@ -20,3 +20,5 @@
!/op-alt-da
!/go.mod
!/go.sum
!/just
!/versions.json
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment