Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 1 | # |
| 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 |
| 18 | SHELL = bash -e -o pipefail |
| 19 | |
| 20 | # Variables |
| 21 | VERSION ?= $(shell cat ./VERSION) |
| 22 | |
| 23 | DOCKER_LABEL_VCS_DIRTY = false |
| 24 | ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0) |
| 25 | DOCKER_LABEL_VCS_DIRTY = true |
| 26 | endif |
| 27 | ## Docker related |
| 28 | DOCKER_EXTRA_ARGS ?= |
| 29 | DOCKER_REGISTRY ?= |
| 30 | DOCKER_REPOSITORY ?= |
| 31 | DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true) |
| 32 | AFROUTER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-afrouter |
| 33 | AFROUTERD_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-afrouterd |
| 34 | |
| 35 | ## Docker labels. Only set ref and commit date if committed |
| 36 | DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote)) |
| 37 | DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD) |
| 38 | DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ") |
| 39 | DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD) |
| 40 | |
| 41 | DOCKER_BUILD_ARGS ?= \ |
| 42 | ${DOCKER_EXTRA_ARGS} \ |
| 43 | --build-arg org_label_schema_version="${VERSION}" \ |
| 44 | --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \ |
| 45 | --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \ |
| 46 | --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \ |
| 47 | --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \ |
| 48 | --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}" |
| 49 | |
| 50 | DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \ |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 51 | --build-arg LOCAL_PROTOS=${LOCAL_PROTOS} |
| 52 | |
Scott Baker | b3a288a | 2019-10-02 14:57:29 -0700 | [diff] [blame] | 53 | # Default is GO111MODULE=auto, which will refuse to use go mod if running |
| 54 | # go less than 1.13.0 and this repository is checked out in GOPATH. For now, |
| 55 | # force module usage. This affects commands executed from this Makefile, but |
| 56 | # not the environment inside the Docker build (which does not build from |
| 57 | # inside a GOPATH). |
| 58 | export GO111MODULE=on |
| 59 | |
Scott Baker | 18123e5 | 2019-10-25 15:13:08 -0700 | [diff] [blame] | 60 | .PHONY: rw_core ro_core afrouter afrouterd local-protos local-lib-go |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 61 | |
| 62 | # This should to be the first and default target in this Makefile |
| 63 | help: |
| 64 | @echo "Usage: make [<target>]" |
| 65 | @echo "where available targets are:" |
| 66 | @echo |
| 67 | @echo "build : Build the docker images." |
| 68 | @echo " - If this is the first time you are building, choose 'make build' option." |
| 69 | @echo "afrouter : Build the afrouter docker image" |
| 70 | @echo "afrouterd : Build the afrouterd docker image" |
| 71 | @echo "clean : Remove files created by the build and tests" |
| 72 | @echo "distclean : Remove venv directory" |
| 73 | @echo "docker-push : Push the docker images to an external repository" |
| 74 | @echo "lint-dockerfile : Perform static analysis on Dockerfiles" |
| 75 | @echo "lint-style : Verify code is properly gofmt-ed" |
| 76 | @echo "lint-sanity : Verify that 'go vet' doesn't report any issues" |
Scott Baker | b3a288a | 2019-10-02 14:57:29 -0700 | [diff] [blame] | 77 | @echo "lint-mod : Verify the integrity of the 'mod' files" |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 78 | @echo "lint : Shorthand for lint-style & lint-sanity" |
Scott Baker | 18123e5 | 2019-10-25 15:13:08 -0700 | [diff] [blame] | 79 | @echo "local-lib-go : Copies a local version of the VOTLHA dependencies into the vendor directory" |
| 80 | @echo "local-protos : Copies a local verison of the VOLTHA protos into the vendor directory" |
| 81 | @echo "sca : Runs various SCA through golangci-lint tool" |
| 82 | @echo "test : Generate reports for all go tests" |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 83 | @echo |
| 84 | |
| 85 | ## Local Development Helpers |
| 86 | local-protos: |
| 87 | ifdef LOCAL_PROTOS |
Scott Baker | 18123e5 | 2019-10-25 15:13:08 -0700 | [diff] [blame] | 88 | rm -rf vendor/github.com/opencord/voltha-protos/go |
| 89 | mkdir -p vendor/github.com/opencord/voltha-protos/go |
| 90 | cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/go |
| 91 | rm -rf vendor/github.com/opencord/voltha-protos/go/vendor |
| 92 | endif |
| 93 | |
| 94 | ## Local Development Helpers |
| 95 | local-lib-go: |
| 96 | ifdef LOCAL_LIB_GO |
| 97 | rm -rf vendor/github.com/opencord/voltha-lib-go |
| 98 | mkdir -p vendor/github.com/opencord/voltha-lib-go/v2/pkg |
| 99 | cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v2/pkg/ |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 100 | endif |
| 101 | |
| 102 | ## Docker targets |
| 103 | |
| 104 | build: docker-build |
| 105 | |
| 106 | docker-build: afrouter afrouterd |
| 107 | |
Scott Baker | 18123e5 | 2019-10-25 15:13:08 -0700 | [diff] [blame] | 108 | afrouter: local-protos local-lib-go |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 109 | docker build $(DOCKER_BUILD_ARGS) -t ${AFROUTER_IMAGENAME}:${DOCKER_TAG} -t ${AFROUTER_IMAGENAME}:latest -f docker/Dockerfile.afrouter . |
| 110 | |
Scott Baker | 18123e5 | 2019-10-25 15:13:08 -0700 | [diff] [blame] | 111 | afrouterd: local-protos local-lib-go |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 112 | docker build $(DOCKER_BUILD_ARGS) -t ${AFROUTERD_IMAGENAME}:${DOCKER_TAG} -t ${AFROUTERD_IMAGENAME}:latest -f docker/Dockerfile.afrouterd . |
| 113 | |
| 114 | docker-push: |
| 115 | docker push ${AFROUTER_IMAGENAME}:${DOCKER_TAG} |
| 116 | docker push ${AFROUTERD_IMAGENAME}:${DOCKER_TAG} |
| 117 | |
| 118 | ## lint and unit tests |
| 119 | |
| 120 | PATH:=$(GOPATH)/bin:$(PATH) |
| 121 | HADOLINT=$(shell PATH=$(GOPATH):$(PATH) which hadolint) |
| 122 | lint-dockerfile: |
| 123 | ifeq (,$(shell PATH=$(GOPATH):$(PATH) which hadolint)) |
| 124 | mkdir -p $(GOPATH)/bin |
| 125 | curl -o $(GOPATH)/bin/hadolint -sNSL https://github.com/hadolint/hadolint/releases/download/v1.17.1/hadolint-$(shell uname -s)-$(shell uname -m) |
| 126 | chmod 755 $(GOPATH)/bin/hadolint |
| 127 | endif |
| 128 | @echo "Running Dockerfile lint check ..." |
| 129 | @hadolint $$(find . -name "Dockerfile.*") |
| 130 | @echo "Dockerfile lint check OK" |
| 131 | |
| 132 | lint-style: |
| 133 | ifeq (,$(shell which gofmt)) |
| 134 | go get -u github.com/golang/go/src/cmd/gofmt |
| 135 | endif |
| 136 | @echo "Running style check..." |
| 137 | @gofmt_out="$$(gofmt -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\ |
| 138 | if [ ! -z "$$gofmt_out" ]; then \ |
| 139 | echo "$$gofmt_out" ;\ |
| 140 | echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\ |
| 141 | exit 1 ;\ |
| 142 | fi |
| 143 | @echo "Style check OK" |
| 144 | |
| 145 | lint-sanity: |
| 146 | @echo "Running sanity check..." |
Scott Baker | b3a288a | 2019-10-02 14:57:29 -0700 | [diff] [blame] | 147 | @go vet -mod=vendor ./... |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 148 | @echo "Sanity check OK" |
| 149 | |
Scott Baker | b3a288a | 2019-10-02 14:57:29 -0700 | [diff] [blame] | 150 | lint-mod: |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 151 | @echo "Running dependency check..." |
Scott Baker | b3a288a | 2019-10-02 14:57:29 -0700 | [diff] [blame] | 152 | @go mod verify |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 153 | @echo "Dependency check OK" |
| 154 | |
Scott Baker | b3a288a | 2019-10-02 14:57:29 -0700 | [diff] [blame] | 155 | lint: lint-style lint-sanity lint-mod lint-dockerfile |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 156 | |
Scott Baker | 1e75f5b | 2019-10-15 13:03:19 -0700 | [diff] [blame] | 157 | # Rules to automatically install golangci-lint |
| 158 | GOLANGCI_LINT_TOOL?=$(shell which golangci-lint) |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 159 | ifeq (,$(GOLANGCI_LINT_TOOL)) |
Scott Baker | 1e75f5b | 2019-10-15 13:03:19 -0700 | [diff] [blame] | 160 | GOLANGCI_LINT_TOOL=$(GOPATH)/bin/golangci-lint |
| 161 | golangci_lint_tool_install: |
| 162 | # Same version as installed by Jenkins ci-management |
| 163 | # Note that install using `go get` is not recommended as per https://github.com/golangci/golangci-lint |
| 164 | curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(GOPATH)/bin v1.17.0 |
| 165 | else |
| 166 | golangci_lint_tool_install: |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 167 | endif |
Scott Baker | 1e75f5b | 2019-10-15 13:03:19 -0700 | [diff] [blame] | 168 | |
| 169 | # Rules to automatically install go-junit-report |
| 170 | GO_JUNIT_REPORT?=$(shell which go-junit-report) |
| 171 | ifeq (,$(GO_JUNIT_REPORT)) |
| 172 | GO_JUNIT_REPORT=$(GOPATH)/bin/go-junit-report |
| 173 | go_junit_install: |
| 174 | go get -u github.com/jstemmer/go-junit-report |
| 175 | else |
| 176 | go_junit_install: |
| 177 | endif |
| 178 | |
| 179 | # Rules to automatically install gocover-covertura |
| 180 | GOCOVER_COBERTURA?=$(shell which gocover-cobertura) |
| 181 | ifeq (,$(GOCOVER_COBERTURA)) |
| 182 | @GOCOVER_COBERTURA=$(GOPATH)/bin/gocover-cobertura |
| 183 | gocover_cobertura_install: |
| 184 | go get -u github.com/t-yuki/gocover-cobertura |
| 185 | else |
| 186 | gocover_cobertura_install: |
| 187 | endif |
| 188 | |
| 189 | sca: golangci_lint_tool_install |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 190 | rm -rf ./sca-report |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 191 | @mkdir -p ./sca-report |
Scott Baker | b654c4d | 2019-10-21 17:33:06 -0700 | [diff] [blame] | 192 | $(GOLANGCI_LINT_TOOL) run --deadline 10m --out-format junit-xml ./... 2>&1 | tee ./sca-report/sca-report.xml |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 193 | |
Scott Baker | 1e75f5b | 2019-10-15 13:03:19 -0700 | [diff] [blame] | 194 | test: go_junit_install gocover_cobertura_install |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 195 | @mkdir -p ./tests/results |
Scott Baker | b3a288a | 2019-10-02 14:57:29 -0700 | [diff] [blame] | 196 | @go test -mod=vendor -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\ |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 197 | RETURN=$$? ;\ |
| 198 | $(GO_JUNIT_REPORT) < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\ |
| 199 | $(GOCOVER_COBERTURA) < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\ |
| 200 | exit $$RETURN |
| 201 | |
| 202 | clean: |
| 203 | |
| 204 | distclean: clean |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 205 | rm -rf ./sca_report |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 206 | |
| 207 | # end file |