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} \ |
| 51 | --build-arg LOCAL_PYVOLTHA=${LOCAL_PYVOLTHA} \ |
| 52 | --build-arg LOCAL_PROTOS=${LOCAL_PROTOS} |
| 53 | |
Scott Baker | b3a288a | 2019-10-02 14:57:29 -0700 | [diff] [blame] | 54 | # Default is GO111MODULE=auto, which will refuse to use go mod if running |
| 55 | # go less than 1.13.0 and this repository is checked out in GOPATH. For now, |
| 56 | # force module usage. This affects commands executed from this Makefile, but |
| 57 | # not the environment inside the Docker build (which does not build from |
| 58 | # inside a GOPATH). |
| 59 | export GO111MODULE=on |
| 60 | |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 61 | .PHONY: rw_core ro_core afrouter afrouterd local-protos local-pyvoltha |
| 62 | |
| 63 | # This should to be the first and default target in this Makefile |
| 64 | help: |
| 65 | @echo "Usage: make [<target>]" |
| 66 | @echo "where available targets are:" |
| 67 | @echo |
| 68 | @echo "build : Build the docker images." |
| 69 | @echo " - If this is the first time you are building, choose 'make build' option." |
| 70 | @echo "afrouter : Build the afrouter docker image" |
| 71 | @echo "afrouterd : Build the afrouterd docker image" |
| 72 | @echo "clean : Remove files created by the build and tests" |
| 73 | @echo "distclean : Remove venv directory" |
| 74 | @echo "docker-push : Push the docker images to an external repository" |
| 75 | @echo "lint-dockerfile : Perform static analysis on Dockerfiles" |
| 76 | @echo "lint-style : Verify code is properly gofmt-ed" |
| 77 | @echo "lint-sanity : Verify that 'go vet' doesn't report any issues" |
Scott Baker | b3a288a | 2019-10-02 14:57:29 -0700 | [diff] [blame] | 78 | @echo "lint-mod : Verify the integrity of the 'mod' files" |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 79 | @echo "lint : Shorthand for lint-style & lint-sanity" |
| 80 | @echo "sca : Runs various SCA through golangci-lint tool" |
| 81 | @echo "test : Generate reports for all go tests" |
| 82 | @echo |
| 83 | |
| 84 | ## Local Development Helpers |
| 85 | local-protos: |
| 86 | ifdef LOCAL_PROTOS |
Scott Baker | b3a288a | 2019-10-02 14:57:29 -0700 | [diff] [blame] | 87 | mkdir -p vendor/github.com/opencord/voltha-protos |
| 88 | cp -r ${GOPATH}/src/github.com/opencord/voltha-protos/voltha.pb vendor/github.com/opencord/voltha-protos/ |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 89 | endif |
| 90 | |
| 91 | ## Docker targets |
| 92 | |
| 93 | build: docker-build |
| 94 | |
| 95 | docker-build: afrouter afrouterd |
| 96 | |
| 97 | afrouter: local-protos |
| 98 | docker build $(DOCKER_BUILD_ARGS) -t ${AFROUTER_IMAGENAME}:${DOCKER_TAG} -t ${AFROUTER_IMAGENAME}:latest -f docker/Dockerfile.afrouter . |
| 99 | |
| 100 | afrouterd: local-protos |
| 101 | docker build $(DOCKER_BUILD_ARGS) -t ${AFROUTERD_IMAGENAME}:${DOCKER_TAG} -t ${AFROUTERD_IMAGENAME}:latest -f docker/Dockerfile.afrouterd . |
| 102 | |
| 103 | docker-push: |
| 104 | docker push ${AFROUTER_IMAGENAME}:${DOCKER_TAG} |
| 105 | docker push ${AFROUTERD_IMAGENAME}:${DOCKER_TAG} |
| 106 | |
| 107 | ## lint and unit tests |
| 108 | |
| 109 | PATH:=$(GOPATH)/bin:$(PATH) |
| 110 | HADOLINT=$(shell PATH=$(GOPATH):$(PATH) which hadolint) |
| 111 | lint-dockerfile: |
| 112 | ifeq (,$(shell PATH=$(GOPATH):$(PATH) which hadolint)) |
| 113 | mkdir -p $(GOPATH)/bin |
| 114 | curl -o $(GOPATH)/bin/hadolint -sNSL https://github.com/hadolint/hadolint/releases/download/v1.17.1/hadolint-$(shell uname -s)-$(shell uname -m) |
| 115 | chmod 755 $(GOPATH)/bin/hadolint |
| 116 | endif |
| 117 | @echo "Running Dockerfile lint check ..." |
| 118 | @hadolint $$(find . -name "Dockerfile.*") |
| 119 | @echo "Dockerfile lint check OK" |
| 120 | |
| 121 | lint-style: |
| 122 | ifeq (,$(shell which gofmt)) |
| 123 | go get -u github.com/golang/go/src/cmd/gofmt |
| 124 | endif |
| 125 | @echo "Running style check..." |
| 126 | @gofmt_out="$$(gofmt -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\ |
| 127 | if [ ! -z "$$gofmt_out" ]; then \ |
| 128 | echo "$$gofmt_out" ;\ |
| 129 | echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\ |
| 130 | exit 1 ;\ |
| 131 | fi |
| 132 | @echo "Style check OK" |
| 133 | |
| 134 | lint-sanity: |
| 135 | @echo "Running sanity check..." |
Scott Baker | b3a288a | 2019-10-02 14:57:29 -0700 | [diff] [blame] | 136 | @go vet -mod=vendor ./... |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 137 | @echo "Sanity check OK" |
| 138 | |
Scott Baker | b3a288a | 2019-10-02 14:57:29 -0700 | [diff] [blame] | 139 | lint-mod: |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 140 | @echo "Running dependency check..." |
Scott Baker | b3a288a | 2019-10-02 14:57:29 -0700 | [diff] [blame] | 141 | @go mod verify |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 142 | @echo "Dependency check OK" |
| 143 | |
Scott Baker | b3a288a | 2019-10-02 14:57:29 -0700 | [diff] [blame] | 144 | lint: lint-style lint-sanity lint-mod lint-dockerfile |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 145 | |
| 146 | GO_JUNIT_REPORT:=$(shell which go-junit-report) |
| 147 | GOCOVER_COBERTURA:=$(shell which gocover-cobertura) |
| 148 | |
| 149 | GOLANGCI_LINT_TOOL:=$(shell which golangci-lint) |
| 150 | sca: |
| 151 | @echo "Please install golangci-lint tool to run sca" |
| 152 | @mkdir -p ./sca-report |
| 153 | golangci-lint run --out-format junit-xml ./... 2>&1 | tee ./sca-report/sca-report.xml ;\ |
| 154 | rm -rf sca-report |
| 155 | |
| 156 | test: |
| 157 | ifeq (,$(GO_JUNIT_REPORT)) |
| 158 | go get -u github.com/jstemmer/go-junit-report |
| 159 | @GO_JUNIT_REPORT=$(GOPATH)/bin/go-junit-report |
| 160 | endif |
| 161 | ifeq (,$(GOCOVER_COBERTURA)) |
| 162 | go get -u github.com/t-yuki/gocover-cobertura |
| 163 | @GOCOVER_COBERTURA=$(GOPATH)/bin/gocover-cobertura |
| 164 | endif |
| 165 | @mkdir -p ./tests/results |
Scott Baker | b3a288a | 2019-10-02 14:57:29 -0700 | [diff] [blame] | 166 | @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] | 167 | RETURN=$$? ;\ |
| 168 | $(GO_JUNIT_REPORT) < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\ |
| 169 | $(GOCOVER_COBERTURA) < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\ |
| 170 | exit $$RETURN |
| 171 | |
| 172 | clean: |
| 173 | |
| 174 | distclean: clean |
| 175 | |
| 176 | # end file |