github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/Makefile (about)

     1  PROTOC ?= $(shell which protoc)
     2  CWD    := $(shell pwd)
     3  TOOLS  := $(CWD)/tools/bin
     4  PACKAGES := go list ./... | grep -vE 'vendor|tools'
     5  COVERED_PACKAGES := $(PACKAGES) | grep -vE 'mock|tests|checkpointspb'
     6  PACKAGE_DIRECTORIES := $(PACKAGES) | sed 's/github.com\/pingcap\/br\/*//'
     7  PACKAGE_FILES := $$(find . -name '*.go' -type f | grep -vE 'vendor|\.pb\.go|mock|tools|res_vfsdata')
     8  CHECKER := awk '{ print } END { if (NR > 0) { exit 1 } }'
     9  
    10  BR_PKG := github.com/pingcap/br
    11  
    12  RELEASE_VERSION =
    13  ifeq ($(RELEASE_VERSION),)
    14  	RELEASE_VERSION := v5.0.0-master
    15  	release_version_regex := ^v5\..*$$
    16  	release_branch_regex := "^release-[0-9]\.[0-9].*$$|^HEAD$$|^.*/*tags/v[0-9]\.[0-9]\..*$$"
    17  	ifneq ($(shell git rev-parse --abbrev-ref HEAD | egrep $(release_branch_regex)),)
    18  		# If we are in release branch, try to use tag version.
    19  		ifneq ($(shell git describe --tags --dirty | egrep $(release_version_regex)),)
    20  			RELEASE_VERSION := $(shell git describe --tags --dirty)
    21  		endif
    22  	else ifneq ($(shell git status --porcelain),)
    23  		# Add -dirty if the working tree is dirty for non release branch.
    24  		RELEASE_VERSION := $(RELEASE_VERSION)-dirty
    25  	endif
    26  endif
    27  
    28  LDFLAGS += -X "$(BR_PKG)/pkg/version/build.ReleaseVersion=$(RELEASE_VERSION)"
    29  LDFLAGS += -X "$(BR_PKG)/pkg/version/build.BuildTS=$(shell date -u '+%Y-%m-%d %I:%M:%S')"
    30  LDFLAGS += -X "$(BR_PKG)/pkg/version/build.GitHash=$(shell git rev-parse HEAD)"
    31  LDFLAGS += -X "$(BR_PKG)/pkg/version/build.GitBranch=$(shell git rev-parse --abbrev-ref HEAD)"
    32  
    33  LIGHTNING_BIN     := bin/tidb-lightning
    34  LIGHTNING_CTL_BIN := bin/tidb-lightning-ctl
    35  BR_BIN            := bin/br
    36  VFSGENDEV_BIN     := tools/bin/vfsgendev
    37  TEST_DIR          := /tmp/backup_restore_test
    38  
    39  path_to_add := $(addsuffix /bin,$(subst :,/bin:,$(GOPATH)))
    40  export PATH := $(path_to_add):$(PATH)
    41  
    42  GOBUILD := CGO_ENABLED=1 GO111MODULE=on go build -trimpath -ldflags '$(LDFLAGS)'
    43  GOTEST  := CGO_ENABLED=1 GO111MODULE=on go test -ldflags '$(LDFLAGS)'
    44  PREPARE_MOD := cp go.mod1 go.mod && cp go.sum1 go.sum
    45  FINISH_MOD := cp go.mod go.mod1 && cp go.sum go.sum1
    46  
    47  RACEFLAG =
    48  ifeq ("$(WITH_RACE)", "1")
    49  	RACEFLAG = -race
    50  	GOBUILD  = CGO_ENABLED=1 GO111MODULE=on $(GO) build -ldflags '$(LDFLAGS)'
    51  endif
    52  
    53  # There is no FreeBSD environment for GitHub actions. So cross-compile on Linux
    54  # but that doesn't work with CGO_ENABLED=1, so disable cgo. The reason to have
    55  # cgo enabled on regular builds is performance.
    56  ifeq ("$(GOOS)", "freebsd")
    57  	GOBUILD  = CGO_ENABLED=0 GO111MODULE=on go build -trimpath -ldflags '$(LDFLAGS)'
    58  endif
    59  
    60  all: build check test
    61  
    62  prepare:
    63  	$(PREPARE_MOD)
    64  
    65  finish-prepare:
    66  	$(FINISH_MOD)
    67  
    68  %_generated.go: %.rl
    69  	ragel -Z -G2 -o tmp_parser.go $<
    70  	@echo '// Code generated by ragel DO NOT EDIT.' | cat - tmp_parser.go | sed 's|//line |//.... |g' > $@
    71  	@rm tmp_parser.go
    72  
    73  data_parsers: tools pkg/lightning/mydump/parser_generated.go web
    74  	PATH="$(GOPATH)/bin":"$(PATH)":"$(TOOLS)" protoc -I. -I"$(GOPATH)/src" pkg/lightning/checkpoints/checkpointspb/file_checkpoints.proto --gogofaster_out=.
    75  	$(TOOLS)/vfsgendev -source='"github.com/pingcap/br/pkg/lightning/web".Res' && mv res_vfsdata.go pkg/lightning/web/
    76  
    77  web:
    78  	cd web && npm install && npm run build
    79  
    80  build: br lightning lightning-ctl
    81  
    82  br:
    83  	$(PREPARE_MOD)
    84  	$(GOBUILD) $(RACEFLAG) -o $(BR_BIN) cmd/br/*.go
    85  
    86  lightning_for_web:
    87  	$(PREPARE_MOD)
    88  	$(GOBUILD) $(RACEFLAG) -tags dev -o $(LIGHTNING_BIN) cmd/tidb-lightning/main.go
    89  
    90  lightning:
    91  	$(PREPARE_MOD)
    92  	$(GOBUILD) $(RACEFLAG) -o $(LIGHTNING_BIN) cmd/tidb-lightning/main.go
    93  
    94  lightning-ctl:
    95  	$(PREPARE_MOD)
    96  	$(GOBUILD) $(RACEFLAG) -o $(LIGHTNING_CTL_BIN) cmd/tidb-lightning-ctl/main.go
    97  
    98  build_for_integration_test:
    99  	$(PREPARE_MOD)
   100  	@make failpoint-enable
   101  	($(GOTEST) -c -cover -covermode=count \
   102  		-coverpkg=$(BR_PKG)/... \
   103  		-o $(BR_BIN).test \
   104  		github.com/pingcap/br/cmd/br && \
   105  	$(GOTEST) -c -cover -covermode=count \
   106  		-coverpkg=$(BR_PKG)/... \
   107  		-o $(LIGHTNING_BIN).test \
   108  		github.com/pingcap/br/cmd/tidb-lightning && \
   109  	$(GOTEST) -c -cover -covermode=count \
   110  		-coverpkg=$(BR_PKG)/... \
   111  		-o $(LIGHTNING_CTL_BIN).test \
   112  		github.com/pingcap/br/cmd/tidb-lightning-ctl && \
   113  	$(GOBUILD) $(RACEFLAG) -o bin/locker tests/br_key_locked/*.go && \
   114  	$(GOBUILD) $(RACEFLAG) -o bin/gc tests/br_z_gc_safepoint/*.go && \
   115  	$(GOBUILD) $(RACEFLAG) -o bin/oauth tests/br_gcs/*.go && \
   116  	$(GOBUILD) $(RACEFLAG) -o bin/rawkv tests/br_rawkv/*.go && \
   117  	$(GOBUILD) $(RACEFLAG) -o bin/parquet_gen tests/lightning_checkpoint_parquet/*.go \
   118  	) || (make failpoint-disable && exit 1)
   119  	@make failpoint-disable
   120  
   121  test: export ARGS=$$($(PACKAGES))
   122  test:
   123  	$(PREPARE_MOD)
   124  	@make failpoint-enable
   125  	$(GOTEST) $(RACEFLAG) -tags leak $(ARGS) || ( make failpoint-disable && exit 1 )
   126  	@make failpoint-disable
   127  
   128  testcover: tools
   129  	mkdir -p "$(TEST_DIR)"
   130  	$(PREPARE_MOD)
   131  	@make failpoint-enable
   132  	$(GOTEST) -cover -covermode=count -coverprofile="$(TEST_DIR)/cov.unit.out" \
   133  		$$($(COVERED_PACKAGES)) || ( make failpoint-disable && exit 1 )
   134  	@make failpoint-disable
   135  
   136  integration_test: bins build build_for_integration_test
   137  	tests/run.sh
   138  
   139  compatibility_test_prepare:
   140  	tests/run_compatible.sh prepare
   141  
   142  compatibility_test: br
   143  	tests/run_compatible.sh run
   144  
   145  coverage: tools
   146  	tools/bin/gocovmerge "$(TEST_DIR)"/cov.* | grep -vE ".*.pb.go|.*__failpoint_binding__.go" > "$(TEST_DIR)/all_cov.out"
   147  ifeq ("$(JenkinsCI)", "1")
   148  	tools/bin/goveralls -coverprofile=$(TEST_DIR)/all_cov.out -service=jenkins-ci -repotoken $(COVERALLS_TOKEN)
   149  else
   150  	go tool cover -html "$(TEST_DIR)/all_cov.out" -o "$(TEST_DIR)/all_cov.html"
   151  	grep -F '<option' "$(TEST_DIR)/all_cov.html"
   152  endif
   153  
   154  bins:
   155  	@which bin/tidb-server
   156  	@which bin/tikv-server
   157  	@which bin/pd-server
   158  	@which bin/pd-ctl
   159  	@which bin/go-ycsb
   160  	@which bin/minio
   161  	@which bin/tiflash
   162  	@which bin/libtiflash_proxy.so
   163  	@which bin/cdc
   164  	@which bin/fake-gcs-server
   165  	@which bin/tikv-importer
   166  	if [ ! -d bin/flash_cluster_manager ]; then echo "flash_cluster_manager not exist"; exit 1; fi
   167  
   168  tools:
   169  	@echo "install tools..."
   170  	@cd tools && make
   171  
   172  check:
   173  	@# Tidy first to avoid go.mod being affected by static and lint
   174  	@make tidy
   175  	@# Build tools for targets errdoc, static and lint
   176  	@make tools errdoc static lint
   177  
   178  static: export GO111MODULE=on
   179  static: prepare tools
   180  	@ # Not running vet and fmt through metalinter becauase it ends up looking at vendor
   181  	tools/bin/gofumports -w -d -format-only -local $(BR_PKG) $(PACKAGE_FILES) 2>&1 | $(CHECKER)
   182  	# TODO: go vet lightning packages too.
   183  	tools/bin/govet --shadow $$($(PACKAGE_DIRECTORIES) | grep -v "lightning") 2>&1 | $(CHECKER)
   184  
   185  	# TODO: lint lightning packages too.
   186  	@# why some lints are disabled?
   187  	@#   gochecknoglobals - disabled because we do use quite a lot of globals
   188  	@#          goimports - executed above already, gofumports
   189  	@#              gofmt - ditto
   190  	@#                gci - ditto
   191  	@#                wsl - too pedantic about the formatting
   192  	@#             funlen - PENDING REFACTORING
   193  	@#           gocognit - PENDING REFACTORING
   194  	@#              godox - TODO
   195  	@#              gomnd - too many magic numbers, and too pedantic (even 2*x got flagged...)
   196  	@#        testpackage - several test packages still rely on private functions
   197  	@#             nestif - PENDING REFACTORING
   198  	@#           goerr113 - it mistaken pingcap/errors with standard errors
   199  	@#                lll - pingcap/errors may need to write a long line
   200  	@#       paralleltest - no need to run test parallel
   201  	@#           nlreturn - no need to ensure a new line before continue or return
   202  	@#   exhaustivestruct - Protobuf structs have hidden fields, like "XXX_NoUnkeyedLiteral"
   203  	@#         exhaustive - no need to check exhaustiveness of enum switch statements
   204  	@#              gosec - too many false positive
   205  	@#          errorlint - pingcap/errors is incompatible with std errors.
   206  	@#          wrapcheck - there are too many unwrapped errors in tidb-lightning
   207  	CGO_ENABLED=0 tools/bin/golangci-lint run --enable-all --deadline 120s \
   208  		--disable gochecknoglobals \
   209  		--disable goimports \
   210  		--disable gofmt \
   211  		--disable gci \
   212  		--disable wsl \
   213  		--disable funlen \
   214  		--disable gocognit \
   215  		--disable godox \
   216  		--disable gomnd \
   217  		--disable testpackage \
   218  		--disable nestif \
   219  		--disable goerr113 \
   220  		--disable lll \
   221  		--disable paralleltest \
   222  		--disable nlreturn \
   223  		--disable exhaustivestruct \
   224  		--disable exhaustive \
   225  		--disable godot \
   226  		--disable gosec \
   227  		--disable errorlint \
   228  		--disable wrapcheck \
   229  		$(PACKAGE_DIRECTORIES)
   230  	# pingcap/errors APIs are mixed with multiple patterns 'pkg/errors',
   231  	# 'juju/errors' and 'pingcap/parser'. To avoid confusion and mistake,
   232  	# we only allow a subset of APIs, that's "Normalize|Annotate|Trace|Cause|Find".
   233  	# TODO: check lightning packages.
   234  	@# TODO: allow more APIs when we need to support "workaound".
   235  	grep -Rn --include="*.go" --exclude="*_test.go" -E "(\t| )errors\.[A-Z]" \
   236  		$$($(PACKAGE_DIRECTORIES) | grep -vE "tests|lightning") | \
   237  		grep -vE "Normalize|Annotate|Trace|Cause|RedactLogEnabled|Find" 2>&1 | $(CHECKER)
   238  	# The package name of "github.com/pingcap/kvproto/pkg/backup" collides
   239  	# "github.com/pingcap/br/pkg/backup", so we rename kvproto to backuppb.
   240  	grep -Rn --include="*.go" -E '"github.com/pingcap/kvproto/pkg/backup"' \
   241  		$$($(PACKAGE_DIRECTORIES)) | \
   242  		grep -vE "backuppb" | $(CHECKER)
   243  
   244  lint: prepare tools
   245  	@echo "linting"
   246  	# TODO: lint lightning packages.
   247  	CGO_ENABLED=0 tools/bin/revive -formatter friendly -config revive.toml $$($(PACKAGES) | grep -v "lightning")
   248  
   249  tidy:
   250  	@echo "go mod tidy"
   251  	$(PREPARE_MOD)
   252  	GO111MODULE=on go mod tidy
   253  	$(FINISH_MOD)
   254  	cd tests && GO111MODULE=on go mod tidy
   255  	git diff --exit-code go.mod1 go.sum1 tools/go.mod tools/go.sum
   256  
   257  errdoc: tools
   258  	@echo "generator errors.toml"
   259  	./tools/check-errdoc.sh
   260  
   261  failpoint-enable: tools
   262  	tools/bin/failpoint-ctl enable
   263  
   264  failpoint-disable: tools
   265  	tools/bin/failpoint-ctl disable
   266  
   267  .PHONY: tools web