Skip to content

Commit

Permalink
build: cleanup go.mk and make tasks
Browse files Browse the repository at this point in the history
GO is just an alias for go now and we use the tools go.mod to run the tool
instead of using go build.
  • Loading branch information
kruskall committed Jun 17, 2024
1 parent a62c0a2 commit a46c304
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 79 deletions.
64 changes: 32 additions & 32 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ GOTESTFLAGS?=-v
GOMODFLAG?=-mod=readonly

PYTHON_ENV?=.
PYTHON_VENV_DIR:=$(PYTHON_ENV)/build/ve/$(shell $(GO) env GOOS)
PYTHON_VENV_DIR:=$(PYTHON_ENV)/build/ve/$(shell go env GOOS)
PYTHON_BIN:=$(PYTHON_VENV_DIR)/bin
PYTHON=$(PYTHON_BIN)/python
CURRENT_DIR=$(shell dirname $(shell readlink -f $(firstword $(MAKEFILE_LIST))))
Expand Down Expand Up @@ -50,7 +50,7 @@ LDFLAGS := \
.PHONY: $(APM_SERVER_BINARIES)
$(APM_SERVER_BINARIES):
env CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) \
$(GO) build -o $@ -trimpath $(GOFLAGS) $(GOMODFLAG) -ldflags "$(LDFLAGS)" ./x-pack/apm-server
go build -o $@ -trimpath $(GOFLAGS) $(GOMODFLAG) -ldflags "$(LDFLAGS)" ./x-pack/apm-server

build/apm-server-linux-%: GOOS=linux
build/apm-server-darwin-%: GOOS=darwin
Expand All @@ -65,24 +65,24 @@ GOVERSIONINFO_FLAGS := \

build/apm-server-windows-amd64.exe: x-pack/apm-server/versioninfo_windows_amd64.syso
x-pack/apm-server/versioninfo_windows_amd64.syso: GOVERSIONINFO_FLAGS+=-64
x-pack/apm-server/versioninfo_%.syso: $(GOVERSIONINFO) $(GITREFFILE) packaging/versioninfo.json
$(GOVERSIONINFO) -o $@ $(GOVERSIONINFO_FLAGS) packaging/versioninfo.json
x-pack/apm-server/versioninfo_%.syso: $(GITREFFILE) packaging/versioninfo.json
go run -modfile=tools/go.mod github.com/josephspurrier/goversioninfo/cmd/goversioninfo -o $@ $(GOVERSIONINFO_FLAGS) packaging/versioninfo.json

.PHONY: apm-server
apm-server: build/apm-server-$(shell $(GO) env GOOS)-$(shell $(GO) env GOARCH)
apm-server: build/apm-server-$(shell go env GOOS)-$(shell go env GOARCH)
@cp $^ $@

.PHONY: apm-server-oss
apm-server-oss:
@$(GO) build $(GOMODFLAG) -o $@ ./cmd/apm-server
@go build $(GOMODFLAG) -o $@ ./cmd/apm-server

.PHONY: test
test:
@$(GO) test $(GOMODFLAG) $(GOTESTFLAGS) ./...
@go test $(GOMODFLAG) $(GOTESTFLAGS) ./...

.PHONY: system-test
system-test:
@(cd systemtest; $(GO) test $(GOMODFLAG) $(GOTESTFLAGS) -timeout=20m ./...)
@(cd systemtest; go test $(GOMODFLAG) $(GOTESTFLAGS) -timeout=20m ./...)

.PHONY:
clean:
Expand All @@ -97,7 +97,7 @@ check-full: update check staticcheck

.PHONY: check-approvals
check-approvals:
@$(GO) run -modfile=tools/go.mod github.com/elastic/apm-tools/cmd/check-approvals
@go run -modfile=tools/go.mod github.com/elastic/apm-tools/cmd/check-approvals

check: check-fmt check-headers check-git-diff

Expand All @@ -109,7 +109,7 @@ BENCH_BENCHTIME?=100ms
BENCH_COUNT?=1
.PHONY: bench
bench:
@$(GO) test -count=$(BENCH_COUNT) -benchmem -run=XXX -benchtime=$(BENCH_BENCHTIME) -bench='.*' ./...
@go test -count=$(BENCH_COUNT) -benchmem -run=XXX -benchtime=$(BENCH_BENCHTIME) -bench='.*' ./...

##############################################################################
# Rules for updating config files, etc.
Expand All @@ -125,13 +125,13 @@ apm-server.docker.yml: apm-server.yml

.PHONY: go-generate
go-generate:
@cd cmd/intake-receiver && APM_SERVER_VERSION=$(APM_SERVER_VERSION) $(GO) generate .
@cd cmd/intake-receiver && APM_SERVER_VERSION=$(APM_SERVER_VERSION) go generate .

.PHONY: add-headers
add-headers: $(GOLICENSER)
add-headers:
ifndef CHECK_HEADERS_DISABLED
@$(GOLICENSER) -exclude x-pack
@$(GOLICENSER) -license Elasticv2 x-pack
@go run -modfile=tools/go.mod github.com/elastic/go-licenser -exclude x-pack
@go run -modfile=tools/go.mod github.com/elastic/go-licenser -license Elasticv2 x-pack
endif

## get-version : Get the apm server version
Expand All @@ -156,10 +156,10 @@ docs: tf-docs
sh script/build_apm_docs.sh apm-server docs/index.asciidoc build

.PHONY: tf-docs
tf-docs: $(TERRAFORMDOCS) $(addsuffix /README.md,$(wildcard testing/infra/terraform/modules/*))
tf-docs: $(addsuffix /README.md,$(wildcard testing/infra/terraform/modules/*))

testing/infra/terraform/modules/%/README.md: .FORCE
$(TERRAFORMDOCS) markdown --hide-empty --header-from header.md --output-file=README.md --output-mode replace $(subst README.md,,$@)
go run -modfile=tools/go.mod github.com/terraform-docs/terraform-docs markdown --hide-empty --header-from header.md --output-file=README.md --output-mode replace $(subst README.md,,$@)

.PHONY: .FORCE
.FORCE:
Expand All @@ -169,8 +169,8 @@ testing/infra/terraform/modules/%/README.md: .FORCE
# TODO in the future we should probably trigger the updates from apm-data,
# and just keep the JSON Schema there.
docs/spec: go.mod
@$(GO) mod download github.com/elastic/apm-data
rsync -v --delete --filter='P spec/openapi/' --chmod=Du+rwx,go+rx --chmod=Fu+rw,go+r -r $$($(GO) list -m -f {{.Dir}} github.com/elastic/apm-data)/input/elasticapm/docs/spec ./docs
@go mod download github.com/elastic/apm-data
rsync -v --delete --filter='P spec/openapi/' --chmod=Du+rwx,go+rx --chmod=Fu+rw,go+r -r $$(go list -m -f {{.Dir}} github.com/elastic/apm-data)/input/elasticapm/docs/spec ./docs

##############################################################################
# Beats synchronisation.
Expand All @@ -181,11 +181,11 @@ BEATS_MODULE:=github.com/elastic/beats/v7

.PHONY: update-beats
update-beats: update-beats-module update
@echo --- Use this commit message: Update to elastic/beats@$(shell $(GO) list -m -f {{.Version}} $(BEATS_MODULE) | cut -d- -f3)
@echo --- Use this commit message: Update to elastic/beats@$(shell go list -m -f {{.Version}} $(BEATS_MODULE) | cut -d- -f3)

.PHONY: update-beats-module
update-beats-module:
$(GO) get -d $(BEATS_MODULE)@$(BEATS_VERSION) && $(GO) mod tidy
go get -d $(BEATS_MODULE)@$(BEATS_VERSION) && go mod tidy

##############################################################################
# Linting, style-checking, license header checks, etc.
Expand All @@ -198,14 +198,14 @@ update-beats-module:
STATICCHECK_CHECKS?=all,-ST1000

.PHONY: staticcheck
staticcheck: $(STATICCHECK)
$(STATICCHECK) -checks=$(STATICCHECK_CHECKS) ./...
staticcheck:
go run -modfile=tools/go.mod honnef.co/go/tools/cmd/staticcheck -checks=$(STATICCHECK_CHECKS) ./...

.PHONY: check-headers
check-headers: $(GOLICENSER)
check-headers:
ifndef CHECK_HEADERS_DISABLED
@$(GOLICENSER) -d -exclude build -exclude x-pack
@$(GOLICENSER) -d -exclude build -license Elasticv2 x-pack
@go run -modfile=tools/go.mod github.com/elastic/go-licenser -d -exclude build -exclude x-pack
@go run -modfile=tools/go.mod github.com/elastic/go-licenser -d -exclude build -license Elasticv2 x-pack
endif

.PHONY: check-docker-compose
Expand All @@ -215,23 +215,23 @@ check-docker-compose:
.PHONY: check-gofmt gofmt
check-fmt: check-gofmt
fmt: gofmt
check-gofmt: $(GOIMPORTS)
@PATH=$(GOOSBUILD):$(PATH) sh script/check_goimports.sh
gofmt: $(GOIMPORTS) add-headers
check-gofmt:
@sh script/check_goimports.sh
gofmt: add-headers
@echo "fmt - goimports: Formatting Go code"
@PATH=$(GOOSBUILD):$(PATH) GOIMPORTSFLAGS=-w sh script/goimports.sh
@GOIMPORTSFLAGS=-w sh script/goimports.sh

##############################################################################
# NOTICE.txt & dependencies.csv generation.
##############################################################################

MODULE_DEPS=$(sort $(shell \
$(GO) list -deps -tags=darwin,linux,windows -f "{{with .Module}}{{if not .Main}}{{.Path}}{{end}}{{end}}" ./x-pack/apm-server))
go list -deps -tags=darwin,linux,windows -f "{{with .Module}}{{if not .Main}}{{.Path}}{{end}}{{end}}" ./x-pack/apm-server))

notice: NOTICE.txt
NOTICE.txt build/dependencies-$(APM_SERVER_VERSION).csv: go.mod tools/go.mod
mkdir -p build/
$(GO) list -m -json $(MODULE_DEPS) | go run -modfile=tools/go.mod go.elastic.co/go-licence-detector \
go list -m -json $(MODULE_DEPS) | go run -modfile=tools/go.mod go.elastic.co/go-licence-detector \
-includeIndirect \
-overrides tools/notice/overrides.json \
-rules tools/notice/rules.json \
Expand Down Expand Up @@ -281,7 +281,7 @@ $(PYTHON_BIN)/esrally: $(PYTHON_BIN)
.PHONY: testing/rally/corpora
testing/rally/corpora:
@rm -fr testing/rally/corpora && mkdir testing/rally/corpora
@cd systemtest/cmd/gencorpora && $(GO) run . -write-dir $(CURRENT_DIR)/testing/rally/corpora/ -replay-count $(RALLY_GENCORPORA_REPLAY_COUNT)
@cd systemtest/cmd/gencorpora && go run . -write-dir $(CURRENT_DIR)/testing/rally/corpora/ -replay-count $(RALLY_GENCORPORA_REPLAY_COUNT)

##############################################################################
# Smoke tests -- Basic smoke tests for APM Server.
Expand Down
39 changes: 1 addition & 38 deletions go.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,8 @@ GITCOMMITTIMESTAMPUNIX ?= $(shell git log -1 --pretty=%ct)
GITREFFILE ?= $(GITDIR)/$(shell git rev-parse --symbolic-full-name HEAD)
GITROOT ?= $(shell git rev-parse --show-toplevel)

GOROOT?=$(go env GOROOT)
GOLANG_VERSION=$(shell cat $(GITROOT)/.go-version)
GO:=go
GOARCH:=$(shell $(GO) env GOARCH)
export PATH:=$(GOROOT)/bin:$(PATH)

GOOSBUILD:=$(GITROOT)/build/$(shell $(GO) env GOOS)
GOIMPORTS=$(GOOSBUILD)/goimports
GOLICENSER=$(GOOSBUILD)/go-licenser
STATICCHECK=$(GOOSBUILD)/staticcheck
TERRAFORMDOCS=$(GOOSBUILD)/terraform-docs
GOBENCH=$(GOOSBUILD)/gobench
GOVERSIONINFO=$(GOOSBUILD)/goversioninfo
NFPM=$(GOOSBUILD)/nfpm
GOARCH:=$(shell go env GOARCH)

APM_SERVER_VERSION=$(shell grep "const Version" $(GITROOT)/internal/version/version.go | cut -d'=' -f2 | tr -d '" ')
APM_SERVER_VERSION_MAJORMINOR=$(shell echo $(APM_SERVER_VERSION) | sed 's/\(.*\..*\)\..*/\1/')

##############################################################################
# Rules for creating and installing build tools.
##############################################################################

$(GOIMPORTS): $(GITROOT)/go.mod
$(GO) build -o $@ golang.org/x/tools/cmd/goimports

$(STATICCHECK): $(GITROOT)/tools/go.mod
$(GO) build -o $@ -modfile=$< honnef.co/go/tools/cmd/staticcheck

$(GOLICENSER): $(GITROOT)/tools/go.mod
$(GO) build -o $@ -modfile=$< github.com/elastic/go-licenser

$(TERRAFORMDOCS): $(GITROOT)/tools/go.mod
$(GO) build -o $@ -modfile=$< github.com/terraform-docs/terraform-docs

$(GOBENCH): $(GITROOT)/tools/go.mod
$(GO) build -o $@ -modfile=$< github.com/elastic/gobench

$(GOVERSIONINFO): $(GITROOT)/tools/go.mod
$(GO) build -o $@ -modfile=$< github.com/josephspurrier/goversioninfo/cmd/goversioninfo

$(NFPM): $(GITROOT)/tools/go.mod
$(GO) build -o $@ -modfile=$< github.com/goreleaser/nfpm/v2/cmd/nfpm
6 changes: 3 additions & 3 deletions packaging.mk
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ RPMS += $(patsubst %, $(DISTDIR)/apm-server-$(APM_SERVER_VERSION)-SNAPSHOT-%.rpm
RPMS_AMD64 := $(filter %-x86_64.rpm, $(RPMS))
RPMS_ARM64 := $(filter %-aarch64.rpm, $(RPMS))

$(DEBS_ARM64) $(RPMS_ARM64): $(NFPM) $(COMMON_PACKAGE_FILES) build/apm-server-linux-arm64 build/nfpm-arm64.yml
$(DEBS_AMD64) $(RPMS_AMD64): $(NFPM) $(COMMON_PACKAGE_FILES) build/apm-server-linux-amd64 build/nfpm-amd64.yml
$(DEBS_ARM64) $(RPMS_ARM64): $(COMMON_PACKAGE_FILES) build/apm-server-linux-arm64 build/nfpm-arm64.yml
$(DEBS_AMD64) $(RPMS_AMD64): $(COMMON_PACKAGE_FILES) build/apm-server-linux-amd64 build/nfpm-amd64.yml

%.deb %.rpm:
@mkdir -p $(DISTDIR)
@$(NFPM) package -f $(filter build/nfpm-%.yml, $^) -t $@
@go run -modfile=tools/go.mod github.com/goreleaser/nfpm/v2/cmd/nfpm package -f $(filter build/nfpm-%.yml, $^) -t $@

# Archive directories. These are the contents of tarball and zip artifacts.
#
Expand Down
2 changes: 1 addition & 1 deletion script/goimports.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh

dirs=$(find . -maxdepth 1 -type d \! \( -name '.*' -or -name build \))
exec goimports $GOIMPORTSFLAGS -local github.com/elastic $dirs
exec go run -modfile=tools/go.mod golang.org/x/tools/cmd/goimports $GOIMPORTSFLAGS -local github.com/elastic $dirs
2 changes: 1 addition & 1 deletion testing/apmsoak/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use-production:
.PHONY: apmsoak
apmsoak:
@echo "-> Building apmsoak..."
@cd $(APMSOAK_PATH) && GOOS=$(APMSOAK_GOOS) GOARCH=$(APMSOAK_GOARCH) $(GO) build .
@cd $(APMSOAK_PATH) && GOOS=$(APMSOAK_GOOS) GOARCH=$(APMSOAK_GOARCH) go build .

.PHONY: init
init:
Expand Down
6 changes: 3 additions & 3 deletions testing/benchmark/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ terraform.tfvars:
.PHONY: apmbench
apmbench:
@echo "-> Building apmbench..."
@cd $(APMBENCH_PATH) && CGO_ENABLED=0 GOOS=$(APMBENCH_GOOS) GOARCH=$(APMBENCH_GOARCH) $(GO) build .
@cd $(APMBENCH_PATH) && CGO_ENABLED=0 GOOS=$(APMBENCH_GOOS) GOARCH=$(APMBENCH_GOARCH) go build .

.PHONY: init
init:
Expand Down Expand Up @@ -104,8 +104,8 @@ run-benchmark-autotuned:
@ $(MAKE) run-benchmark BENCHMARK_AGENTS=$(shell echo $$(( $(BENCHMARK_AGENTS) * $(APM_SERVER_SIZE) )) )

.PHONY: index-benchmark-results
index-benchmark-results: $(GOBENCH) _default-gobench-vars
@cat $(BENCHMARK_RESULT) | $(GOBENCH) -es $(GOBENCH_HOST) -es-username $(GOBENCH_USERNAME) -es-password $(GOBENCH_PASSWORD) \
index-benchmark-results: _default-gobench-vars
@cat $(BENCHMARK_RESULT) | go run -modfile=tools/go.mod github.com/elastic/gobench -es $(GOBENCH_HOST) -es-username $(GOBENCH_USERNAME) -es-password $(GOBENCH_PASSWORD) \
-index $(GOBENCH_INDEX) -tag "$(GOBENCH_DEFAULT_TAGS),$(GOBENCH_TAGS)"

.PHONY: _default-gobench-vars
Expand Down
2 changes: 1 addition & 1 deletion testing/rally-cloud/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ destroy:
.PHONY: rally/corpora
rally/corpora:
@rm -fr $(ROOT_DIR)/testing/rally/corpora && mkdir $(ROOT_DIR)/testing/rally/corpora
@cd ../../systemtest/cmd/gencorpora && $(GO) run . \
@cd ../../systemtest/cmd/gencorpora && go run . \
-write-dir $(ROOT_DIR)/testing/rally/corpora/ \
-replay-count $(RALLY_GENCORPORA_REPLAY_COUNT) \
-override-meta-source-rootdir /home/rally/rally/corpora

0 comments on commit a46c304

Please sign in to comment.