Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
137 lines (112 loc) · 4.46 KB
/
Copy pathMakefile
File metadata and controls
137 lines (112 loc) · 4.46 KB
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
# This Makefile lets you run the devkit image and all related commands in the
# exact same way they are executed in CI. The only requirement is that you have
# Podman or Docker installed and running on your machine.
SHELL := /bin/bash
INTERACTIVE := $(shell [ -t 0 ] && echo 1)
root_mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
REPO_ROOT := $(realpath $(dir $(root_mkfile_path)))
CONTAINER_TOOL ?= podman
ifeq ($(shell command -v $(CONTAINER_TOOL)),)
CONTAINER_TOOL := docker
endif
# Rootless Podman service socket (override if your socket lives elsewhere).
PODMAN_SOCK ?= $(XDG_RUNTIME_DIR)/podman/podman.sock
CONTAINER_URL :=
ifeq ($(CONTAINER_TOOL),podman)
ifneq ($(wildcard $(PODMAN_SOCK)),)
CONTAINER_URL := --url unix://$(PODMAN_SOCK)
endif
endif
# Preserve the executable name for tool-specific branches.
CONTAINER_TOOL_NAME := $(CONTAINER_TOOL)
# Single helper for container CLI invocations. Usage:
# $(call container,<subcmd>,<args...>)
container = $(CONTAINER_TOOL) $(CONTAINER_URL) $(1) $(2)
# Common mounts/working-dir used by most run invocations.
WORKSPACE_MOUNT := -v $(REPO_ROOT):/workspace -w /workspace
DEVKIT_IMAGE ?= ipv6relayd-devkit:local
DEVKIT_STAMP ?= $(REPO_ROOT)/.devkit-$(subst :,.,$(subst /,-,$(DEVKIT_IMAGE)))
# Keep stamp in sync: if the image is gone locally, drop the stamp so Make rebuilds.
ifneq ($(shell command -v $(CONTAINER_TOOL_NAME)),)
ifeq ($(shell $(CONTAINER_TOOL_NAME) image ls --quiet "$(DEVKIT_IMAGE)"),)
junk := $(shell rm -f "$(DEVKIT_STAMP)")
endif
endif
# Defaults for non-podman engines; override when using podman.
DEVKIT_CGROUP_FLAG := --cgroupns=host
CGROUP_MOUNT := -v /sys/fs/cgroup:/sys/fs/cgroup:rw
ifeq ($(CONTAINER_TOOL),podman)
DEVKIT_CGROUP_FLAG := --systemd=always
CGROUP_MOUNT :=
endif
DEVKIT_EXTRA_ARGS :=
ifeq ($(INTERACTIVE),1)
DEVKIT_EXTRA_ARGS += --tty --interactive
endif
DEVKIT_ARGS := --rm $(WORKSPACE_MOUNT) $(DEVKIT_EXTRA_ARGS)
.PHONY: devkit-image
devkit-image: $(DEVKIT_STAMP)
$(DEVKIT_STAMP): Containerfile.devkit go.mod go.sum
@if $(call container,image,inspect $(DEVKIT_IMAGE) >/dev/null 2>&1); then \
echo "Devkit image $(DEVKIT_IMAGE) already present; skipping build."; \
else \
echo "Building devkit image $(DEVKIT_IMAGE)"; \
$(call container,build,-f Containerfile.devkit -t $(DEVKIT_IMAGE) $(REPO_ROOT)); \
fi
@touch $@
.PHONY: shell
shell: devkit-image
$(call container,run,$(DEVKIT_ARGS) $(DEVKIT_IMAGE) /bin/bash)
.PHONY: lint
lint: devkit-image
$(call container,run,$(DEVKIT_ARGS) $(DEVKIT_IMAGE) golangci-lint run -v --fix ./...)
.PHONY: test
test: devkit-image
@pkgs=$$($(call container,run,--rm $(WORKSPACE_MOUNT) $(DEVKIT_IMAGE) go list ./... | grep -v "/integration")); \
$(call container,run,$(DEVKIT_ARGS) $(DEVKIT_IMAGE) go test -v $$pkgs)
.PHONY: clean-devkit
clean-devkit:
rm -f $(DEVKIT_STAMP)
$(call container,image,rm -f $(DEVKIT_IMAGE) >/dev/null 2>&1 || true)
.PHONY: clean
clean: clean-devkit
.PHONY: run
run: devkit-image
ifndef WHAT
$(error WHAT is required, e.g. make run WHAT="go test -v ./pkg/config/...")
else
$(call container,run,$(DEVKIT_ARGS) $(DEVKIT_IMAGE) $(WHAT))
endif
go.sum: go.mod
go mod tidy -v
.PHONY: tidy
tidy: devkit-image
$(call container,run,$(DEVKIT_ARGS) $(DEVKIT_IMAGE) make go.sum)
.PHONY: check-tidy
check-tidy:
@echo "Check Go Modules"
@if ! git diff --quiet go.mod go.sum; then \
echo -e "\n\n*****************************************"; \
echo "'go.mod' and/or 'go.sum' are out of date."; \
echo "Please run 'make tidy' to update them."; \
echo -e "*****************************************\n\n"; \
exit 1; \
fi
.PHONY: tidy-ci
tidy-ci: devkit-image
$(call container,run,$(DEVKIT_ARGS) $(DEVKIT_IMAGE) make go.sum)
$(MAKE) check-tidy
.PHONY: integration
integration: devkit-image
@cid=$$($(call container,run,-d --privileged --cap-add=NET_ADMIN --cap-add=NET_RAW --device /dev/net/tun $(DEVKIT_CGROUP_FLAG) --tmpfs /run --tmpfs /tmp $(if $(CGROUP_MOUNT),$(CGROUP_MOUNT)) $(WORKSPACE_MOUNT) -- $(DEVKIT_IMAGE) /lib/systemd/systemd)); \
status=0; \
$(call container,exec,$$cid sh -c '\
tmpbin=$$(mktemp /integration.test.XXXXXX); \
trap "rm -f $$tmpbin" EXIT; \
go test -count=1 -c -o $$tmpbin ./integration && \
chmod +x $$tmpbin && \
(cd /workspace/integration && $$tmpbin -test.v -test.count=1)') || status=$$?; \
if [ $$status -ne 0 ]; then $(call container,logs,$$cid) || true; fi; \
$(call container,stop,-t 0 $$cid >/dev/null 2>&1 || true); \
$(call container,rm,-f $$cid >/dev/null 2>&1 || true); \
exit $$status