Skip to content

Commit

Permalink
Provide empty asset offsets without codegen
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
twz123 committed Jan 9, 2024
1 parent a8bf7be commit 6a129e6
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 33 deletions.
9 changes: 2 additions & 7 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,7 @@ jobs:
- 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
Expand Down Expand Up @@ -194,9 +191,7 @@ jobs:
GO: go
GO_ENV: ''
run: |
make --touch .k0sbuild.docker-image.k0s go.sum
make bindata
make --touch codegen
make --touch .k0sbuild.docker-image.k0s
make check-unit
smoketests:
Expand Down
47 changes: 23 additions & 24 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 -- [email protected] [email protected]
else
cat -- [email protected] bindata_$(TARGET_OS) >[email protected]
rm -f -- [email protected]
endif
chmod +x -- [email protected]
mv -- [email protected] $@
printf '\nk0s size: %s\n\n' "$$(du -sh -- $@ | cut -f1)"

.bins.windows.stamp .bins.linux.stamp: embedded-bins/Makefile.variables
$(MAKE) -C embedded-bins \
Expand All @@ -206,17 +201,20 @@ codegen: $(codegen_targets)

# bindata contains the parts of codegen which aren't version controlled.
.PHONY: bindata
bindata: static/zz_generated_assets.go pkg/assets/zz_generated_offsets_$(TARGET_OS).go
bindata: static/zz_generated_assets.go
ifneq ($(EMBEDDED_BINS_BUILDMODE),none)
bindata: pkg/assets/zz_generated_offsets_$(TARGET_OS).go
endif

.PHONY: lint-copyright
lint-copyright:
hack/copyright.sh

.PHONY: lint-go
lint-go: GOLANGCI_LINT_FLAGS ?=
lint-go: .k0sbuild.docker-image.k0s go.sum codegen
lint-go: .k0sbuild.docker-image.k0s go.sum bindata
CGO_ENABLED=0 $(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@v$(golangci-lint_version)
CGO_CFLAGS='$(BUILD_CGO_CFLAGS)' $(GO_ENV) golangci-lint run --verbose $(GOLANGCI_LINT_FLAGS) $(GO_LINT_DIRS)
CGO_CFLAGS='$(BUILD_CGO_CFLAGS)' $(GO_ENV) golangci-lint run --verbose --build-tags=$(subst $(space),$(comma),$(BUILD_GO_TAGS)) $(GOLANGCI_LINT_FLAGS) $(GO_LINT_DIRS)

.PHONY: lint
lint: lint-copyright lint-go
Expand Down Expand Up @@ -253,8 +251,9 @@ 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: BUILD_GO_TAGS += hack
check-unit: .k0sbuild.docker-image.k0s go.sum bindata
CGO_CFLAGS='$(BUILD_CGO_CFLAGS)' $(GO) test -tags=$(subst $(space),$(comma),$(BUILD_GO_TAGS)) $(GO_TEST_RACE) -ldflags='$(LD_FLAGS)' `$(GO) list -tags=hack $(GO_CHECK_UNIT_DIRS)`

.PHONY: check-image-validity
check-image-validity: go.sum
Expand Down
3 changes: 2 additions & 1 deletion hack/gen-bindata/gen_bindata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
3 changes: 2 additions & 1 deletion hack/gen-bindata/gen_bindata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions pkg/assets/offsets_other.go
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6a129e6

Please sign in to comment.