github.com/mongodb/grip@v0.0.0-20240213223901-f906268d82b9/makefile (about)

     1  # start project configuration
     2  name := grip
     3  buildDir := build
     4  packages := recovery logging message send slogger $(name)
     5  orgPath := github.com/mongodb
     6  projectPath := $(orgPath)/$(name)
     7  # end project configuration
     8  
     9  # start environment setup
    10  gobin := go
    11  ifneq (,$(GOROOT))
    12  gobin := $(GOROOT)/bin/go
    13  endif
    14  
    15  goCache := $(GOCACHE)
    16  ifeq (,$(goCache))
    17  goCache := $(abspath $(buildDir)/.cache)
    18  endif
    19  goModCache := $(GOMODCACHE)
    20  ifeq (,$(goModCache))
    21  goModCache := $(abspath $(buildDir)/.mod-cache)
    22  endif
    23  lintCache := $(GOLANGCI_LINT_CACHE)
    24  ifeq (,$(lintCache))
    25  lintCache := $(abspath $(buildDir)/.lint-cache)
    26  endif
    27  
    28  ifeq ($(OS),Windows_NT)
    29  gobin := $(shell cygpath $(gobin))
    30  goCache := $(shell cygpath -m $(goCache))
    31  goModCache := $(shell cygpath -m $(goModCache))
    32  lintCache := $(shell cygpath -m $(lintCache))
    33  export GOROOT := $(shell cygpath -m $(GOROOT))
    34  endif
    35  
    36  export GOFLAGS := -mod=mod -modcacherw
    37  
    38  ifneq ($(goCache),$(GOCACHE))
    39  export GOCACHE := $(goCache)
    40  endif
    41  ifneq ($(goModCache),$(GOMODCACHE))
    42  export GOMODCACHE := $(goModCache)
    43  endif
    44  ifneq ($(lintCache),$(GOLANGCI_LINT_CACHE))
    45  export GOLANGCI_LINT_CACHE := $(lintCache)
    46  endif
    47  
    48  ifneq (,$(RACE_DETECTOR))
    49  # cgo is required for using the race detector.
    50  export CGO_ENABLED := 1
    51  else
    52  export CGO_ENABLED := 0
    53  endif
    54  # end environment setup
    55  
    56  # Ensure the build directory exists, since most targets require it.
    57  $(shell mkdir -p $(buildDir))
    58  
    59  .DEFAULT_GOAL := compile
    60  
    61  # start lint setup targets
    62  $(buildDir)/golangci-lint:
    63  	@curl --retry 10 --retry-max-time 60 -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(buildDir) v1.51.2 >/dev/null 2>&1
    64  $(buildDir)/run-linter: cmd/run-linter/run-linter.go $(buildDir)/golangci-lint
    65  	$(gobin) build -o $@ $<
    66  # end lint setup targets
    67  
    68  # start package and file lists
    69  testOutput := $(foreach target,$(packages),$(buildDir)/output.$(target).test)
    70  lintOutput := $(foreach target,$(packages),$(buildDir)/output.$(target).lint)
    71  coverageOutput := $(foreach target,$(packages),$(buildDir)/output.$(target).coverage)
    72  htmlCoverageOutput := $(foreach target,$(packages),$(buildDir)/output.$(target).coverage.html)
    73  .PRECIOUS: $(coverageOutput) $(htmlCoverageOutput) $(lintOutput) $(testOutput)
    74  # end package and file lists
    75  
    76  # start basic development targets
    77  compile: 
    78  	$(gobin) build $(subst $(name),,$(subst -,/,$(foreach pkg,$(packages),./$(pkg))))
    79  test: $(testOutput)
    80  lint: $(lintOutput)
    81  coverage: $(coverageOutput)
    82  html-coverage: $(htmlCoverageOutput)
    83  benchmark-send:
    84  	$(gobin) test -v -bench=$(if $(RUN_BENCH),$(RUN_BENCH),BenchmarkAllSenders) ./send/ ./send/benchmark/ -run=^^$$
    85  phony := compile lint test coverage html-coverage benchmark-send
    86  
    87  # start convenience targets for running tests and coverage tasks on a
    88  # specific package.
    89  test-%: $(buildDir)/output.%.test
    90  	
    91  coverage-%: $(buildDir)/output.%.coverage
    92  	
    93  html-coverage-%: $(buildDir)/output.%.coverage $(buildDir)/output.%.coverage.html
    94  	
    95  lint-%: $(buildDir)/output.%.lint
    96  	
    97  # end convenience targets
    98  # end basic development targets
    99  
   100  # start test and coverage artifacts
   101  testArgs := -v
   102  ifneq (,$(RUN_TEST))
   103  testArgs += -run='$(RUN_TEST)'
   104  endif
   105  ifneq (,$(RUN_COUNT))
   106  testArgs += -count=$(RUN_COUNT)
   107  endif
   108  ifneq (,$(RACE_DETECTOR))
   109  testArgs += -race
   110  endif
   111  $(buildDir)/output.%.test: .FORCE
   112  	$(gobin) test $(testArgs) ./$(if $(subst $(name),,$*),$(subst -,/,$*),) | tee $@
   113  	@grep -s -q -e "^PASS" $@
   114  $(buildDir)/output.%.coverage: .FORCE
   115  	$(gobin) test $(testArgs) -covermode=count -coverprofile=$@ | tee $(buildDir)/output.$*.test
   116  	@-[ -f $@ ] && $(gobin) tool cover -func=$@ | sed 's%$(projectPath)/%%' | column -t
   117  	@grep -s -q -e "^PASS" $(subst coverage,test,$@)
   118  $(buildDir)/output.%.coverage.html: $(buildDir)/output.%.coverage
   119  	$(gobin) tool cover -html=$< -o $@
   120  
   121  ifneq (go,$(gobin))
   122  # We have to handle the PATH specially for linting in CI, because if the PATH has a different version of the Go
   123  # binary in it, the linter won't work properly.
   124  lintEnvVars := PATH="$(shell dirname $(gobin)):$(PATH)"
   125  endif
   126  $(buildDir)/output.%.lint: $(buildDir)/run-linter .FORCE
   127  	@$(lintEnvVars) ./$< --output=$@ --lintBin=$(buildDir)/golangci-lint --packages='$*'
   128  # end test and coverage artifacts
   129  
   130  # start module management targets
   131  mod-tidy:
   132  	$(gobin) mod tidy
   133  # Check if go.mod and go.sum are clean. If they're clean, then mod tidy should not produce a different result.
   134  verify-mod-tidy:
   135  	$(gobin) run cmd/verify-mod-tidy/verify-mod-tidy.go -goBin="$(gobin)"
   136  phony += mod-tidy verify-mod-tidy
   137  # end module management targets
   138  
   139  # start cleanup targets
   140  clean:
   141  	rm -rf $(buildDir)
   142  clean-results:
   143  	$(buildDir)/output.*
   144  phony += clean clean-results
   145  # end cleanup targets
   146  
   147  # configure phony targets
   148  .FORCE:
   149  .PHONY: $(phony) .FORCE