From a5347ee68ed1f3e76fc4c9e6a13f3fb89957573c Mon Sep 17 00:00:00 2001 From: Stanislav Bogatyrev Date: Fri, 10 Feb 2023 15:47:06 +0300 Subject: [PATCH] [#3] Simplify Makefile Signed-off-by: Stanislav Bogatyrev --- .gitignore | 38 ++++++++++++++-------- Makefile | 93 ++++++++++++++++++++++++++++-------------------------- help.mk | 11 +++++++ 3 files changed, 85 insertions(+), 57 deletions(-) create mode 100644 help.mk diff --git a/.gitignore b/.gitignore index 519795c..4f56262 100644 --- a/.gitignore +++ b/.gitignore @@ -1,15 +1,27 @@ -# Binaries for programs and plugins -*.exe -*.exe~ -*.dll -*.so -*.dylib - -# Test binary, build with `go test -c` -*.test - -# Output of the go coverage tool, specifically when used with LiteIDE -*.out +# IDE +.idea +.vscode +# Vendoring vendor -tzsum + +# tempfiles +.DS_Store +*~ +.cache + +temp +tmp + +# binary +bin/ +release/ + +# coverage +coverage.txt +coverage.html + +# testing +cmd/test +/plugins/ +testfile diff --git a/Makefile b/Makefile index 96cabdf..ab2a222 100644 --- a/Makefile +++ b/Makefile @@ -1,56 +1,61 @@ -B=\033[0;1m -G=\033[0;92m -R=\033[0m +#!/usr/bin/make -f +SHELL = bash -NAME ?= homo +REPO ?= $(shell go list -m) +VERSION ?= $(shell git describe --tags --dirty --match "v*" --always --abbrev=8 2>/dev/null || cat VERSION 2>/dev/null || echo "develop") -.PHONY: help attach auto up down deps +BIN = bin +DIRS = $(BIN) -# Show this help prompt -help: - @echo ' Usage:' - @echo '' - @echo ' make ' - @echo '' - @echo ' Targets:' - @echo '' - @awk '/^#/{ comment = substr($$0,3) } comment && /^[a-zA-Z][a-zA-Z0-9_-]+ ?:/{ print " ", $$1, comment }' $(MAKEFILE_LIST) | column -t -s ':' | grep -v 'IGNORE' | sort | uniq +# List of binaries to build. +CMDS = $(notdir $(basename $(wildcard cmd/*))) +BINS = $(addprefix $(BIN)/, $(CMDS)) -# Install dependencies -deps: - @go mod tidy -v - @go mod vendor +.PHONY: all help clean -# Auto Tillich-Zémor hasher demo -auto: down deps - @echo "\n${B}${G}build container${R}\n" - @time docker build -t poc-demo . - @echo "\n${B}${G}Bootup container:${R}\n" - @time docker run -d --rm -it --name hash-demo poc-demo:latest sh - @bash ./auto.sh - @make down +# To build a specific binary, use it's name prefix with bin/ as a target +# For example `make bin/tzsum` will build only storage node binary +# Just `make` will build all possible binaries +all: $(DIRS) $(BINS) -# Stop demo container -down: - @echo "\n${B}${G}Stop container${R}\n" - @docker kill hash-demo || true - @docker rm -f hash-demo || true +# help target +include help.mk -# Run Tillich-Zémor hasher demo -up: down deps - @echo "\n${B}${G}build container${R}\n" - @time docker build -t poc-demo . - @echo "\n${B}${G}enter inside container:${R}\n" - @time docker run --rm -it --name hash-demo poc-demo:latest sh +$(BINS): $(DIRS) dep + @echo "⇒ Build $@" + CGO_ENABLED=0 \ + go build -v -trimpath \ + -ldflags "-X $(REPO)/misc.Version=$(VERSION)" \ + -o $@ ./cmd/$(notdir $@) -# Attach to existing container -attach: - @echo "\n${B}${G} attach to hash-container ${R}\n" - @time docker exec -it --name hash-demo /bin/sh +$(DIRS): + @echo "⇒ Ensure dir: $@" + @mkdir -p $@ -# Test code with all backends +# Pull go dependencies +dep: + @printf "⇒ Download requirements: " + CGO_ENABLED=0 \ + go mod download && echo OK + @printf "⇒ Tidy requirements : " + CGO_ENABLED=0 \ + go mod tidy -v && echo OK + +# Run Unit Test with go test test: - go test ./... + @echo "⇒ Running go test" + @go test ./... +# Run Unit Test with go test test.generic: - go test ./... --tags=generic + @echo "⇒ Running go test with generic tag" + @go test ./... --tags=generic + +# Print version +version: + @echo $(VERSION) + +clean: + rm -rf vendor + rm -rf .cache + rm -rf $(BIN) diff --git a/help.mk b/help.mk new file mode 100644 index 0000000..c065ec8 --- /dev/null +++ b/help.mk @@ -0,0 +1,11 @@ +.PHONY: help + +# Show this help prompt +help: + @echo ' Usage:' + @echo '' + @echo ' make ' + @echo '' + @echo ' Targets:' + @echo '' + @awk '/^#/{ comment = substr($$0,3) } comment && /^[a-zA-Z][a-zA-Z0-9_-]+ ?:/{ print " ", $$1, comment }' $(MAKEFILE_LIST) | column -t -s ':' | grep -v 'IGNORE' | sort | uniq