From 9852c51732075a3dcdfc6d392215d00a713e62d8 Mon Sep 17 00:00:00 2001 From: Tom Wieczorek Date: Tue, 9 Jan 2024 16:02:07 +0100 Subject: [PATCH] Provide empty asset offsets without codegen This removes the necessity to generate the empty asset file in the Makefile. Switch between the empty and generated files at compile time via the noembedbins go build tag. Signed-off-by: Tom Wieczorek --- .github/workflows/go.yml | 7 +----- Makefile | 37 ++++++++++++---------------- hack/gen-bindata/gen_bindata.go | 3 ++- hack/gen-bindata/gen_bindata_test.go | 3 ++- pkg/assets/offsets_noembedbins.go | 22 +++++++++++++++++ 5 files changed, 43 insertions(+), 29 deletions(-) create mode 100644 pkg/assets/offsets_noembedbins.go diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index bb7902b81bf1..7b1b29b464a2 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -137,12 +137,7 @@ jobs: build/cache/go/mod - name: Run unit tests - env: - EMBEDDED_BINS_BUILDMODE: none - run: | - make bindata - make --touch codegen - make check-unit + run: make check-unit - name: Validate OCI images manifests run: make check-image-validity diff --git a/Makefile b/Makefile index a7cf558a09eb..6f6507fbc019 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,8 @@ EMBEDDED_BINS_BUILDMODE ?= docker TARGET_OS ?= linux BUILD_UID ?= $(shell id -u) BUILD_GID ?= $(shell id -g) -BUILD_GO_FLAGS := -tags osusergo -buildvcs=false -trimpath +BUILD_GO_TAGS ?= osusergo +BUILD_GO_FLAGS = -tags $(subst $(space),$(comma),$(BUILD_GO_TAGS)) -buildvcs=false -trimpath BUILD_CGO_CFLAGS := BUILD_GO_LDFLAGS_EXTRA := DEBUG ?= false @@ -157,17 +158,11 @@ static/zz_generated_assets.go: $(shell find $(static_asset_dirs) -type f) CGO_ENABLED=0 $(GO) install github.com/kevinburke/go-bindata/go-bindata@v$(go-bindata_version) $(GO_ENV) go-bindata -o '$@' -pkg static -prefix static $(patsubst %,%/...,$(static_asset_dirs)) -codegen_targets += pkg/assets/zz_generated_offsets_$(TARGET_OS).go -zz_os = $(patsubst pkg/assets/zz_generated_offsets_%.go,%,$@) -print_empty_generated_offsets = printf "%s\n\n%s\n%s\n" \ - "package assets" \ - "var BinData = map[string]struct{ offset, size, originalSize int64 }{}" \ - "var BinDataSize int64" ifeq ($(EMBEDDED_BINS_BUILDMODE),none) -pkg/assets/zz_generated_offsets_linux.go pkg/assets/zz_generated_offsets_windows.go: - rm -f bindata_$(zz_os) && touch bindata_$(zz_os) - $(print_empty_generated_offsets) > $@ +BUILD_GO_TAGS += noembedbins else +codegen_targets += pkg/assets/zz_generated_offsets_$(TARGET_OS).go +zz_os = $(patsubst pkg/assets/zz_generated_offsets_%.go,%,$@) pkg/assets/zz_generated_offsets_linux.go: .bins.linux.stamp pkg/assets/zz_generated_offsets_windows.go: .bins.windows.stamp pkg/assets/zz_generated_offsets_linux.go pkg/assets/zz_generated_offsets_windows.go: .k0sbuild.docker-image.k0s go.sum @@ -176,10 +171,6 @@ pkg/assets/zz_generated_offsets_linux.go pkg/assets/zz_generated_offsets_windows -prefix embedded-bins/staging/$(zz_os)/ embedded-bins/staging/$(zz_os)/bin endif -# needed for unit tests on macos -pkg/assets/zz_generated_offsets_darwin.go: - $(print_empty_generated_offsets) > $@ - k0s: TARGET_OS = linux k0s: BUILD_GO_CGO_ENABLED = 1 k0s: .k0sbuild.docker-image.k0s @@ -189,11 +180,15 @@ k0s.exe: BUILD_GO_CGO_ENABLED = 0 k0s.exe k0s: $(GO_SRCS) $(codegen_targets) go.sum CGO_ENABLED=$(BUILD_GO_CGO_ENABLED) CGO_CFLAGS='$(BUILD_CGO_CFLAGS)' GOOS=$(TARGET_OS) $(GO) build $(BUILD_GO_FLAGS) -ldflags='$(LD_FLAGS)' -o $@.code main.go - cat $@.code bindata_$(TARGET_OS) > $@.tmp \ - && rm -f $@.code \ - && printf "\nk0s size: %s\n\n" "$$(du -sh $@.tmp | cut -f1)" \ - && chmod +x $@.tmp \ - && mv $@.tmp $@ +ifeq ($(EMBEDDED_BINS_BUILDMODE),none) + mv -- $@.code $@.tmp +else + cat -- $@.code bindata_$(TARGET_OS) >$@.tmp + rm -f -- $@.code +endif + chmod +x -- $@.tmp + mv -- $@.tmp $@ + printf '\nk0s size: %s\n\n' "$$(du -sh -- $@ | cut -f1)" .bins.windows.stamp .bins.linux.stamp: embedded-bins/Makefile.variables $(MAKE) -C embedded-bins \ @@ -253,8 +248,8 @@ check-unit: GO_TEST_RACE ?= else check-unit: GO_TEST_RACE ?= -race endif -check-unit: go.sum codegen - CGO_CFLAGS='$(BUILD_CGO_CFLAGS)' $(GO) test -tags=hack $(GO_TEST_RACE) -ldflags='$(LD_FLAGS)' `$(GO) list -tags=hack $(GO_CHECK_UNIT_DIRS)` +check-unit: go.sum static/zz_generated_assets.go + CGO_CFLAGS='$(BUILD_CGO_CFLAGS)' $(GO) test -tags=hack,noembedbins $(GO_TEST_RACE) -ldflags='$(LD_FLAGS)' `$(GO) list -tags=hack $(GO_CHECK_UNIT_DIRS)` .PHONY: check-image-validity check-image-validity: go.sum diff --git a/hack/gen-bindata/gen_bindata.go b/hack/gen-bindata/gen_bindata.go index 64579feba0b2..ad72cb9dd62f 100644 --- a/hack/gen-bindata/gen_bindata.go +++ b/hack/gen-bindata/gen_bindata.go @@ -180,7 +180,8 @@ func GenBindata(name string, args ...string) error { }) } -var packageTemplate = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT. +var packageTemplate = template.Must(template.New("").Parse(`//go:build !noembedbins +// Code generated by go generate; DO NOT EDIT. // datafile: {{ .OutFile }} diff --git a/hack/gen-bindata/gen_bindata_test.go b/hack/gen-bindata/gen_bindata_test.go index 07b10270400b..e87230e904b9 100644 --- a/hack/gen-bindata/gen_bindata_test.go +++ b/hack/gen-bindata/gen_bindata_test.go @@ -49,7 +49,8 @@ func TestGenBindata_BasicExecution(t *testing.T) { tmpDir := t.TempDir() defer testutil.Chdir(t, tmpDir)() - const expected = `// Code generated by go generate; DO NOT EDIT. + const expected = `//go:build !noembedbins +// Code generated by go generate; DO NOT EDIT. // datafile: ./bindata diff --git a/pkg/assets/offsets_noembedbins.go b/pkg/assets/offsets_noembedbins.go new file mode 100644 index 000000000000..d1380651b709 --- /dev/null +++ b/pkg/assets/offsets_noembedbins.go @@ -0,0 +1,22 @@ +//go:build noembedbins || (!linux && !windows) + +/* +Copyright 2024 k0s authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package assets + +var BinData = map[string]struct{ offset, size, originalSize int64 }{} +var BinDataSize int64