blob: 72bcfaac94573f313f5d3654d9d52c594bdf7335 [file] [log] [blame]
Scott Baker2d897982019-09-24 11:50:08 -07001#
2# Copyright 2016 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)
32SIMULATEDOLT_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-adapter-simulated-olt
Andrea Campanella17146ee2019-12-17 15:33:23 -080033TYPE ?= minimal
Scott Baker2d897982019-09-24 11:50:08 -070034
35## Docker labels. Only set ref and commit date if committed
36DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
37DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
38DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
39DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
40
Scott Bakerf8f97fa2019-09-30 13:07:01 -070041# Default is GO111MODULE=auto, which will refuse to use go mod if running
42# go less than 1.13.0 and this repository is checked out in GOPATH. For now,
43# force module usage. This affects commands executed from this Makefile, but
44# not the environment inside the Docker build (which does not build from
45# inside a GOPATH).
46export GO111MODULE=on
47
Scott Baker2d897982019-09-24 11:50:08 -070048DOCKER_BUILD_ARGS ?= \
49 ${DOCKER_EXTRA_ARGS} \
50 --build-arg org_label_schema_version="${VERSION}" \
51 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
52 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
53 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
54 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
55 --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}"
56
57DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \
58 --build-arg LOCAL_PYVOLTHA=${LOCAL_PYVOLTHA} \
59 --build-arg LOCAL_PROTOS=${LOCAL_PROTOS}
60
Scott Baker5d389402019-10-25 14:55:13 -070061.PHONY: simulated_olt simulated_onu local-protos local-lib-go
Scott Baker2d897982019-09-24 11:50:08 -070062
63# This should to be the first and default target in this Makefile
64help:
65 @echo "Usage: make [<target>]"
66 @echo "where available targets are:"
67 @echo
68 @echo "build : Build the docker images."
69 @echo " - If this is the first time you are building, choose 'make build' option."
70 @echo "simulated_olt : Build the simulated_olt docker image"
71 @echo "clean : Remove files created by the build and tests"
72 @echo "docker-push : Push the docker images to an external repository"
73 @echo "lint-dockerfile : Perform static analysis on Dockerfiles"
74 @echo "lint-style : Verify code is properly gofmt-ed"
75 @echo "lint-sanity : Verify that 'go vet' doesn't report any issues"
Scott Bakerf8f97fa2019-09-30 13:07:01 -070076 @echo "lint-mod : Verify the integrity of the 'mod' files"
Scott Baker2d897982019-09-24 11:50:08 -070077 @echo "lint : Shorthand for lint-style & lint-sanity"
Scott Baker5d389402019-10-25 14:55:13 -070078 @echo "local-lib-go : Copies a local version of the VOTLHA dependencies into the vendor directory"
79 @echo "local-protos : Copies a local verison of the VOLTHA protos into the vendor directory"
Scott Baker2d897982019-09-24 11:50:08 -070080 @echo "test : Generate reports for all go tests"
81 @echo
82
83## Local Development Helpers
84local-protos:
85ifdef LOCAL_PROTOS
Scott Baker5d389402019-10-25 14:55:13 -070086 rm -rf vendor/github.com/opencord/voltha-protos/go
Scott Baker2d897982019-09-24 11:50:08 -070087 mkdir -p vendor/github.com/opencord/voltha-protos/go
Scott Baker5d389402019-10-25 14:55:13 -070088 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/go
89 rm -rf vendor/github.com/opencord/voltha-protos/go/vendor
90endif
91
92## Local Development Helpers
93local-lib-go:
94ifdef LOCAL_LIB_GO
95 rm -rf vendor/github.com/opencord/voltha-lib-go
96 mkdir -p vendor/github.com/opencord/voltha-lib-go/v2/pkg
97 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v2/pkg/
Scott Baker2d897982019-09-24 11:50:08 -070098endif
99
100## Docker targets
101
102build: docker-build
103
104docker-build: simulated_olt
105
Scott Baker5d389402019-10-25 14:55:13 -0700106simulated_olt: local-protos local-lib-go
Scott Baker2d897982019-09-24 11:50:08 -0700107 docker build $(DOCKER_BUILD_ARGS) -t ${SIMULATEDOLT_IMAGENAME}:${DOCKER_TAG} -t ${SIMULATEDOLT_IMAGENAME}:latest -f docker/Dockerfile.simulated_olt .
108
109docker-push:
110 docker push ${SIMULATEDOLT_IMAGENAME}:${DOCKER_TAG}
111
Andrea Campanella17146ee2019-12-17 15:33:23 -0800112docker-kind-load:
113 @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi
114 kind load docker-image ${SIMULATEDOLT_IMAGENAME}:${DOCKER_TAG} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | sed 's/,$$//')
115
Scott Baker2d897982019-09-24 11:50:08 -0700116## lint and unit tests
117
118PATH:=$(GOPATH)/bin:$(PATH)
119HADOLINT=$(shell PATH=$(GOPATH):$(PATH) which hadolint)
120lint-dockerfile:
121ifeq (,$(shell PATH=$(GOPATH):$(PATH) which hadolint))
122 mkdir -p $(GOPATH)/bin
123 curl -o $(GOPATH)/bin/hadolint -sNSL https://github.com/hadolint/hadolint/releases/download/v1.17.1/hadolint-$(shell uname -s)-$(shell uname -m)
124 chmod 755 $(GOPATH)/bin/hadolint
125endif
126 @echo "Running Dockerfile lint check ..."
127 @hadolint $$(find . -name "Dockerfile.*")
128 @echo "Dockerfile lint check OK"
129
130lint-style:
131ifeq (,$(shell which gofmt))
132 go get -u github.com/golang/go/src/cmd/gofmt
133endif
134 @echo "Running style check..."
135 @gofmt_out="$$(gofmt -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\
136 if [ ! -z "$$gofmt_out" ]; then \
137 echo "$$gofmt_out" ;\
138 echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\
139 exit 1 ;\
140 fi
141 @echo "Style check OK"
142
143lint-sanity:
144 @echo "Running sanity check..."
Scott Bakerf8f97fa2019-09-30 13:07:01 -0700145 @go vet -mod=vendor ./...
Scott Baker2d897982019-09-24 11:50:08 -0700146 @echo "Sanity check OK"
147
Scott Bakerf8f97fa2019-09-30 13:07:01 -0700148lint-mod:
Scott Baker2d897982019-09-24 11:50:08 -0700149 @echo "Running dependency check..."
Scott Bakerf8f97fa2019-09-30 13:07:01 -0700150 @go mod verify
Scott Baker3f6b3212019-11-26 08:14:28 -0800151 @echo "Dependency check OK. Running vendor check..."
152 @git status > /dev/null
153 @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)
154 @[[ `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)
155 go mod tidy
156 go mod vendor
157 @git status > /dev/null
158 @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)
159 @[[ `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)
160 @echo "Vendor check OK."
Scott Baker2d897982019-09-24 11:50:08 -0700161
Scott Bakerf8f97fa2019-09-30 13:07:01 -0700162lint: lint-style lint-sanity lint-mod lint-dockerfile
Scott Baker2d897982019-09-24 11:50:08 -0700163
164GO_JUNIT_REPORT:=$(shell which go-junit-report)
165GOCOVER_COBERTURA:=$(shell which gocover-cobertura)
166
167test:
168ifeq (,$(GO_JUNIT_REPORT))
169 go get -u github.com/jstemmer/go-junit-report
170 @GO_JUNIT_REPORT=$(GOPATH)/bin/go-junit-report
171endif
172ifeq (,$(GOCOVER_COBERTURA))
173 go get -u github.com/t-yuki/gocover-cobertura
174 @GOCOVER_COBERTURA=$(GOPATH)/bin/gocover-cobertura
175endif
176 @mkdir -p ./tests/results
Scott Bakerf8f97fa2019-09-30 13:07:01 -0700177 @go test --mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
Scott Baker2d897982019-09-24 11:50:08 -0700178 RETURN=$$? ;\
179 $(GO_JUNIT_REPORT) < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
180 $(GOCOVER_COBERTURA) < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
181 exit $$RETURN
182
183clean:
184
185distclean: clean
186
187# end file