forked from unikraft/kraftkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
175 lines (152 loc) · 5.52 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
# 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
GOMOD ?= kraftkit.sh
IMAGE_TAG ?= latest
GO_VERSION ?= 1.20
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= \
-w /go/src/$(GOMOD) \
-v $(WORKDIR):/go/src/$(GOMOD) \
$(REGISTRY)/$(2):$(IMAGE_TAG) \
$(3)
GO ?= go
GOFUMPT ?= gofumpt
GOCILINT ?= golangci-lint
MKDIR ?= mkdir
GIT ?= git
CURL ?= curl
# Misc
Q ?= @
# 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) -e $@)
$(Q)exit 0
endif
# Targets
.PHONY: all
.DEFAULT: all
all: help
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)): git2go tidy
$(addprefix $(.PROXY), $(BIN)):
$(GO) build \
-tags static \
-gcflags=all='$(GO_GCFLAGS)' \
-ldflags='$(GO_LDFLAGS)' \
-o $(DISTDIR)/$@ \
$(WORKDIR)/cmd/$@
# 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: $(VENDORDIR)/github.com/libgit2/git2go/v31/vendor/libgit2
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
.PHONY: install-golangci-lint
install-golangci-lint: GOLANGCI_LINT_VERSION ?= 1.51.2
install-golangci-lint: GOLANGCI_LINT_INSTALL_DIR ?= $$($(GO) env GOPATH)/bin
install-golangci-lint: ## Install the Golang CI lint tool
$(CURL) -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOLANGCI_LINT_INSTALL_DIR) v$(GOLANGCI_LINT_VERSION)
.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: git2go
git2go: $(VENDORDIR)/github.com/libgit2/git2go/v31/static-build/install/lib/pkgconfig/libgit2.pc
$(VENDORDIR)/github.com/libgit2/git2go/v31/static-build/install/lib/pkgconfig/libgit2.pc: $(VENDORDIR)/github.com/libgit2/git2go/v31/vendor/libgit2
$(MAKE) -C $(VENDORDIR)/github.com/libgit2/git2go/v31 install-static
$(VENDORDIR)/github.com/libgit2/git2go/v31/vendor/libgit2: $(VENDORDIR)/github.com/libgit2/git2go
$(GIT) -C $(VENDORDIR)/github.com/libgit2/git2go/v31 submodule update --init --recursive
$(VENDORDIR)/github.com/libgit2/git2go:
$(GIT) clone --branch v31.7.9 --recurse-submodules https://github.com/libgit2/git2go.git $@/v31
.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-Z_-]+:.*?##/ { \
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-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.
kraft: ## The kraft binary.