Skip to content

Instantly share code, notes, and snippets.

@turtlemonvh
Last active January 23, 2024 03:33
Show Gist options
  • Save turtlemonvh/38bd3d73e61769767c35931d8c70ccb4 to your computer and use it in GitHub Desktop.
Save turtlemonvh/38bd3d73e61769767c35931d8c70ccb4 to your computer and use it in GitHub Desktop.
Golang Project Makefile Template

Makefile Template

A Makefile example for simple golang projects.

Usage

You should change values of the following variables

  • BINARY change from superdo to the name of your project
  • GITHUB_USERNAME change from turtlemonvh to your username

That should be it.

Features

  • you can put the project anywhere (doesn't have to be on your gopath) and it will work fine
  • builds binaries for windows, linux, osx
  • injects COMMIT and BRANCH as build variables
    • You need to define variables named COMMIT and BRANCH in main.go to have those variables injected into your build
  • creates a test report in xunit format that can be consumed by build tools like jenkins
  • creates a vet.report file that is the output of go vet

Caveats

  • This Makefile works for a project that uses godeps.
# Borrowed from:
# https://github.com/silven/go-example/blob/master/Makefile
# https://vic.demuzere.be/articles/golang-makefile-crosscompile/
BINARY = superdo
VET_REPORT = vet.report
TEST_REPORT = tests.xml
GOARCH = amd64
VERSION?=?
COMMIT=$(shell git rev-parse HEAD)
BRANCH=$(shell git rev-parse --abbrev-ref HEAD)
# Symlink into GOPATH
GITHUB_USERNAME=turtlemonvh
BUILD_DIR=${GOPATH}/src/github.com/${GITHUB_USERNAME}/${BINARY}
CURRENT_DIR=$(shell pwd)
BUILD_DIR_LINK=$(shell readlink ${BUILD_DIR})
# Setup the -ldflags option for go build here, interpolate the variable values
LDFLAGS = -ldflags "-X main.VERSION=${VERSION} -X main.COMMIT=${COMMIT} -X main.BRANCH=${BRANCH}"
# Build the project
all: link clean test vet linux darwin windows
link:
BUILD_DIR=${BUILD_DIR}; \
BUILD_DIR_LINK=${BUILD_DIR_LINK}; \
CURRENT_DIR=${CURRENT_DIR}; \
if [ "$${BUILD_DIR_LINK}" != "$${CURRENT_DIR}" ]; then \
echo "Fixing symlinks for build"; \
rm -f $${BUILD_DIR}; \
ln -s $${CURRENT_DIR} $${BUILD_DIR}; \
fi
linux:
cd ${BUILD_DIR}; \
GOOS=linux GOARCH=${GOARCH} go build ${LDFLAGS} -o ${BINARY}-linux-${GOARCH} . ; \
cd - >/dev/null
darwin:
cd ${BUILD_DIR}; \
GOOS=darwin GOARCH=${GOARCH} go build ${LDFLAGS} -o ${BINARY}-darwin-${GOARCH} . ; \
cd - >/dev/null
windows:
cd ${BUILD_DIR}; \
GOOS=windows GOARCH=${GOARCH} go build ${LDFLAGS} -o ${BINARY}-windows-${GOARCH}.exe . ; \
cd - >/dev/null
test:
if ! hash go2xunit 2>/dev/null; then go install github.com/tebeka/go2xunit; fi
cd ${BUILD_DIR}; \
godep go test -v ./... 2>&1 | go2xunit -output ${TEST_REPORT} ; \
cd - >/dev/null
vet:
-cd ${BUILD_DIR}; \
godep go vet ./... > ${VET_REPORT} 2>&1 ; \
cd - >/dev/null
fmt:
cd ${BUILD_DIR}; \
go fmt $$(go list ./... | grep -v /vendor/) ; \
cd - >/dev/null
clean:
-rm -f ${TEST_REPORT}
-rm -f ${VET_REPORT}
-rm -f ${BINARY}-*
.PHONY: link linux darwin windows test vet fmt clean
@fs111
Copy link

fs111 commented Sep 5, 2022

What is the license of this Makefile?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment