Skip to content

Instantly share code, notes, and snippets.

@necrophonic
Created January 16, 2020 11:21
Show Gist options
  • Save necrophonic/b6df4bfb95f9ce085be9060ef42c4305 to your computer and use it in GitHub Desktop.
Save necrophonic/b6df4bfb95f9ce085be9060ef42c4305 to your computer and use it in GitHub Desktop.
Generic Makefile for compiling multi-platform Go apps
PLATFORMS := darwin/amd64 linux/amd64
package = cmd/${NAME}/main.go
binary = build/${NAME}
help:
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
compile: clean ## Build binaries for all platforms defined in $PLATFORMS
ifndef NAME
$(error NAME is undefined)
endif
ifndef VERSION
$(error VERSION is undefined)
endif
@ echo "Building binaries for version $(VERSION):"
@ for platform in $(PLATFORMS); do \
platform_split=($${platform//\// }); \
GOOS=$${platform_split[0]}; \
GOARCH=$${platform_split[1]}; \
output_name=$(binary)'_'$(VERSION)'_'$$GOOS'-'$$GOARCH; \
if [ $$GOOS = "windows" ]; then \
output_name+='.exe'; \
fi; \
echo "- Build for $$platform -> $$output_name"; \
env GOOS=$$GOOS GOARCH=$$GOARCH go build -ldflags "-X main.Version=$${VERSION}" -o $$output_name $(package); \
done
@ echo "Done"
clean: ## Cleanup build artifacts and remove build/
rm -rf build/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment