blob: 3a131d05d49929f1014689f5f940626e1d372408 [file] [log] [blame]
Joey Armstrong811e9542022-12-25 21:45:27 -05001# -*- makefile -*-
2# -----------------------------------------------------------------------#
Joey Armstrong7a9af442024-01-03 19:26:36 -05003# Copyright 2016-2024 Open Networking Foundation (ONF) and the ONF Contributors
Joey Armstrong393daca2023-07-06 08:47:54 -04004#
khenaidoocfee5f42018-07-19 22:47:38 -04005# 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.
Joey Armstrong811e9542022-12-25 21:45:27 -050016# -----------------------------------------------------------------------
khenaidoocfee5f42018-07-19 22:47:38 -040017
Joey Armstrong393daca2023-07-06 08:47:54 -040018$(if $(DEBUG),$(warning ENTER))
19
Joey Armstrong1a78c6c2024-05-03 18:55:00 -040020##-------------------##
21##---] GLOBALS [---##
22##-------------------##
23ifndef .DEFAULT_GOAL
24 .DEFAULT_GOAL := help # ?= help evaluated late
25endif
26MAKECMDGOALS ?= help
Joey Armstrong811e9542022-12-25 21:45:27 -050027
Joey Armstrong1a78c6c2024-05-03 18:55:00 -040028$(if $(findstring disabled-joey,$(USER)),\
29 $(eval USE_LF_MK := 1)) # special snowflake
Joey Armstrong811e9542022-12-25 21:45:27 -050030
31##--------------------##
32##---] INCLUDES [---##
33##--------------------##
Joey Armstrong1a78c6c2024-05-03 18:55:00 -040034ifdef USE_LF_MK
35 include lf/include.mk
36else
37 include lf/transition.mk
38endif # ifdef USE_LF_MK
Kent Hagerman074d0e02019-04-24 17:58:16 -040039
Zack Williams27f59a42019-05-10 09:12:07 -070040# Variables
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040041VERSION ?= $(shell cat ./VERSION)
Zack Williams27f59a42019-05-10 09:12:07 -070042
Maninder9a1bc0d2020-10-26 11:34:02 +053043# Packages
44PACKAGES = $(shell go list ./...)
45
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040046DOCKER_LABEL_VCS_DIRTY = false
47ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0)
48 DOCKER_LABEL_VCS_DIRTY = true
49endif
Joey Armstrong811e9542022-12-25 21:45:27 -050050
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040051## Docker related
David K. Bainbridgee14914d2019-05-24 13:43:05 -070052DOCKER_EXTRA_ARGS ?=
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040053DOCKER_REGISTRY ?=
54DOCKER_REPOSITORY ?=
55DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true)
David K. Bainbridge41835142021-04-01 17:26:12 +000056DOCKER_TARGET ?= prod
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040057RWCORE_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-rw-core
Andrea Campanella7a6cce22019-12-17 14:10:27 -080058TYPE ?= minimal
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040059
60## Docker labels. Only set ref and commit date if committed
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040061DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
62DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
63DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
64DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040065
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040066DOCKER_BUILD_ARGS ?= \
David K. Bainbridgee14914d2019-05-24 13:43:05 -070067 ${DOCKER_EXTRA_ARGS} \
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040068 --build-arg org_label_schema_version="${VERSION}" \
69 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
70 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
71 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
72 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
73 --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}"
74
75DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040076 --build-arg LOCAL_PROTOS=${LOCAL_PROTOS}
khenaidoocfee5f42018-07-19 22:47:38 -040077
David K. Bainbridge41835142021-04-01 17:26:12 +000078.PHONY: docker-build local-protos local-lib-go help
79.DEFAULT_GOAL := help
khenaidoocfee5f42018-07-19 22:47:38 -040080
khenaidoocfee5f42018-07-19 22:47:38 -040081
David K. Bainbridge1678e192019-05-17 11:48:29 -070082## Local Development Helpers
David K. Bainbridge41835142021-04-01 17:26:12 +000083local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory
David K. Bainbridge1678e192019-05-17 11:48:29 -070084ifdef LOCAL_PROTOS
Joey Armstrong393daca2023-07-06 08:47:54 -040085 $(RM) -r vendor/github.com/opencord/voltha-protos/v5/go
khenaidood948f772021-08-11 17:49:24 -040086 mkdir -p vendor/github.com/opencord/voltha-protos/v5/go
87 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v5/go
David K. Bainbridge1678e192019-05-17 11:48:29 -070088endif
89
Scott Bakercb7c88a2019-10-16 18:32:48 -070090## Local Development Helpers
David K. Bainbridge41835142021-04-01 17:26:12 +000091local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory
Scott Bakercb7c88a2019-10-16 18:32:48 -070092ifdef LOCAL_LIB_GO
Joey Armstrong393daca2023-07-06 08:47:54 -040093 $(RM) -r vendor/github.com/opencord/voltha-lib-go/v7/pkg
khenaidood948f772021-08-11 17:49:24 -040094 mkdir -p vendor/github.com/opencord/voltha-lib-go/v7/pkg
95 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v7/pkg/
Scott Bakercb7c88a2019-10-16 18:32:48 -070096endif
97
Joey Armstrong811e9542022-12-25 21:45:27 -050098## -----------------------------------------------------------------------
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040099## Docker targets
Joey Armstrong811e9542022-12-25 21:45:27 -0500100## -----------------------------------------------------------------------
David K. Bainbridge41835142021-04-01 17:26:12 +0000101build: docker-build ## Alias for 'docker-build'
khenaidoocfee5f42018-07-19 22:47:38 -0400102
Joey Armstrong811e9542022-12-25 21:45:27 -0500103docker-build-args += --debug
104docker-build-args += --log-level 'debug'
105docker-build-args := $(null)# comment line for debug mode
106
David K. Bainbridge41835142021-04-01 17:26:12 +0000107docker-build: local-protos local-lib-go ## Build core docker image (set BUILD_PROFILED=true to also build the profiled image)
Joey Armstrong393daca2023-07-06 08:47:54 -0400108
109 $(call banner-enter,$@)
110 $(MAKE) --no-print-directory test-coverage-init
Joey Armstrong811e9542022-12-25 21:45:27 -0500111
112 docker $(docker-build-args) build $(DOCKER_BUILD_ARGS) -t ${RWCORE_IMAGENAME}:${DOCKER_TAG} --target ${DOCKER_TARGET} -f docker/Dockerfile.rw_core .
Matteo Scandolo450933a2020-04-30 07:47:08 -0700113ifdef BUILD_PROFILED
Andrea Campanella8d4f3e32021-04-13 10:02:03 +0200114# Force target to dev as profile build must be built with dynamic linking
115 docker build $(DOCKER_BUILD_ARGS) --target dev --build-arg EXTRA_GO_BUILD_TAGS="-tags profile" --build-arg CGO_PARAMETER="CGO_ENABLED=1" -t ${RWCORE_IMAGENAME}:${DOCKER_TAG}-profile -f docker/Dockerfile.rw_core .
Matteo Scandolo450933a2020-04-30 07:47:08 -0700116endif
David K. Bainbridge3768fde2020-07-29 21:15:35 -0700117ifdef BUILD_RACE
Andrea Campanella8d4f3e32021-04-13 10:02:03 +0200118# Force target to dev as race detection build must be built with dynamic linking
119 docker build $(DOCKER_BUILD_ARGS) --target dev --build-arg GOLANG_IMAGE=golang:1.13.8-buster --build-arg CGO_PARAMETER="CGO_ENABLED=1" --build-arg DEPLOY_IMAGE=debian:buster-slim --build-arg EXTRA_GO_BUILD_TAGS="--race" -t ${RWCORE_IMAGENAME}:${DOCKER_TAG}-rd -f docker/Dockerfile.rw_core .
David K. Bainbridge3768fde2020-07-29 21:15:35 -0700120endif
Don Newton8eca4622020-02-10 16:44:48 -0500121
Joey Armstrong393daca2023-07-06 08:47:54 -0400122 $(call banner-leave,$@)
123
Joey Armstrong811e9542022-12-25 21:45:27 -0500124## -----------------------------------------------------------------------
Joey Armstrong5f51f2e2023-01-17 17:06:26 -0500125## Intent:
Joey Armstrong811e9542022-12-25 21:45:27 -0500126## -----------------------------------------------------------------------
David K. Bainbridge41835142021-04-01 17:26:12 +0000127docker-push: ## Push the docker images to an external repository
Joey Armstrong393daca2023-07-06 08:47:54 -0400128
129 $(call banner-enter,$@)
130
Jan Klare07d5bb12023-11-01 14:55:42 +0100131 docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}
Matteo Scandolo8ed186a2020-05-04 10:52:42 -0700132ifdef BUILD_PROFILED
133 docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}-profile
134endif
David K. Bainbridge3768fde2020-07-29 21:15:35 -0700135ifdef BUILD_RACE
136 docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}-rd
137endif
David K. Bainbridge41835142021-04-01 17:26:12 +0000138docker-kind-load: ## Load docker images into a KinD cluster
Andrea Campanella7a6cce22019-12-17 14:10:27 -0800139 @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi
140 kind load docker-image ${RWCORE_IMAGENAME}:${DOCKER_TAG} --name=voltha-$(TYPE) --nodes $(shell kubectl get nodes --template='{{range .items}}{{.metadata.name}},{{end}}' | rev | cut -c 2- | rev)
Andrea Campanella7a6cce22019-12-17 14:10:27 -0800141
Joey Armstrong393daca2023-07-06 08:47:54 -0400142 $(call banner-leave,$@)
143
144## -----------------------------------------------------------------------
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400145## lint and unit tests
Joey Armstrong393daca2023-07-06 08:47:54 -0400146## -----------------------------------------------------------------------
147
148# [TODO]
149# o Merge lint-* targets with repo:openolt-adapter/Makefile
khenaidood2b6df92018-12-13 16:37:20 -0500150
David K. Bainbridge41835142021-04-01 17:26:12 +0000151lint-dockerfile: ## Perform static analysis on Dockerfile
Kent Hagermandcd4dcc2020-02-25 17:56:17 -0500152 @echo "Running Dockerfile lint check..."
Kent Hagerman812b2572020-01-28 11:57:51 -0500153 @${HADOLINT} $$(find . -name "Dockerfile.*")
David Bainbridge5f3619c2019-07-10 22:51:09 +0000154 @echo "Dockerfile lint check OK"
155
David K. Bainbridge41835142021-04-01 17:26:12 +0000156lint-mod: ## Verify the Go dependencies
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400157 @echo "Running dependency check..."
Kent Hagerman812b2572020-01-28 11:57:51 -0500158 @${GO} mod verify
Scott Bakere4c2a982019-11-21 16:32:03 -0800159 @echo "Dependency check OK. Running vendor check..."
160 @git status > /dev/null
Kent Hagermandcd4dcc2020-02-25 17:56:17 -0500161 @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)
162 @[[ `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)
Joey Armstrong811e9542022-12-25 21:45:27 -0500163
Joey Armstrong653504f2024-01-18 12:58:56 -0500164 $(HIDE)$(MAKE) --no-print-directory mod-update
Joey Armstrong811e9542022-12-25 21:45:27 -0500165
Scott Bakere4c2a982019-11-21 16:32:03 -0800166 @git status > /dev/null
Kent Hagermandcd4dcc2020-02-25 17:56:17 -0500167 @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)
168 @[[ `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)
Scott Bakere4c2a982019-11-21 16:32:03 -0800169 @echo "Vendor check OK."
170
Joey Armstrong811e9542022-12-25 21:45:27 -0500171## -----------------------------------------------------------------------
172## -----------------------------------------------------------------------
David K. Bainbridge41835142021-04-01 17:26:12 +0000173lint: lint-mod lint-dockerfile ## Run all lint targets
Kent Hagerman074d0e02019-04-24 17:58:16 -0400174
Joey Armstrong1a78c6c2024-05-03 18:55:00 -0400175include $(legacy-mk)/analysis/include.mk
Scott Baker18ea5a42019-10-15 16:08:42 -0700176
Joey Armstrong811e9542022-12-25 21:45:27 -0500177## -----------------------------------------------------------------------
178## -----------------------------------------------------------------------
179tests-dir := ./tests/results
180tests-coverage := $(tests-dir)/go-test-coverage
181tests-results := $(tests-dir)/go-test-results
Joey Armstrong393daca2023-07-06 08:47:54 -0400182
183test :: test-coverage ## Run unit tests
184test-coverage : local-lib-go
Kent Hagerman074d0e02019-04-24 17:58:16 -0400185
Joey Armstrong367d76b2023-06-08 17:16:46 -0400186clean :: distclean ## Removes any local filesystem artifacts generated by a build
David K. Bainbridge1678e192019-05-17 11:48:29 -0700187
Joey Armstrong367d76b2023-06-08 17:16:46 -0400188distclean sterile :: ## Removes any local filesystem artifacts generated by a build or test run
Joey Armstrong811e9542022-12-25 21:45:27 -0500189 $(RM) -r ./sca-report
David K. Bainbridge1678e192019-05-17 11:48:29 -0700190
Joey Armstrong1a78c6c2024-05-03 18:55:00 -0400191fmt: ## Formats the source code to go best practice style
Joey Armstrong7a9af442024-01-03 19:26:36 -0500192 gofmt -s -e -w $(PACKAGES)
193# @go fmt ${PACKAGES}
David K. Bainbridge41835142021-04-01 17:26:12 +0000194
Joey Armstrong653504f2024-01-18 12:58:56 -0500195
Joey Armstrong393daca2023-07-06 08:47:54 -0400196## -----------------------------------------------------------------------
197## -----------------------------------------------------------------------
198.PHONY: mod-update
Joey Armstrong653504f2024-01-18 12:58:56 -0500199mod-update: go-version mod-tidy mod-vendor
200
201## -----------------------------------------------------------------------
202## -----------------------------------------------------------------------
203.PHONY: go-version
204go-version :
205 $(call banner-enter,Target $@)
206 ${GO} version
207 $(call banner-leave,Target $@)
Joey Armstrong393daca2023-07-06 08:47:54 -0400208
209## -----------------------------------------------------------------------
210## -----------------------------------------------------------------------
211.PHONY: mod-tidy
Joey Armstrong653504f2024-01-18 12:58:56 -0500212mod-tidy :
213 $(call banner-enter,Target $@)
Joey Armstrong393daca2023-07-06 08:47:54 -0400214 ${GO} mod tidy
Joey Armstrong653504f2024-01-18 12:58:56 -0500215 $(call banner-leave,Target $@)
Joey Armstrong393daca2023-07-06 08:47:54 -0400216
217## -----------------------------------------------------------------------
Joey Armstrong393daca2023-07-06 08:47:54 -0400218## -----------------------------------------------------------------------
219.PHONY: mod-vendor
Joey Armstrong653504f2024-01-18 12:58:56 -0500220mod-vendor : mod-tidy
221mod-vendor :
222 $(call banner-enter,Target $@)
223 $(if $(LOCAL_FIX_PERMS),chmod o+w $(CURDIR))
Joey Armstrong393daca2023-07-06 08:47:54 -0400224 ${GO} mod vendor
Joey Armstrong653504f2024-01-18 12:58:56 -0500225 $(if $(LOCAL_FIX_PERMS),chmod o-w $(CURDIR))
226 $(call banner-leave,Target $@)
Joey Armstrong393daca2023-07-06 08:47:54 -0400227
228## -----------------------------------------------------------------------
229## -----------------------------------------------------------------------
230help ::
231 @echo '[MOD UPDATE]'
232 @echo ' mod-update'
233 @echo ' LOCAL_FIX_PERMS=1 Hack to fix docker filesystem access problems'
234 @echo ' mod-tidy'
235 @echo ' mod-vendor'
Joey Armstrong811e9542022-12-25 21:45:27 -0500236
237## ---------------------------------------------------------------------------
David K. Bainbridge41835142021-04-01 17:26:12 +0000238# For each makefile target, add ## <description> on the target line and it will be listed by 'make help'
Joey Armstrong811e9542022-12-25 21:45:27 -0500239## ---------------------------------------------------------------------------
Joey Armstrong367d76b2023-06-08 17:16:46 -0400240help :: ## Print help for each Makefile target
Joey Armstrong7a9af442024-01-03 19:26:36 -0500241 @echo 'Usage: make [options] [target] ...'
242 @printf ' %-33.33s %s\n' 'fmt' 'Run gofmt -s -e -w to normalize syntax'
Joey Armstrong811e9542022-12-25 21:45:27 -0500243 @echo " help This message"
244 @echo " todo Future enhancements"
245 @echo " versions Display version-by-tool used while building"
246 ifdef VERBOSE
David K. Bainbridge41835142021-04-01 17:26:12 +0000247 @echo
Joey Armstrong811e9542022-12-25 21:45:27 -0500248 endif
249 @echo
250 @grep --no-filename '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \
251 | sort \
252 | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};'
253
Joey Armstrong393daca2023-07-06 08:47:54 -0400254$(if $(DEBUG),$(warning LEAVE))
255
Joey Armstrong811e9542022-12-25 21:45:27 -0500256# [EOF]