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 9852c51
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 29 deletions.
7 changes: 1 addition & 6 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 16 additions & 21 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 Down Expand Up @@ -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
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_noembedbins.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 9852c51

Please sign in to comment.