@@ -8,6 +8,7 @@ CHARTS := production/helm/loki production/helm/promtail production/helm/loki-sta
8
8
IMAGE_PREFIX ?= grafana/
9
9
IMAGE_TAG := $(shell ./tools/image-tag)
10
10
UPTODATE := .uptodate
11
+ DEBUG_UPTODATE := .uptodate-debug
11
12
GIT_REVISION := $(shell git rev-parse --short HEAD)
12
13
GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
13
14
@@ -21,15 +22,25 @@ GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
21
22
$(SUDO ) docker tag $(IMAGE_PREFIX )$(shell basename $(@D ) ) $(IMAGE_PREFIX )$(shell basename $(@D ) ) :$(IMAGE_TAG )
22
23
touch $@
23
24
25
+ % /$(DEBUG_UPTODATE ) : % /Dockerfile.debug
26
+ $(SUDO ) docker build -f $(@D ) /Dockerfile.debug -t $(IMAGE_PREFIX )$(shell basename $(@D ) ) -debug $(@D ) /
27
+ $(SUDO ) docker tag $(IMAGE_PREFIX )$(shell basename $(@D ) ) -debug $(IMAGE_PREFIX )$(shell basename $(@D ) ) -debug:$(IMAGE_TAG )
28
+ touch $@
29
+
24
30
# We don't want find to scan inside a bunch of directories, to accelerate the
25
31
# 'make: Entering directory '/go/src/github.com/grafana/loki' phase.
26
32
DONT_FIND := -name tools -prune -o -name vendor -prune -o -name .git -prune -o -name .cache -prune -o -name .pkg -prune -o
27
33
28
34
# Get a list of directories containing Dockerfiles
29
35
DOCKERFILES := $(shell find . $(DONT_FIND ) -type f -name 'Dockerfile' -print)
30
36
UPTODATE_FILES := $(patsubst % /Dockerfile,% /$(UPTODATE ) ,$(DOCKERFILES ) )
37
+ DEBUG_DOCKERFILES := $(shell find . $(DONT_FIND ) -type f -name 'Dockerfile.debug' -print)
38
+ DEBUG_UPTODATE_FILES := $(patsubst % /Dockerfile.debug,% /$(DEBUG_UPTODATE ) ,$(DEBUG_DOCKERFILES ) )
39
+ DEBUG_DLV_FILES := $(patsubst % /Dockerfile.debug,% /dlv,$(DEBUG_DOCKERFILES ) )
31
40
DOCKER_IMAGE_DIRS := $(patsubst % /Dockerfile,% ,$(DOCKERFILES ) )
32
41
IMAGE_NAMES := $(foreach dir,$(DOCKER_IMAGE_DIRS ) ,$(patsubst % ,$(IMAGE_PREFIX ) % ,$(shell basename $(dir ) ) ) )
42
+ DEBUG_DOCKER_IMAGE_DIRS := $(patsubst % /Dockerfile.debug,% ,$(DEBUG_DOCKERFILES ) )
43
+ DEBUG_IMAGE_NAMES := $(foreach dir,$(DEBUG_DOCKER_IMAGE_DIRS ) ,$(patsubst % ,$(IMAGE_PREFIX ) % ,$(shell basename $(dir ) ) -debug) )
33
44
images :
34
45
$(info $(patsubst % ,% :$(IMAGE_TAG ) ,$(IMAGE_NAMES ) ) )
35
46
@echo > /dev/null
@@ -49,13 +60,29 @@ YACC_GOS := $(patsubst %.y,%.go,$(YACC_DEFS))
49
60
# for every directory with main.go in it, in the ./cmd directory.
50
61
MAIN_GO := $(shell find . $(DONT_FIND ) -type f -name 'main.go' -print)
51
62
EXES := $(foreach exe, $(patsubst ./cmd/% /main.go, % , $(MAIN_GO ) ) , ./cmd/$(exe ) /$(exe ) )
63
+ DEBUG_EXES := $(foreach exe, $(patsubst ./cmd/% /main.go, % , $(MAIN_GO ) ) , ./cmd/$(exe ) /$(exe ) -debug)
52
64
GO_FILES := $(shell find . $(DONT_FIND ) -name cmd -prune -o -type f -name '* .go' -print)
65
+
66
+ # This is the important part of how `make all` enters this file
67
+ # the above EXES finds all the main.go files and for each of them
68
+ # it creates the dep_exe targets which look like this:
69
+ # cmd/promtail/promtail: loki-build-image/.uptodate cmd/promtail//main.go pkg/loki/loki.go pkg/loki/fake_auth.go ...
70
+ # cmd/promtail/.uptodate: cmd/promtail/promtail
71
+ # Then when `make all` expands `$(UPTODATE_FILES)` it will call the second generated target and initiate the build process
53
72
define dep_exe
54
73
$(1 ) : $(dir $(1 ) ) /main.go $(GO_FILES ) $(PROTO_GOS ) $(YACC_GOS )
55
74
$(dir $(1 ) )$(UPTODATE ) : $(1 )
56
75
endef
57
76
$(foreach exe, $(EXES), $(eval $(call dep_exe, $(exe))))
58
77
78
+ # Everything is basically duplicated for debug builds,
79
+ # but with a different set of Dockerfiles and binaries appended with -debug.
80
+ define debug_dep_exe
81
+ $(1 ) : $(dir $(1 ) ) /main.go $(GO_FILES ) $(PROTO_GOS ) $(YACC_GOS )
82
+ $(dir $(1 ) )$(DEBUG_UPTODATE ) : $(1 )
83
+ endef
84
+ $(foreach exe, $(DEBUG_EXES), $(eval $(call debug_dep_exe, $(exe))))
85
+
59
86
# Manually declared dependancies and what goes into each exe
60
87
pkg/logproto/logproto.pb.go : pkg/logproto/logproto.proto
61
88
vendor/github.com/cortexproject/cortex/pkg/ring/ring.pb.go : vendor/github.com/cortexproject/cortex/pkg/ring/ring.proto
@@ -65,6 +92,7 @@ pkg/parser/labels.go: pkg/parser/labels.y
65
92
pkg/parser/matchers.go : pkg/parser/matchers.y
66
93
all : $(UPTODATE_FILES )
67
94
test : $(PROTO_GOS ) $(YACC_GOS )
95
+ debug : $(DEBUG_UPTODATE_FILES )
68
96
yacc : $(YACC_GOS )
69
97
protos : $(PROTO_GOS )
70
98
yacc : $(YACC_GOS )
@@ -86,6 +114,10 @@ TTY := --tty
86
114
87
115
VPREFIX := github.com/grafana/loki/vendor/github.com/prometheus/common/version
88
116
GO_FLAGS := -ldflags "-extldflags \"-static\" -s -w -X $(VPREFIX ) .Branch=$(GIT_BRANCH ) -X $(VPREFIX ) .Version=$(IMAGE_TAG ) -X $(VPREFIX ) .Revision=$(GIT_REVISION ) " -tags netgo
117
+ # Per some websites I've seen to add `-gcflags "all=-N -l"`, the gcflags seem poorly if at all documented
118
+ # the best I could dig up is -N disables optimizations and -l disables inlining which should make debugging match source better.
119
+ # Also remove the -s and -w flags present in the normal build which strip the symbol table and the DWARF symbol table.
120
+ DEBUG_GO_FLAGS := -gcflags "all=-N -l" -ldflags "-extldflags \"-static\" -X $(VPREFIX ) .Branch=$(GIT_BRANCH ) -X $(VPREFIX ) .Version=$(IMAGE_TAG ) -X $(VPREFIX ) .Revision=$(GIT_REVISION ) " -tags netgo
89
121
90
122
NETGO_CHECK = @strings $@ | grep cgo_stub\\\.go >/dev/null || { \
91
123
rm $@ ; \
@@ -96,9 +128,15 @@ NETGO_CHECK = @strings $@ | grep cgo_stub\\\.go >/dev/null || { \
96
128
false; \
97
129
}
98
130
131
+ # If BUILD_IN_CONTAINER is true, the build image is run which launches
132
+ # an image that mounts this project as a volume. The image invokes a build.sh script
133
+ # which essentially re-enters this file with BUILD_IN_CONTAINER=FALSE
134
+ # causing the else block target below to be called and the files to be built.
135
+ # If BUILD_IN_CONTAINER were false to begin with, the else block is
136
+ # executed and the binaries are built without ever launching the build container.
99
137
ifeq ($(BUILD_IN_CONTAINER ) ,true)
100
138
101
- $(EXES ) $(PROTO_GOS ) $(YACC_GOS ) lint test shell check-generated-files : loki-build-image/$(UPTODATE )
139
+ $(EXES ) $(DEBUG_EXES ) $( PROTO_GOS ) $(YACC_GOS ) lint test shell check-generated-files : loki-build-image/$(UPTODATE )
102
140
@mkdir -p $(shell pwd) /.pkg
103
141
@mkdir -p $(shell pwd) /.cache
104
142
$(SUDO ) docker run $(RM ) $(TTY ) -i \
@@ -109,6 +147,12 @@ $(EXES) $(PROTO_GOS) $(YACC_GOS) lint test shell check-generated-files: loki-bui
109
147
110
148
else
111
149
150
+ $(DEBUG_EXES ) : loki-build-image/$(UPTODATE )
151
+ CGO_ENABLED=0 go build $(DEBUG_GO_FLAGS ) -o $@ ./$(@D )
152
+ $(NETGO_CHECK )
153
+ # Copy the delve binary to make it easily available to put in the binary's container.
154
+ [ -f " /go/bin/dlv" ] && mv " /go/bin/dlv" $(@D ) /dlv
155
+
112
156
$(EXES ) : loki-build-image/$(UPTODATE )
113
157
CGO_ENABLED=0 go build $(GO_FLAGS ) -o $@ ./$(@D )
114
158
$(NETGO_CHECK )
@@ -200,6 +244,6 @@ helm-publish: helm
200
244
git push origin gh-pages
201
245
202
246
clean :
203
- $(SUDO ) docker rmi $(IMAGE_NAMES ) > /dev/null 2>&1 || true
204
- rm -rf $(UPTODATE_FILES ) $(EXES ) .cache
247
+ $(SUDO ) docker rmi $(IMAGE_NAMES ) $( DEBUG_IMAGE_NAMES ) > /dev/null 2>&1 || true
248
+ rm -rf $(UPTODATE_FILES ) $(EXES ) $( DEBUG_UPTODATE_FILES ) $( DEBUG_EXES ) $( DEBUG_DLV_FILES ) .cache
205
249
go clean ./...
0 commit comments