blob: ec4ebd3db1b30ace63d6f38b82eacd34057f66aa [file] [log] [blame]
Scott Bakereee8dd82019-09-24 12:52:34 -07001#
2# Copyright 2016-present the original author or authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17# set default shell
18SHELL = bash -e -o pipefail
19
20# Variables
21VERSION ?= $(shell cat ./VERSION)
22
23DOCKER_LABEL_VCS_DIRTY = false
24ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0)
25 DOCKER_LABEL_VCS_DIRTY = true
26endif
27## Docker related
28DOCKER_EXTRA_ARGS ?=
29DOCKER_REGISTRY ?=
30DOCKER_REPOSITORY ?=
31DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true)
32SIMULATEDONU_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-adapter-simulated-onu
33
34## Docker labels. Only set ref and commit date if committed
35DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
36DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
37DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
38DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
39
Scott Baker70c16092019-09-30 17:08:54 -070040# Default is GO111MODULE=auto, which will refuse to use go mod if running
41# go less than 1.13.0 and this repository is checked out in GOPATH. For now,
42# force module usage. This affects commands executed from this Makefile, but
43# not the environment inside the Docker build (which does not build from
44# inside a GOPATH).
45export GO111MODULE=on
46
Scott Bakereee8dd82019-09-24 12:52:34 -070047DOCKER_BUILD_ARGS ?= \
48 ${DOCKER_EXTRA_ARGS} \
49 --build-arg org_label_schema_version="${VERSION}" \
50 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
51 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
52 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
53 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
54 --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}"
55
56DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \
57 --build-arg LOCAL_PYVOLTHA=${LOCAL_PYVOLTHA} \
58 --build-arg LOCAL_PROTOS=${LOCAL_PROTOS}
59
Scott Baker378d25c2019-10-25 15:04:37 -070060.PHONY: simulated_onu local-protos local-lib-go
Scott Bakereee8dd82019-09-24 12:52:34 -070061
62# This should to be the first and default target in this Makefile
63help:
64 @echo "Usage: make [<target>]"
65 @echo "where available targets are:"
66 @echo
67 @echo "build : Build the docker images."
68 @echo " - If this is the first time you are building, choose 'make build' option."
69 @echo "simulated_onu : Build the simulated_onu docker image"
70 @echo "clean : Remove files created by the build and tests"
71 @echo "docker-push : Push the docker images to an external repository"
72 @echo "lint-dockerfile : Perform static analysis on Dockerfiles"
73 @echo "lint-style : Verify code is properly gofmt-ed"
74 @echo "lint-sanity : Verify that 'go vet' doesn't report any issues"
Scott Baker70c16092019-09-30 17:08:54 -070075 @echo "lint-mod : Verify the integrity of the 'mod' files"
Scott Bakereee8dd82019-09-24 12:52:34 -070076 @echo "lint : Shorthand for lint-style & lint-sanity"
Scott Baker378d25c2019-10-25 15:04:37 -070077 @echo "local-lib-go : Copies a local version of the VOTLHA dependencies into the vendor directory"
78 @echo "local-protos : Copies a local verison of the VOLTHA protos into the vendor directory"
Scott Bakereee8dd82019-09-24 12:52:34 -070079 @echo "test : Generate reports for all go tests"
80 @echo
81
82## Local Development Helpers
83local-protos:
84ifdef LOCAL_PROTOS
Scott Baker378d25c2019-10-25 15:04:37 -070085 rm -rf vendor/github.com/opencord/voltha-protos/go
Scott Bakereee8dd82019-09-24 12:52:34 -070086 mkdir -p vendor/github.com/opencord/voltha-protos/go
Scott Baker378d25c2019-10-25 15:04:37 -070087 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/go
88 rm -rf vendor/github.com/opencord/voltha-protos/go/vendor
89endif
90
91## Local Development Helpers
92local-lib-go:
93ifdef LOCAL_LIB_GO
94 rm -rf vendor/github.com/opencord/voltha-lib-go
95 mkdir -p vendor/github.com/opencord/voltha-lib-go/v2/pkg
96 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v2/pkg/
Scott Bakereee8dd82019-09-24 12:52:34 -070097endif
98
99## Docker targets
100
101build: docker-build
102
103docker-build: simulated_onu
104
Scott Baker378d25c2019-10-25 15:04:37 -0700105simulated_onu: local-protos local-lib-go
Scott Bakereee8dd82019-09-24 12:52:34 -0700106 docker build $(DOCKER_BUILD_ARGS) -t ${SIMULATEDONU_IMAGENAME}:${DOCKER_TAG} -t ${SIMULATEDONU_IMAGENAME}:latest -f docker/Dockerfile.simulated_onu .
107
108docker-push:
109 docker push ${SIMULATEDONU_IMAGENAME}:${DOCKER_TAG}
110
111## lint and unit tests
112
113PATH:=$(GOPATH)/bin:$(PATH)
114HADOLINT=$(shell PATH=$(GOPATH):$(PATH) which hadolint)
115lint-dockerfile:
116ifeq (,$(shell PATH=$(GOPATH):$(PATH) which hadolint))
117 mkdir -p $(GOPATH)/bin
118 curl -o $(GOPATH)/bin/hadolint -sNSL https://github.com/hadolint/hadolint/releases/download/v1.17.1/hadolint-$(shell uname -s)-$(shell uname -m)
119 chmod 755 $(GOPATH)/bin/hadolint
120endif
121 @echo "Running Dockerfile lint check ..."
122 @hadolint $$(find . -name "Dockerfile.*")
123 @echo "Dockerfile lint check OK"
124
125lint-style:
126ifeq (,$(shell which gofmt))
127 go get -u github.com/golang/go/src/cmd/gofmt
128endif
129 @echo "Running style check..."
130 @gofmt_out="$$(gofmt -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\
131 if [ ! -z "$$gofmt_out" ]; then \
132 echo "$$gofmt_out" ;\
133 echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\
134 exit 1 ;\
135 fi
136 @echo "Style check OK"
137
138lint-sanity:
139 @echo "Running sanity check..."
Scott Baker70c16092019-09-30 17:08:54 -0700140 @go vet -mod=vendor ./...
Scott Bakereee8dd82019-09-24 12:52:34 -0700141 @echo "Sanity check OK"
142
Scott Baker70c16092019-09-30 17:08:54 -0700143lint-mod:
Scott Bakereee8dd82019-09-24 12:52:34 -0700144 @echo "Running dependency check..."
Scott Baker70c16092019-09-30 17:08:54 -0700145 @go mod verify
Scott Bakeraf515692019-11-26 08:16:21 -0800146 @echo "Dependency check OK. Running vendor check..."
147 @git status > /dev/null
148 @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Staged or modified files must be committed before running this test" && echo "`git status`" && exit 1)
149 @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files must be cleaned up before running this test" && echo "`git status`" && exit 1)
150 go mod tidy
151 go mod vendor
152 @git status > /dev/null
153 @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Modified files detected after running go mod tidy / go mod vendor" && echo "`git status`" && exit 1)
154 @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files detected after running go mod tidy / go mod vendor" && echo "`git status`" && exit 1)
155 @echo "Vendor check OK."
Scott Bakereee8dd82019-09-24 12:52:34 -0700156
Scott Baker70c16092019-09-30 17:08:54 -0700157lint: lint-style lint-sanity lint-mod lint-dockerfile
Scott Bakereee8dd82019-09-24 12:52:34 -0700158
159GO_JUNIT_REPORT:=$(shell which go-junit-report)
160GOCOVER_COBERTURA:=$(shell which gocover-cobertura)
161
162test:
163ifeq (,$(GO_JUNIT_REPORT))
164 go get -u github.com/jstemmer/go-junit-report
165 @GO_JUNIT_REPORT=$(GOPATH)/bin/go-junit-report
166endif
167ifeq (,$(GOCOVER_COBERTURA))
168 go get -u github.com/t-yuki/gocover-cobertura
169 @GOCOVER_COBERTURA=$(GOPATH)/bin/gocover-cobertura
170endif
171 @mkdir -p ./tests/results
Scott Baker70c16092019-09-30 17:08:54 -0700172 @go test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
Scott Bakereee8dd82019-09-24 12:52:34 -0700173 RETURN=$$? ;\
174 $(GO_JUNIT_REPORT) < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
175 $(GOCOVER_COBERTURA) < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
176 exit $$RETURN
177
178clean:
179
180distclean: clean
181
182# end file