-
Notifications
You must be signed in to change notification settings - Fork 65
/
Makefile
269 lines (236 loc) · 8.56 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
# Licensed under the BSD-3-Clause License (the "License").
# You may not use this file except in compliance with the License.
# Directories
WORKDIR ?= $(CURDIR)
TESTDIR ?= $(WORKDIR)/tests
DISTDIR ?= $(WORKDIR)/dist
INSTALLDIR ?= /usr/local/bin/
VENDORDIR ?= $(WORKDIR)/third_party
# Arguments
REGISTRY ?= kraftkit.sh
ORG ?= unikraft
REPO ?= kraftkit
BIN ?= kraft \
runu
TOOLS ?= github-action \
go-generate-qemu-devices \
protoc-gen-go-netconn \
webinstall
GOMOD ?= kraftkit.sh
IMAGE_TAG ?= latest
GO_VERSION ?= 1.23
# Add a special version tag for pull requests
ifneq ($(shell grep 'refs/pull' $(WORKDIR)/.git/FETCH_HEAD),)
HASH_COMMIT ?= HEAD
HASH += pr-$(shell cat $(WORKDIR)/.git/FETCH_HEAD | awk -F/ '{print $$3}')
endif
# Calculate the project version based on git history
ifeq ($(HASH),)
HASH_COMMIT ?= HEAD
HASH ?= $(shell git update-index -q --refresh && \
git describe --tags)
# Others can't be dirty by definition
ifneq ($(HASH_COMMIT),HEAD)
HASH_COMMIT ?= HEAD
endif
DIRTY ?= $(shell git update-index -q --refresh && \
git diff-index --quiet HEAD -- $(WORKDIR) || \
echo "-dirty")
endif
VERSION ?= $(HASH)$(DIRTY)
GIT_SHA ?= $(shell git update-index -q --refresh && \
git rev-parse --short HEAD)
# Tools
DOCKER ?= docker
DOCKER_RUN ?= $(DOCKER) run --rm $(1) \
-e DOCKER= \
-e GOOS=$(GOOS) \
-e GOARCH=$(GOARCH) \
-w /go/src/$(GOMOD) \
--platform linux/amd64 \
-v $(WORKDIR):/go/src/$(GOMOD) \
$(REGISTRY)/$(2):$(IMAGE_TAG) \
$(3)
GO ?= go
MKDIR ?= mkdir
GIT ?= git
CURL ?= curl
CMAKE ?= cmake
# Go tools
GOFUMPT_VERSION ?= v0.7.0
GOFUMPT ?= $(GO) run mvdan.cc/gofumpt@$(GOFUMPT_VERSION)
GOCILINT_VERSION ?= v1.62.2
GOCILINT ?= $(GO) run github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOCILINT_VERSION)
YTT_VERSION ?= v0.51.0
YTT ?= $(GO) run carvel.dev/ytt/cmd/ytt@$(YTT_VERSION)
GORELEASER_VERSION ?= v1.25.1
GORELEASER ?= $(GO) run github.com/goreleaser/goreleaser@$(GORELEASER_VERSION)
GINKGO_VERSION ?= v2.22.0
GINKGO ?= $(GO) run github.com/onsi/ginkgo/v2/ginkgo@$(GINKGO_VERSION)
# Misc
Q ?= @
UNAME_OS ?= $(shell uname -s)
UNAME_ARCH ?= $(shell uname -m)
GOOS ?= linux
GOARCH ?= amd64
# Don't try to pass the path to Darwin host's make into the container
ifeq ($(UNAME_OS),Darwin)
MAKE_COMMAND = make
endif
# If on Darwin, we want to build a runnable binary.
# Check the OS and set GOOS/GOARCH flags accordingly.
# Note that we are still running a linux/amd64 container.
# TODO: For better performance, build an image for darwin/arm64 and darwin/amd64
ifeq ($(UNAME_OS),Darwin)
GOOS = darwin
ifeq ($(UNAME_ARCH),arm64)
GOARCH = arm64
else ifeq ($(UNAME_ARCH),x86_64)
GOARCH = amd64
endif
endif
# If run with DOCKER= or within a container, unset DOCKER_RUN so all commands
# are not proxied via docker container.
ifeq ($(DOCKER),)
DOCKER_RUN :=
else ifneq ($(wildcard /.dockerenv),)
DOCKER_RUN :=
endif
.PROXY :=
ifneq ($(DOCKER_RUN),)
.PROXY := docker-proxy-
$(BIN): ENVIRONMENT ?= myself-full
$(BIN):
$(info Running target via $(DOCKER)...)
$(Q)$(call DOCKER_RUN,,$(ENVIRONMENT),$(MAKE) GOOS=$(GOOS) GOARCH=$(GOARCH) -e $@)
$(Q)exit 0
endif
# Targets
.PHONY: all
.DEFAULT: all
all: help
.PHONY: build
build: CHANNEL ?= staging
build: $(WORKDIR)/goreleaser-$(CHANNEL).yaml
build: ## Build all KraftKit binary artifacts.
$(GORELEASER) build --config $(WORKDIR)/goreleaser-$(CHANNEL).yaml --clean --skip=validate
$(WORKDIR)/goreleaser-$(CHANNEL).yaml: CHANNEL ?= staging
$(WORKDIR)/goreleaser-$(CHANNEL).yaml:
$(YTT) -f .goreleaser-$(CHANNEL).yaml > goreleaser-$(CHANNEL).yaml
ifeq ($(DEBUG),y)
$(addprefix $(.PROXY), $(BIN)): GO_GCFLAGS ?= -N -l
else
$(addprefix $(.PROXY), $(BIN)): GO_LDFLAGS ?= -s -w
endif
$(addprefix $(.PROXY), $(BIN)): GO_LDFLAGS += -X "$(GOMOD)/internal/version.version=$(VERSION)"
$(addprefix $(.PROXY), $(BIN)): GO_LDFLAGS += -X "$(GOMOD)/internal/version.commit=$(GIT_SHA)"
$(addprefix $(.PROXY), $(BIN)): GO_LDFLAGS += -X "$(GOMOD)/internal/version.buildTime=$(shell date)"
$(addprefix $(.PROXY), $(BIN)): tidy
$(addprefix $(.PROXY), $(BIN)):
GOOS=$(GOOS) \
GOARCH=$(GOARCH) \
$(GO) build \
-v \
-tags "containers_image_storage_stub,containers_image_openpgp" \
-buildmode=pie \
-gcflags=all='$(GO_GCFLAGS)' \
-ldflags='$(GO_LDFLAGS)' \
-o $(DISTDIR)/$@ \
$(WORKDIR)/cmd/$@
.PHONY: tools
tools: $(TOOLS)
ifeq ($(DEBUG),y)
$(addprefix $(.PROXY), $(TOOLS)): GO_GCFLAGS ?= -N -l
else
$(addprefix $(.PROXY), $(TOOLS)): GO_LDFLAGS ?= -s -w
endif
$(addprefix $(.PROXY), $(TOOLS)): GO_LDFLAGS += -X "$(GOMOD)/internal/version.version=$(VERSION)"
$(addprefix $(.PROXY), $(TOOLS)): GO_LDFLAGS += -X "$(GOMOD)/internal/version.commit=$(GIT_SHA)"
$(addprefix $(.PROXY), $(TOOLS)): GO_LDFLAGS += -X "$(GOMOD)/internal/version.buildTime=$(shell date)"
$(addprefix $(.PROXY), $(TOOLS)):
(cd $(WORKDIR)/tools/$@ && \
$(GO) build -v \
-tags "containers_image_storage_stub,containers_image_openpgp" \
-o $(DISTDIR)/$@ \
-gcflags=all='$(GO_GCFLAGS)' \
-ldflags='$(GO_LDFLAGS)' \
./...)
# Proxy all "build environment" (buildenvs) targets
buildenv-%:
$(MAKE) -C $(WORKDIR)/buildenvs $*
# Run an environment where we can build
.PHONY: devenv
devenv: DOCKER_RUN_EXTRA ?= -it --name $(REPO)-devenv
devenv: WITH_KVM ?= n
devenv: ## Start the development environment container.
ifeq ($(WITH_KVM),y)
$(Q)$(call DOCKER_RUN,--device /dev/kvm $(DOCKER_RUN_EXTRA),myself-full,bash)
else
$(Q)$(call DOCKER_RUN,$(DOCKER_RUN_EXTRA),myself-full,bash)
endif
.PHONY: tidy
tidy: ## Tidy import Go modules.
$(GO) mod tidy -compat=$(GO_VERSION)
.PHONY: fmt
fmt: ## Format all files according to linting preferences.
$(GOFUMPT) -e -l -w $(WORKDIR)
.PHONY: cicheck
cicheck: ## Run CI checks.
$(GOCILINT) run --build-tags "containers_image_storage_stub,containers_image_openpgp"
.PHONY: test
test: test-unit test-framework test-e2e test-cloud-e2e ## Run all tests.
.PHONY: test-unit
test-unit: GOTEST_EXCLUDE := third_party/ test/ hack/ buildenvs/ dist/ docs/ tools/
test-unit: GOTEST_PKGS := $(foreach pkg,$(filter-out $(GOTEST_EXCLUDE),$(wildcard */)),$(pkg)...)
test-unit: ## Run unit tests.
$(GINKGO) -v -p -randomize-all -tags "containers_image_storage_stub,containers_image_openpgp" $(GOTEST_PKGS)
.PHONY: test-e2e
test-e2e: kraft ## Run CLI end-to-end tests.
$(GINKGO) -v -p -randomize-all test/e2e/cli/...
.PHONY: test-framework
test-framework: kraft ## Run framework tests.
$(GINKGO) -v -p -randomize-all ./test/e2e/framework/...
.PHONY: test-cloud-e2e
test-cloud-e2e: ## Run cloud end-to-end tests.
$(GINKGO) -v -randomize-all --flake-attempts 2 --nodes 8 ./test/e2e/cloud/...
.PHONY: clean
clean:
$(GO) clean -modcache -cache -i -r
.PHONY: properclean
properclean: ENVIRONMENT ?= myself-full
properclean: IMAGE ?= $(REGISTRY)/$(ENVIRONMENT):$(IMAGE_TAG)
properclean: ## Completely clean the repository's build artifacts.
rm -rf $(DISTDIR) $(TESTDIR)
$(DOCKER) rmi $(IMAGE)
.PHONY: docs
docs: OUTDIR ?= $(WORKDIR)/docs/
docs: ## Generate Markdown documentation.
$(GO) run $(WORKDIR)/tools/gendocs $(OUTDIR)
.PHONY: help
help: ## Show this help menu and exit.
@awk 'BEGIN { \
FS = ":.*##"; \
printf "KraftKit developer build targets.\n\n"; \
printf "\033[1mUSAGE\033[0m\n"; \
printf " make [VAR=... [VAR=...]] \033[36mTARGET\033[0m\n\n"; \
printf "\033[1mTARGETS\033[0m\n"; \
} \
/^[a-zA-Z0-9_-]+:.*?##/ { \
printf " \033[36m%-23s\033[0m %s\n", $$1, $$2 \
} \
/^##@/ { \
printf "\n\033[1m%s\033[0m\n", substr($$0, 5) \
} ' $(MAKEFILE_LIST)
# Additional help entries
buildenv-base: ## OCI image used for building Unikraft unikernels with kraft.
buildenv-base-golang: ## OCI image used for building Unikraft unikernels with kraft and golang included.
buildenv-gcc: ## OCI image containing a Unikraft-centric build of gcc.
buildenv-myself-full: ## OCI image containing the build environment for KraftKit.
buildenv-myself: ## OCI image containing KraftKit binaries.
buildenv-qemu: ## OCI image containing a Unikraft-centric build of QEMU.
buildenv-github-action: ## OCI image used when building Unikraft unikernels in GitHub Actions.
tools: ## Build all tools.
kraft: ## The kraft binary.
runu: ## The runu binary.