program.mk 1.1 KB

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