Joey Armstrong | aadcaa4 | 2024-02-01 19:54:17 -0500 | [diff] [blame^] | 1 | # Copyright 2020-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame] | 15 | $(if $(DEBUG),$(warning ENTER)) |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 16 | |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame] | 17 | .PHONY: help clean help test |
| 18 | .DEFAULT_GOAL := help |
| 19 | |
| 20 | ##-------------------## |
| 21 | ##---] GLOBALS [---## |
| 22 | ##-------------------## |
| 23 | TOP ?=$(strip \ |
| 24 | $(dir \ |
| 25 | $(abspath $(lastword $(MAKEFILE_LIST)))\ |
| 26 | )\ |
| 27 | ) |
| 28 | |
| 29 | ##--------------------## |
| 30 | ##---] INCLUDES [---## |
| 31 | ##--------------------## |
| 32 | include $(TOP)/config.mk# # configure |
| 33 | |
| 34 | # ----------------------------------------------------------------------- |
| 35 | # https://jira.opencord.org/browse/VOL-5163 |
| 36 | # - only a tiny subset of logic is enabled right now. |
| 37 | # - several targets can also be refactored across several repos |
| 38 | # into additional library targets. |
| 39 | # ----------------------------------------------------------------------- |
| 40 | include $(TOP)/makefiles/include.mk # top level include |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 41 | |
| 42 | # Variables |
| 43 | VERSION ?= $(shell cat ./VERSION) |
| 44 | |
| 45 | DOCKER_LABEL_VCS_DIRTY = false |
| 46 | ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0) |
| 47 | DOCKER_LABEL_VCS_DIRTY = true |
| 48 | endif |
| 49 | ## Docker related |
| 50 | DOCKER_EXTRA_ARGS ?= |
| 51 | DOCKER_REGISTRY ?= |
| 52 | DOCKER_REPOSITORY ?= |
| 53 | DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true) |
| 54 | IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}bbsim-sadis-server |
| 55 | TYPE ?= minimal |
| 56 | |
| 57 | ## Docker labels. Only set ref and commit date if committed |
| 58 | DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote)) |
| 59 | DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD) |
| 60 | DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ") |
| 61 | DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD) |
| 62 | |
| 63 | DOCKER_BUILD_ARGS ?= \ |
| 64 | ${DOCKER_EXTRA_ARGS} \ |
| 65 | --build-arg org_label_schema_version="${VERSION}" \ |
| 66 | --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \ |
| 67 | --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \ |
| 68 | --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \ |
| 69 | --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \ |
| 70 | --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}" |
| 71 | |
| 72 | DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \ |
| 73 | --build-arg LOCAL_PROTOS=${LOCAL_PROTOS} |
| 74 | |
| 75 | # tool containers |
David K. Bainbridge | 4242c72 | 2021-04-09 16:16:36 +0000 | [diff] [blame] | 76 | VOLTHA_TOOLS_VERSION ?= 2.4.0 |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 77 | |
| 78 | GO = 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 |
| 79 | 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 | abf872d | 2020-12-14 08:22:06 -1000 | [diff] [blame] | 80 | GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app/src/github.com/opencord/bbsim-sadis-server -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 81 | GOLANGCI_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 |
| 82 | HADOLINT = 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 |
| 83 | |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame] | 84 | help :: |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 85 | @echo "TODO write the help" |
| 86 | |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame] | 87 | .PHONY: build |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 88 | build: |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame] | 89 | $(call banner-enter,Target $@) |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 90 | @${GO} build -mod=vendor ./cmd/bbsim-sadis-server.go |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame] | 91 | $(call banner-leave,Target $@) |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 92 | |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame] | 93 | .PHONY: build-local |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 94 | build-local: |
| 95 | @go build -mod=vendor ./cmd/bbsim-sadis-server.go |
| 96 | |
| 97 | ## Docker targets |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame] | 98 | .PHONY: docker-build |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 99 | docker-build: |
| 100 | docker build $(DOCKER_BUILD_ARGS) -t ${IMAGENAME}:${DOCKER_TAG} -f build/package/Dockerfile . |
| 101 | |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame] | 102 | .PHONY: docker-push |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 103 | docker-push: |
| 104 | docker push ${IMAGENAME}:${DOCKER_TAG} |
| 105 | |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame] | 106 | .PHONY: docker-kind-load |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 107 | docker-kind-load: |
| 108 | @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi |
| 109 | kind load docker-image ${IMAGENAME}:${DOCKER_TAG} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | rev | cut -c 2- | rev) |
| 110 | |
| 111 | ## lint and unit tests |
| 112 | |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame] | 113 | .PHONY: lint-dockerfile |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 114 | lint-dockerfile: |
| 115 | @echo "Running Dockerfile lint check..." |
| 116 | @${HADOLINT} $$(find ./build -name "Dockerfile*") |
| 117 | @echo "Dockerfile lint check OK" |
| 118 | |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame] | 119 | ## ----------------------------------------------------------------------- |
| 120 | ## ----------------------------------------------------------------------- |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 121 | lint-mod: |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame] | 122 | $(call banner-entry,Target $@) |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 123 | @echo "Running dependency check..." |
| 124 | @${GO} mod verify |
| 125 | @echo "Dependency check OK. Running vendor check..." |
| 126 | @git status > /dev/null |
| 127 | @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Staged or modified files must be committed before running this test" && git status -- go.mod go.sum vendor && exit 1) |
| 128 | @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files must be cleaned up before running this test" && git status -- go.mod go.sum vendor && exit 1) |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame] | 129 | |
| 130 | $(HIDE)$(MAKE) --no-print-directory mod-update |
| 131 | |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 132 | @git status > /dev/null |
| 133 | @git diff-index --quiet HEAD -- go.mod go.sum vendor || (echo "ERROR: Modified files detected after running go mod tidy / go mod vendor" && git status -- go.mod go.sum vendor && git checkout -- go.mod go.sum vendor && exit 1) |
| 134 | @[[ `git ls-files --exclude-standard --others go.mod go.sum vendor` == "" ]] || (echo "ERROR: Untracked files detected after running go mod tidy / go mod vendor" && git status -- go.mod go.sum vendor && git checkout -- go.mod go.sum vendor && exit 1) |
| 135 | @echo "Vendor check OK." |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame] | 136 | $(call banner-leave,Target $@) |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 137 | |
| 138 | |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame] | 139 | ## ----------------------------------------------------------------------- |
| 140 | ## ----------------------------------------------------------------------- |
| 141 | .PHONY: mod-update |
Joey Armstrong | aadcaa4 | 2024-02-01 19:54:17 -0500 | [diff] [blame^] | 142 | mod-update: go-version mod-tidy mod-vendor |
| 143 | |
| 144 | ## ----------------------------------------------------------------------- |
| 145 | ## ----------------------------------------------------------------------- |
| 146 | .PHONY: go-version |
| 147 | go-version : |
| 148 | $(call banner-enter,Target $@) |
| 149 | ${GO} version |
| 150 | $(call banner-leave,Target $@) |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame] | 151 | |
| 152 | ## ----------------------------------------------------------------------- |
| 153 | ## ----------------------------------------------------------------------- |
| 154 | .PHONY: mod-tidy |
| 155 | mod-tidy : |
| 156 | $(call banner-enter,Target $@) |
| 157 | ${GO} mod tidy |
| 158 | $(call banner-leave,Target $@) |
| 159 | |
| 160 | ## ----------------------------------------------------------------------- |
| 161 | ## ----------------------------------------------------------------------- |
| 162 | .PHONY: mod-vendor |
| 163 | mod-vendor : mod-tidy |
| 164 | mod-vendor : |
| 165 | $(call banner-enter,Target $@) |
| 166 | $(if $(LOCAL_FIX_PERMS),chmod o+w $(CURDIR)) |
| 167 | ${GO} mod vendor |
| 168 | $(if $(LOCAL_FIX_PERMS),chmod o-w $(CURDIR)) |
| 169 | $(call banner-leave,Target $@) |
| 170 | |
| 171 | ## ----------------------------------------------------------------------- |
| 172 | ## ----------------------------------------------------------------------- |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 173 | lint: lint-mod lint-dockerfile |
| 174 | |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame] | 175 | ## ----------------------------------------------------------------------- |
| 176 | ## Coverage report: Static code analysis |
| 177 | ## ----------------------------------------------------------------------- |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 178 | sca: |
| 179 | @rm -rf ./sca-report |
| 180 | @mkdir -p ./sca-report |
| 181 | @echo "Running static code analysis..." |
| 182 | @${GOLANGCI_LINT} run --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml |
| 183 | @echo "" |
| 184 | @echo "Static code analysis OK" |
| 185 | |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame] | 186 | ## ----------------------------------------------------------------------- |
| 187 | ## ----------------------------------------------------------------------- |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 188 | test: |
| 189 | @mkdir -p ./tests/results |
| 190 | @${GO} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\ |
| 191 | RETURN=$$? ;\ |
| 192 | ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\ |
| 193 | ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\ |
| 194 | exit $$RETURN |
| 195 | |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame] | 196 | ## ----------------------------------------------------------------------- |
| 197 | ## ----------------------------------------------------------------------- |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 198 | distclean: |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame] | 199 | $(RM) -r ./sca-report |
Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 200 | |
Joey Armstrong | ee4d826 | 2023-08-22 15:19:19 -0400 | [diff] [blame] | 201 | # [EOF] |