Skip to content

Instantly share code, notes, and snippets.

@leitu
Forked from cjbarker/Makefile
Created October 22, 2018 06:41
Show Gist options
  • Save leitu/390692609c59a0011581ab0dca28dc1c to your computer and use it in GitHub Desktop.
Save leitu/390692609c59a0011581ab0dca28dc1c 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment