program.mk 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright (c) 2021 VMware, Inc. All Rights Reserved.
  2. # SPDX-License-Identifier: Apache-2.0
  3. ifneq (,$(strip $(GOOS)))
  4. ifeq (,$(strip $(GOARCH)))
  5. GOARCH := $(shell go env | grep GOARCH | awk -F= '{print $$2}' | tr -d '"')
  6. endif
  7. endif
  8. ifneq (,$(strip $(GOARCH)))
  9. ifeq (,$(strip $(GOOS)))
  10. GOOS := $(shell go env | grep GOOS | awk -F= '{print $$2}' | tr -d '"')
  11. endif
  12. endif
  13. ifeq (2,$(words $(GOOS) $(GOARCH)))
  14. PROGRAM := $(PROGRAM)_$(GOOS)_$(GOARCH)
  15. endif
  16. ifeq (windows,$(GOOS))
  17. PROGRAM := $(PROGRAM).exe
  18. endif
  19. all: $(PROGRAM)
  20. TAGS += netgo
  21. ifeq (,$(strip $(findstring -w,$(LDFLAGS))))
  22. LDFLAGS += -w
  23. endif
  24. BUILD_ARGS := -tags '$(TAGS)' -ldflags '$(LDFLAGS)' -v
  25. $(PROGRAM):
  26. CGO_ENABLED=0 go build -a $(BUILD_ARGS) -o $@
  27. install:
  28. CGO_ENABLED=0 go install -v $(BUILD_ARGS)
  29. ifneq (,$(strip $(BUILD_OS)))
  30. ifneq (,$(strip $(BUILD_ARCH)))
  31. GOOS_GOARCH_TARGETS := $(foreach a,$(BUILD_ARCH),$(patsubst %,%_$a,$(BUILD_OS)))
  32. XBUILD := $(addprefix $(PROGRAM)_,$(GOOS_GOARCH_TARGETS))
  33. $(XBUILD):
  34. GOOS=$(word 2,$(subst _, ,$@)) GOARCH=$(word 3,$(subst _, ,$@)) $(MAKE) --output-sync=target
  35. build-all: $(XBUILD)
  36. endif
  37. endif
  38. clean:
  39. @rm -f $(PROGRAM) $(XBUILD)
  40. .PHONY: build-all install clean