Create a gist now

Instantly share code, notes, and snippets.

@heewa /Makefile
Last active Jun 18, 2016

Embed
What would you like to do?
gomake - sharable golang makefile
# Makefile to build Golang projects that can be used without putting the
# makefile in the project directory. Using make to build & run go projects
# allows you to reuse build output when files haven't changed, considering
# building go can be slow.
#
# Based On: https://ariejan.net/2015/10/03/a-makefile-for-golang-cli-tools/
#
# By default it builds main.go from the directory you're in, to a binary
# called main. To amke it easier to use, set bash aliases & functions:
#
# alias gomake='make -f /path/to/Makefile'
# gorun() { gomake RUN_ARGS="$*" run ;}
#
# Then you can use like `gomake clean all` or `gorun args to your program`
# Project vars - You can override these with the below vars directly when
# calling make `BINARY=myproject make`, or `make BINARY=myproject`, or modify
# this file. But it's probably best to set them using aliases.
SOURCEDIR ?= $(shell pwd)
MAIN ?= main.go
BINARY ?= /tmp/gorun--$(notdir $(basename $(MAIN)))
BUILD_FLAGS ?=
RUN_ARGS ?=
# Auto vars - you shouldn't need to modify these
SOURCES := $(shell find $(SOURCEDIR) -name '*.go')
all: $(BINARY)
run: $(BINARY) $(SOURCES)
@$(BINARY) $(RUN_ARGS)
$(BINARY): $(SOURCES)
go build ${BUILD_FLAGS} -o $@ ${MAIN}
.PHONY: clean list
list:
@echo MAIN:${MAIN}
@echo BINARY:${BINARY}
@echo BUILD_FLAGS:${BUILD_FLAGS}
@echo SOURCES:${SOURCES}
clean:
if [ -f ${BINARY} ] ; then rm ${BINARY} ; fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment