Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2016-present 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 | SIMULATEDONU_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-adapter-simulated-onu |
| 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 | |
| 40 | DOCKER_BUILD_ARGS ?= \ |
| 41 | ${DOCKER_EXTRA_ARGS} \ |
| 42 | --build-arg org_label_schema_version="${VERSION}" \ |
| 43 | --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \ |
| 44 | --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \ |
| 45 | --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \ |
| 46 | --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \ |
| 47 | --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}" |
| 48 | |
| 49 | DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \ |
| 50 | --build-arg LOCAL_PYVOLTHA=${LOCAL_PYVOLTHA} \ |
| 51 | --build-arg LOCAL_PROTOS=${LOCAL_PROTOS} |
| 52 | |
| 53 | .PHONY: simulated_onu local-protos |
| 54 | |
| 55 | # This should to be the first and default target in this Makefile |
| 56 | help: |
| 57 | @echo "Usage: make [<target>]" |
| 58 | @echo "where available targets are:" |
| 59 | @echo |
| 60 | @echo "build : Build the docker images." |
| 61 | @echo " - If this is the first time you are building, choose 'make build' option." |
| 62 | @echo "simulated_onu : Build the simulated_onu docker image" |
| 63 | @echo "clean : Remove files created by the build and tests" |
| 64 | @echo "docker-push : Push the docker images to an external repository" |
| 65 | @echo "lint-dockerfile : Perform static analysis on Dockerfiles" |
| 66 | @echo "lint-style : Verify code is properly gofmt-ed" |
| 67 | @echo "lint-sanity : Verify that 'go vet' doesn't report any issues" |
| 68 | @echo "lint-dep : Verify the integrity of the 'dep' files" |
| 69 | @echo "lint : Shorthand for lint-style & lint-sanity" |
| 70 | @echo "test : Generate reports for all go tests" |
| 71 | @echo |
| 72 | |
| 73 | ## Local Development Helpers |
| 74 | local-protos: |
| 75 | ifdef LOCAL_PROTOS |
| 76 | mkdir -p vendor/github.com/opencord/voltha-protos/go |
| 77 | cp -r ${GOPATH}/src/github.com/opencord/voltha-protos/go/* vendor/github.com/opencord/voltha-protos/go |
| 78 | endif |
| 79 | |
| 80 | ## Docker targets |
| 81 | |
| 82 | build: docker-build |
| 83 | |
| 84 | docker-build: simulated_onu |
| 85 | |
| 86 | simulated_onu: local-protos |
| 87 | docker build $(DOCKER_BUILD_ARGS) -t ${SIMULATEDONU_IMAGENAME}:${DOCKER_TAG} -t ${SIMULATEDONU_IMAGENAME}:latest -f docker/Dockerfile.simulated_onu . |
| 88 | |
| 89 | docker-push: |
| 90 | docker push ${SIMULATEDONU_IMAGENAME}:${DOCKER_TAG} |
| 91 | |
| 92 | ## lint and unit tests |
| 93 | |
| 94 | PATH:=$(GOPATH)/bin:$(PATH) |
| 95 | HADOLINT=$(shell PATH=$(GOPATH):$(PATH) which hadolint) |
| 96 | lint-dockerfile: |
| 97 | ifeq (,$(shell PATH=$(GOPATH):$(PATH) which hadolint)) |
| 98 | mkdir -p $(GOPATH)/bin |
| 99 | curl -o $(GOPATH)/bin/hadolint -sNSL https://github.com/hadolint/hadolint/releases/download/v1.17.1/hadolint-$(shell uname -s)-$(shell uname -m) |
| 100 | chmod 755 $(GOPATH)/bin/hadolint |
| 101 | endif |
| 102 | @echo "Running Dockerfile lint check ..." |
| 103 | @hadolint $$(find . -name "Dockerfile.*") |
| 104 | @echo "Dockerfile lint check OK" |
| 105 | |
| 106 | lint-style: |
| 107 | ifeq (,$(shell which gofmt)) |
| 108 | go get -u github.com/golang/go/src/cmd/gofmt |
| 109 | endif |
| 110 | @echo "Running style check..." |
| 111 | @gofmt_out="$$(gofmt -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\ |
| 112 | if [ ! -z "$$gofmt_out" ]; then \ |
| 113 | echo "$$gofmt_out" ;\ |
| 114 | echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\ |
| 115 | exit 1 ;\ |
| 116 | fi |
| 117 | @echo "Style check OK" |
| 118 | |
| 119 | lint-sanity: |
| 120 | @echo "Running sanity check..." |
| 121 | @go vet ./... |
| 122 | @echo "Sanity check OK" |
| 123 | |
| 124 | lint-dep: |
| 125 | @echo "Running dependency check..." |
| 126 | @dep check |
| 127 | @echo "Dependency check OK" |
| 128 | |
| 129 | lint: lint-style lint-sanity lint-dep lint-dockerfile |
| 130 | |
| 131 | GO_JUNIT_REPORT:=$(shell which go-junit-report) |
| 132 | GOCOVER_COBERTURA:=$(shell which gocover-cobertura) |
| 133 | |
| 134 | test: |
| 135 | ifeq (,$(GO_JUNIT_REPORT)) |
| 136 | go get -u github.com/jstemmer/go-junit-report |
| 137 | @GO_JUNIT_REPORT=$(GOPATH)/bin/go-junit-report |
| 138 | endif |
| 139 | ifeq (,$(GOCOVER_COBERTURA)) |
| 140 | go get -u github.com/t-yuki/gocover-cobertura |
| 141 | @GOCOVER_COBERTURA=$(GOPATH)/bin/gocover-cobertura |
| 142 | endif |
| 143 | @mkdir -p ./tests/results |
| 144 | @go test -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\ |
| 145 | RETURN=$$? ;\ |
| 146 | $(GO_JUNIT_REPORT) < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\ |
| 147 | $(GOCOVER_COBERTURA) < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\ |
| 148 | exit $$RETURN |
| 149 | |
| 150 | clean: |
| 151 | |
| 152 | distclean: clean |
| 153 | |
| 154 | # end file |