diff --git a/just/default.just b/just/default.just new file mode 100644 index 0000000000000..220c27dca5c23 --- /dev/null +++ b/just/default.just @@ -0,0 +1,5 @@ +set shell := ["bash", "-c"] + +PARALLEL := num_cpus() + +MAP_JUST := "/usr/bin/env -S parallel --shebang --jobs " + PARALLEL + " --colsep ' ' -r " + just_executable() diff --git a/just/deprecated.mk b/just/deprecated.mk new file mode 100644 index 0000000000000..a18d665c05484 --- /dev/null +++ b/just/deprecated.mk @@ -0,0 +1,45 @@ +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) diff --git a/just/git.just b/just/git.just new file mode 100644 index 0000000000000..922286d7ab5ae --- /dev/null +++ b/just/git.just @@ -0,0 +1,26 @@ +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) diff --git a/just/go.just b/just/go.just new file mode 100644 index 0000000000000..5af766295099b --- /dev/null +++ b/just/go.just @@ -0,0 +1,27 @@ +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") diff --git a/op-batcher/Makefile b/op-batcher/Makefile index 22bb7a6138610..1501e0242d9b8 100644 --- a/op-batcher/Makefile +++ b/op-batcher/Makefile @@ -1,52 +1,3 @@ -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 diff --git a/op-batcher/justfile b/op-batcher/justfile new file mode 100644 index 0000000000000..2647debbcb430 --- /dev/null +++ b/op-batcher/justfile @@ -0,0 +1,35 @@ +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 diff --git a/ops/docker/op-stack-go/Dockerfile b/ops/docker/op-stack-go/Dockerfile index e7e183804ab97..da0fc156f027d 100644 --- a/ops/docker/op-stack-go/Dockerfile +++ b/ops/docker/op-stack-go/Dockerfile @@ -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 diff --git a/ops/docker/op-stack-go/Dockerfile.dockerignore b/ops/docker/op-stack-go/Dockerfile.dockerignore index bd700e291e494..edf3bbc1c51db 100644 --- a/ops/docker/op-stack-go/Dockerfile.dockerignore +++ b/ops/docker/op-stack-go/Dockerfile.dockerignore @@ -20,3 +20,5 @@ !/op-alt-da !/go.mod !/go.sum +!/just +!/versions.json