Scott Baker | 2d89798 | 2019-09-24 11:50:08 -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 | SIMULATEDOLT_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-adapter-simulated-olt |
| 33 | |
| 34 | ## Docker labels. Only set ref and commit date if committed |
| 35 | DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote)) |
| 36 | DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD) |
| 37 | DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ") |
| 38 | DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD) |
| 39 | |
Scott Baker | f8f97fa | 2019-09-30 13:07:01 -0700 | [diff] [blame] | 40 | # Default is GO111MODULE=auto, which will refuse to use go mod if running |
| 41 | # go less than 1.13.0 and this repository is checked out in GOPATH. For now, |
| 42 | # force module usage. This affects commands executed from this Makefile, but |
| 43 | # not the environment inside the Docker build (which does not build from |
| 44 | # inside a GOPATH). |
| 45 | export GO111MODULE=on |
| 46 | |
Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame] | 47 | DOCKER_BUILD_ARGS ?= \ |
| 48 | ${DOCKER_EXTRA_ARGS} \ |
| 49 | --build-arg org_label_schema_version="${VERSION}" \ |
| 50 | --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \ |
| 51 | --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \ |
| 52 | --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \ |
| 53 | --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \ |
| 54 | --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}" |
| 55 | |
| 56 | DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \ |
| 57 | --build-arg LOCAL_PYVOLTHA=${LOCAL_PYVOLTHA} \ |
| 58 | --build-arg LOCAL_PROTOS=${LOCAL_PROTOS} |
| 59 | |
| 60 | .PHONY: simulated_olt simulated_onu local-protosa |
| 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 "simulated_olt : Build the simulated_olt docker image" |
| 70 | @echo "clean : Remove files created by the build and tests" |
| 71 | @echo "docker-push : Push the docker images to an external repository" |
| 72 | @echo "lint-dockerfile : Perform static analysis on Dockerfiles" |
| 73 | @echo "lint-style : Verify code is properly gofmt-ed" |
| 74 | @echo "lint-sanity : Verify that 'go vet' doesn't report any issues" |
Scott Baker | f8f97fa | 2019-09-30 13:07:01 -0700 | [diff] [blame] | 75 | @echo "lint-mod : Verify the integrity of the 'mod' files" |
Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame] | 76 | @echo "lint : Shorthand for lint-style & lint-sanity" |
| 77 | @echo "test : Generate reports for all go tests" |
| 78 | @echo |
| 79 | |
| 80 | ## Local Development Helpers |
| 81 | local-protos: |
| 82 | ifdef LOCAL_PROTOS |
| 83 | mkdir -p vendor/github.com/opencord/voltha-protos/go |
| 84 | cp -r ${GOPATH}/src/github.com/opencord/voltha-protos/go/* vendor/github.com/opencord/voltha-protos/go |
| 85 | endif |
| 86 | |
| 87 | ## Docker targets |
| 88 | |
| 89 | build: docker-build |
| 90 | |
| 91 | docker-build: simulated_olt |
| 92 | |
| 93 | simulated_olt: local-protos |
| 94 | docker build $(DOCKER_BUILD_ARGS) -t ${SIMULATEDOLT_IMAGENAME}:${DOCKER_TAG} -t ${SIMULATEDOLT_IMAGENAME}:latest -f docker/Dockerfile.simulated_olt . |
| 95 | |
| 96 | docker-push: |
| 97 | docker push ${SIMULATEDOLT_IMAGENAME}:${DOCKER_TAG} |
| 98 | |
| 99 | ## lint and unit tests |
| 100 | |
| 101 | PATH:=$(GOPATH)/bin:$(PATH) |
| 102 | HADOLINT=$(shell PATH=$(GOPATH):$(PATH) which hadolint) |
| 103 | lint-dockerfile: |
| 104 | ifeq (,$(shell PATH=$(GOPATH):$(PATH) which hadolint)) |
| 105 | mkdir -p $(GOPATH)/bin |
| 106 | curl -o $(GOPATH)/bin/hadolint -sNSL https://github.com/hadolint/hadolint/releases/download/v1.17.1/hadolint-$(shell uname -s)-$(shell uname -m) |
| 107 | chmod 755 $(GOPATH)/bin/hadolint |
| 108 | endif |
| 109 | @echo "Running Dockerfile lint check ..." |
| 110 | @hadolint $$(find . -name "Dockerfile.*") |
| 111 | @echo "Dockerfile lint check OK" |
| 112 | |
| 113 | lint-style: |
| 114 | ifeq (,$(shell which gofmt)) |
| 115 | go get -u github.com/golang/go/src/cmd/gofmt |
| 116 | endif |
| 117 | @echo "Running style check..." |
| 118 | @gofmt_out="$$(gofmt -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\ |
| 119 | if [ ! -z "$$gofmt_out" ]; then \ |
| 120 | echo "$$gofmt_out" ;\ |
| 121 | echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\ |
| 122 | exit 1 ;\ |
| 123 | fi |
| 124 | @echo "Style check OK" |
| 125 | |
| 126 | lint-sanity: |
| 127 | @echo "Running sanity check..." |
Scott Baker | f8f97fa | 2019-09-30 13:07:01 -0700 | [diff] [blame] | 128 | @go vet -mod=vendor ./... |
Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame] | 129 | @echo "Sanity check OK" |
| 130 | |
Scott Baker | f8f97fa | 2019-09-30 13:07:01 -0700 | [diff] [blame] | 131 | lint-mod: |
Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame] | 132 | @echo "Running dependency check..." |
Scott Baker | f8f97fa | 2019-09-30 13:07:01 -0700 | [diff] [blame] | 133 | @go mod verify |
Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame] | 134 | @echo "Dependency check OK" |
| 135 | |
Scott Baker | f8f97fa | 2019-09-30 13:07:01 -0700 | [diff] [blame] | 136 | lint: lint-style lint-sanity lint-mod lint-dockerfile |
Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame] | 137 | |
| 138 | GO_JUNIT_REPORT:=$(shell which go-junit-report) |
| 139 | GOCOVER_COBERTURA:=$(shell which gocover-cobertura) |
| 140 | |
| 141 | test: |
| 142 | ifeq (,$(GO_JUNIT_REPORT)) |
| 143 | go get -u github.com/jstemmer/go-junit-report |
| 144 | @GO_JUNIT_REPORT=$(GOPATH)/bin/go-junit-report |
| 145 | endif |
| 146 | ifeq (,$(GOCOVER_COBERTURA)) |
| 147 | go get -u github.com/t-yuki/gocover-cobertura |
| 148 | @GOCOVER_COBERTURA=$(GOPATH)/bin/gocover-cobertura |
| 149 | endif |
| 150 | @mkdir -p ./tests/results |
Scott Baker | f8f97fa | 2019-09-30 13:07:01 -0700 | [diff] [blame] | 151 | @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 | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame] | 152 | RETURN=$$? ;\ |
| 153 | $(GO_JUNIT_REPORT) < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\ |
| 154 | $(GOCOVER_COBERTURA) < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\ |
| 155 | exit $$RETURN |
| 156 | |
| 157 | clean: |
| 158 | |
| 159 | distclean: clean |
| 160 | |
| 161 | # end file |