Makefile 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # Copyright (c) 2021 VMware, Inc. All Rights Reserved.
  2. # SPDX-License-Identifier: Apache-2.0
  3. # If you update this file, please follow
  4. # https://www.thapaliya.com/en/writings/well-documented-makefiles/
  5. # Ensure Make is run with bash shell as some syntax below is bash-specific
  6. SHELL := /usr/bin/env bash
  7. # Print the help/usage when make is executed without any other arguments
  8. .DEFAULT_GOAL := help
  9. ## --------------------------------------
  10. ## Help
  11. ## --------------------------------------
  12. .PHONY: help
  13. help: ## Display usage
  14. @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make [target] \033[36m\033[0m\n\nTargets:\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-20s\033[0m %s\n", $$1, $$2 }' $(MAKEFILE_LIST)
  15. ## --------------------------------------
  16. ## Locations and programs
  17. ## --------------------------------------
  18. # Directories
  19. BIN_DIR := bin
  20. TOOLS_DIR := hack/tools
  21. TOOLS_BIN_DIR := $(TOOLS_DIR)/bin
  22. # Tooling binaries
  23. GO ?= $(shell command -v go 2>/dev/null)
  24. GOLANGCI_LINT := $(TOOLS_BIN_DIR)/golangci-lint
  25. ## --------------------------------------
  26. ## Prerequisites
  27. ## --------------------------------------
  28. # Do not proceed unless the go binary is present.
  29. ifeq (,$(strip $(GO)))
  30. $(error The "go" program cannot be found)
  31. endif
  32. ## --------------------------------------
  33. ## Linting and fixing linter errors
  34. ## --------------------------------------
  35. .PHONY: lint
  36. lint: ## Run all the lint targets
  37. $(MAKE) lint-go-full
  38. GOLANGCI_LINT_FLAGS ?= --fast=true
  39. .PHONY: lint-go
  40. lint-go: $(GOLANGCI_LINT) ## Lint codebase
  41. $(GOLANGCI_LINT) run -v $(GOLANGCI_LINT_FLAGS)
  42. .PHONY: lint-go-full
  43. lint-go-full: GOLANGCI_LINT_FLAGS = --fast=false --max-same-issues=200
  44. lint-go-full: lint-go ## Run slower linters to detect possible issues
  45. .PHONY: fix
  46. fix: GOLANGCI_LINT_FLAGS = --fast=false --fix
  47. fix: lint-go ## Tries to fix errors reported by lint-go-full target
  48. .PHONY: check
  49. check: lint-go-full
  50. check: ## Run linters
  51. ## --------------------------------------
  52. ## Tooling Binaries
  53. ## --------------------------------------
  54. TOOLING_BINARIES := $(GOLANGCI_LINT)
  55. tools: $(TOOLING_BINARIES) ## Build tooling binaries
  56. .PHONY: $(TOOLING_BINARIES)
  57. $(TOOLING_BINARIES):
  58. cd $(TOOLS_DIR); make $(@F)
  59. ## --------------------------------------
  60. ## Build / Install
  61. ## --------------------------------------
  62. .PHONY: install
  63. install: ## Install govc and vcsim
  64. $(MAKE) -C govc install
  65. $(MAKE) -C vcsim install
  66. ## --------------------------------------
  67. ## Generate
  68. ## --------------------------------------
  69. .PHONY: mod
  70. mod: ## Runs go mod tidy to validate modules
  71. go mod tidy -v
  72. .PHONY: mod-get
  73. mod-get: ## Downloads and caches the modules
  74. go mod download
  75. .PHONY: doc
  76. doc: install
  77. doc: ## Generates govc USAGE.md
  78. ./govc/usage.sh > ./govc/USAGE.md
  79. .PHONY: generate-types
  80. generate-types: ## Generate the types
  81. $(MAKE) -C ./gen/ $@
  82. ## --------------------------------------
  83. ## Tests
  84. ## --------------------------------------
  85. # Test options
  86. TEST_COUNT ?= 1
  87. TEST_TIMEOUT ?= 5m
  88. TEST_RACE_HISTORY_SIZE ?= 5
  89. GORACE ?= history_size=$(TEST_RACE_HISTORY_SIZE)
  90. ifeq (-count,$(findstring -count,$(TEST_OPTS)))
  91. $(error Use TEST_COUNT to override this option)
  92. endif
  93. ifeq (-race,$(findstring -race,$(TEST_OPTS)))
  94. $(error The -race flag is enabled by default & cannot be specified in TEST_OPTS)
  95. endif
  96. ifeq (-timeout,$(findstring -timeout,$(TEST_OPTS)))
  97. $(error Use TEST_TIMEOUT to override this option)
  98. endif
  99. .PHONY: go-test
  100. go-test: ## Runs go unit tests with race detector enabled
  101. GORACE=$(GORACE) CGO_ENABLED=1 $(GO) test \
  102. -count $(TEST_COUNT) \
  103. -race \
  104. -timeout $(TEST_TIMEOUT) \
  105. -v $(TEST_OPTS) \
  106. ./...
  107. .PHONY: govc-test
  108. govc-test: install
  109. govc-test: ## Runs govc bats tests
  110. ./govc/test/images/update.sh
  111. (cd govc/test && ./vendor/github.com/bats-core/bats-core/bin/bats -t .)
  112. .PHONY: test
  113. test: go-test govc-test ## Runs go-test and govc-test