Golang Makefile模版

我的main.go和makefile 是同一级别目录的

#Author-> CSDN : MetaverseMan
#Tag : 别熬夜
GOCMD		=		go
GORUN		=		$(GOCMD) run
GOBUILD		=		CGO_ENABLED=0 $(GOCMD) build
DIST		=		./build
BINARY		=		$(DIST)/myapp

# 64-Bit
# Linux
TARGET_LINUX=		GOOS=linux GOARCH=amd64
# MacOS
TARGET_MACOS=		GOOS=darwin GOARCH=amd64
# windows
TARGET_WINDOWS=		GOOS=windows GOARCH=amd64
# freebsd
TARGET_FREEBSD=		GOOS=freebsd GOARCH=amd64

#GIT_COMMIT=$(shell git rev-list -1 HEAD)
GO_VERSION	=		$(shell go version)
BUILD_DATE	=		$(shell date -u '+%Y-%m-%dT%H:%M:%SZ')

LDFLAGS		=	-ldflags "-w -s"
GCFLAGS		=   -gcflags "-N -l"
#LDFLAGS=-ldflags "-w -s \
#-X 'gitlab.test.cn/doc/gocommon/build.GitCommit=$(GIT_COMMIT)' \
#-X 'gitlab.test.cn/doc/gocommon/build.GoVersion=$(GO_VERSION)' \
#-X 'gitlab.test.cn/doc/gocommon/build.BuildDate=$(BUILD_DATE)'"
.PHONY: build-windows
build-windows:
	mkdir -p $(DIST);
	$(TARGET_WINDOWS) $(GOBUILD) -o $(DIST)/myapp-win ./
	cp ./dev.yaml $(DIST)/;

.PHONY: build-mac
build-mac:
	mkdir -p $(DIST);
	$(TARGET_MACOS) $(GOBUILD) -o $(DIST)/myapp-mac ./
	cp ./dev.yaml $(DIST)/;
.PHONY: build-linux
build-linux:
	mkdir -p $(DIST);
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o $(BINARY) ./
	cp ./dev.yaml $(DIST)/;

.PHONY: build-linux-mini
build-linux-mini:
	mkdir -p $(DIST);
	$(TARGET_LINUX) $(GOBUILD) $(LDFLAGS) -o $(BINARY) ./
	cp ./dev.yaml $(DIST)/;

.PHONY: build-all
build-all:build-windows build-linux build-mac

.PHONY: run
run:
	$(GORUN) $(DIST)/myapp

.PHONY: clean
clean:
	@if [ -f ./$(BINARY) ] ; then rm ./$(BINARY); fi
	@if [ -f ./$(DIST)/myapp-win ] ; then rm ./$(DIST)/myapp-win; fi
	@if [ -f ./$(DIST)/myapp-mac ] ; then rm ./$(DIST)/myapp-mac; fi

.PHONY: tidy
tidy:
	@go mod tidy

.PHONY: help
help:
	@echo "make - 格式化 Go 代码, 并编译生成二进制文件"
	@echo "make build-linux - 编译 Go 代码, 生成二进制文件(linux)"
	@echo "make build-linux-mini - 编译 Go 代码, 生成缩小版的二进制文件(linux)"
	@echo "make build-all - 编译 Go 代码,生成多平台的二进制文件"
	@echo "make run - 直接运行 Go 代码"
	@echo "make clean - 移除二进制文件和 vim swap files"
	@echo "make tidy - 执行go mod tidy"

学习资料:学习资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值