blob: 7e4bf689d9b7987d73b319ff0c1bfb3593627c65 [file] [log] [blame]
Scott Bakere7144bc2019-10-01 14:16:47 -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)
32AFROUTER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-afrouter
33AFROUTERD_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-afrouterd
34
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
41DOCKER_BUILD_ARGS ?= \
42 ${DOCKER_EXTRA_ARGS} \
43 --build-arg org_label_schema_version="${VERSION}" \
44 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
45 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
46 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
47 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
48 --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}"
49
50DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \
51 --build-arg LOCAL_PYVOLTHA=${LOCAL_PYVOLTHA} \
52 --build-arg LOCAL_PROTOS=${LOCAL_PROTOS}
53
54.PHONY: rw_core ro_core afrouter afrouterd local-protos local-pyvoltha
55
56# This should to be the first and default target in this Makefile
57help:
58 @echo "Usage: make [<target>]"
59 @echo "where available targets are:"
60 @echo
61 @echo "build : Build the docker images."
62 @echo " - If this is the first time you are building, choose 'make build' option."
63 @echo "afrouter : Build the afrouter docker image"
64 @echo "afrouterd : Build the afrouterd docker image"
65 @echo "clean : Remove files created by the build and tests"
66 @echo "distclean : Remove venv directory"
67 @echo "docker-push : Push the docker images to an external repository"
68 @echo "lint-dockerfile : Perform static analysis on Dockerfiles"
69 @echo "lint-style : Verify code is properly gofmt-ed"
70 @echo "lint-sanity : Verify that 'go vet' doesn't report any issues"
71 @echo "lint-dep : Verify the integrity of the 'dep' files"
72 @echo "lint : Shorthand for lint-style & lint-sanity"
73 @echo "sca : Runs various SCA through golangci-lint tool"
74 @echo "test : Generate reports for all go tests"
75 @echo
76
77## Local Development Helpers
78local-protos:
79ifdef LOCAL_PROTOS
80 mkdir -p vendor/github.com/opencord/voltha-protos/go
81 cp -r ${GOPATH}/src/github.com/opencord/voltha-protos/go/* vendor/github.com/opencord/voltha-protos/go
82endif
83
84## Docker targets
85
86build: docker-build
87
88docker-build: afrouter afrouterd
89
90afrouter: local-protos
91 docker build $(DOCKER_BUILD_ARGS) -t ${AFROUTER_IMAGENAME}:${DOCKER_TAG} -t ${AFROUTER_IMAGENAME}:latest -f docker/Dockerfile.afrouter .
92
93afrouterd: local-protos
94 docker build $(DOCKER_BUILD_ARGS) -t ${AFROUTERD_IMAGENAME}:${DOCKER_TAG} -t ${AFROUTERD_IMAGENAME}:latest -f docker/Dockerfile.afrouterd .
95
96docker-push:
97 docker push ${AFROUTER_IMAGENAME}:${DOCKER_TAG}
98 docker push ${AFROUTERD_IMAGENAME}:${DOCKER_TAG}
99
100## lint and unit tests
101
102PATH:=$(GOPATH)/bin:$(PATH)
103HADOLINT=$(shell PATH=$(GOPATH):$(PATH) which hadolint)
104lint-dockerfile:
105ifeq (,$(shell PATH=$(GOPATH):$(PATH) which hadolint))
106 mkdir -p $(GOPATH)/bin
107 curl -o $(GOPATH)/bin/hadolint -sNSL https://github.com/hadolint/hadolint/releases/download/v1.17.1/hadolint-$(shell uname -s)-$(shell uname -m)
108 chmod 755 $(GOPATH)/bin/hadolint
109endif
110 @echo "Running Dockerfile lint check ..."
111 @hadolint $$(find . -name "Dockerfile.*")
112 @echo "Dockerfile lint check OK"
113
114lint-style:
115ifeq (,$(shell which gofmt))
116 go get -u github.com/golang/go/src/cmd/gofmt
117endif
118 @echo "Running style check..."
119 @gofmt_out="$$(gofmt -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\
120 if [ ! -z "$$gofmt_out" ]; then \
121 echo "$$gofmt_out" ;\
122 echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\
123 exit 1 ;\
124 fi
125 @echo "Style check OK"
126
127lint-sanity:
128 @echo "Running sanity check..."
129 @go vet ./...
130 @echo "Sanity check OK"
131
132lint-dep:
133 @echo "Running dependency check..."
134 @dep check
135 @echo "Dependency check OK"
136
137lint: lint-style lint-sanity lint-dep lint-dockerfile
138
139GO_JUNIT_REPORT:=$(shell which go-junit-report)
140GOCOVER_COBERTURA:=$(shell which gocover-cobertura)
141
142GOLANGCI_LINT_TOOL:=$(shell which golangci-lint)
143sca:
144 @echo "Please install golangci-lint tool to run sca"
145 @mkdir -p ./sca-report
146 golangci-lint run --out-format junit-xml ./... 2>&1 | tee ./sca-report/sca-report.xml ;\
147 rm -rf sca-report
148
149test:
150ifeq (,$(GO_JUNIT_REPORT))
151 go get -u github.com/jstemmer/go-junit-report
152 @GO_JUNIT_REPORT=$(GOPATH)/bin/go-junit-report
153endif
154ifeq (,$(GOCOVER_COBERTURA))
155 go get -u github.com/t-yuki/gocover-cobertura
156 @GOCOVER_COBERTURA=$(GOPATH)/bin/gocover-cobertura
157endif
158 @mkdir -p ./tests/results
159 @go test -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
160 RETURN=$$? ;\
161 $(GO_JUNIT_REPORT) < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
162 $(GOCOVER_COBERTURA) < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
163 exit $$RETURN
164
165clean:
166
167distclean: clean
168
169# end file