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 | DOCKER_NAME := voltha-go-controller |
| 27 | DOCKER_TAG := latest |
| 28 | GOLINT := golint |
| 29 | 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 |
| 30 | GOBUILD=$(GOCMD) build |
| 31 | GOCLEAN=$(GOCMD) clean |
| 32 | GOTEST=$(GOCMD) test |
| 33 | GOTOOL=$(GOCMD) tool |
| 34 | |
| 35 | # Static code analyzers |
| 36 | GOIMPORTS := goimports |
| 37 | GO_FILES := `find . -path vendor -prune -o -name '*.go'` |
| 38 | STATIC_TOOLS := $(GOLINT) $(GOFMT) $(GOIMPORTS) |
| 39 | #STATIC_TOOLS := $(GOFMT) |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 40 | |
| 41 | # set default shell |
| 42 | SHELL = bash -e -o pipefail |
| 43 | |
| 44 | # Variables |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 45 | VERSION ?= $(shell head -n 1 ./VERSION) |
| 46 | # DOCKER_TAG ?= ${VERSION} |
| 47 | IMAGENAME ?= ${DOCKER_NAME}:${DOCKER_TAG} |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 48 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 49 | COVERAGE_DIR = ./tests/results |
| 50 | COVERAGE_PROFILE = $(COVERAGE_DIR)/profile.out |
| 51 | TEST_TARGETS := test-default test-verbose test-short |
| 52 | test-short: ARGS=-short |
| 53 | test-verbose: ARGS=-v |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 54 | # tool containers |
| 55 | VOLTHA_TOOLS_VERSION ?= 2.4.0 |
| 56 | |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 57 | 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] | 58 | 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 |
| 59 | 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 |
| 60 | 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 |
| 61 | |
| 62 | ## Local Development Helpers |
| 63 | local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory |
| 64 | ifdef LOCAL_PROTOS |
| 65 | rm -rf vendor/github.com/opencord/voltha-protos/v5/go |
| 66 | mkdir -p vendor/github.com/opencord/voltha-protos/v5/go |
| 67 | cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v5/go |
| 68 | rm -rf vendor/github.com/opencord/voltha-protos/v5/go/vendor |
| 69 | endif |
| 70 | |
| 71 | local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory |
| 72 | ifdef LOCAL_LIB_GO |
| 73 | mkdir -p vendor/github.com/opencord/voltha-lib-go/v7/pkg |
| 74 | cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v7/pkg/ |
| 75 | endif |
| 76 | |
| 77 | # This should to be the first and default target in this Makefile |
Joey Armstrong | ec895f8 | 2024-04-25 14:26:07 -0400 | [diff] [blame^] | 78 | help :: ## Print help for each Makefile target |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 79 | @echo "Usage: make [<target>]" |
| 80 | @echo "where available targets are:" |
| 81 | @echo |
| 82 | @grep '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \ |
| 83 | | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};' |
| 84 | |
| 85 | |
| 86 | exe: |
| 87 | @echo Building voltha-go-controller.... |
| 88 | @cd voltha-go-controller && go build |
| 89 | |
| 90 | vgcctl: |
| 91 | @echo Building vgcctl.... |
| 92 | @cd voltha-go-controller/cli && go build -o vgcctl |
| 93 | |
| 94 | docker: exe vgcctl |
| 95 | @echo Building Docker $(DOCKER_NAME).... |
| 96 | sudo docker build -t $(IMAGENAME) -f docker/Dockerfile.voltha-go-controller . |
| 97 | |
| 98 | ## Docker targets |
| 99 | build: local-protos local-lib-go docker ## Build voltha-go-controller image |
| 100 | |
| 101 | build-docker-profile: sca exe-profile vgcctl |
| 102 | @echo Building Docker $(DOCKER_NAME).... |
| 103 | sudo docker build -t $(IMAGENAME)-profile -f docker/Dockerfile.voltha-go-controller . |
| 104 | |
| 105 | sca: ## Runs static code analysis with the golangci-lint tool |
Tinoj Joseph | c2ccd6b | 2022-07-19 04:32:15 +0530 | [diff] [blame] | 106 | @rm -rf ./sca-report |
| 107 | @mkdir -p ./sca-report |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 108 | @echo "Running static code analysis..." |
Tinoj Joseph | c2ccd6b | 2022-07-19 04:32:15 +0530 | [diff] [blame] | 109 | @${GOLANGCI_LINT} run -vv --deadline=6m --out-format junit-xml ./... | tee ./sca-report/sca-report.xml |
| 110 | @echo "" |
| 111 | @echo "Static code analysis OK" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 112 | |
Joey Armstrong | ec895f8 | 2024-04-25 14:26:07 -0400 | [diff] [blame^] | 113 | clean :: ## Removes any local filesystem artifacts generated by a build |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 114 | rm -f voltha-go-controller/voltha-go-controller |
| 115 | rm -f voltha-go-controller/cli/vgcctl |
| 116 | |
| 117 | mock-gen: |
| 118 | mockery -dir intf/ -name DatabaseIntf -structname MockDb -filename MockDatabase.go |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 119 | |
| 120 | ## lint and unit tests |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 121 | lint-dockerfile: ## Perform static analysis on Dockerfile |
| 122 | @echo "Running Dockerfile lint check ..." |
| 123 | @${HADOLINT} $$(find . -name "Dockerfile.*") |
| 124 | @echo "Dockerfile lint check OK" |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 125 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 126 | lint-mod: ## Verify the Go dependencies |
| 127 | @echo "Running dependency check..." |
| 128 | @${GOCMD} mod verify |
| 129 | @echo "Dependency check OK. Running vendor check..." |
| 130 | @git status > /dev/null |
| 131 | @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) |
| 132 | @[[ `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) |
| 133 | ${GOCMD} mod tidy |
| 134 | ${GOCMD} mod vendor |
| 135 | @git status > /dev/null |
| 136 | @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) |
| 137 | @[[ `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) |
| 138 | @echo "Vendor check OK." |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 139 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 140 | lint: local-lib-go lint-mod lint-dockerfile ## Run all lint targets |
Matteo Scandolo | 9061e43 | 2022-05-10 13:56:29 -0700 | [diff] [blame] | 141 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 142 | mod-update: ## Update go mod files |
| 143 | ${GOCMD} mod tidy |
| 144 | ${GOCMD} mod vendor |
| 145 | |
| 146 | test: ## Run unit tests |
| 147 | @mkdir -p ./tests/results |
| 148 | @${GOCMD} test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\ |
| 149 | RETURN=$$? ;\ |
| 150 | ${GO_JUNIT_REPORT} < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\ |
| 151 | ${GOCOVER_COBERTURA} < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\ |
| 152 | exit $$RETURN |
Joey Armstrong | ec895f8 | 2024-04-25 14:26:07 -0400 | [diff] [blame^] | 153 | |
| 154 | ## ----------------------------------------------------------------------- |
| 155 | ## ----------------------------------------------------------------------- |
| 156 | pre-commit : .venv |
| 157 | source .venv/bin/activate && pre-commit |
| 158 | |
| 159 | ## ----------------------------------------------------------------------- |
| 160 | ## [TODO] Replace inlined target with repo:onf-make/makefiles/virtualenv/ |
| 161 | ## ----------------------------------------------------------------------- |
| 162 | venv : .venv |
| 163 | .venv : |
| 164 | virtualenv -p python3 $@ |
| 165 | $@/bin/pip install -r requirements.txt |
| 166 | |
| 167 | ## ----------------------------------------------------------------------- |
| 168 | ## ----------------------------------------------------------------------- |
| 169 | help :: |
| 170 | @printf ' %-33.33s %s\n' 'pre-commit' \ |
| 171 | 'Invoke the pre-commit hook linting tool' |
| 172 | |
| 173 | clean :: |
| 174 | $(RM) -r .venv |
| 175 | |
| 176 | # [EOF] |