Skip to content

Instantly share code, notes, and snippets.

@dnishimura
Forked from yanatan16/Makefile.golang
Created June 20, 2012 17:51
Show Gist options
  • Save dnishimura/2961173 to your computer and use it in GitHub Desktop.
Save dnishimura/2961173 to your computer and use it in GitHub Desktop.
Makefile for Golang projects
# Makefile for a go project
#
# Author: Jon Eisen
# site: joneisen.me
#
# Targets:
# all: Builds the code
# build: Builds the code
# fmt: Formats the source files
# clean: cleans the code
# install: Installs the code to the GOPATH
# iref: Installs referenced projects
# test: Runs the tests
#
# Blog post on it: http://joneisen.me/post/25503842796
#
# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOINSTALL=$(GOCMD) install
GOTEST=$(GOCMD) test
GODEP=$(GOTEST) -i
GOFMT=gofmt -w
# Package lists
TOPLEVEL_PKG := whatever
INT_LIST := myinterface another/impl #<-- Interface directories
IMPL_LIST := myimplementation another/pkg/impl #<-- Implementation directories
CMD_LIST := mycmd1 another/pkg/mycmd1 #<-- Command directories
# List building
ALL_LIST = $(INT_LIST) $(IMPL_LIST) $(CMD_LIST)
BUILD_LIST = $(foreach int, $(ALL_LIST), $(int)_build)
CLEAN_LIST = $(foreach int, $(ALL_LIST), $(int)_clean)
INSTALL_LIST = $(foreach int, $(ALL_LIST), $(int)_install)
IREF_LIST = $(foreach int, $(ALL_LIST), $(int)_iref)
TEST_LIST = $(foreach int, $(ALL_LIST), $(int)_test)
FMT_TEST = $(foreach int, $(ALL_LIST), $(int)_fmt)
# All are .PHONY for now because dependencyness is hard
.PHONY: $(CLEAN_LIST) $(TEST_LIST) $(FMT_LIST) $(INSTALL_LIST) $(BUILD_LIST) $(IREF_LIST)
all: build
build: $(BUILD_LIST)
clean: $(CLEAN_LIST)
install: $(INSTALL_LIST)
test: $(TEST_LIST)
iref: $(IREF_LIST)
fmt: $(FMT_TEST)
$(BUILD_LIST): %_build: %_fmt %_iref
$(GOBUILD) $(TOPLEVEL_PKG)/$*
$(CLEAN_LIST): %_clean:
$(GOCLEAN) $(TOPLEVEL_PKG)/$*
$(INSTALL_LIST): %_install:
$(GOINSTALL) $(TOPLEVEL_PKG)/$*
$(IREF_LIST): %_iref:
$(GODEP) $(TOPLEVEL_PKG)/$*
$(TEST_LIST): %_test:
$(GOTEST) $(TOPLEVEL_PKG)/$*
$(FMT_TEST): %_fmt:
$(GOFMT) ./$*
@kyrofa
Copy link

kyrofa commented Jul 15, 2015

FMT_TEST should probably be FMT_LIST here. If not, update the .PHONY target deps.

@loint
Copy link

loint commented May 17, 2016

Thank you

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