github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/Makefile (about)

     1  # Make does not offer a recursive wildcard function, so here's one:
     2  rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))
     3  
     4  SHELL := /bin/bash
     5  NAME := jx
     6  BUILD_TARGET = build
     7  MAIN_SRC_FILE=cmd/jx/jx.go
     8  GO := GO111MODULE=on go
     9  GO_NOMOD :=GO111MODULE=off go
    10  REV := $(shell git rev-parse --short HEAD 2> /dev/null || echo 'unknown')
    11  ORG := jenkins-x
    12  ORG_REPO := $(ORG)/$(NAME)
    13  RELEASE_ORG_REPO := $(ORG_REPO)
    14  ROOT_PACKAGE := github.com/$(ORG_REPO)/v2
    15  GO_VERSION := $(shell $(GO) version | sed -e 's/^[^0-9.]*\([0-9.]*\).*/\1/')
    16  GO_DEPENDENCIES := $(call rwildcard,pkg/,*.go) $(call rwildcard,cmd/jx/,*.go)
    17  
    18  BUILD_DATE     := $(shell date +%Y-%m-%dT%H:%M:%SZ)
    19  GIT_TREE_STATE := $(shell test -z "`git status --porcelain`" && echo "clean" || echo "dirty")
    20  
    21  CGO_ENABLED = 0
    22  
    23  REPORTS_DIR=$(BUILD_TARGET)/reports
    24  
    25  GOTEST := $(GO) test
    26  # If available, use gotestsum which provides more comprehensive output
    27  # This is used in the CI builds
    28  ifneq (, $(shell which gotestsum 2> /dev/null))
    29  GOTESTSUM_FORMAT ?= standard-quiet
    30  GOTEST := GO111MODULE=on gotestsum --junitfile $(REPORTS_DIR)/integration.junit.xml --format $(GOTESTSUM_FORMAT) --
    31  endif
    32  
    33  # set dev version unless VERSION is explicitly set via environment
    34  VERSION ?= $(shell echo "$$(git for-each-ref refs/tags/ --count=1 --sort=-version:refname --format='%(refname:short)' 2>/dev/null)-dev+$(REV)" | sed 's/^v//')
    35  
    36  # Build flags for setting build-specific configuration at build time - defaults to empty
    37  BUILD_TIME_CONFIG_FLAGS ?= ""
    38  
    39  # Full build flags used when building binaries. Not used for test compilation/execution.
    40  BUILDFLAGS :=  -ldflags \
    41    " -X $(ROOT_PACKAGE)/pkg/version.Version=$(VERSION)\
    42  		-X $(ROOT_PACKAGE)/pkg/version.Revision=$(REV)\
    43  		-X $(ROOT_PACKAGE)/pkg/version.BuildDate=$(BUILD_DATE)\
    44  		-X $(ROOT_PACKAGE)/pkg/version.GoVersion=$(GO_VERSION)\
    45  		-X $(ROOT_PACKAGE)/pkg/version.GitTreeState=$(GIT_TREE_STATE)\
    46  		$(BUILD_TIME_CONFIG_FLAGS)"
    47  
    48  # Some tests expect default values for version.*, so just use the config package values there.
    49  TEST_BUILDFLAGS :=  -ldflags "$(BUILD_TIME_CONFIG_FLAGS)"
    50  
    51  ifdef DEBUG
    52  BUILDFLAGS := -gcflags "all=-N -l" $(BUILDFLAGS)
    53  endif
    54  
    55  ifdef PARALLEL_BUILDS
    56  BUILDFLAGS += -p $(PARALLEL_BUILDS)
    57  GOTEST += -p $(PARALLEL_BUILDS)
    58  else
    59  # -p 4 seems to work well for people
    60  GOTEST += -p 4
    61  endif
    62  
    63  ifdef DISABLE_TEST_CACHING
    64  GOTEST += -count=1
    65  endif
    66  
    67  TEST_PACKAGE ?= ./...
    68  COVER_OUT:=$(REPORTS_DIR)/cover.out
    69  COVERFLAGS=-coverprofile=$(COVER_OUT) --covermode=count --coverpkg=./...
    70  
    71  .PHONY: list
    72  list: ## List all make targets
    73  	@$(MAKE) -pRrn : -f $(MAKEFILE_LIST) 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | sort
    74  
    75  .PHONY: help
    76  .DEFAULT_GOAL := help
    77  help:
    78  	@grep -h -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
    79  
    80  all: build ## Build the binary
    81  full: check ## Build and run the tests
    82  check: build test ## Build and run the tests
    83  get-test-deps: ## Install test dependencies
    84  	$(GO_NOMOD) get github.com/axw/gocov/gocov
    85  	$(GO_NOMOD) get -u gopkg.in/matm/v1/gocov-html
    86  
    87  print-version: ## Print version
    88  	@echo $(VERSION)
    89  
    90  build: build/$(NAME) ## Build jx binary for current OS
    91  build/$(NAME): $(GO_DEPENDENCIES)
    92  	CGO_ENABLED=$(CGO_ENABLED) $(GO) $(BUILD_TARGET) $(BUILDFLAGS) -o build/$(NAME) $(MAIN_SRC_FILE)
    93  
    94  build-all: $(GO_DEPENDENCIES) build make-reports-dir ## Build all files - runtime, all tests etc.
    95  	CGO_ENABLED=$(CGO_ENABLED) $(GOTEST) -run=nope -tags=integration,unit -failfast -short ./... $(BUILDFLAGS)
    96  
    97  tidy-deps: ## Cleans up dependencies
    98  	$(GO) mod tidy
    99  	# mod tidy only takes compile dependencies into account, let's make sure we capture tooling dependencies as well
   100  	@$(MAKE) install-generate-deps
   101  
   102  .PHONY: make-reports-dir
   103  make-reports-dir:
   104  	mkdir -p $(REPORTS_DIR)
   105  
   106  test: ## Run tests with the "unit" build tag
   107  	KUBECONFIG=/cluster/connections/not/allowed CGO_ENABLED=$(CGO_ENABLED) $(GOTEST) --tags=unit -failfast -short ./... $(TEST_BUILDFLAGS)
   108  
   109  test-coverage : make-reports-dir ## Run tests and coverage for all tests with the "unit" build tag
   110  	CGO_ENABLED=$(CGO_ENABLED) $(GOTEST) --tags=unit $(COVERFLAGS) -failfast -short ./... $(TEST_BUILDFLAGS)
   111  
   112  test-report: make-reports-dir get-test-deps test-coverage ## Create the test report
   113  	@gocov convert $(COVER_OUT) | gocov report
   114  
   115  test-report-html: make-reports-dir get-test-deps test-coverage ## Create the test report in HTML format
   116  	@gocov convert $(COVER_OUT) | gocov-html > $(REPORTS_DIR)/cover.html && open $(REPORTS_DIR)/cover.html
   117  
   118  test-verbose: make-reports-dir ## Run the unit tests in verbose mode
   119  	CGO_ENABLED=$(CGO_ENABLED) $(GOTEST) -v $(COVERFLAGS) --tags=unit -failfast ./... $(TEST_BUILDFLAGS)
   120  
   121  test-integration: get-test-deps ## Run the integration tests
   122  	@CGO_ENABLED=$(CGO_ENABLED) $(GOTEST) -tags=integration  -short ./... $(TEST_BUILDFLAGS)
   123  
   124  test-integration1: make-reports-dir
   125  	@CGO_ENABLED=$(CGO_ENABLED) $(GOTEST) -tags=integration $(COVERFLAGS) -short ./... $(TEST_BUILDFLAGS) -test.v -run $(TEST)
   126  
   127  test-integration1-pkg: make-reports-dir
   128  	@CGO_ENABLED=$(CGO_ENABLED) $(GOTEST) -tags=integration $(COVERFLAGS) -short $(PKG) -test.v -run $(TEST)
   129  
   130  test-rich-integration1: make-reports-dir
   131  	@CGO_ENABLED=$(CGO_ENABLED) richgo test -tags=integration $(COVERFLAGS) -short -test.v $(TEST_PACKAGE) $(TEST_BUILDFLAGS) -run $(TEST)
   132  
   133  test-integration-report: make-reports-dir get-test-deps test-integration ## Create the integration tests report
   134  	@gocov convert $(COVER_OUT) | gocov report
   135  
   136  test-integration-report-html: make-reports-dir get-test-deps test-integration
   137  	@gocov convert $(COVER_OUT) | gocov-html > $(REPORTS_DIR)/cover.html && open $(REPORTS_DIR)/cover.html
   138  
   139  test-slow-integration: make-reports-dir ## Run the any tests without a build tag as well as those that have the "integration" build tag. This target is run during CI.
   140  	@CGO_ENABLED=$(CGO_ENABLED) $(GOTEST) -tags=integration $(COVERFLAGS) ./... $(TEST_BUILDFLAGS)
   141  
   142  test-slow-integration-report: make-reports-dir get-test-deps test-slow-integration
   143  	@gocov convert $(COVER_OUT) | gocov report
   144  
   145  test-slow-integration-report-html: make-reports-dir get-test-deps test-slow-integration
   146  	@gocov convert $(COVER_OUT) | gocov-html > $(REPORTS_DIR)/cover.html && open $(REPORTS_DIR)/cover.html
   147  
   148  test1: get-test-deps make-reports-dir ## Runs single test specified by test name and optional package, eg 'make test1 TEST_PACKAGE=./pkg/gits TEST=TestGitCLI'
   149  	CGO_ENABLED=$(CGO_ENABLED) $(GOTEST) $(TEST_BUILDFLAGS) -tags="unit integration" $(TEST_PACKAGE) -run $(TEST)
   150  
   151  testbin: get-test-deps make-reports-dir
   152  	CGO_ENABLED=$(CGO_ENABLED) $(GOTEST) -c github.com/olli-ai/jx/v2/pkg/cmd -o build/jx-test $(TEST_BUILDFLAGS)
   153  
   154  install: $(GO_DEPENDENCIES) ## Install the binary
   155  	GOBIN=${GOPATH}/bin $(GO) install $(BUILDFLAGS) $(MAIN_SRC_FILE)
   156  
   157  linux: build/linux/$(NAME) ## Build for Linux
   158  build/linux/$(NAME): $(GO_DEPENDENCIES)
   159  	CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GOARCH=amd64 $(GO) $(BUILD_TARGET) $(BUILDFLAGS) -o build/linux/$(NAME) $(MAIN_SRC_FILE)
   160  	chmod +x build/linux/$(NAME)
   161  
   162  arm: build/arm/$(NAME) ## Build for ARM
   163  build/arm/$(NAME): $(GO_DEPENDENCIES)
   164  	CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GOARCH=arm $(GO) $(BUILD_TARGET) $(BUILDFLAGS) -o build/arm/$(NAME) $(MAIN_SRC_FILE)
   165  	chmod +x build/arm/$(NAME)
   166  
   167  win: build/win/$(NAME) ## Build for Windows
   168  build/win/$(NAME): $(GO_DEPENDENCIES)
   169  	CGO_ENABLED=$(CGO_ENABLED) GOOS=windows GOARCH=amd64 $(GO) $(BUILD_TARGET) $(BUILDFLAGS) -o build/win/$(NAME)-windows-amd64.exe $(MAIN_SRC_FILE)
   170  
   171  darwin: build/darwin/$(NAME) ## Build for OSX
   172  build/darwin/$(NAME): $(GO_DEPENDENCIES)
   173  	CGO_ENABLED=$(CGO_ENABLED) GOOS=darwin GOARCH=amd64 $(GO) $(BUILD_TARGET) $(BUILDFLAGS) -o build/darwin/$(NAME) $(MAIN_SRC_FILE)
   174  	chmod +x build/darwin/$(NAME)
   175  
   176  .PHONY: test-release
   177  test-release: clean build
   178  	git fetch --tags
   179  	REV=$(REV) BRANCH=$(BRANCH) BUILDDATE=$(BUILD_DATE) GOVERSION=$(GO_VERSION) ROOTPACKAGE=$(ROOT_PACKAGE) VERSION=$(VERSION) goreleaser --config=./.goreleaser.yml --snapshot --skip-publish --rm-dist --skip-validate --debug
   180  
   181  .PHONY: release
   182  release: clean build test-slow-integration linux # Release the binary
   183  	git fetch origin refs/tags/v$(VERSION)
   184  	# Don't create a changelog for the distro
   185  	@if [[ -z "${DISTRO}" ]]; then \
   186  		./build/linux/jx step changelog --verbose --header-file=docs/dev/changelog-header.md --version=$(VERSION) --rev=$(PULL_BASE_SHA) --output-markdown=changelog.md --update-release=false; \
   187  		GITHUB_TOKEN=$(GITHUB_ACCESS_TOKEN) REV=$(REV) BRANCH=$(BRANCH) BUILDDATE=$(BUILD_DATE) GOVERSION=$(GO_VERSION) ROOTPACKAGE=$(ROOT_PACKAGE) VERSION=$(VERSION) goreleaser release --config=.goreleaser.yml --rm-dist --release-notes=./changelog.md --skip-validate; \
   188  	else \
   189  		GITHUB_TOKEN=$(GITHUB_ACCESS_TOKEN) REV=$(REV) BRANCH=$(BRANCH) BUILDDATE=$(BUILD_DATE) GOVERSION=$(GO_VERSION) ROOTPACKAGE=$(ROOT_PACKAGE) VERSION=$(VERSION) goreleaser release --config=.goreleaser.yml --rm-dist; \
   190  	fi
   191  
   192  .PHONY: release-distro
   193  release-distro:
   194  	@$(MAKE) DISTRO=true release
   195  
   196  .PHONY: clean
   197  clean: ## Clean the generated artifacts
   198  	rm -rf build release dist
   199  
   200  get-fmt-deps: ## Install test dependencies
   201  	$(GO_NOMOD) get golang.org/x/tools/cmd/goimports
   202  
   203  .PHONY: fmt
   204  fmt: importfmt ## Format the code
   205  	$(eval FORMATTED = $(shell $(GO) fmt ./...))
   206  	@if [ "$(FORMATTED)" == "" ]; \
   207        	then \
   208        	    echo "All Go files properly formatted"; \
   209        	else \
   210        		echo "Fixed formatting for: $(FORMATTED)"; \
   211        	fi
   212  
   213  .PHONY: importfmt
   214  importfmt: get-fmt-deps
   215  	@echo "Formatting the imports..."
   216  	goimports -w $(GO_DEPENDENCIES)
   217  
   218  .PHONY: lint
   219  lint: ## Lint the code
   220  	./hack/gofmt.sh
   221  	./hack/linter.sh
   222  
   223  .PHONY: code-generate
   224  code-generate:
   225  	./hack/generate.sh
   226  
   227  .PHONY: mod
   228  mod: build ## Would like to have tidy-deps here but that tends to cause problems
   229  
   230  include Makefile.docker
   231  include Makefile.codegen