Last active
March 21, 2023 21:39
-
-
Save marcomalva/1ca09aed849b4dc9953d43eb3f922ab4 to your computer and use it in GitHub Desktop.
[Makefile with Build-In-Help]help and also git tag, git commit hash, and build time injection into binary #make
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Makefile to build ... | |
# | |
# for help run: make help | |
# nicer output with: make help | bat -l mk | |
# | |
# an alternative for build-in help w/ color coding and w/o .PHONY: | |
# https://github.com/Byron/dua-cli/blob/d0e85fec1586a8937928472e361837ef21e40b14/Makefile | |
# | |
BINARY=$(shell basename $(shell pwd)) | |
VERSION=$(shell git describe --tags --dirty) | |
# Get name of Makefile so the grep picks the right one | |
name1 := $(lastword $(MAKEFILE_LIST)) | |
# Set help as the default goal | |
.DEFAULT_GOAL := help | |
help: | |
@echo "$(BINARY) $(VERSION) $(shell git rev-parse HEAD)" | |
@echo | |
@grep '^.PHONY: .* #' "$(name1)" | sed 's/\.PHONY: \(.*\) # \(.*\)/\1: \2/' | expand -t20 | |
@echo | |
.PHONY: build # build binary on current platform | |
build: | |
go build -o $(BINARY) -ldflags '-extldflags "-static"' -ldflags "-X main.Buildstamp=`date -u '+%Y-%m-%d_%I:%M:%S%p'` -X main.Githash=`git rev-parse HEAD` -X main.Version=$(VERSION)" . | |
.PHONY: buildLinux # build binary for Linux and enforce rebuild of packages that are already up-to-date (-a); takes longer | |
buildLinux: | |
env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOGC=off go build -o $(BINARY) -a -ldflags '-extldflags "-static"' -ldflags "-X main.Buildstamp=`date -u '+%Y-%m-%d_%I:%M:%S%p'` -X main.Githash=`git rev-parse HEAD` -X main.Version=`$(VERSION)`" . | |
.PHONY: lint # lint the code | |
lint: | |
golangci-lint run --sort-results | |
.PHONY: tidy # tidy up the GO build environment | |
tidy: | |
go mod tidy -compat=1.20 | |
.PHONY: update # update the GO dependencies | |
update: | |
go get -u all | |
.PHONY: currentTag # get the tag that will be used for current binary | |
currentTag: | |
@echo "$(VERSION)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment