Skip to content

Instantly share code, notes, and snippets.

@Rid-lin
Forked from cjbarker/Makefile
Last active August 1, 2020 08:04
Show Gist options
  • Save Rid-lin/b419fdf7d963a7246abf61b4397af8ad to your computer and use it in GitHub Desktop.
Save Rid-lin/b419fdf7d963a7246abf61b4397af8ad 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=$(shell basename $(PWD))
VERSION=$(shell git describe --tags)
BUILD=$(shell git rev-parse --short HEAD)
PLATFORMS=darwin linux windows
ARCHITECTURES=386 amd64
# Setup linker flags option for build that interoperate with variable names in src code
LDFLAGS=-ldflags "-w -s -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)_$(VERSION)_$(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