Joey Armstrong | ec895f8 | 2024-04-25 14:26:07 -0400 | [diff] [blame] | 1 | # -*- makefile -*- |
| 2 | # ----------------------------------------------------------------------- |
| 3 | # Copyright 2022-2024 Open Networking Foundation Contributors |
| 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. |
| 16 | # ----------------------------------------------------------------------- |
| 17 | # SPDX-FileCopyrightText: 2022-2024 Open Networking Foundation Contributors |
| 18 | # SPDX-License-Identifier: Apache-2.0 |
| 19 | # ----------------------------------------------------------------------- |
| 20 | # Intent: Build, test and release voltha-go-controller |
| 21 | # ----------------------------------------------------------------------- |
| 22 | # Todo: Refactor common makefile logic and targets shared by |
| 23 | # several VOLTHA repositories. |
| 24 | # ----------------------------------------------------------------------- |
| 25 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 26 | GOLINT := golint |
| 27 | GOCMD = 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 |
| 28 | GOBUILD=$(GOCMD) build |
| 29 | GOCLEAN=$(GOCMD) clean |
| 30 | GOTEST=$(GOCMD) test |
| 31 | GOTOOL=$(GOCMD) tool |
| 32 | |
| 33 | # Static code analyzers |
| 34 | GOIMPORTS := goimports |
| 35 | GO_FILES := `find . -path vendor -prune -o -name '*.go'` |
| 36 | STATIC_TOOLS := $(GOLINT) $(GOFMT) $(GOIMPORTS) |
| 37 | #STATIC_TOOLS := $(GOFMT) |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 38 | |
| 39 | # set default shell |
| 40 | SHELL = bash -e -o pipefail |
| 41 | |
| 42 | # Variables |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 43 | VERSION ?= $(shell head -n 1 ./VERSION) |
abhay | 9690fd7 | 2025-01-07 14:22:03 +0530 | [diff] [blame^] | 44 | ## Docker related |
| 45 | DOCKER_NAME := voltha-go-controller |
| 46 | DOCKER_LABEL_VCS_DIRTY = false |
| 47 | ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0) |
| 48 | DOCKER_LABEL_VCS_DIRTY = true |
| 49 | endif |
| 50 | DOCKER_EXTRA_ARGS ?= |
| 51 | DOCKER_REGISTRY ?= |
| 52 | DOCKER_REPOSITORY ?= voltha/ |
| 53 | DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true) |
| 54 | IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-go-controller:${DOCKER_TAG} |
| 55 | DOCKER_TARGET ?= prod |
| 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 | DOCKER_BUILD_ARGS ?= \ |
| 63 | ${DOCKER_EXTRA_ARGS} \ |
| 64 | --build-arg org_label_schema_version="${VERSION}" \ |
| 65 | --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \ |
| 66 | --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \ |
| 67 | --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \ |
| 68 | --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \ |
| 69 | --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}" |
| 70 | |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 71 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 72 | COVERAGE_DIR = ./tests/results |
| 73 | COVERAGE_PROFILE = $(COVERAGE_DIR)/profile.out |
| 74 | TEST_TARGETS := test-default test-verbose test-short |
| 75 | test-short: ARGS=-short |
| 76 | test-verbose: ARGS=-v |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 77 | # tool containers |
| 78 | VOLTHA_TOOLS_VERSION ?= 2.4.0 |
| 79 | |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 80 | 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 |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [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 | 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 |
| 83 | GOCOVER_COBERTURA = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app/src/github.com/opencord/voltha-openolt-adapter -i voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-gocover-cobertura gocover-cobertura |
| 84 | |
| 85 | ## Local Development Helpers |
| 86 | local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory |
| 87 | ifdef LOCAL_PROTOS |
| 88 | rm -rf vendor/github.com/opencord/voltha-protos/v5/go |
| 89 | mkdir -p vendor/github.com/opencord/voltha-protos/v5/go |
| 90 | cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v5/go |
| 91 | rm -rf vendor/github.com/opencord/voltha-protos/v5/go/vendor |
| 92 | endif |
| 93 | |
| 94 | local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory |
| 95 | ifdef LOCAL_LIB_GO |
| 96 | mkdir -p vendor/github.com/opencord/voltha-lib-go/v7/pkg |
| 97 | cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v7/pkg/ |
| 98 | endif |
| 99 | |
| 100 | # This should to be the first and default target in this Makefile |
Joey Armstrong | ec895f8 | 2024-04-25 14:26:07 -0400 | [diff] [blame] | 101 | help :: ## Print help for each Makefile target |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 102 | @echo "Usage: make [<target>]" |
| 103 | @echo "where available targets are:" |
| 104 | @echo |
| 105 | @grep '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \ |
| 106 | | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};' |
| 107 | |
| 108 | |
| 109 | exe: |
| 110 | @echo Building voltha-go-controller.... |
| 111 | @cd voltha-go-controller && go build |
| 112 | |
| 113 | vgcctl: |
| 114 | @echo Building vgcctl.... |
| 115 | @cd voltha-go-controller/cli && go build -o vgcctl |
| 116 | |
abhay | 55a8b26 | 2025-01-06 18:22:24 +0530 | [diff] [blame] | 117 | docker-build: |
| 118 | @echo Building Docker $(DOCKER_NAME).... |
abhay | 9690fd7 | 2025-01-07 14:22:03 +0530 | [diff] [blame^] | 119 | docker build $(DOCKER_BUILD_ARGS) --target=${DOCKER_TARGET} -t $(IMAGENAME) -f docker/Dockerfile.voltha-go-controller . |
| 120 | ifdef BUILD_PROFILED |
| 121 | docker build $(DOCKER_BUILD_ARGS) --target=dev --build-arg EXTRA_GO_BUILD_TAGS="-tags profile" -t ${IMAGENAME}-profile -f docker/Dockerfile.voltha-go-controller . |
| 122 | endif |
| 123 | ifdef BUILD_RACE |
| 124 | docker build $(DOCKER_BUILD_ARGS) --target=dev --build-arg EXTRA_GO_BUILD_TAGS="-race" -t ${IMAGENAME}-rd -f docker/Dockerfile.voltha-go-controller . |
| 125 | endif |
abhay | 41e845e | 2025-01-06 15:54:18 +0530 | [diff] [blame] | 126 | |
abhay | 83672ba | 2025-01-06 19:06:13 +0530 | [diff] [blame] | 127 | docker-push: ## Push the docker images to an external repository |
| 128 | docker push ${IMAGENAME} |
| 129 | ifdef BUILD_PROFILED |
| 130 | docker push ${IMAGENAME}-profile |
| 131 | endif |
| 132 | ifdef BUILD_RACE |
| 133 | docker push ${IMAGENAME}-rd |
| 134 | endif |
| 135 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 136 | docker: exe vgcctl |
| 137 | @echo Building Docker $(DOCKER_NAME).... |
| 138 | sudo docker build -t $(IMAGENAME) -f docker/Dockerfile.voltha-go-controller . |
| 139 | |
| 140 | ## Docker targets |
| 141 | build: local-protos local-lib-go docker ## Build voltha-go-controller image |
| 142 | |
| 143 | build-docker-profile: sca exe-profile vgcctl |
| 144 | @echo Building Docker $(DOCKER_NAME).... |
| 145 | sudo docker build -t $(IMAGENAME)-profile -f docker/Dockerfile.voltha-go-controller . |
| 146 | |
| 147 | sca: ## Runs static code analysis with the golangci-lint tool |
Tinoj Joseph | c2ccd6b | 2022-07-19 04:32:15 +0530 | [diff] [blame] | 148 | @rm -rf ./sca-report |
| 149 | @mkdir -p ./sca-report |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 150 | @echo "Running static code analysis..." |
Tinoj Joseph | c2ccd6b | 2022-07-19 04:32:15 +0530 | [diff] [blame] | 151 | @${GOLANGCI_LINT} run -vv --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml |
| 152 | @echo "" |
| 153 | @echo "Static code analysis OK" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 154 | |
Joey Armstrong | ec895f8 | 2024-04-25 14:26:07 -0400 | [diff] [blame] | 155 | clean :: ## Removes any local filesystem artifacts generated by a build |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 156 | rm -f voltha-go-controller/voltha-go-controller |
| 157 | rm -f voltha-go-controller/cli/vgcctl |
| 158 | |
| 159 | mock-gen: |
| 160 | mockery -dir intf/ -name DatabaseIntf -structname MockDb -filename MockDatabase.go |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 161 | |
| 162 | ## lint and unit tests |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 163 | lint-dockerfile: ## Perform static analysis on Dockerfile |
| 164 | @echo "Running Dockerfile lint check ..." |
| 165 | @${HADOLINT} $$(find . -name "Dockerfile.*") |
| 166 | @echo "Dockerfile lint check OK" |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 167 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 168 | lint-mod: ## Verify the Go dependencies |
| 169 | @echo "Running dependency check..." |
| 170 | @${GOCMD} mod verify |
| 171 | @echo "Dependency check OK. Running vendor check..." |
| 172 | @git status > /dev/null |
| 173 | @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) |
| 174 | @[[ `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) |
| 175 | ${GOCMD} mod tidy |
| 176 | ${GOCMD} mod vendor |
| 177 | @git status > /dev/null |
| 178 | @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) |
| 179 | @[[ `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) |
| 180 | @echo "Vendor check OK." |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 181 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 182 | lint: local-lib-go lint-mod lint-dockerfile ## Run all lint targets |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 183 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 184 | mod-update: ## Update go mod files |
| 185 | ${GOCMD} mod tidy |
| 186 | ${GOCMD} mod vendor |
| 187 | |
| 188 | test: ## Run unit tests |
| 189 | @mkdir -p ./tests/results |
| 190 | @${GOCMD} 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 |
Joey Armstrong | ec895f8 | 2024-04-25 14:26:07 -0400 | [diff] [blame] | 195 | |
| 196 | ## ----------------------------------------------------------------------- |
| 197 | ## ----------------------------------------------------------------------- |
| 198 | pre-commit : .venv |
| 199 | source .venv/bin/activate && pre-commit |
| 200 | |
| 201 | ## ----------------------------------------------------------------------- |
| 202 | ## [TODO] Replace inlined target with repo:onf-make/makefiles/virtualenv/ |
| 203 | ## ----------------------------------------------------------------------- |
| 204 | venv : .venv |
| 205 | .venv : |
| 206 | virtualenv -p python3 $@ |
| 207 | $@/bin/pip install -r requirements.txt |
| 208 | |
| 209 | ## ----------------------------------------------------------------------- |
| 210 | ## ----------------------------------------------------------------------- |
| 211 | help :: |
| 212 | @printf ' %-33.33s %s\n' 'pre-commit' \ |
| 213 | 'Invoke the pre-commit hook linting tool' |
| 214 | |
| 215 | clean :: |
| 216 | $(RM) -r .venv |
| 217 | |
| 218 | # [EOF] |