Skip to content

Instantly share code, notes, and snippets.

@jintao-zero
Forked from subfuzion/Makefile.md
Created June 20, 2018 08:49
Show Gist options
  • Save jintao-zero/4bff352018d1731cb2890dc966be9915 to your computer and use it in GitHub Desktop.
Save jintao-zero/4bff352018d1731cb2890dc966be9915 to your computer and use it in GitHub Desktop.
Makefile for Go projects

Go has excellent build tools that mitigate the need for using make. For example, go install won't update the target unless it's older than the source files.

However, a Makefile can be convenient for wrapping Go commands with specific build targets that simplify usage on the command line. Since most of the targets are "phony", it's up to you to weigh the pros and cons of having a dependency on make versus using a shell script. For the simplicity of being able to specify targets that can be chained and can take advantage of make's chained targets, having a good Makefile can be effective.

Makefile

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