diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index c43ab8fe57f6..7988aae65bdd 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -151,10 +151,7 @@ jobs: run: | cat <>"$GITHUB_ENV" TARGET_OS=${{ matrix.target-os }} - GO=go - GO_ENV= - GOCACHE=$GITHUB_WORKSPACE/build/cache/go/build - GOMODCACHE=$GITHUB_WORKSPACE/build/cache/go/mod + DOCKER= UNITTEST_EXTRA_ARGS=BUILD_GO_LDFLAGS_EXTRA= EOF @@ -162,9 +159,6 @@ jobs: cat -- "$GITHUB_ENV" echo ::endgroup:: - mkdir -p build/cache - make --touch .k0sbuild.docker-image.k0s - - name: Prepare build environment run: .github/workflows/prepare-build-env.sh diff --git a/Makefile b/Makefile index d5f5f90accbc..03d215872d55 100644 --- a/Makefile +++ b/Makefile @@ -22,21 +22,34 @@ GO_SRCS := $(shell find . -type f -name '*.go' -not -path './$(K0S_GO_BUILD_CACH GO_CHECK_UNIT_DIRS := . ./cmd/... ./pkg/... ./internal/... ./static/... ./hack/... DOCKER ?= docker -# EMBEDDED_BINS_BUILDMODE can be either: -# docker builds the binaries in docker -# none does not embed any binaries - # Disable Docker build integration if DOCKER is set to the empty string. ifeq ($(DOCKER),) - EMBEDDED_BINS_BUILDMODE ?= none - K0S_BUILD_IMAGE_FILE = - GO ?= go + GO_ENV_REQUISITES ?= + GO_ENV ?= PATH=$(abspath $(K0S_GO_BUILD_CACHE))/go/bin:"$$PATH" \ + GOBIN="$(abspath $(K0S_GO_BUILD_CACHE))/go/bin" \ + GOCACHE="$(abspath $(K0S_GO_BUILD_CACHE))/go/build" \ + GOMODCACHE="$(abspath $(K0S_GO_BUILD_CACHE))/go/mod" + GO ?= $(GO_ENV) go else - EMBEDDED_BINS_BUILDMODE ?= docker - K0S_BUILD_IMAGE_FILE = .k0sbuild.docker-image.k0s + GO_ENV_REQUISITES ?= .k0sbuild.docker-image.k0s + GO_ENV ?= $(DOCKER) run --rm \ + -v '$(realpath $(K0S_GO_BUILD_CACHE))':/run/k0s-build \ + -v '$(CURDIR)':/go/src/github.com/k0sproject/k0s \ + -w /go/src/github.com/k0sproject/k0s \ + -e GOOS \ + -e CGO_ENABLED \ + -e CGO_CFLAGS \ + -e GOARCH \ + --user $(BUILD_UID):$(BUILD_GID) \ + -- '$(shell cat .k0sbuild.docker-image.k0s)' GO ?= $(GO_ENV) go endif +# EMBEDDED_BINS_BUILDMODE can be either: +# docker builds the binaries in docker +# none does not embed any binaries +EMBEDDED_BINS_BUILDMODE ?= docker + # k0s runs on linux even if it's built on mac or windows TARGET_OS ?= linux BUILD_UID ?= $(shell id -u) @@ -81,17 +94,6 @@ endif LD_FLAGS += $(BUILD_GO_LDFLAGS_EXTRA) GOLANG_IMAGE ?= $(golang_buildimage) -K0S_GO_BUILD_CACHE_VOLUME_PATH=$(realpath $(K0S_GO_BUILD_CACHE)) -GO_ENV ?= $(DOCKER) run --rm \ - -v '$(K0S_GO_BUILD_CACHE_VOLUME_PATH)':/run/k0s-build \ - -v '$(CURDIR)':/go/src/github.com/k0sproject/k0s \ - -w /go/src/github.com/k0sproject/k0s \ - -e GOOS \ - -e CGO_ENABLED \ - -e CGO_CFLAGS \ - -e GOARCH \ - --user $(BUILD_UID):$(BUILD_GID) \ - -- '$(shell cat $(K0S_BUILD_IMAGE_FILE))' # https://www.gnu.org/software/make/manual/make.html#index-spaces_002c-in-variable-values nullstring := @@ -114,12 +116,12 @@ all: k0s k0s.exe $(K0S_GO_BUILD_CACHE): mkdir -p -- '$@' -$(K0S_BUILD_IMAGE_FILE): build/Dockerfile embedded-bins/Makefile.variables | $(K0S_GO_BUILD_CACHE) +.k0sbuild.docker-image.k0s: build/Dockerfile embedded-bins/Makefile.variables | $(K0S_GO_BUILD_CACHE) $(DOCKER) build --progress=plain --iidfile '$@' \ --build-arg BUILDIMAGE=$(GOLANG_IMAGE) \ - -t $(patsubst .%,%,$(K0S_BUILD_IMAGE_FILE)) - '$@' airgap-image-bundle-linux-amd64.tar: TARGET_PLATFORM := linux/amd64 @@ -265,7 +266,7 @@ else check-unit: GO_TEST_RACE ?= -race endif check-unit: BUILD_GO_TAGS += hack -check-unit: $(K0S_BUILD_IMAGE_FILE) go.sum bindata +check-unit: $(GO_ENV_REQUISITES) 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=$(subst $(space),$(comma),$(BUILD_GO_TAGS)) $(GO_CHECK_UNIT_DIRS)` .PHONY: clean-gocache @@ -274,7 +275,7 @@ clean-gocache: rm -rf -- '$(K0S_GO_BUILD_CACHE)/go' .PHONY: clean-docker-image -clean-docker-image: IID_FILES = $(K0S_BUILD_IMAGE_FILE) +clean-docker-image: IID_FILES = .k0sbuild.docker-image.k0s clean-docker-image: $(clean-iid-files) diff --git a/README.md b/README.md index b3b8d018851d..4c8dfc8cd4e9 100644 --- a/README.md +++ b/README.md @@ -138,23 +138,34 @@ The requirements for building k0s from source are as follows: All of the compilation steps are performed inside Docker containers, no installation of Go is required. -The k0s binary can be built in two different ways: +The k0s binary can be built in different ways: The "k0s" way, self-contained, all binaries compiled from source, statically -linked and embedded: +linked, including embedded binaries: ```shell make ``` -The "package maintainer" way, without any embedded binaries (requires that the -required binaries are provided separately at runtime): +The "package maintainer" way, without building and embedding the required +binaries. This assumes necessary binaries are provided separately at runtime: ```shell make EMBEDDED_BINS_BUILDMODE=none ``` -The embedded binaries can be built on their own: +Docker build integration is enabled by default. However, in environments without +Docker, you can use the Go toolchain installed on the host system to build k0s +without embedding binaries. Note that static linking is not possible with +glibc-based toolchains: + +```shell +make DOCKER='' EMBEDDED_BINS_BUILDMODE=none BUILD_GO_LDFLAGS_EXTRA='' +``` + +Note that the k0s build system does not currently support building the embedded +binaries without Docker. However, the embedded binaries can be built +independently using Docker: ```shell make -C embedded-bins