-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
131 lines (127 loc) · 3.75 KB
/
Makefile
File metadata and controls
131 lines (127 loc) · 3.75 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#编译配置
MAKEFLAGS += --no-print-directory
#工作目录
WORKSPACE=$(dir $(abspath $(lastword $(MAKEFILE_LIST))))
#包名
PACKAGE=$(shell basename $(WORKSPACE))
#编译
TARGET?=$(WORKSPACE)build/
GOBUILD=go build
#版本信息
GITCOMMIT=`git describe --always`
VERSION=$$(git describe --tags || echo "0.0.1")
GITDATE=`TZ=UTC git show -s --date=iso-strict-local --format=%cd HEAD`
BUILDDATE=`date -u +"%Y-%m-%dT%H:%M:%S%:z"`
LDFLAGS="-X forthxu/kv/service.Version=${VERSION} -X forthxu/kv/service.BuildDate=${BUILDDATE} -X forthxu/kv/service.GitCommit=${GITCOMMIT} -X forthxu/kv/service.GitDate=${GITDATE}"
CGO_ENABLED=0
#编译目标
ACTION=$(MAKECMDGOALS)
#系统
OS?=$(shell uname|tr A-Z a-z)
ifeq ($(ACTION), mac)
OS=darwin
SUFFIX=cmd
else ifeq ($(ACTION), darwin)
OS=darwin
SUFFIX=cmd
else ifeq ($(ACTION), dylib)
OS=darwin
SUFFIX=dylib
CGO_ENABLED=1
else ifeq ($(ACTION), docker)
OS=linux
SUFFIX=bin
else ifeq ($(ACTION), linux)
OS=linux
SUFFIX=bin
CGO_ENABLED=0
else ifeq ($(ACTION), so)
OS=linux
SUFFIX=so
CGO_ENABLED=1
else ifeq ($(ACTION), windows)
OS=windows
SUFFIX=exe
else ifeq ($(ACTION), dll)
OS=windows
SUFFIX=dll
CGO_ENABLED=1
endif
ifeq ($(OS), darwin)
CC=gcc
CXX=g++
LD=ld
else ifeq ($(OS), linux)
CC=gcc
CXX=g++
LD=ld
else ifeq ($(OS), windows)
CC=clang
CXX=clang++
LD=ld
endif
#架构
ARCHORIGIN=$(shell uname -m | tr A-Z a-z | sed 's/x86_64/amd64/' | sed 's/aarch64/arm64/')
ARCH?=ARCHORIGIN
ifeq ($(ARCH), arm64)
ARCH=arm64
ifeq ($(ARCHORIGIN), amd64)
ifeq ($(OS), linux)
CC=aarch64-linux-musl-gcc
CXX=aarch64-linux-musl-g++
LD=aarch64-linux-musl-ld
endif
endif
else
ARCH=amd64
ifeq ($(ARCHORIGIN), arm64)
ifeq ($(OS), linux)
CC=x86_64-linux-musl-gcc
CXX=x86_64-linux-musl-g++
LD=x86_64-linux-musl-ld
endif
endif
endif
#docker仓库
REGISTRY=forthxu
#忽略目录
.PHONY: $(PACKAGE)
#默认
default: $(OS)
@[ -f "$(WORKSPACE)cmd/main.go" ] && $(TARGET)$(PACKAGE).$(ARCH).$(SUFFIX) || exit 0
#帮助
help: info
@printf "命令格式:\n\tmake $(OS) ARCH=$(ARCH) TARGET=./build\n"
info:
@printf "目标:\t\t$(PACKAGE)\n"
@printf "目标后缀:\t$(SUFFIX)\n"
@printf "目标系统: \t$(OS)\n"
@printf "目标架构: \t$(ARCH)\n"
@printf "工作目录:\t$(WORKSPACE)\n"
@printf "编译目录: \t$(abspath $(TARGET))/\n"
#拷贝资源
resource:
@mkdir -p $(TARGET)
@[ -d "$(WORKSPACE)cmd" ] && find $(WORKSPACE)cmd/* -maxdepth 0 -not -regex '.*\.go$$' -not -regex '.*\.sh$$' |xargs -I {} cp -r {} $(TARGET)/ || exit 0
#生成protobuf
protobuf:
@[ -d "$(WORKSPACE)proto" ] && find $(WORKSPACE) -maxdepth 1 -type d -name proto |xargs -I {} find {} -type f -maxdepth 1 | xargs -I {} protoc {} -I $(WORKSPACE) --go_out=$(WORKSPACE) --go-grpc_out=$(WORKSPACE) --experimental_allow_proto3_optional || exit 0
@[ -d "$(WORKSPACE)proto" ] && find $(WORKSPACE) -regex ".*.pb.go" -exec sh -c 'sed -i".bak" "s/,omitempty//g" "{}" && rm -f "{}.bak"' \; || exit 0
#编译程序
mac: darwin
darwin: program
linux: program
windows: program
program: info resource protobuf
[ -f "$(WORKSPACE)cmd/main.go" ] && CGO_ENABLED=${CGO_ENABLED} CC=${CC} CXX=${CXX} LD=${LD} GOOS=${OS} GOARCH=${ARCH} $(GOBUILD) --ldflags=${LDFLAGS} -o $(TARGET)$(PACKAGE).$(ARCH).${SUFFIX} $(WORKSPACE)cmd/main.go || exit 0
#编译动态库
dylib: dynamic
so: dynamic
dll: dynamic
dynamic: info protobuf
@[ -d "$(WORKSPACE)plugin" ] && CGO_ENABLED=${CGO_ENABLED} CC=${CC} CXX=${CXX} LD=${LD} GOOS=${OS} GOARCH=${ARCH} $(GOBUILD) --ldflags=${LDFLAGS} -buildmode=plugin -o $(TARGET)$(PACKAGE).$(ARCH).$(SUFFIX) $(WORKSPACE)/plugin/*.go || exit 0
#打包docker镜像
docker: linux
ifneq ($(wildcard $(WORKSPACE)cmd/Dockerfile),)
@docker build --platform=linux/$(ARCH) -t $(REGISTRY)/$(PACKAGE):latest-$(ARCH) -f $(TARGET)Dockerfile --build-arg arch=$(ARCH) $(TARGET)
endif