github.com/jenkins-x/jx/v2@v2.1.155/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: $(GO_DEPENDENCIES) ## Build jx binary for current OS
    91  	CGO_ENABLED=$(CGO_ENABLED) $(GO) $(BUILD_TARGET) $(BUILDFLAGS) -o build/$(NAME) $(MAIN_SRC_FILE)
    92  
    93  build-all: $(GO_DEPENDENCIES) build make-reports-dir ## Build all files - runtime, all tests etc.
    94  	CGO_ENABLED=$(CGO_ENABLED) $(GOTEST) -run=nope -tags=integration,unit -failfast -short ./... $(BUILDFLAGS)
    95  
    96  tidy-deps: ## Cleans up dependencies
    97  	$(GO) mod tidy
    98  	# mod tidy only takes compile dependencies into account, let's make sure we capture tooling dependencies as well
    99  	@$(MAKE) install-generate-deps
   100  
   101  .PHONY: make-reports-dir
   102  make-reports-dir:
   103  	mkdir -p $(REPORTS_DIR)
   104  
   105  test: ## Run tests with the "unit" build tag
   106  	KUBECONFIG=/cluster/connections/not/allowed CGO_ENABLED=$(CGO_ENABLED) $(GOTEST) --tags=unit -failfast -short ./... $(TEST_BUILDFLAGS)
   107  
   108  test-coverage : make-reports-dir ## Run tests and coverage for all tests with the "unit" build tag
   109  	CGO_ENABLED=$(CGO_ENABLED) $(GOTEST) --tags=unit $(COVERFLAGS) -failfast -short ./... $(TEST_BUILDFLAGS)
   110  
   111  test-report: make-reports-dir get-test-deps test-coverage ## Create the test report
   112  	@gocov convert $(COVER_OUT) | gocov report
   113  
   114  test-report-html: make-reports-dir get-test-deps test-coverage ## Create the test report in HTML format
   115  	@gocov convert $(COVER_OUT) | gocov-html > $(REPORTS_DIR)/cover.html && open $(REPORTS_DIR)/cover.html
   116  
   117  test-verbose: make-reports-dir ## Run the unit tests in verbose mode
   118  	CGO_ENABLED=$(CGO_ENABLED) $(GOTEST) -v $(COVERFLAGS) --tags=unit -failfast ./... $(TEST_BUILDFLAGS)
   119  
   120  test-integration: get-test-deps ## Run the integration tests
   121  	@CGO_ENABLED=$(CGO_ENABLED) $(GOTEST) -tags=integration  -short ./... $(TEST_BUILDFLAGS)
   122  
   123  test-integration1: make-reports-dir
   124  	@CGO_ENABLED=$(CGO_ENABLED) $(GOTEST) -tags=integration $(COVERFLAGS) -short ./... $(TEST_BUILDFLAGS) -test.v -run $(TEST)
   125  
   126  test-integration1-pkg: make-reports-dir
   127  	@CGO_ENABLED=$(CGO_ENABLED) $(GOTEST) -tags=integration $(COVERFLAGS) -short $(PKG) -test.v -run $(TEST)
   128  
   129  test-rich-integration1: make-reports-dir
   130  	@CGO_ENABLED=$(CGO_ENABLED) richgo test -tags=integration $(COVERFLAGS) -short -test.v $(TEST_PACKAGE) $(TEST_BUILDFLAGS) -run $(TEST)
   131  
   132  test-integration-report: make-reports-dir get-test-deps test-integration ## Create the integration tests report
   133  	@gocov convert $(COVER_OUT) | gocov report
   134  
   135  test-integration-report-html: make-reports-dir get-test-deps test-integration
   136  	@gocov convert $(COVER_OUT) | gocov-html > $(REPORTS_DIR)/cover.html && open $(REPORTS_DIR)/cover.html
   137  
   138  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.
   139  	@CGO_ENABLED=$(CGO_ENABLED) $(GOTEST) -tags=integration $(COVERFLAGS) ./... $(TEST_BUILDFLAGS)
   140  
   141  test-slow-integration-report: make-reports-dir get-test-deps test-slow-integration
   142  	@gocov convert $(COVER_OUT) | gocov report
   143  
   144  test-slow-integration-report-html: make-reports-dir get-test-deps test-slow-integration
   145  	@gocov convert $(COVER_OUT) | gocov-html > $(REPORTS_DIR)/cover.html && open $(REPORTS_DIR)/cover.html
   146  
   147  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'
   148  	CGO_ENABLED=$(CGO_ENABLED) $(GOTEST) $(TEST_BUILDFLAGS) -tags="unit integration" $(TEST_PACKAGE) -run $(TEST)
   149  
   150  testbin: get-test-deps make-reports-dir
   151  	CGO_ENABLED=$(CGO_ENABLED) $(GOTEST) -c github.com/jenkins-x/jx/v2/pkg/cmd -o build/jx-test $(TEST_BUILDFLAGS)
   152  
   153  install: $(GO_DEPENDENCIES) ## Install the binary
   154  	GOBIN=${GOPATH}/bin $(GO) install $(BUILDFLAGS) $(MAIN_SRC_FILE)
   155  
   156  linux: ## Build for Linux
   157  	CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GOARCH=amd64 $(GO) $(BUILD_TARGET) $(BUILDFLAGS) -o build/linux/$(NAME) $(MAIN_SRC_FILE)
   158  	chmod +x build/linux/$(NAME)
   159  
   160  arm: ## Build for ARM
   161  	CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GOARCH=arm $(GO) $(BUILD_TARGET) $(BUILDFLAGS) -o build/arm/$(NAME) $(MAIN_SRC_FILE)
   162  	chmod +x build/arm/$(NAME)
   163  
   164  win: ## Build for Windows
   165  	CGO_ENABLED=$(CGO_ENABLED) GOOS=windows GOARCH=amd64 $(GO) $(BUILD_TARGET) $(BUILDFLAGS) -o build/win/$(NAME)-windows-amd64.exe $(MAIN_SRC_FILE)
   166  
   167  darwin: ## Build for OSX
   168  	CGO_ENABLED=$(CGO_ENABLED) GOOS=darwin GOARCH=amd64 $(GO) $(BUILD_TARGET) $(BUILDFLAGS) -o build/darwin/$(NAME) $(MAIN_SRC_FILE)
   169  	chmod +x build/darwin/$(NAME)
   170  
   171  .PHONY: test-release
   172  test-release: clean build
   173  	git fetch --tags
   174  	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
   175  
   176  .PHONY: release
   177  release: clean build test-slow-integration linux # Release the binary
   178  	git fetch origin refs/tags/v$(VERSION)
   179  	# Don't create a changelog for the distro
   180  	@if [[ -z "${DISTRO}" ]]; then \
   181  		./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; \
   182  		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; \
   183  	else \
   184  		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; \
   185  	fi
   186  
   187  .PHONY: release-distro
   188  release-distro:
   189  	@$(MAKE) DISTRO=true release
   190  
   191  .PHONY: clean
   192  clean: ## Clean the generated artifacts
   193  	rm -rf build release dist
   194  
   195  get-fmt-deps: ## Install test dependencies
   196  	$(GO_NOMOD) get golang.org/x/tools/cmd/goimports
   197  
   198  .PHONY: fmt
   199  fmt: importfmt ## Format the code
   200  	$(eval FORMATTED = $(shell $(GO) fmt ./...))
   201  	@if [ "$(FORMATTED)" == "" ]; \
   202        	then \
   203        	    echo "All Go files properly formatted"; \
   204        	else \
   205        		echo "Fixed formatting for: $(FORMATTED)"; \
   206        	fi
   207  
   208  .PHONY: importfmt
   209  importfmt: get-fmt-deps
   210  	@echo "Formatting the imports..."
   211  	goimports -w $(GO_DEPENDENCIES)
   212  
   213  .PHONY: lint
   214  lint: ## Lint the code
   215  	./hack/gofmt.sh
   216  	./hack/linter.sh
   217  
   218  .PHONY: code-generate
   219  code-generate:
   220  	./hack/generate.sh
   221  
   222  .PHONY: mod
   223  mod: build ## Would like to have tidy-deps here but that tends to cause problems
   224  
   225  include Makefile.docker
   226  include Makefile.codegen