Skip to main content

Converting from a Makefile

Here's an example Makefile that we'll "Divide and Conquer" in the next conversion tutorials.

Makefile

Makefile
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)

GITHUB_USERNAME := turtlemonvh
BUILD_DIR := ${GOPATH}/src/github.com/${GITHUB_USERNAME}/${BINARY}
CURRENT_DIR := $(shell pwd)
BUILD_DIR_LINK := $(shell readlink ${BUILD_DIR})

LDFLAGS := -ldflags "-X main.VERSION=${VERSION} -X main.COMMIT=${COMMIT} -X main.BRANCH=${BRANCH}"

.PHONY: %

all: link clean test vet build

link:
if [ "${BUILD_DIR_LINK}" != "${CURRENT_DIR}" ]; then \
echo "Fixing symlinks for build"; \
rm -f ${BUILD_DIR}; \
ln -s ${CURRENT_DIR} ${BUILD_DIR}; \
fi

build: build.linux build.darwin build.windows

build.linux:
cd ${BUILD_DIR}; \
GOOS=linux GOARCH=${GOARCH} \
go build ${LDFLAGS} -o ${BINARY}-linux-${GOARCH} .

build.darwin:
cd ${BUILD_DIR}; \
GOOS=darwin GOARCH=${GOARCH} \
go build ${LDFLAGS} -o ${BINARY}-darwin-${GOARCH} .

build.windows:
cd ${BUILD_DIR}; \
GOOS=windows GOARCH=${GOARCH} \
go build ${LDFLAGS} -o ${BINARY}-windows-${GOARCH}.exe .

test:
if ! hash go2xunit 2>/dev/null; then go install github.com/tebeka/go2xunit; fi
cd ${BUILD_DIR}; \
go test -v ./... 2>&1 | go2xunit -output ${TEST_REPORT}

vet:
cd ${BUILD_DIR}; \
go vet ./... > ${VET_REPORT} 2>&1

fmt:
cd ${BUILD_DIR}; \
go fmt $$(go list ./... | grep -v /vendor/)

clean:
-rm -f ${TEST_REPORT}
-rm -f ${VET_REPORT}
-rm -f ${BINARY}-*