build: respect GOBIN when using make targets

This PR updates GNUMakefile to respect $GOBIN if it is set in the
environment or via an $GOENV file. Previously we hard-coded the output
to $GOPATH/bin, which is not necessarily the desired behavior.
This commit is contained in:
Seth Hoenig 2022-02-16 11:59:50 -06:00
parent c30b4617aa
commit 98758d4287
2 changed files with 14 additions and 5 deletions

3
.changelog/12077.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:improvement
build: make targets now respect GOBIN variable
```

View File

@ -12,7 +12,13 @@ ifneq (MSYS_NT,$(THIS_OS))
# GOPATH supports PATH style multi-paths; assume the first entry is favorable. # GOPATH supports PATH style multi-paths; assume the first entry is favorable.
# Necessary because new Circle images override GOPATH with multiple values. # Necessary because new Circle images override GOPATH with multiple values.
# See: https://discuss.circleci.com/t/gopath-is-set-to-multiple-directories/7174 # See: https://discuss.circleci.com/t/gopath-is-set-to-multiple-directories/7174
GOPATH=$(shell go env GOPATH | cut -d: -f1) GOPATH := $(shell go env GOPATH | cut -d: -f1)
endif
# Respect $GOBIN if set in environment or via $GOENV file.
BIN := $(shell go env GOBIN)
ifndef BIN
BIN := $(shell go env GOPATH)/bin
endif endif
GO_TAGS ?= GO_TAGS ?=
@ -253,15 +259,15 @@ dev: hclfmt ## Build for the current development platform
@echo "==> Removing old development build..." @echo "==> Removing old development build..."
@rm -f $(PROJECT_ROOT)/$(DEV_TARGET) @rm -f $(PROJECT_ROOT)/$(DEV_TARGET)
@rm -f $(PROJECT_ROOT)/bin/nomad @rm -f $(PROJECT_ROOT)/bin/nomad
@rm -f $(GOPATH)/bin/nomad @rm -f $(BIN)/nomad
@if [ -d vendor ]; then echo -e "==> WARNING: Found vendor directory. This may cause build errors, consider running 'rm -r vendor' or 'make clean' to remove.\n"; fi @if [ -d vendor ]; then echo -e "==> WARNING: Found vendor directory. This may cause build errors, consider running 'rm -r vendor' or 'make clean' to remove.\n"; fi
@$(MAKE) --no-print-directory \ @$(MAKE) --no-print-directory \
$(DEV_TARGET) \ $(DEV_TARGET) \
GO_TAGS="$(GO_TAGS) $(NOMAD_UI_TAG)" GO_TAGS="$(GO_TAGS) $(NOMAD_UI_TAG)"
@mkdir -p $(PROJECT_ROOT)/bin @mkdir -p $(PROJECT_ROOT)/bin
@mkdir -p $(GOPATH)/bin @mkdir -p $(BIN)
@cp $(PROJECT_ROOT)/$(DEV_TARGET) $(PROJECT_ROOT)/bin/ @cp $(PROJECT_ROOT)/$(DEV_TARGET) $(PROJECT_ROOT)/bin/
@cp $(PROJECT_ROOT)/$(DEV_TARGET) $(GOPATH)/bin @cp $(PROJECT_ROOT)/$(DEV_TARGET) $(BIN)
.PHONY: prerelease .PHONY: prerelease
prerelease: GO_TAGS=ui codegen_generated release prerelease: GO_TAGS=ui codegen_generated release
@ -341,7 +347,7 @@ clean: ## Remove build artifacts
@rm -rf "$(PROJECT_ROOT)/bin/" @rm -rf "$(PROJECT_ROOT)/bin/"
@rm -rf "$(PROJECT_ROOT)/pkg/" @rm -rf "$(PROJECT_ROOT)/pkg/"
@rm -rf "$(PROJECT_ROOT)/vendor/" @rm -rf "$(PROJECT_ROOT)/vendor/"
@rm -f "$(GOPATH)/bin/nomad" @rm -f "$(BIN)/nomad"
.PHONY: testcluster .PHONY: testcluster
testcluster: ## Bring up a Linux test cluster using Vagrant. Set PROVIDER if necessary. testcluster: ## Bring up a Linux test cluster using Vagrant. Set PROVIDER if necessary.