Skip to content

Instantly share code, notes, and snippets.

@ducmeit1
Created November 23, 2019 16:00
Show Gist options
  • Save ducmeit1/4d73e6d4b031d808508f80d5932a14e5 to your computer and use it in GitHub Desktop.
Save ducmeit1/4d73e6d4b031d808508f80d5932a14e5 to your computer and use it in GitHub Desktop.
Makefile for build and zip go application
.PHONY: all
all: build
#Set binary file name
PKG = main
#Set zip file name
ZIPNAME = build.zip
#Set default value for GOOS and GOARCH
GOOS = linux
GOARCH = amd64
#Directories
WD = $(subst $(BSLASH),$(FLASH),$(shell pwd))
DISTDIR = $(WD)/dist
_build:
#Set output dir of binary file
$(eval OUTPUTDIR := $(DISTDIR))
#Make directory if not exist
@mkdir -p "$(DISTDIR)"
@echo "[build] ..."
#Set GOOS and GOARCH
@GOOS=$(GOOS) GOARCH=$(GOARCH)
#Build
go build -ldflags="-s -w" -o "$(OUTPUTDIR)/bin/$(PKG)" main.go
@echo "[build] done"
#Zip file binary
_zip_build:
$(eval OUTPUTDIR := $(DISTDIR)/bin)
@echo "[zip build] ..."
@cd "$(OUTPUTDIR)" && zip "$(ZIPNAME)" "./$(PKG)" && mv "$(OUTPUTDIR)/$(ZIPNAME)" "$(WD)"
@echo "[zip done]"
#Clean output directory
_clean_build:
$(eval OUTPUTDIR := $(DISTDIR))
@echo "[clean build] $(OUTPUTDIR) ..."
@rm -rf "$(OUTPUTDIR)"
@echo "[clean build] done"
#build
build: _build
build: _zip_build
build: _clean_build
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment