Skip to content

Instantly share code, notes, and snippets.

@sandalsoft
Forked from subfuzion/Makefile.md
Last active September 24, 2018 02:37
Show Gist options
  • Save sandalsoft/acb43f6f45d9df62ed4b9b8c9d09e047 to your computer and use it in GitHub Desktop.
Save sandalsoft/acb43f6f45d9df62ed4b9b8c9d09e047 to your computer and use it in GitHub Desktop.
Makefile for Go projects
SHELL := /bin/bash
# The name of the executable (default is current directory name)
TARGET := $(shell echo $${PWD\#\#*/})
.DEFAULT_GOAL: $(TARGET)
# These will be provided to the target
VERSION := 1.0.0
BUILD := `git rev-parse HEAD`
# Use linker flags to provide version/build settings to the target
LDFLAGS=-ldflags "-X=main.Version=$(VERSION) -X=main.Build=$(BUILD)"
# go source files, ignore vendor directory
SRC = $(shell find . -type f -name '*.go' -not -path "./vendor/*")
.PHONY: all build clean install uninstall fmt simplify check run
all: check install
$(TARGET): $(SRC)
@go build $(LDFLAGS) -o $(TARGET)
build: $(TARGET)
@true
clean:
@rm -f $(TARGET)
install:
@go install $(LDFLAGS)
uninstall: clean
@rm -f $$(which ${TARGET})
fmt:
@gofmt -l -w $(SRC)
simplify:
@gofmt -s -l -w $(SRC)
check:
@test -z $(shell gofmt -l main.go | tee /dev/stderr) || echo "[WARN] Fix formatting issues with 'make fmt'"
@for d in $$(go list ./... | grep -v /vendor/); do golint $${d}; done
@go tool vet ${SRC}
run: install
@$(TARGET)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment