-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
103 lines (79 loc) · 2.8 KB
/
Makefile
File metadata and controls
103 lines (79 loc) · 2.8 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
##
# make テストと全リリースビルド
# make darwin darwin用リリースビルド
# make DEBUG=1 darwin darwin用デバッグビルド
# make ARGS="main.go -arg1 -arg2=Xyz" run 即時実行
##
NAME := study-golang1
VERSION := v0.0.1
REVISION := $(shell git rev-parse --short HEAD)
##
# ビルドオプション
##
GOARCH := amd64
LDFLAGS := -X 'main.Name=$(NAME)' \
-X 'main.Version=$(VERSION)' \
-X 'main.Revision=$(REVISION)'
ifeq ($(DEBUG), 1)
BUILD_OPTIONS := -race -tags DEBUG -ldflags="$(LDFLAGS)"
BUILD_MODE := debug
else
BUILD_OPTIONS := -ldflags="-s -w $(LDFLAGS)"
BUILD_MODE := release
endif
##
# 使用するgoコマンドの決定。バージョンとかOS環境とかいろいろあって自動判定はあきらめました。
##
NATIVE_GO := GOPATH= go
DOCKER_GO := docker run -it -v "$(PWD):/go" -e GOPATH= golang:1.13 go
GO := $(NATIVE_GO)
GO4LINUX := $(DOCKER_GO)
GO4DARWIN := $(NATIVE_GO)
GO4WINDOWS := $(DOCKER_GO)
##
# 作業対象
##
GO_SRCS := $(shell find . -type f -name '*.go')
all: test linux darwin windows
##
# ビルド成果物
##
target/$(BUILD_MODE)/$(NAME)-linux-$(GOARCH): $(GO_SRCS)
GOOS=linux GOARCH=$(GOARCH) $(GO4LINUX) build $(BUILD_OPTIONS) -o target/$(BUILD_MODE)/$(NAME)-linux-$(GOARCH)
target/$(BUILD_MODE)/$(NAME)-darwin-$(GOARCH): $(GO_SRCS)
GOOS=darwin GOARCH=$(GOARCH) $(GO4DARWIN) build $(BUILD_OPTIONS) -o target/$(BUILD_MODE)/$(NAME)-darwin-$(GOARCH)
target/$(BUILD_MODE)/$(NAME)-windows-$(GOARCH).exe: $(GO_SRCS)
GOOS=windows GOARCH=$(GOARCH) $(GO4WINDOWS) build $(BUILD_OPTIONS) -o target/$(BUILD_MODE)/$(NAME)-windows-$(GOARCH).exe
target/tests.xml: $(GO_SRCS)
$(GO) build -o ./build/bin/go2xunit github.com/tebeka/go2xunit
$(GO) test -v ./... 2>&1 | ./build/bin/go2xunit -output target/tests.xml
$(GO) mod tidy # 現状のtidyは実行ファイルへの依存を検知できないためここでgo.modをrevertする
##
# タスク
##
# 実行バイナリファイルを作成します
linux: target/$(BUILD_MODE)/$(NAME)-linux-$(GOARCH)
darwin: target/$(BUILD_MODE)/$(NAME)-darwin-$(GOARCH)
windows: target/$(BUILD_MODE)/$(NAME)-windows-$(GOARCH).exe
# xUnit互換のテスト結果ファイルを作成します
xunit: target/tests.xml
# コード検査を実施します
vet:
$(GO) vet $(BUILD_OPTIONS) ./...
$(GO) vet $(BUILD_OPTIONS) ./...
# 単体テストを実施します
test:
$(GO) mod verify
$(GO) test -v ./...
# コードフォーマッタを適用します
fmt:
$(GO) mod tidy
$(GO) fmt $(shell $(GO) list ./...)
# ビルド生成ファイルを全掃除します
clean:
-rm -rf target/*
-rm -rf ./build/*
# 即時実行します
run:
@ $(GO) run $(BUILD_OPTIONS) $(ARGS)
.PHONY: all linux darwin windows xunit vet test fmt clean run