Skip to content

Commit fb6802f

Browse files
committed
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 <[email protected]>
1 parent a8bf7be commit fb6802f

File tree

5 files changed

+51
-33
lines changed

5 files changed

+51
-33
lines changed

.github/workflows/go.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,7 @@ jobs:
139139
- name: Run unit tests
140140
env:
141141
EMBEDDED_BINS_BUILDMODE: none
142-
run: |
143-
make bindata
144-
make --touch codegen
145-
make check-unit
142+
run: make check-unit
146143

147144
- name: Validate OCI images manifests
148145
run: make check-image-validity
@@ -194,9 +191,7 @@ jobs:
194191
GO: go
195192
GO_ENV: ''
196193
run: |
197-
make --touch .k0sbuild.docker-image.k0s go.sum
198-
make bindata
199-
make --touch codegen
194+
make --touch .k0sbuild.docker-image.k0s
200195
make check-unit
201196
202197
smoketests:

Makefile

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ EMBEDDED_BINS_BUILDMODE ?= docker
3030
TARGET_OS ?= linux
3131
BUILD_UID ?= $(shell id -u)
3232
BUILD_GID ?= $(shell id -g)
33-
BUILD_GO_FLAGS := -tags osusergo -buildvcs=false -trimpath
33+
BUILD_GO_TAGS ?= osusergo
34+
BUILD_GO_FLAGS = -tags=$(subst $(space),$(comma),$(BUILD_GO_TAGS)) -buildvcs=false -trimpath
3435
BUILD_CGO_CFLAGS :=
3536
BUILD_GO_LDFLAGS_EXTRA :=
3637
DEBUG ?= false
@@ -157,17 +158,11 @@ static/zz_generated_assets.go: $(shell find $(static_asset_dirs) -type f)
157158
CGO_ENABLED=0 $(GO) install github.com/kevinburke/go-bindata/go-bindata@v$(go-bindata_version)
158159
$(GO_ENV) go-bindata -o '$@' -pkg static -prefix static $(patsubst %,%/...,$(static_asset_dirs))
159160

160-
codegen_targets += pkg/assets/zz_generated_offsets_$(TARGET_OS).go
161-
zz_os = $(patsubst pkg/assets/zz_generated_offsets_%.go,%,$@)
162-
print_empty_generated_offsets = printf "%s\n\n%s\n%s\n" \
163-
"package assets" \
164-
"var BinData = map[string]struct{ offset, size, originalSize int64 }{}" \
165-
"var BinDataSize int64"
166161
ifeq ($(EMBEDDED_BINS_BUILDMODE),none)
167-
pkg/assets/zz_generated_offsets_linux.go pkg/assets/zz_generated_offsets_windows.go:
168-
rm -f bindata_$(zz_os) && touch bindata_$(zz_os)
169-
$(print_empty_generated_offsets) > $@
162+
BUILD_GO_TAGS += noembedbins
170163
else
164+
codegen_targets += pkg/assets/zz_generated_offsets_$(TARGET_OS).go
165+
zz_os = $(patsubst pkg/assets/zz_generated_offsets_%.go,%,$@)
171166
pkg/assets/zz_generated_offsets_linux.go: .bins.linux.stamp
172167
pkg/assets/zz_generated_offsets_windows.go: .bins.windows.stamp
173168
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
176171
-prefix embedded-bins/staging/$(zz_os)/ embedded-bins/staging/$(zz_os)/bin
177172
endif
178173

179-
# needed for unit tests on macos
180-
pkg/assets/zz_generated_offsets_darwin.go:
181-
$(print_empty_generated_offsets) > $@
182-
183174
k0s: TARGET_OS = linux
184175
k0s: BUILD_GO_CGO_ENABLED = 1
185176
k0s: .k0sbuild.docker-image.k0s
@@ -189,11 +180,15 @@ k0s.exe: BUILD_GO_CGO_ENABLED = 0
189180

190181
k0s.exe k0s: $(GO_SRCS) $(codegen_targets) go.sum
191182
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
192-
cat $@.code bindata_$(TARGET_OS) > $@.tmp \
193-
&& rm -f $@.code \
194-
&& printf "\nk0s size: %s\n\n" "$$(du -sh $@.tmp | cut -f1)" \
195-
&& chmod +x $@.tmp \
196-
&& mv $@.tmp $@
183+
ifeq ($(EMBEDDED_BINS_BUILDMODE),none)
184+
185+
else
186+
cat -- [email protected] bindata_$(TARGET_OS) >[email protected]
187+
188+
endif
189+
chmod +x -- [email protected]
190+
191+
printf '\nk0s size: %s\n\n' "$$(du -sh -- $@ | cut -f1)"
197192

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

207202
# bindata contains the parts of codegen which aren't version controlled.
208203
.PHONY: bindata
209-
bindata: static/zz_generated_assets.go pkg/assets/zz_generated_offsets_$(TARGET_OS).go
204+
bindata: static/zz_generated_assets.go
205+
ifneq ($(EMBEDDED_BINS_BUILDMODE),none)
206+
bindata: pkg/assets/zz_generated_offsets_$(TARGET_OS).go
207+
endif
210208

211209
.PHONY: lint-copyright
212210
lint-copyright:
213211
hack/copyright.sh
214212

215213
.PHONY: lint-go
216214
lint-go: GOLANGCI_LINT_FLAGS ?=
217-
lint-go: .k0sbuild.docker-image.k0s go.sum codegen
215+
lint-go: .k0sbuild.docker-image.k0s go.sum bindata
218216
CGO_ENABLED=0 $(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@v$(golangci-lint_version)
219-
CGO_CFLAGS='$(BUILD_CGO_CFLAGS)' $(GO_ENV) golangci-lint run --verbose $(GOLANGCI_LINT_FLAGS) $(GO_LINT_DIRS)
217+
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)
220218

221219
.PHONY: lint
222220
lint: lint-copyright lint-go
@@ -253,8 +251,9 @@ check-unit: GO_TEST_RACE ?=
253251
else
254252
check-unit: GO_TEST_RACE ?= -race
255253
endif
256-
check-unit: go.sum codegen
257-
CGO_CFLAGS='$(BUILD_CGO_CFLAGS)' $(GO) test -tags=hack $(GO_TEST_RACE) -ldflags='$(LD_FLAGS)' `$(GO) list -tags=hack $(GO_CHECK_UNIT_DIRS)`
254+
check-unit: BUILD_GO_TAGS += hack
255+
check-unit: .k0sbuild.docker-image.k0s go.sum bindata
256+
CGO_CFLAGS='$(BUILD_CGO_CFLAGS)' $(GO) test -tags=$(subst $(space),$(comma),$(BUILD_GO_TAGS)) $(GO_TEST_RACE) -ldflags='$(LD_FLAGS)' `$(GO) list -tags=$(subst $(space),$(comma),$(BUILD_GO_TAGS)) $(GO_CHECK_UNIT_DIRS)`
258257

259258
.PHONY: check-image-validity
260259
check-image-validity: go.sum

hack/gen-bindata/gen_bindata.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ func GenBindata(name string, args ...string) error {
180180
})
181181
}
182182

183-
var packageTemplate = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT.
183+
var packageTemplate = template.Must(template.New("").Parse(`//go:build !noembedbins
184+
// Code generated by go generate; DO NOT EDIT.
184185
185186
// datafile: {{ .OutFile }}
186187

hack/gen-bindata/gen_bindata_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ func TestGenBindata_BasicExecution(t *testing.T) {
4949
tmpDir := t.TempDir()
5050
defer testutil.Chdir(t, tmpDir)()
5151

52-
const expected = `// Code generated by go generate; DO NOT EDIT.
52+
const expected = `//go:build !noembedbins
53+
// Code generated by go generate; DO NOT EDIT.
5354
5455
// datafile: ./bindata
5556

pkg/assets/offsets_other.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//go:build noembedbins || (!linux && !windows)
2+
3+
/*
4+
Copyright 2024 k0s authors
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package assets
20+
21+
var BinData = map[string]struct{ offset, size, originalSize int64 }{}
22+
var BinDataSize int64

0 commit comments

Comments
 (0)