Skip to content

Instantly share code, notes, and snippets.

@cjbarker
Created June 21, 2017 22:10
Show Gist options
  • Save cjbarker/5ce66fcca74a1928a155cfb3fea8fac4 to your computer and use it in GitHub Desktop.
Save cjbarker/5ce66fcca74a1928a155cfb3fea8fac4 to your computer and use it in GitHub Desktop.
Makefile for cross-compiling Golang. Just update BINARY var in Makefile and create empty vars in main.go for Version and Build
# ########################################################## #
# Makefile for Golang Project
# Includes cross-compiling, installation, cleanup
# ########################################################## #
# Check for required command tools to build or stop immediately
EXECUTABLES = git go find pwd
K := $(foreach exec,$(EXECUTABLES),\
$(if $(shell which $(exec)),some string,$(error "No $(exec) in PATH)))
ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
BINARY=your-app-name
VERSION=1.0.1
BUILD=`git rev-parse HEAD`
PLATFORMS=darwin linux windows
ARCHITECTURES=386 amd64
# Setup linker flags option for build that interoperate with variable names in src code
LDFLAGS=-ldflags "-X main.Version=${VERSION} -X main.Build=${BUILD}"
default: build
all: clean build_all install
build:
go build ${LDFLAGS} -o ${BINARY}
build_all:
$(foreach GOOS, $(PLATFORMS),\
$(foreach GOARCH, $(ARCHITECTURES), $(shell export GOOS=$(GOOS); export GOARCH=$(GOARCH); go build -v -o $(BINARY)-$(GOOS)-$(GOARCH))))
install:
go install ${LDFLAGS}
# Remove only what we've created
clean:
find ${ROOT_DIR} -name '${BINARY}[-?][a-zA-Z0-9]*[-?][a-zA-Z0-9]*' -delete
.PHONY: check clean install build_all all
@tbenz9
Copy link

tbenz9 commented May 26, 2019

I know this is 2 years old, but you're missing a closing " on line 9.

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