|
| 1 | +# If you update this file, please follow |
| 2 | +# https://suva.sh/posts/well-documented-makefiles |
| 3 | + |
| 4 | +# Ensure Make is run with bash shell as some syntax below is bash-specific |
| 5 | +SHELL := /usr/bin/env bash |
| 6 | + |
| 7 | +.DEFAULT_GOAL := help |
| 8 | + |
| 9 | +# Active module mode, as we use go modules to manage dependencies |
| 10 | +export GO111MODULE := on |
| 11 | + |
| 12 | +# Versions. |
| 13 | +K8S_VERSION=1.34.1 |
| 14 | + |
| 15 | +# Get the information about the platform on which the tools are built/run. |
| 16 | +GOHOSTOS := $(shell go env GOHOSTOS) |
| 17 | +GOHOSTARCH := $(shell go env GOHOSTARCH) |
| 18 | +GOHOSTOSARCH := $(GOHOSTOS)_$(GOHOSTARCH) |
| 19 | + |
| 20 | +# Default the GOOS and GOARCH values to be the same as the platform on which |
| 21 | +# this Makefile is being executed. |
| 22 | +export GOOS := $(GOHOSTOS) |
| 23 | +export GOARCH := $(GOHOSTARCH) |
| 24 | + |
| 25 | +# The directory in which this Makefile is located. Please note this will not |
| 26 | +# behave correctly if the path to any Makefile in the list contains any spaces. |
| 27 | +ROOT_DIR ?= $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) |
| 28 | + |
| 29 | +# Get the GOPATH, but do not export it. This is used to determine if the project |
| 30 | +# is in the GOPATH and if not, to use the container runtime for the |
| 31 | +# generate-go-conversions target. |
| 32 | +GOPATH ?= $(shell go env GOPATH) |
| 33 | +PROJECT_SLUG := github.com/vmware/govmomi/crd |
| 34 | + |
| 35 | +# ROOT_DIR_IN_GOPATH is non-empty if ROOT_DIR is in the GOPATH. |
| 36 | +ROOT_DIR_IN_GOPATH := $(findstring $(GOPATH)/src/$(PROJECT_SLUG),$(ROOT_DIR)) |
| 37 | + |
| 38 | +# Directories. |
| 39 | +TOOLS_DIR := hack/tools |
| 40 | +TOOLS_BIN_DIR := $(TOOLS_DIR)/bin/$(GOHOSTOSARCH) |
| 41 | + |
| 42 | +API_VIM_DIR := pkg/vim |
| 43 | +API_VIM_IMPORT := github.com/vmware/govmomi/crd/pkg/vim |
| 44 | +API_VIM_CRD_BASES := $(API_VIM_DIR)/config/crd/bases |
| 45 | +API_VIM_API_DIR := $(API_VIM_DIR)/api |
| 46 | +API_VIM_VERSIONS := ./v1alpha1 |
| 47 | + |
| 48 | + |
| 49 | +## -------------------------------------- |
| 50 | +## Help |
| 51 | +## -------------------------------------- |
| 52 | + |
| 53 | +help: ## Display this help |
| 54 | + @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) |
| 55 | + |
| 56 | + |
| 57 | +## -------------------------------------- |
| 58 | +## Binaries |
| 59 | +## -------------------------------------- |
| 60 | + |
| 61 | +# Tooling binaries |
| 62 | +CONTROLLER_GEN := $(TOOLS_BIN_DIR)/controller-gen |
| 63 | +CONVERSION_GEN := $(TOOLS_BIN_DIR)/conversion-gen |
| 64 | + |
| 65 | +# CRI_BIN is the path to the container runtime binary. |
| 66 | +ifeq (,$(strip $(GITHUB_RUN_ID))) |
| 67 | +# Prefer podman locally. |
| 68 | +CRI_BIN := $(shell command -v podman 2>/dev/null || command -v docker 2>/dev/null) |
| 69 | +else |
| 70 | +# Prefer docker in GitHub actions. |
| 71 | +CRI_BIN := $(shell command -v docker 2>/dev/null || command -v podman 2>/dev/null) |
| 72 | +endif |
| 73 | +export CRI_BIN |
| 74 | + |
| 75 | + |
| 76 | +## -------------------------------------- |
| 77 | +## Tooling Binaries |
| 78 | +## -------------------------------------- |
| 79 | + |
| 80 | +TOOLING_BINARIES := $(CONTROLLER_GEN) $(CONVERSION_GEN) |
| 81 | +tools: $(TOOLING_BINARIES) ## Build tooling binaries |
| 82 | +$(TOOLING_BINARIES): |
| 83 | + make -C $(TOOLS_DIR) $(@F) |
| 84 | + |
| 85 | +ifneq (,$(strip $(wildcard $(GOPATH)))) |
| 86 | +.PHONY: tools-install |
| 87 | +tools-install: $(TOOLING_BINARIES) |
| 88 | +tools-install: ## Install the tooling binaries to ${GOPATH}/bin |
| 89 | +ifeq (,$(strip $(wildcard $(GOPATH)/bin))) |
| 90 | + mkdir -p "$(GOPATH)/bin" |
| 91 | +endif # (,$(strip $(wildcard $(GOPATH)/bin))) |
| 92 | + cp -f $(TOOLS_BIN_DIR)/* "$(GOPATH)/bin" |
| 93 | +endif # (,$(strip $(wildcard $(GOPATH)))) |
| 94 | + |
| 95 | + |
| 96 | +## -------------------------------------- |
| 97 | +## Generate |
| 98 | +## -------------------------------------- |
| 99 | + |
| 100 | +reverse = $(if $(1),$(call reverse,$(wordlist 2,$(words $(1)),$(1)))) $(firstword $(1)) |
| 101 | +GO_MOD_FILES := $(call reverse,$(shell find . -name go.mod)) |
| 102 | +GO_MOD_OP := tidy |
| 103 | + |
| 104 | +.PHONY: $(GO_MOD_FILES) |
| 105 | +$(GO_MOD_FILES): |
| 106 | + go -C $(@D) mod $(GO_MOD_OP) |
| 107 | + |
| 108 | +.PHONY: modules |
| 109 | +modules: $(GO_MOD_FILES) |
| 110 | +modules: ## Validates the modules |
| 111 | + |
| 112 | +.PHONY: modules-download |
| 113 | +modules-download: GO_MOD_OP=download |
| 114 | +modules-download: $(GO_MOD_FILES) |
| 115 | +modules-download: ## Downloads and caches the modules |
| 116 | + |
| 117 | +.PHONY: generate-go |
| 118 | +generate-go: $(CONTROLLER_GEN) |
| 119 | +generate-go: ## Generate golang sources |
| 120 | + go -C $(API_VIM_API_DIR) generate ./... |
| 121 | + $(CONTROLLER_GEN) \ |
| 122 | + paths=$(API_VIM_IMPORT)/... \ |
| 123 | + object:headerFile=./hack/boilerplate/boilerplate.generatego.txt |
| 124 | + |
| 125 | +.PHONY: generate-manifests |
| 126 | +generate-manifests: $(CONTROLLER_GEN) |
| 127 | +generate-manifests: ## Generate manifests e.g. CRD, RBAC etc. |
| 128 | + $(CONTROLLER_GEN) \ |
| 129 | + paths=$(API_VIM_IMPORT)/... \ |
| 130 | + crd:crdVersions=v1 \ |
| 131 | + output:crd:dir=$(API_VIM_CRD_BASES) \ |
| 132 | + output:none |
| 133 | + |
| 134 | +# CONVERSION_GEN_FALLBACK_MODE determines how to run the conversion-gen tool if |
| 135 | +# this project is not in the GOPATH at the expected location. Possible values |
| 136 | +# include "symlink" and "docker|podman". |
| 137 | +CONVERSION_GEN_FALLBACK_MODE ?= symlink |
| 138 | + |
| 139 | +.PHONY: generate-go-conversions-vim |
| 140 | +generate-go-conversions-vim: |
| 141 | + cd $(API_VIM_DIR)/api && \ |
| 142 | + $(abspath $(CONVERSION_GEN)) \ |
| 143 | + -v 10 \ |
| 144 | + --output-file=zz_generated.conversion.go \ |
| 145 | + --go-header-file=$(abspath hack/boilerplate/boilerplate.generatego.txt) \ |
| 146 | + $(API_VIM_VERSIONS) |
| 147 | + |
| 148 | +.PHONY: generate-go-conversions |
| 149 | +generate-go-conversions: ## Generate conversions go code |
| 150 | + |
| 151 | +ifneq (,$(ROOT_DIR_IN_GOPATH)) |
| 152 | + |
| 153 | +# If the project is not cloned in the correct location in the GOPATH then the |
| 154 | +# conversion-gen tool does not work. If ROOT_DIR_IN_GOPATH is non-empty, then |
| 155 | +# the project is in the correct location for conversion-gen to work. Otherwise, |
| 156 | +# there are two fallback modes controlled by CONVERSION_GEN_FALLBACK_MODE. |
| 157 | + |
| 158 | +# When the CONVERSION_GEN_FALLBACK_MODE is symlink, the conversion-gen binary |
| 159 | +# is rebuilt every time due to GNU Make, MTIME values, and symlinks. This ifeq |
| 160 | +# statement ensures that there is not an order-only dependency on CONVERSION_GEN |
| 161 | +# if it already exists. |
| 162 | +ifeq (,$(strip $(wildcard $(CONVERSION_GEN)))) |
| 163 | +generate-go-conversions: $(CONVERSION_GEN) |
| 164 | +endif |
| 165 | + |
| 166 | +generate-go-conversions: |
| 167 | + $(MAKE) generate-go-conversions-vim |
| 168 | + |
| 169 | +else ifeq (symlink,$(CONVERSION_GEN_FALLBACK_MODE)) |
| 170 | + |
| 171 | +# The generate-go-conversions target uses a symlink. Step-by-step, the target: |
| 172 | +# |
| 173 | +# 1. Creates a temporary directory to act as a GOPATH location and stores it |
| 174 | +# in NEW_GOPATH. |
| 175 | +# |
| 176 | +# 2. Determines the path to this project under the NEW_GOPATH and stores it in |
| 177 | +# NEW_ROOT_DIR. |
| 178 | +# |
| 179 | +# 3. Creates all of the path components for NEW_ROOT_DIR. |
| 180 | +# |
| 181 | +# 4. Removes the last path component in NEW_ROOT_DIR so it can be recreated as |
| 182 | +# a symlink in the next step. |
| 183 | +# |
| 184 | +# 5. Creates a symlink from this project to its new location under NEW_GOPATH. |
| 185 | +# |
| 186 | +# 6. Changes directories into NEW_ROOT_DIR. |
| 187 | +# |
| 188 | +# 7. Invokes "make generate-go-conversions" from NEW_ROOT_DIR while sending in |
| 189 | +# the values of GOPATH and ROOT_DIR to make this Makefile think it is in the |
| 190 | +# NEW_GOPATH. |
| 191 | +# |
| 192 | +# Because make runs targets in a separate shell, it is not necessary to change |
| 193 | +# back to the original directory. |
| 194 | +generate-go-conversions: |
| 195 | + NEW_GOPATH="$$(mktemp -d)" && \ |
| 196 | + NEW_ROOT_DIR="$${NEW_GOPATH}/src/$(PROJECT_SLUG)" && \ |
| 197 | + mkdir -p "$${NEW_ROOT_DIR}" && \ |
| 198 | + rm -fr "$${NEW_ROOT_DIR}" && \ |
| 199 | + ln -s "$(ROOT_DIR)" "$${NEW_ROOT_DIR}" && \ |
| 200 | + cd "$${NEW_ROOT_DIR}" && \ |
| 201 | + GOPATH="$${NEW_GOPATH}" ROOT_DIR="$${NEW_ROOT_DIR}" make $@ |
| 202 | + |
| 203 | +else ifeq ($(notdir $(CRI_BIN)),$(CONVERSION_GEN_FALLBACK_MODE)) |
| 204 | + |
| 205 | +ifeq (,$(CRI_BIN)) |
| 206 | +$(error Container runtime is required for generate-go-conversions and not detected in path!) |
| 207 | +endif |
| 208 | + |
| 209 | +# The generate-go-conversions target will use a container runtime. Step-by-step, |
| 210 | +# the target: |
| 211 | +# |
| 212 | +# 1. GOLANG_IMAGE is set to golang:YOUR_LOCAL_GO_VERSION and is the image used |
| 213 | +# to run make generate-go-conversions. |
| 214 | +# |
| 215 | +# 2. If using an arm host, the GOLANG_IMAGE is prefixed with arm64v8, which is |
| 216 | +# the prefix for Golang's container images for arm systems. |
| 217 | +# |
| 218 | +# 3. A new, temporary directory is created and its path is stored in |
| 219 | +# TOOLS_BIN_DIR. More on this later. |
| 220 | +# |
| 221 | +# 4. The flag --rm ensures that the container will be removed upon success or |
| 222 | +# failure, preventing orphaned containers from hanging around. |
| 223 | +# |
| 224 | +# 5. The first -v flag is used to bind mount the project's root directory to |
| 225 | +# the path /go/src/github.com/vmware-tanzu/vm-operator inside of the |
| 226 | +# container. This is required for the conversion-gen tool to work correctly. |
| 227 | +# |
| 228 | +# 6. The second -v flag is used to bind mount the temporary directory stored |
| 229 | +# TOOLS_BIN_DIR to /go/src/github.com/vmware-tanzu/vm-operator/hack/tools/bin |
| 230 | +# inside the container. This ensures the local host's binaries are not |
| 231 | +# overwritten case the local host is not Linux. Otherwise the container would |
| 232 | +# fail to run the binaries because they are the wrong architecture or replace |
| 233 | +# the binaries with Linux's elf architecture when the localhost uses |
| 234 | +# something else (ex. macOS is Darwin and uses mach). |
| 235 | +# |
| 236 | +# 7. The -w flag sets the container's working directory to where the project's |
| 237 | +# sources are bind mounted, /go/src/github.com/vmware-tanzu/vm-operator. |
| 238 | +# |
| 239 | +# 8. The image calculated earlier, GOLANG_IMAGE, is specified. |
| 240 | +# |
| 241 | +# 9. Finally, the command "make generate-go-conversions" is specified as what |
| 242 | +# the container will run. |
| 243 | +# |
| 244 | +# Once this target completes, it will be as if the generate-go-conversions |
| 245 | +# target was executed locally. Any necessary updates to the generated conversion |
| 246 | +# sources will be found on the local filesystem. Use "git status" to verify the |
| 247 | +# changes. |
| 248 | +generate-go-conversions: |
| 249 | + GOLANG_IMAGE="golang:$$(go env GOVERSION | cut -c3-)"; \ |
| 250 | + [ "$$(go env GOHOSTARCH)" = "arm64" ] && GOLANG_IMAGE="arm64v8/$${GOLANG_IMAGE}"; \ |
| 251 | + TOOLS_BIN_DIR="$$(mktemp -d)"; \ |
| 252 | + $(CRI_BIN) run -it --rm \ |
| 253 | + -v "$(ROOT_DIR)":/go/src/$(PROJECT_SLUG) \ |
| 254 | + -v "$${TOOLS_BIN_DIR}":/go/src/$(PROJECT_SLUG)/hack/tools/bin \ |
| 255 | + -w /go/src/$(PROJECT_SLUG) \ |
| 256 | + "$${GOLANG_IMAGE}" \ |
| 257 | + make generate-go-conversions |
| 258 | +endif |
| 259 | + |
| 260 | +.PHONY: generate |
| 261 | +generate: |
| 262 | + $(MAKE) generate-go |
| 263 | + $(MAKE) generate-manifests |
| 264 | + |
| 265 | + |
| 266 | +## -------------------------------------- |
| 267 | +## Cleanup / Verification |
| 268 | +## -------------------------------------- |
| 269 | + |
| 270 | +.PHONY: clean |
| 271 | +clean: ## Run all the clean targets |
| 272 | + $(MAKE) -C $(TOOLS_DIR) $(@) |
| 273 | + |
| 274 | +.PHONY: verify-codegen |
| 275 | +verify-codegen: ## Verify generated code |
| 276 | + hack/verify-codegen.sh |
| 277 | + |
| 278 | +.PHONY: verify |
| 279 | +verify: ## Verify the CRDs |
| 280 | + $(MAKE) verify-codegen |
| 281 | + |
0 commit comments