This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Makefile
88 lines (75 loc) · 2.23 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
projectName ?= billing
hostOsType := $(shell go env GOHOSTOS)
ifeq ($(hostOsType),windows)
$(error build $(projectName) is not support on $(hostOsType))
endif
export CGO_ENABLED=0
appVersion ?= 1.3.3
appArchList := x32 x64
appBuildTime ?= $(shell TZ=Asia/Shanghai date "+%F %T GMT%:z")
appGitCommitHash ?= $(shell git rev-parse HEAD)
appModuleName := github.com/liuguangw/billing_go/services
# 获取机器信息
builderMachine ?= unknown
osReleasePath = $(wildcard /etc/os-release)
issuePath = $(wildcard /etc/issue.net)
ifneq ($(osReleasePath),)
builderMachine=$(shell . $(osReleasePath); echo $$PRETTY_NAME)
else ifneq ($(issuePath),)
builderMachine=$(file < $(issuePath))
else
builderMachine = $(hostOsType)
endif
# github
ifneq (${GITHUB_ACTIONS},)
builderMachine += (GitHub Actions)
endif
buildLdFlags =-X $(appModuleName).appVersion=$(appVersion)
buildLdFlags += -X '$(appModuleName).appBuildTime=$(appBuildTime)'
buildLdFlags += -X $(appModuleName).gitCommitHash=$(appGitCommitHash)
buildLdFlags += -X '$(appModuleName).builderMachine=$(builderMachine)'
GO_BUILD:=go build -ldflags "-w -s $(buildLdFlags)"
EXTRA_FILES := config.yaml LICENSE README.md
releasePath := ./release
# 是否使用upx
useUpx ?= 0
upxBin :=
ifneq ($(useUpx),0)
upxBin = $(shell which upx)
endif
# release
define release_app
@echo build for $(2)
@mkdir -p $(releasePath)
@echo "build $(projectName) (linux/$(2))"
@GOOS=linux GOARCH=$(1) $(GO_BUILD) -o $(releasePath)/$(projectName)
@echo "build $(projectName) (windows/$(2))"
@GOOS=windows GOARCH=$(1) $(GO_BUILD) -o $(releasePath)/$(projectName).exe
@cp $(EXTRA_FILES) $(releasePath)/
endef
# 打包
define tar_app
@mv $(releasePath) ./$(1)
@tar -zcf $(1).tar.gz $(1)
@rm -rf ./$(1)
@echo output path: $(1).tar.gz
endef
build:
@$(GO_BUILD) -o $(projectName)
@echo build $(projectName) ok
# x32 x64
$(appArchList):
# call release_app,386,x32
# or
# call release_app,amd64,x64
$(call release_app,$(subst x64,amd64,$(subst x32,386,$@)),$@)
ifneq ($(upxBin),)
@$(upxBin) --best $(releasePath)/$(projectName)
@$(upxBin) --best $(releasePath)/$(projectName).exe
endif
$(call tar_app,$(projectName)-release-$@)
all:$(appArchList)
clean:
@rm -rf ./$(projectName)*
@rm -rf $(releasePath)
.PHONY: build x32 x64 all clean