Created
November 23, 2019 16:00
-
-
Save ducmeit1/4d73e6d4b031d808508f80d5932a14e5 to your computer and use it in GitHub Desktop.
Makefile for build and zip go application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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