Skip to content
Snippets Groups Projects
Makefile 5.73 KiB
Newer Older
.PHONY: all test clean images protos
.DEFAULT_GOAL := all

# Boiler plate for bulding Docker containers.
# All this must go at top of file I'm afraid.
IMAGE_PREFIX ?= grafana/
IMAGE_TAG := $(shell ./tools/image-tag)
UPTODATE := .uptodate

# Building Docker images is now automated. The convention is every directory
Tom Wilkie's avatar
Tom Wilkie committed
# with a Dockerfile in it builds an image calls quay.io/grafana/tempo-<dirname>.
# Dependencies (i.e. things that go in the image) still need to be explicitly
# declared.
%/$(UPTODATE): %/Dockerfile
	$(SUDO) docker build -t $(IMAGE_PREFIX)$(shell basename $(@D)) $(@D)/
	$(SUDO) docker tag $(IMAGE_PREFIX)$(shell basename $(@D)) $(IMAGE_PREFIX)$(shell basename $(@D)):$(IMAGE_TAG)
	touch $@

Tom Wilkie's avatar
Tom Wilkie committed
# We don't want find to scan inside a bunch of directories, to accelerate the
Tom Wilkie's avatar
Tom Wilkie committed
# 'make: Entering directory '/go/src/github.com/grafana/tempo' phase.
Tom Wilkie's avatar
Tom Wilkie committed
DONT_FIND := -name tools -prune -o -name vendor -prune -o -name .git -prune -o -name .cache -prune -o -name .pkg -prune -o

# Get a list of directories containing Dockerfiles
Tom Wilkie's avatar
Tom Wilkie committed
DOCKERFILES := $(shell find . $(DONT_FIND) -type f -name 'Dockerfile' -print)
UPTODATE_FILES := $(patsubst %/Dockerfile,%/$(UPTODATE),$(DOCKERFILES))
DOCKER_IMAGE_DIRS := $(patsubst %/Dockerfile,%,$(DOCKERFILES))
IMAGE_NAMES := $(foreach dir,$(DOCKER_IMAGE_DIRS),$(patsubst %,$(IMAGE_PREFIX)%,$(shell basename $(dir))))
images:
	$(info $(patsubst %,%:$(IMAGE_TAG),$(IMAGE_NAMES)))
	@echo > /dev/null

# Generating proto code is automated.
Tom Wilkie's avatar
Tom Wilkie committed
PROTO_DEFS := $(shell find . $(DONT_FIND) -type f -name '*.proto' -print)
PROTO_GOS := $(patsubst %.proto,%.pb.go,$(PROTO_DEFS)) \
Tom Wilkie's avatar
Tom Wilkie committed
	vendor/github.com/cortexproject/cortex/pkg/ring/ring.pb.go \
	vendor/github.com/cortexproject/cortex/pkg/ingester/client/cortex.pb.go

# Generating yacc code is automated.
YACC_DEFS := $(shell find . $(DONT_FIND) -type f -name *.y -print)
YACC_GOS := $(patsubst %.y,%.go,$(YACC_DEFS))

# Building binaries is now automated.  The convention is to build a binary
# for every directory with main.go in it, in the ./cmd directory.
Tom Wilkie's avatar
Tom Wilkie committed
MAIN_GO := $(shell find . $(DONT_FIND) -type f -name 'main.go' -print)
EXES := $(foreach exe, $(patsubst ./cmd/%/main.go, %, $(MAIN_GO)), ./cmd/$(exe)/$(exe))
Tom Wilkie's avatar
Tom Wilkie committed
GO_FILES := $(shell find . $(DONT_FIND) -name cmd -prune -o -type f -name '*.go' -print)
define dep_exe
$(1): $(dir $(1))/main.go $(GO_FILES) $(PROTO_GOS) $(YACC_GOS)
$(dir $(1))$(UPTODATE): $(1)
endef
$(foreach exe, $(EXES), $(eval $(call dep_exe, $(exe))))

# Manually declared dependancies and what goes into each exe
pkg/logproto/logproto.pb.go: pkg/logproto/logproto.proto
Tom Wilkie's avatar
Tom Wilkie committed
vendor/github.com/cortexproject/cortex/pkg/ring/ring.pb.go: vendor/github.com/cortexproject/cortex/pkg/ring/ring.proto
pkg/parser/labels.go: pkg/parser/labels.y
pkg/parser/matchers.go: pkg/parser/matchers.y
all: $(UPTODATE_FILES)
test: $(PROTO_GOS) $(YACC_GOS)
yacc: $(YACC_GOS)
protos: $(PROTO_GOS)

# And now what goes into each image
tempo-build-image/$(UPTODATE): tempo-build-image/*

# All the boiler plate for building golang follows:
SUDO := $(shell docker info >/dev/null 2>&1 || echo "sudo -E")
BUILD_IN_CONTAINER := true
# RM is parameterized to allow CircleCI to run builds, as it
# currently disallows `docker run --rm`. This value is overridden
# in circle.yml
RM := --rm
# TTY is parameterized to allow Google Cloud Builder to run builds,
# as it currently disallows TTY devices. This value needs to be overridden
# in any custom cloudbuild.yaml files
TTY := --tty
GO_FLAGS := -ldflags "-extldflags \"-static\" -linkmode=external -s -w" -tags netgo -i
NETGO_CHECK = @strings $@ | grep cgo_stub\\\.go >/dev/null || { \
       rm $@; \
       echo "\nYour go standard library was built without the 'netgo' build tag."; \
       echo "To fix that, run"; \
       echo "    sudo go clean -i net"; \
       echo "    sudo go install -tags netgo std"; \
       false; \
}

ifeq ($(BUILD_IN_CONTAINER),true)

Tom Wilkie's avatar
Tom Wilkie committed
$(EXES) $(PROTO_GOS) $(YACC_GOS) lint test shell check-generated-files: tempo-build-image/$(UPTODATE)
	@mkdir -p $(shell pwd)/.pkg
Tom Wilkie's avatar
Tom Wilkie committed
	@mkdir -p $(shell pwd)/.cache
	$(SUDO) docker run $(RM) $(TTY) -i \
Tom Wilkie's avatar
Tom Wilkie committed
		-v $(shell pwd)/.cache:/go/cache \
		-v $(shell pwd)/.pkg:/go/pkg \
Tom Wilkie's avatar
Tom Wilkie committed
		-v $(shell pwd):/go/src/github.com/grafana/tempo \
		$(IMAGE_PREFIX)tempo-build-image $@;
$(EXES): tempo-build-image/$(UPTODATE)
	go build $(GO_FLAGS) -o $@ ./$(@D)
	$(NETGO_CHECK)

%.pb.go: tempo-build-image/$(UPTODATE)
	case "$@" in 	\
	vendor*)			\
		protoc -I ./vendor:./$(@D) --gogoslick_out=plugins=grpc:./vendor ./$(patsubst %.pb.go,%.proto,$@); \
		;;					\
	*)						\
		protoc -I ./vendor:./$(@D) --gogoslick_out=Mgoogle/protobuf/timestamp.proto=github.com/gogo/protobuf/types,plugins=grpc:./$(@D) ./$(patsubst %.pb.go,%.proto,$@); \
		;;					\
	esac

%.go: %.y
	goyacc -p $(basename $(notdir $<)) -o $@ $<

lint: tempo-build-image/$(UPTODATE)
Tom Wilkie's avatar
Tom Wilkie committed
	gometalinter ./...

check-generated-files: tempo-build-image/$(UPTODATE) yacc protos
	@git diff-files || (echo "changed files; failing check" && exit 1)
test: tempo-build-image/$(UPTODATE)
Tom Wilkie's avatar
Tom Wilkie committed
	go test ./...
shell: tempo-build-image/$(UPTODATE)
Tom Wilkie's avatar
Tom Wilkie committed
	@set -e; \
	mkdir -p images; \
	for image_name in $(IMAGE_NAMES); do \
		if ! echo $$image_name | grep build; then \
			docker save $$image_name:$(IMAGE_TAG) -o images/$$(echo $$image_name | tr "/" _):$(IMAGE_TAG); \
		fi \
	done

load-images:
Tom Wilkie's avatar
Tom Wilkie committed
	@set -e; \
	mkdir -p images; \
	for image_name in $(IMAGE_NAMES); do \
		if ! echo $$image_name | grep build; then \
			docker load -i images/$$(echo $$image_name | tr "/" _):$(IMAGE_TAG); \
		fi \
	done

Tom Wilkie's avatar
Tom Wilkie committed
push-images:
Tom Wilkie's avatar
Tom Wilkie committed
	@set -e; \
Tom Wilkie's avatar
Tom Wilkie committed
	for image_name in $(IMAGE_NAMES); do \
Tom Wilkie's avatar
Tom Wilkie committed
		if ! echo $$image_name | grep build; then \
			docker push $$image_name:$(IMAGE_TAG); \
		fi \
Tom Wilkie's avatar
Tom Wilkie committed
	done

clean:
	$(SUDO) docker rmi $(IMAGE_NAMES) >/dev/null 2>&1 || true
	rm -rf $(UPTODATE_FILES) $(EXES) $(PROTO_GOS) $(YACC_GOS) .cache