Joey Armstrong | 7f9c4df | 2022-12-26 19:39:59 -0500 | [diff] [blame] | 1 | # -*- makefile -*- |
| 2 | # ----------------------------------------------------------------------- |
| 3 | # Copyright 2016-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
Joey Armstrong | 7f9c4df | 2022-12-26 19:39:59 -0500 | [diff] [blame] | 16 | # ----------------------------------------------------------------------- |
| 17 | |
| 18 | $(if $(DEBUG),$(warning ENTER)) |
| 19 | |
| 20 | .DEFAULT_GOAL := help |
| 21 | |
| 22 | TOP ?= . |
| 23 | MAKEDIR ?= $(TOP)/makefiles |
| 24 | |
| 25 | $(if $(VERBOSE),$(eval export VERBOSE=$(VERBOSE))) # visible to include(s) |
| 26 | |
| 27 | ##--------------------## |
| 28 | ##---] INCLUDES [---## |
| 29 | ##--------------------## |
| 30 | include $(MAKEDIR)/include.mk |
| 31 | ifdef LOCAL_LINT |
| 32 | include $(MAKEDIR)/lint/golang/sca.mk |
| 33 | endif |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 34 | |
| 35 | # set default shell |
| 36 | SHELL = bash -e -o pipefail |
| 37 | |
| 38 | # Variables |
| 39 | VERSION ?= $(shell cat ./VERSION) |
| 40 | |
| 41 | DOCKER_LABEL_VCS_DIRTY = false |
| 42 | ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0) |
| 43 | DOCKER_LABEL_VCS_DIRTY = true |
| 44 | endif |
Joey Armstrong | 7f9c4df | 2022-12-26 19:39:59 -0500 | [diff] [blame] | 45 | |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 46 | ## Docker related |
| 47 | DOCKER_EXTRA_ARGS ?= |
| 48 | DOCKER_REGISTRY ?= |
| 49 | DOCKER_REPOSITORY ?= |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 50 | DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true) |
| 51 | DOCKER_TARGET ?= prod |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 52 | ADAPTER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-openonu-adapter-go:${DOCKER_TAG} |
| 53 | TYPE ?= minimal |
| 54 | |
| 55 | ## Docker labels. Only set ref and commit date if committed |
| 56 | DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote)) |
| 57 | DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD) |
| 58 | DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ") |
| 59 | DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD) |
| 60 | |
| 61 | DOCKER_BUILD_ARGS ?= \ |
| 62 | ${DOCKER_EXTRA_ARGS} \ |
| 63 | --build-arg org_label_schema_version="${VERSION}" \ |
| 64 | --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \ |
| 65 | --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \ |
| 66 | --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \ |
| 67 | --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \ |
| 68 | --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}" |
| 69 | |
| 70 | # tool containers |
David K. Bainbridge | d80007b | 2021-04-12 12:22:29 +0000 | [diff] [blame] | 71 | VOLTHA_TOOLS_VERSION ?= 2.4.0 |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 72 | |
| 73 | GO = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang go |
| 74 | GO_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 |
Matteo Scandolo | 761f751 | 2020-11-23 15:52:40 -0800 | [diff] [blame] | 75 | GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app/src/github.com/opencord/voltha-openonu-adapter-go -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 76 | GOFMT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang gofmt |
| 77 | GOLANGCI_LINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golangci-lint golangci-lint |
| 78 | HADOLINT = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-hadolint hadolint |
| 79 | |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 80 | .PHONY: docker-build local-protos local-lib-go help |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 81 | |
| 82 | ## Local Development Helpers |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 83 | local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 84 | ifdef LOCAL_PROTOS |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 85 | rm -rf vendor/github.com/opencord/voltha-protos/v5/go |
| 86 | mkdir -p vendor/github.com/opencord/voltha-protos/v5/go |
| 87 | cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v5/go |
| 88 | rm -rf vendor/github.com/opencord/voltha-protos/v5/go/vendor |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 89 | endif |
| 90 | |
| 91 | ## Local Development Helpers |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 92 | local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 93 | ifdef LOCAL_LIB_GO |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 94 | rm -rf vendor/github.com/opencord/voltha-lib-go/v7/pkg |
| 95 | mkdir -p vendor/github.com/opencord/voltha-lib-go/v7/pkg |
| 96 | cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v7/pkg/ |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 97 | endif |
| 98 | |
Joey Armstrong | 7f9c4df | 2022-12-26 19:39:59 -0500 | [diff] [blame] | 99 | ## ----------------------------------------------------------------------- |
| 100 | ## ----------------------------------------------------------------------- |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 101 | build: docker-build ## Alias for 'docker build' |
| 102 | |
Joey Armstrong | 7f9c4df | 2022-12-26 19:39:59 -0500 | [diff] [blame] | 103 | ## ----------------------------------------------------------------------- |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 104 | ## Docker targets |
Joey Armstrong | 7f9c4df | 2022-12-26 19:39:59 -0500 | [diff] [blame] | 105 | ## ----------------------------------------------------------------------- |
Holger Hildebrandt | 6999fe5 | 2021-02-11 11:49:05 +0000 | [diff] [blame] | 106 | docker-build: local-protos local-lib-go ## Build openonu adapter docker image (set BUILD_PROFILED=true to also build the profiled image) |
David K. Bainbridge | 740ca85 | 2021-04-15 00:23:35 +0000 | [diff] [blame] | 107 | docker build $(DOCKER_BUILD_ARGS) --target=${DOCKER_TARGET} --build-arg CGO_PARAMETER=0 -t ${ADAPTER_IMAGENAME} -f docker/Dockerfile.openonu . |
Holger Hildebrandt | 6999fe5 | 2021-02-11 11:49:05 +0000 | [diff] [blame] | 108 | ifdef BUILD_PROFILED |
David K. Bainbridge | 740ca85 | 2021-04-15 00:23:35 +0000 | [diff] [blame] | 109 | docker build $(DOCKER_BUILD_ARGS) --target=dev --build-arg CGO_PARAMETER=1 --build-arg EXTRA_GO_BUILD_TAGS="-tags profile" -t ${ADAPTER_IMAGENAME}-profile -f docker/Dockerfile.openonu . |
| 110 | endif |
| 111 | ifdef BUILD_RACE |
| 112 | docker build $(DOCKER_BUILD_ARGS) --target=dev --build-arg CGO_PARAMETER=1 --build-arg EXTRA_GO_BUILD_TAGS="-race" -t ${ADAPTER_IMAGENAME}-rd -f docker/Dockerfile.openonu . |
Holger Hildebrandt | 6999fe5 | 2021-02-11 11:49:05 +0000 | [diff] [blame] | 113 | endif |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 114 | |
Joey Armstrong | 7f9c4df | 2022-12-26 19:39:59 -0500 | [diff] [blame] | 115 | ## ----------------------------------------------------------------------- |
| 116 | ## ----------------------------------------------------------------------- |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 117 | docker-push: ## Push the docker images to an external repository |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 118 | docker push ${ADAPTER_IMAGENAME} |
Holger Hildebrandt | 6999fe5 | 2021-02-11 11:49:05 +0000 | [diff] [blame] | 119 | ifdef BUILD_PROFILED |
| 120 | docker push ${ADAPTER_IMAGENAME}-profile |
| 121 | endif |
David K. Bainbridge | 740ca85 | 2021-04-15 00:23:35 +0000 | [diff] [blame] | 122 | ifdef BUILD_RACE |
| 123 | docker push ${ADAPTER_IMAGENAME}-rd |
| 124 | endif |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 125 | |
Joey Armstrong | 7f9c4df | 2022-12-26 19:39:59 -0500 | [diff] [blame] | 126 | ## ----------------------------------------------------------------------- |
| 127 | ## ----------------------------------------------------------------------- |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 128 | docker-kind-load: ## Load docker images into a KinD cluster |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 129 | @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi |
| 130 | kind load docker-image ${ADAPTER_IMAGENAME} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | sed 's/,$$//') |
| 131 | |
Joey Armstrong | 7f9c4df | 2022-12-26 19:39:59 -0500 | [diff] [blame] | 132 | ## ----------------------------------------------------------------------- |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 133 | ## lint and unit tests |
Joey Armstrong | 7f9c4df | 2022-12-26 19:39:59 -0500 | [diff] [blame] | 134 | ## ----------------------------------------------------------------------- |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 135 | lint-dockerfile: ## Perform static analysis on Dockerfile |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 136 | @echo "Running Dockerfile lint check ..." |
| 137 | @${HADOLINT} $$(find . -name "Dockerfile.*") |
| 138 | @echo "Dockerfile lint check OK" |
| 139 | |
Joey Armstrong | 7f9c4df | 2022-12-26 19:39:59 -0500 | [diff] [blame] | 140 | ## ----------------------------------------------------------------------- |
| 141 | ## ----------------------------------------------------------------------- |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 142 | lint-style: ## Perform lint style checks on source code |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 143 | @echo "Running style check..." |
| 144 | @gofmt_out="$$(${GOFMT} -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\ |
| 145 | if [ ! -z "$$gofmt_out" ]; then \ |
| 146 | echo "$$gofmt_out" ;\ |
| 147 | echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\ |
| 148 | exit 1 ;\ |
| 149 | fi |
| 150 | @echo "Style check OK" |
| 151 | |
Joey Armstrong | 7f9c4df | 2022-12-26 19:39:59 -0500 | [diff] [blame] | 152 | ## ----------------------------------------------------------------------- |
| 153 | ## ----------------------------------------------------------------------- |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 154 | lint-sanity: ## Perform basic code checks on source |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 155 | @echo "Running sanity check..." |
| 156 | @${GO} vet -mod=vendor ./... |
| 157 | @echo "Sanity check OK" |
| 158 | |
Joey Armstrong | 7f9c4df | 2022-12-26 19:39:59 -0500 | [diff] [blame] | 159 | ## ----------------------------------------------------------------------- |
| 160 | ## ----------------------------------------------------------------------- |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 161 | lint-mod: ## Verify the Go dependencies |
Joey Armstrong | 7f9c4df | 2022-12-26 19:39:59 -0500 | [diff] [blame] | 162 | $(HIDE)echo "Running dependency check..." |
| 163 | $(HIDE)$(GO) mod verify |
| 164 | $(HIDE)echo "Dependency check OK. Running vendor check..." |
| 165 | $(HIDE)git status > /dev/null |
| 166 | $(HIDE)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) |
| 167 | |
| 168 | $(MAKE) --no-print-directory detect-local-edits |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 169 | ${GO} mod tidy |
| 170 | ${GO} mod vendor |
Joey Armstrong | 7f9c4df | 2022-12-26 19:39:59 -0500 | [diff] [blame] | 171 | $(HIDE)git status > /dev/null |
| 172 | $(HIDE)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) |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 173 | |
Joey Armstrong | 7f9c4df | 2022-12-26 19:39:59 -0500 | [diff] [blame] | 174 | $(MAKE) --no-print-directory detect-local-edits |
| 175 | $(HIDE)echo "Vendor check OK." |
| 176 | |
| 177 | ## --------------------------------------------------------------------- |
| 178 | ## Intent: Determine if sandbox contains locally modified files |
| 179 | ## --------------------------------------------------------------------- |
| 180 | clean-tree := git status --porcelain |
| 181 | detect-local-edits: |
| 182 | $(HIDE)[[ `$(clean-tree)` == "" ]] || (echo "ERROR: Untracked files detected, commit or remove to continue" && echo "`git status`" && exit 1) |
| 183 | |
| 184 | ## ----------------------------------------------------------------------- |
| 185 | ## ----------------------------------------------------------------------- |
Joey Armstrong | e8c091f | 2023-01-17 16:56:26 -0500 | [diff] [blame] | 186 | lint: local-lib-go lint-style lint-sanity lint-mod lint-dockerfile |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 187 | |
Joey Armstrong | 7f9c4df | 2022-12-26 19:39:59 -0500 | [diff] [blame] | 188 | ## ----------------------------------------------------------------------- |
| 189 | ## ----------------------------------------------------------------------- |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 190 | test: lint ## Run unit tests |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 191 | @mkdir -p ./tests/results |
| 192 | @${GO} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\ |
| 193 | RETURN=$$? ;\ |
| 194 | ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\ |
| 195 | ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\ |
| 196 | exit $$RETURN |
| 197 | |
Joey Armstrong | 7f9c4df | 2022-12-26 19:39:59 -0500 | [diff] [blame] | 198 | ## ----------------------------------------------------------------------- |
| 199 | ## ----------------------------------------------------------------------- |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 200 | sca: ## Runs static code analysis with the golangci-lint tool |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 201 | @mkdir -p ./sca-report |
| 202 | @echo "Running static code analysis..." |
ozgecanetsia | b644196 | 2021-03-10 10:58:48 +0300 | [diff] [blame] | 203 | @${GOLANGCI_LINT} run --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 204 | @echo "Static code analysis OK" |
| 205 | |
Joey Armstrong | 7f9c4df | 2022-12-26 19:39:59 -0500 | [diff] [blame] | 206 | ## ----------------------------------------------------------------------- |
| 207 | ## ----------------------------------------------------------------------- |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 208 | clean: distclean ## Removes any local filesystem artifacts generated by a build |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 209 | |
Joey Armstrong | 7f9c4df | 2022-12-26 19:39:59 -0500 | [diff] [blame] | 210 | ## ----------------------------------------------------------------------- |
| 211 | ## ----------------------------------------------------------------------- |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 212 | distclean: ## Removes any local filesystem artifacts generated by a build or test run |
Joey Armstrong | 7f9c4df | 2022-12-26 19:39:59 -0500 | [diff] [blame] | 213 | $(RM) -r ./sca-report |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 214 | |
Joey Armstrong | 7f9c4df | 2022-12-26 19:39:59 -0500 | [diff] [blame] | 215 | ## ----------------------------------------------------------------------- |
| 216 | ## ----------------------------------------------------------------------- |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 217 | mod-update: ## Update go mod files |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 218 | ${GO} mod tidy |
| 219 | ${GO} mod vendor |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 220 | |
| 221 | # For each makefile target, add ## <description> on the target line and it will be listed by 'make help' |
Joey Armstrong | 7f9c4df | 2022-12-26 19:39:59 -0500 | [diff] [blame] | 222 | help :: ## Print help for each Makefile target |
David K. Bainbridge | ee01bc4 | 2021-04-06 17:38:20 +0000 | [diff] [blame] | 223 | @echo |
Joey Armstrong | 7f9c4df | 2022-12-26 19:39:59 -0500 | [diff] [blame] | 224 | @grep --no-filename '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \ |
| 225 | | sort \ |
| 226 | | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};' |
| 227 | |
| 228 | # [EOF] |