Skip to content

Commit

Permalink
build: Cleanup Makefiles of Go components
Browse files Browse the repository at this point in the history
- ensure the copyright header is present
- improve variable naming in `Makefile.defs`
- standardise go invocations with `GO_BUILD`, `GO_TEST`, etc
- remove dependency declaration as `go build` manages build
  cache much more intelligently now and makefile maintenance
  overhead is unnecessary
- ensure all of the common rules are defined consistently,
  so these can be sourced into a common file in the future
- remove trailing and doubly blank lines

Signed-off-by: Ilya Dmitrichenko <errordeveloper@gmail.com>
  • Loading branch information
errordeveloper committed Apr 14, 2020
1 parent 38bb91e commit 156bd37
Show file tree
Hide file tree
Showing 22 changed files with 349 additions and 157 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ coverage-all.html
.idea/
*.plist

cilium/bash_autocomplete
*_bash_completion
*.swo
outgoing

Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ coverage-all.html
.idea/
*.plist

cilium/bash_autocomplete
*_bash_completion
*.swo
outgoing

Expand Down
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ ifdef LIBNETWORK_PLUGIN
SUBDIRS_CILIUM_CONTAINER += plugins/cilium-docker
endif
SUBDIRS := $(SUBDIRS_CILIUM_CONTAINER) operator plugins tools hubble-proxy
GOFILES_EVAL := $(subst _$(ROOT_DIR)/,,$(shell $(CGO_DISABLED) $(GOLIST) $(GO) list -e ./...))
GOFILES_EVAL := $(subst _$(ROOT_DIR)/,,$(shell $(GO_LIST) -e ./...))
GOFILES ?= $(GOFILES_EVAL)
TESTPKGS_EVAL := $(subst github.com/cilium/cilium/,,$(shell $(CGO_DISABLED) $(GOLIST) $(GO) list -e ./... | grep -v '/api/v1\|/vendor\|/contrib' | grep -v -P 'test(?!/helpers/logutils)'))
TESTPKGS_EVAL := $(subst github.com/cilium/cilium/,,$(shell $(GO_LIST) -e ./... | grep -v '/api/v1\|/vendor\|/contrib' | grep -v -P 'test(?!/helpers/logutils)'))
TESTPKGS ?= $(TESTPKGS_EVAL)
GOLANGVERSION := $(shell $(GO) version 2>/dev/null | grep -Eo '(go[0-9].[0-9])')
GOLANG_SRCFILES := $(shell for pkg in $(subst github.com/cilium/cilium/,,$(GOFILES)); do find $$pkg -name *.go -print; done | grep -v vendor | sort | uniq)
Expand Down Expand Up @@ -89,7 +89,7 @@ tests-privileged:
# cilium-map-migrate is a dependency of some unit tests.
$(QUIET) $(MAKE) $(SUBMAKEOPTS) -C bpf cilium-map-migrate
$(QUIET)$(foreach pkg,$(PRIV_TEST_PKGS),\
$(GO) test $(GOFLAGS) $(TEST_LDFLAGS) github.com/cilium/cilium/$(pkg) $(GOTEST_PRIV_OPTS) || exit 1;)
$(GO_TEST) $(TEST_LDFLAGS) github.com/cilium/cilium/$(pkg) $(GOTEST_PRIV_OPTS) || exit 1;)

start-kvstores:
ifeq ($(SKIP_KVSTORES),"false")
Expand Down Expand Up @@ -151,7 +151,7 @@ endif
# hence will trigger an error of too many arguments. As a workaround, we
# have to process these packages in different subshells.
for pkg in $(patsubst %,github.com/cilium/cilium/%,$(TESTPKGS)); do \
$(_GO) test $(GOFLAGS) $(TEST_UNITTEST_LDFLAGS) $$pkg $(GOTEST_BASE) $(GOTEST_COVER_OPTS) || exit 1; \
$(GO_TEST) $(TEST_UNITTEST_LDFLAGS) $$pkg $(GOTEST_BASE) $(GOTEST_COVER_OPTS) || exit 1; \
tail -n +2 coverage.out >> coverage-all-tmp.out; \
done
$(MAKE) generate-cov
Expand All @@ -160,14 +160,14 @@ endif

bench: start-kvstores
$(QUIET)$(foreach pkg,$(patsubst %,github.com/cilium/cilium/%,$(TESTPKGS)),\
$(GO) test $(GOFLAGS) $(TEST_UNITTEST_LDFLAGS) $(GOTEST_BASE) $(BENCHFLAGS) \
$(GO_TEST) $(TEST_UNITTEST_LDFLAGS) $(GOTEST_BASE) $(BENCHFLAGS) \
$(pkg) \
|| exit 1;)
$(MAKE) stop-kvstores

bench-privileged:
$(QUIET)$(foreach pkg,$(patsubst %,github.com/cilium/cilium/%,$(TESTPKGS)),\
$(GO) test $(GOFLAGS) $(TEST_UNITTEST_LDFLAGS) $(GOTEST_BASE) $(BENCHFLAGS) \
$(GO_TEST) $(TEST_UNITTEST_LDFLAGS) $(GOTEST_BASE) $(BENCHFLAGS) \
-tags=privileged_tests $(pkg) \
|| exit 1;)

Expand Down Expand Up @@ -365,7 +365,7 @@ gofmt:

govet:
@$(ECHO_CHECK) vetting all GOFILES...
$(QUIET) $(GOLIST) $(_GO) vet \
$(QUIET) $(GO_VET) \
./api/... \
./bugtool/... \
./cilium/... \
Expand Down
77 changes: 40 additions & 37 deletions Makefile.defs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,24 @@ LOCALSTATEDIR?=/var
RUNDIR?=/var/run
CONFDIR?=/etc

CGO_DISABLED = CGO_ENABLED=0
GOLIST ?= GO111MODULE=off
ifeq ($(GO),)
GO = $(CGO_DISABLED) go
_GO = go
INSTALL = install

# Container engine
export CONTAINER_ENGINE?=docker
CONTAINER_ENGINE_FULL=$(QUIET)$(CONTAINER_ENGINE)

# Set DOCKER_IMAGE_TAG with "latest" by default
ifeq ($(DOCKER_IMAGE_TAG),)
DOCKER_IMAGE_TAG="latest"
endif

ifeq ($(shell uname -m),aarch64)
ETCD_IMAGE=quay.io/coreos/etcd:v3.2.17-arm64
else
_GO = $(GO)
ETCD_IMAGE=quay.io/coreos/etcd:v3.2.17
endif

INSTALL = install
GO ?= go

VERSION = $(shell cat $(dir $(lastword $(MAKEFILE_LIST)))/VERSION)
VERSION_MAJOR = $(shell cat $(dir $(lastword $(MAKEFILE_LIST)))/VERSION | cut -d. -f1)
Expand All @@ -33,70 +41,65 @@ ifneq ($(wildcard $(dir $(lastword $(MAKEFILE_LIST)))/.git),)
else
GIT_VERSION = $(shell cat $(ROOT_DIR)/GIT_VERSION)
endif
BUILD = $(VERSION) $(GIT_VERSION) $(shell $(GO) version)
GOLDFLAGS = -X "github.com/cilium/cilium/pkg/version.Version=$(BUILD)"
FULL_BUILD_VERSION = $(VERSION) $(GIT_VERSION) $(shell $(GO) version)
GO_BUILD_LDFLAGS += -X "github.com/cilium/cilium/pkg/version.Version=$(FULL_BUILD_VERSION)"

ifeq ($(NOSTRIP),)
# Note: these options will not remove annotations needed for stack
# traces, so panic backtraces will still be readable.
#
# -w: Omit the DWARF symbol table.
# -s: Omit the symbol table and debug information.
GOLDFLAGS += -s -w
GO_BUILD_LDFLAGS += -s -w
endif

CILIUM_ENVOY_SHA=$(shell grep -o "FROM.*cilium/cilium-envoy:[0-9a-fA-F]*" $(ROOT_DIR)/Dockerfile | cut -d : -f 2)
GOLDFLAGS += -X "github.com/cilium/cilium/pkg/envoy.RequiredEnvoyVersionSHA=$(CILIUM_ENVOY_SHA)"
GO_BUILD_LDFLAGS += -X "github.com/cilium/cilium/pkg/envoy.RequiredEnvoyVersionSHA=$(CILIUM_ENVOY_SHA)"

BPF_FILES_EVAL := $(shell git ls-files $(ROOT_DIR)/bpf/ | grep -v .gitignore | tr "\n" ' ')
BPF_FILES ?= $(BPF_FILES_EVAL)
BPF_SRCFILES := $(subst ../,,$(BPF_FILES))

CILIUM_DATAPATH_SHA=$(shell cat $(BPF_FILES) | sha1sum | awk '{print $$1}')
GOLDFLAGS += -X "github.com/cilium/cilium/pkg/datapath/loader.DatapathSHA=$(CILIUM_DATAPATH_SHA)"

# Set DOCKER_IMAGE_TAG with "latest" by default
ifeq ($(DOCKER_IMAGE_TAG),)
DOCKER_IMAGE_TAG="latest"
endif
GO_BUILD_LDFLAGS += -X "github.com/cilium/cilium/pkg/datapath/loader.DatapathSHA=$(CILIUM_DATAPATH_SHA)"

# Set -mod=vendor if running >= go 1.13 or if GO111MODULE is set.
# A go build is being executed with go modules if:
# * The go command is invoked with GO111MODULE=on environment variable set.
# * The go command is invoked in a directory outside of the $GOPATH/src tree
# and the environment variable GO111MODULE unset (or explicitly set to 'auto').
ifeq ($(GO111MODULE),on)
GOFLAGS ?= -mod=vendor
GOCLEAN ?= -mod=vendor
GO_BUILD_FLAGS += -mod=vendor
GO_TEST_FLAGS += -mod=vendor
GO_CLEAN_FLAGS += -mod=vendor
endif

# Compile with '-mod=vendor' if go >= 1.13
GO_MAJOR_VERSION_GE_1 := $(shell expr `$(GO) version | grep -E 'go[0-9]{1}+' -o | sed 's/go//g'` \>= 1)
ifeq ($(GO_MAJOR_VERSION_GE_1),1)
GO_MINOR_VERSION_GE_13 := $(shell expr `$(GO) version | grep -E 'go[^ ]+' -o | sed 's/go1.//g'` \>= 13)
ifeq ($(GO_MINOR_VERSION_GE_13),1)
GOFLAGS ?= -mod=vendor
GOCLEAN ?= -mod=vendor
GO_BUILD_FLAGS += -mod=vendor
GO_TEST_FLAGS += -mod=vendor
GO_CLEAN_FLAGS += -mod=vendor
endif
endif

GOBUILD = $(GOFLAGS) -ldflags '$(GOLDFLAGS)' $(EXTRA_GOBUILD_FLAGS)

# Uncomment to enable race detection
#GOBUILD += -race

# Uncomment to enable deadlock detection
#GOBUILD += -tags lockdebug
ifneq ($(RACE),)
GO_BUILD_FLAGS += -race
GO_TEST_FLAGS += -race
endif

ifneq ($(LOCKDEBUG),)
GOBUILD += -tags lockdebug
GO_BUILD_FLAGS += -tags lockdebug
endif

# Container engine
export CONTAINER_ENGINE?=docker
CONTAINER_ENGINE_FULL=$(QUIET)$(CONTAINER_ENGINE)
GO_BUILD_FLAGS += -ldflags '$(GO_BUILD_LDFLAGS) $(EXTRA_GO_BUILD_LDFLAGS)' $(EXTRA_GO_BUILD_FLAGS)

ifeq ($(shell uname -m),aarch64)
ETCD_IMAGE=quay.io/coreos/etcd:v3.2.17-arm64
else
ETCD_IMAGE=quay.io/coreos/etcd:v3.2.17
endif
GO_BUILD = CGO_ENABLED=0 $(GO) build $(GO_BUILD_FLAGS)
GO_BUILD_WITH_CGO = CGO_ENABLED=1 $(GO) build $(GO_BUILD_FLAGS)
GO_TEST = $(GO) test $(GO_TEST_FLAGS)
GO_CLEAN = $(GO) clean $(GO_TEST_FLAGS)
# TODO: remove `GO111MODULE=off` once Go 1.13 is deprecated by Go maintainers
GO_VET = GO111MODULE=off $(GO) vet
GO_LIST = GO111MODULE=off $(GO) list
14 changes: 8 additions & 6 deletions bugtool/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@

include ../Makefile.defs

TARGET=cilium-bugtool
SOURCES := $(shell find ../common . -name '*.go')
$(TARGET): $(SOURCES)
@$(ECHO_GO)
$(QUIET)$(GO) build $(GOBUILD) -o $(TARGET)
TARGET := cilium-bugtool

.PHONY: all $(TARGET) clean install

all: $(TARGET)

$(TARGET):
@$(ECHO_GO)
$(QUIET)$(GO_BUILD) -o $@

clean:
@$(ECHO_CLEAN)
-$(QUIET)rm -f .cilium-bugtool.config $(TARGET)
$(QUIET)$(GO) clean $(GOCLEAN)
$(QUIET)$(GO_CLEAN)

install:
$(QUIET)$(INSTALL) -m 0755 -d $(DESTDIR)$(BINDIR)
Expand Down
2 changes: 1 addition & 1 deletion cilium-dev.Dockerfile.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ coverage-all.html
.idea/
*.plist

cilium/bash_autocomplete
*_bash_completion
*.swo
outgoing

Expand Down
35 changes: 24 additions & 11 deletions cilium-health/Makefile
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
# Copyright 2017-2020 Authors of Cilium
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

include ../Makefile.defs

SUBDIRS = responder
TARGET = cilium-health
TARGET := cilium-health
SUBDIRS := responder

.PHONY: all $(SUBDIRS) install clean
.PHONY: all $(TARGET) $(SUBDIRS) clean install

all: $(TARGET) $(SUBDIRS)

SOURCES := $(shell find ../api/v1/health ../pkg/health cmd . \
\( -name '*.go' ! -name '*_test.go' $(foreach dir,$(SUBDIRS),! -path './$(dir)/*') \) )
$(TARGET): $(SOURCES)
$(TARGET):
@$(ECHO_GO)
$(QUIET)$(GO) build $(GOBUILD) -o $(TARGET)

$(SUBDIRS): force
@ $(MAKE) $(SUBMAKEOPTS) -C $@ all
$(QUIET)$(GO_BUILD) -o $@

clean:
@$(ECHO_CLEAN)
$(QUIET)for i in $(SUBDIRS); do $(MAKE) $(SUBMAKEOPTS) -C $$i clean; done
-$(QUIET)rm -f $(TARGET)
$(QUIET)$(GO) clean $(GOCLEAN)
$(QUIET)$(GO_CLEAN)

install:
$(QUIET)for i in $(SUBDIRS); do $(MAKE) $(SUBMAKEOPTS) -C $$i install; done
$(QUIET)$(INSTALL) -m 0755 -d $(DESTDIR)$(BINDIR)
$(QUIET)$(INSTALL) -m 0755 $(TARGET) $(DESTDIR)$(BINDIR)


$(SUBDIRS): force
@ $(MAKE) $(SUBMAKEOPTS) -C $@ all

.PHONY: force
force :;
28 changes: 22 additions & 6 deletions cilium-health/responder/Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
# Copyright 2017-2020 Authors of Cilium
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

include ../../Makefile.defs

TARGET=cilium-health-responder
SOURCES := $(shell find ../../pkg/health/probe/responder . \( -name '*.go' ! -name '*_test.go' \))
$(TARGET): $(SOURCES)
@$(ECHO_GO)
$(QUIET)$(GO) build $(GOBUILD) -o $(TARGET)
TARGET := cilium-health-responder

.PHONY: all $(TARGET) clean install

all: $(TARGET)

$(TARGET):
@$(ECHO_GO)
$(QUIET)$(GO_BUILD) -o $@

clean:
@$(ECHO_CLEAN)
-$(QUIET)rm -f $(TARGET)
$(QUIET)$(GO) clean $(GOCLEAN)
$(QUIET)$(GO_CLEAN)

install:
$(QUIET)$(INSTALL) -m 0755 -d $(DESTDIR)$(BINDIR)
Expand Down
2 changes: 1 addition & 1 deletion cilium-operator.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ LABEL maintainer="maintainer@cilium.io"
ADD . /go/src/github.com/cilium/cilium
WORKDIR /go/src/github.com/cilium/cilium/operator
ARG LOCKDEBUG
RUN make LOCKDEBUG=$LOCKDEBUG EXTRA_GOBUILD_FLAGS="-tags operator_aws,operator_azure"
RUN make LOCKDEBUG=$LOCKDEBUG EXTRA_GO_BUILD_FLAGS="-tags operator_aws,operator_azure"

FROM docker.io/library/alpine:3.9.3 as certs
RUN apk --update add ca-certificates
Expand Down
1 change: 0 additions & 1 deletion cilium/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
cilium
bash_autocomplete
34 changes: 24 additions & 10 deletions cilium/Makefile
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
include ../Makefile.quiet
# Copyright 2017-2020 Authors of Cilium
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

include ../Makefile.defs

TARGET=cilium
SOURCES := $(shell find ../api ../daemon ../common ../pkg cmd . \( -name '*.go' ! -name '*_test.go' \))
TARGET := cilium

$(TARGET): $(SOURCES)
@$(ECHO_GO)
$(QUIET)$(GO) build $(GOBUILD) -o $(TARGET)
.PHONY: all $(TARGET) clean install

all: $(TARGET)

$(TARGET):
@$(ECHO_GO)
$(QUIET)$(GO_BUILD) -o $@

clean:
@$(ECHO_CLEAN) $(notdir $(shell pwd))
@$(ECHO_CLEAN)
-$(QUIET)rm -f $(TARGET)
$(QUIET)$(GO) clean $(GOCLEAN)
$(QUIET)$(GO_CLEAN)

install:
$(QUIET)$(INSTALL) -m 0755 -d $(DESTDIR)$(BINDIR)
$(QUIET)$(INSTALL) -m 0755 $(TARGET) $(DESTDIR)$(BINDIR)
$(QUIET)$(INSTALL) -m 0755 -d $(DESTDIR)$(CONFDIR)/bash_completion.d
./$(TARGET) completion bash > bash_autocomplete
$(QUIET)$(INSTALL) -m 0644 -T bash_autocomplete $(DESTDIR)$(CONFDIR)/bash_completion.d/cilium
./$(TARGET) completion bash > $(TARGET)_bash_completion
$(QUIET)$(INSTALL) -m 0644 -T $(TARGET)_bash_completion $(DESTDIR)$(CONFDIR)/bash_completion.d/$(TARGET)

0 comments on commit 156bd37

Please sign in to comment.