Makefile 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. SHELL := sh
  2. UPX := $(shell command -v upx || printf %s "true")
  3. STRIP := $(shell command -v strip || printf %s "true")
  4. STATS := $(shell command -v stat || printf %s "true")
  5. SUDO := $(shell command -v sudo || printf %s "true")
  6. BATS := $(shell command -v bats || printf %s "true")
  7. CCACHE := $(shell command -v ccache || printf %s "true")
  8. GO ?= go
  9. BUILD_DIR := ./bin
  10. BIN_DIR := /usr/local/bin
  11. NAME := gfpsgo
  12. PROJECT := github.com/johnsonjh/gfpsgo
  13. BATS_TESTS := *.bats
  14. GO_SRC=$(shell find . -name "*.go" | grep -v "_test.go")
  15. GO_BUILD=$(GO) build
  16. GOBIN ?= $(GO)/bin
  17. all:
  18. @printf %s\\n "See \"help\" for more information."
  19. help:
  20. .PHONY: shrink
  21. shrink: build
  22. @printf %s\\n "Initial strip "
  23. @$(STRIP) "$(BUILD_DIR)/$(NAME)" || printf %s\\n "Error: strip failure."
  24. @printf %s\\n "Full strip "
  25. @$(STRIP) --strip-all "$(BUILD_DIR)/$(NAME)" || printf %s\\n "Error: strip failure."
  26. @printf %s\\n "UPX overlay-strip "
  27. @${UPX} --overlay=strip -qqq "$(BUILD_DIR)/$(NAME)" || printf %s\\n "Error: Compression failure."
  28. @printf %s\\n "UPX decompress "
  29. @${UPX} -d -qqq "$(BUILD_DIR)/$(NAME)" || printf %s\\n "Error: Decompression failure."
  30. @printf %s\\n "UPX recompress "
  31. @${UPX} -qqq --ultra-brute "$(BUILD_DIR)/$(NAME)" || printf %s\\n "Error: Recompression failure."
  32. @printf %s\\n "UPX test "
  33. @${UPX} -qqq -t "$(BUILD_DIR)/$(NAME)" || printf %s\\n "Error: UPX failure."
  34. @${STATS} -c "Binary size: %s bytes" "$(BUILD_DIR)/$(NAME)"
  35. .PHONY: help
  36. help:
  37. @printf %s\\n "Targets: allclean, clean, build, rebuild, shrink, test, install, uninstall"
  38. .PHONY: rebuild
  39. rebuild:
  40. @export GFPSGO_REBUILDFLAG="-a" && $(MAKE) build
  41. .PHONY: build
  42. build: $(GO_SRC)
  43. @printf %s\\n "Building $(BUILD_DIR)/$(NAME)"
  44. @CGO_ENABLED="1" GO111MODULES="on" $(GO_BUILD) -v ${GFPSGO_REBUILDFLAG} -trimpath -o "$(BUILD_DIR)/$(NAME)" -ldflags='-w -s -buildid= -linkmode=internal' "$(PROJECT)/cmd"
  45. @${STATS} -c "Binary size: %s bytes" "$(BUILD_DIR)/$(NAME)"
  46. .PHONY: allclean
  47. allclean: clean
  48. @printf %s\\n "Removing caches... "
  49. @$(GO) clean -cache -testcache -modcache -x
  50. @$(CCACHE) -cC
  51. .PHONY: clean
  52. clean:
  53. @printf %s\\n "Removing $(BUILD_DIR) directory... "
  54. @rm -rf "$(BUILD_DIR)"
  55. .PHONY: test
  56. test:
  57. @printf %s\\n "Run \"make bats-test\" for integration tests, or \"make go-test\" for unit tests."
  58. .PHONY: bats-test
  59. bats-test: build go-test
  60. @printf %s\\n "(sudo required for integration testing)"
  61. @sudo true && $(BATS) test/$(BATS_TESTS)
  62. .PHONY: go-test
  63. go-test: build
  64. @CGO_ENABLED="1" GO111MODULES="on" $(GO) test -v -tags="leaktest" -cover -covermode="atomic" -bench="." "./..."
  65. .PHONY: install
  66. install: build bats-test shrink
  67. @sudo install -D -m755 "$(BUILD_DIR)/$(NAME)" "$(BIN_DIR)"
  68. .PHONY: uninstall
  69. uninstall:
  70. @sudo rm "$(BIN_DIR)/$(NAME)"