blob: 7b7e3e597daf4083d24d72dafb6aa6d90ba5c016 [file] [log] [blame]
Don Newton98fd8812019-09-23 15:15:02 -04001#
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
Kent Hagerman6379e092020-02-18 18:05:57 -050021VERSION ?= $(shell cat ./VERSION)
Don Newton98fd8812019-09-23 15:15:02 -040022
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
Kent Hagerman6379e092020-02-18 18:05:57 -050028DOCKER_EXTRA_ARGS ?=
29DOCKER_REGISTRY ?=
30DOCKER_REPOSITORY ?=
31DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true)
32ADAPTER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}ofagent-go
Don Newton98fd8812019-09-23 15:15:02 -040033
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
40DOCKER_BUILD_ARGS ?= \
41 ${DOCKER_EXTRA_ARGS} \
42 --build-arg org_label_schema_version="${VERSION}" \
43 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
44 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
45 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
46 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
47 --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}"
48
49DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \
50 --build-arg LOCAL_PROTOS=${LOCAL_PROTOS}
51
Kent Hagerman6379e092020-02-18 18:05:57 -050052# tool containers
53VOLTHA_TOOLS_VERSION ?= 1.0.3
54
55GO = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang go
56GO_JUNIT_REPORT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-go-junit-report go-junit-report
57GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura
58GOFMT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang gofmt
59GOLANGCI_LINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golangci-lint golangci-lint
60HADOLINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-hadolint hadolint
61
Don Newton98fd8812019-09-23 15:15:02 -040062.PHONY: docker-build local-protos local-voltha
63
64# This should to be the first and default target in this Makefile
65help:
66 @echo "Usage: make [<target>]"
67 @echo "where available targets are:"
68 @echo
69 @echo "docker-build : Build ofagent-go docker image"
70 @echo "help : Print this help"
71 @echo "docker-push : Push the docker images to an external repository"
72 @echo "lint : Run lint verification, depenancy, gofmt and reference check"
73 @echo "sca : Runs various SCA through golangci-lint tool"
74 @echo "test : Run unit tests, if any"
75 @echo
76
77
78## Local Development Helpers
79
80local-protos:
81ifdef LOCAL_PROTOS
82 mkdir -p vendor/github.com/opencord/voltha-protos/go
83 cp -r ${GOPATH}/src/github.com/opencord/voltha-protos/go/* vendor/github.com/opencord/voltha-protos/go
84endif
85
86local-voltha:
87ifdef LOCAL_VOLTHA
88 mkdir -p vendor/github.com/opencord/voltha-go/
89 cp -rf ${GOPATH}/src/github.com/opencord/voltha-go/ vendor/github.com/opencord/
90 rm -rf vendor/github.com/opencord/voltha-go/vendor
91endif
92
93
94## Docker targets
95
96docker-build: local-protos local-voltha
Kent Hagerman6379e092020-02-18 18:05:57 -050097 docker build $(DOCKER_BUILD_ARGS) -t ${ADAPTER_IMAGENAME}:${DOCKER_TAG} -t ${ADAPTER_IMAGENAME}:latest -f docker/Dockerfile.ofagent-go .
Don Newton98fd8812019-09-23 15:15:02 -040098
99docker-push:
Kent Hagerman6379e092020-02-18 18:05:57 -0500100 docker push ${ADAPTER_IMAGENAME}:${DOCKER_TAG}
Don Newton98fd8812019-09-23 15:15:02 -0400101
102## lint and unit tests
103
Kent Hagerman6379e092020-02-18 18:05:57 -0500104lint-dockerfile:
105 @echo "Running Dockerfile lint check..."
106 @${HADOLINT} $$(find . -name "Dockerfile.*")
107 @echo "Dockerfile lint check OK"
108
Don Newton98fd8812019-09-23 15:15:02 -0400109lint-style:
Don Newton98fd8812019-09-23 15:15:02 -0400110 @echo "Running style check..."
Kent Hagerman6379e092020-02-18 18:05:57 -0500111 @gofmt_out="$$(${GOFMT} -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\
Don Newton98fd8812019-09-23 15:15:02 -0400112 if [ ! -z "$$gofmt_out" ]; then \
113 echo "$$gofmt_out" ;\
114 echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\
115 exit 1 ;\
116 fi
117 @echo "Style check OK"
118
119lint-sanity:
120 @echo "Running sanity check..."
Kent Hagerman6379e092020-02-18 18:05:57 -0500121 @${GO} vet -mod=vendor ./...
Don Newton98fd8812019-09-23 15:15:02 -0400122 @echo "Sanity check OK"
123
Don Newtone0d34a82019-11-14 10:58:06 -0500124lint-mod:
Don Newton98fd8812019-09-23 15:15:02 -0400125 @echo "Running dependency check..."
Kent Hagerman6379e092020-02-18 18:05:57 -0500126 @${GO} mod verify
127 @echo "Dependency check OK. Running vendor check..."
128 @git status > /dev/null
129 @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)
130 @[[ `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)
131 ${GO} mod tidy
132 ${GO} mod vendor
133 @git status > /dev/null
134 @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)
135 @[[ `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)
136 @echo "Vendor check OK."
Don Newton98fd8812019-09-23 15:15:02 -0400137
Kent Hagerman6379e092020-02-18 18:05:57 -0500138lint: lint-style lint-sanity lint-mod lint-dockerfile
Don Newton98fd8812019-09-23 15:15:02 -0400139
Don Newton98fd8812019-09-23 15:15:02 -0400140test:
Don Newton98fd8812019-09-23 15:15:02 -0400141 @mkdir -p ./tests/results
Kent Hagerman6379e092020-02-18 18:05:57 -0500142 @${GO} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
Don Newton98fd8812019-09-23 15:15:02 -0400143 RETURN=$$? ;\
Kent Hagerman6379e092020-02-18 18:05:57 -0500144 ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
145 ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
Don Newton98fd8812019-09-23 15:15:02 -0400146 exit $$RETURN
147
Don Newton98fd8812019-09-23 15:15:02 -0400148sca:
Kent Hagerman6379e092020-02-18 18:05:57 -0500149 @rm -rf ./sca-report
Don Newton98fd8812019-09-23 15:15:02 -0400150 @mkdir -p ./sca-report
Kent Hagerman6379e092020-02-18 18:05:57 -0500151 @echo "Running static code analysis..."
152 @${GOLANGCI_LINT} run --deadline=4m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml
153 @echo ""
154 @echo "Static code analysis OK"
Don Newton98fd8812019-09-23 15:15:02 -0400155
156clean:
Kent Hagerman6379e092020-02-18 18:05:57 -0500157 rm -rf ./sca-report
Don Newton98fd8812019-09-23 15:15:02 -0400158
159distclean: clean
160 rm -rf ${VENVDIR}
161
Kent Hagerman6379e092020-02-18 18:05:57 -0500162mod-update:
163 ${GO} mod tidy
164 ${GO} mod vendor