blob: 88cc745ba9f50d52a6b0921adb478a69c9a84486 [file] [log] [blame]
Joey Armstrong811e9542022-12-25 21:45:27 -05001# -*- makefile -*-
2# -----------------------------------------------------------------------#
3# Copyright 2016-2023 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 Armstrong811e9542022-12-25 21:45:27 -050020.DEFAULT_GOAL := help
21
Joey Armstrong811e9542022-12-25 21:45:27 -050022$(if $(VERBOSE),$(eval export VERBOSE=$(VERBOSE))) # visible to include(s)
23
24##--------------------##
25##---] INCLUDES [---##
26##--------------------##
Joey Armstrong367d76b2023-06-08 17:16:46 -040027-include config.mk
28include makefiles/include.mk
Kent Hagerman074d0e02019-04-24 17:58:16 -040029
Zack Williams27f59a42019-05-10 09:12:07 -070030# Variables
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040031VERSION ?= $(shell cat ./VERSION)
Zack Williams27f59a42019-05-10 09:12:07 -070032
Maninder9a1bc0d2020-10-26 11:34:02 +053033# Packages
34PACKAGES = $(shell go list ./...)
35
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040036DOCKER_LABEL_VCS_DIRTY = false
37ifneq ($(shell git ls-files --others --modified --exclude-standard 2>/dev/null | wc -l | sed -e 's/ //g'),0)
38 DOCKER_LABEL_VCS_DIRTY = true
39endif
Joey Armstrong811e9542022-12-25 21:45:27 -050040
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040041## Docker related
David K. Bainbridgee14914d2019-05-24 13:43:05 -070042DOCKER_EXTRA_ARGS ?=
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040043DOCKER_REGISTRY ?=
44DOCKER_REPOSITORY ?=
45DOCKER_TAG ?= ${VERSION}$(shell [[ ${DOCKER_LABEL_VCS_DIRTY} == "true" ]] && echo "-dirty" || true)
David K. Bainbridge41835142021-04-01 17:26:12 +000046DOCKER_TARGET ?= prod
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040047RWCORE_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}voltha-rw-core
Andrea Campanella7a6cce22019-12-17 14:10:27 -080048TYPE ?= minimal
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040049
50## Docker labels. Only set ref and commit date if committed
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040051DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
52DOCKER_LABEL_VCS_REF = $(shell git rev-parse HEAD)
53DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
54DOCKER_LABEL_COMMIT_DATE = $(shell git show -s --format=%cd --date=iso-strict HEAD)
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040055
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040056DOCKER_BUILD_ARGS ?= \
David K. Bainbridgee14914d2019-05-24 13:43:05 -070057 ${DOCKER_EXTRA_ARGS} \
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040058 --build-arg org_label_schema_version="${VERSION}" \
59 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
60 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
61 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
62 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
63 --build-arg org_opencord_vcs_dirty="${DOCKER_LABEL_VCS_DIRTY}"
64
65DOCKER_BUILD_ARGS_LOCAL ?= ${DOCKER_BUILD_ARGS} \
Kent Hagermanf4a3aab2019-05-22 14:42:34 -040066 --build-arg LOCAL_PROTOS=${LOCAL_PROTOS}
khenaidoocfee5f42018-07-19 22:47:38 -040067
David K. Bainbridge41835142021-04-01 17:26:12 +000068.PHONY: docker-build local-protos local-lib-go help
69.DEFAULT_GOAL := help
khenaidoocfee5f42018-07-19 22:47:38 -040070
khenaidoocfee5f42018-07-19 22:47:38 -040071
David K. Bainbridge1678e192019-05-17 11:48:29 -070072## Local Development Helpers
David K. Bainbridge41835142021-04-01 17:26:12 +000073local-protos: ## Copies a local version of the voltha-protos dependency into the vendor directory
David K. Bainbridge1678e192019-05-17 11:48:29 -070074ifdef LOCAL_PROTOS
Joey Armstrong393daca2023-07-06 08:47:54 -040075 $(RM) -r vendor/github.com/opencord/voltha-protos/v5/go
khenaidood948f772021-08-11 17:49:24 -040076 mkdir -p vendor/github.com/opencord/voltha-protos/v5/go
77 cp -r ${LOCAL_PROTOS}/go/* vendor/github.com/opencord/voltha-protos/v5/go
David K. Bainbridge1678e192019-05-17 11:48:29 -070078endif
79
Scott Bakercb7c88a2019-10-16 18:32:48 -070080## Local Development Helpers
David K. Bainbridge41835142021-04-01 17:26:12 +000081local-lib-go: ## Copies a local version of the voltha-lib-go dependency into the vendor directory
Scott Bakercb7c88a2019-10-16 18:32:48 -070082ifdef LOCAL_LIB_GO
Joey Armstrong393daca2023-07-06 08:47:54 -040083 $(RM) -r vendor/github.com/opencord/voltha-lib-go/v7/pkg
khenaidood948f772021-08-11 17:49:24 -040084 mkdir -p vendor/github.com/opencord/voltha-lib-go/v7/pkg
85 cp -r ${LOCAL_LIB_GO}/pkg/* vendor/github.com/opencord/voltha-lib-go/v7/pkg/
Scott Bakercb7c88a2019-10-16 18:32:48 -070086endif
87
Joey Armstrong811e9542022-12-25 21:45:27 -050088## -----------------------------------------------------------------------
Matt Jeanneret2e3051a2019-05-11 15:01:46 -040089## Docker targets
Joey Armstrong811e9542022-12-25 21:45:27 -050090## -----------------------------------------------------------------------
David K. Bainbridge41835142021-04-01 17:26:12 +000091build: docker-build ## Alias for 'docker-build'
khenaidoocfee5f42018-07-19 22:47:38 -040092
Joey Armstrong811e9542022-12-25 21:45:27 -050093docker-build-args += --debug
94docker-build-args += --log-level 'debug'
95docker-build-args := $(null)# comment line for debug mode
96
David K. Bainbridge41835142021-04-01 17:26:12 +000097docker-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 -040098
99 $(call banner-enter,$@)
100 $(MAKE) --no-print-directory test-coverage-init
Joey Armstrong811e9542022-12-25 21:45:27 -0500101
102 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 -0700103ifdef BUILD_PROFILED
Andrea Campanella8d4f3e32021-04-13 10:02:03 +0200104# Force target to dev as profile build must be built with dynamic linking
105 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 -0700106endif
David K. Bainbridge3768fde2020-07-29 21:15:35 -0700107ifdef BUILD_RACE
Andrea Campanella8d4f3e32021-04-13 10:02:03 +0200108# Force target to dev as race detection build must be built with dynamic linking
109 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 -0700110endif
Don Newton8eca4622020-02-10 16:44:48 -0500111
Joey Armstrong393daca2023-07-06 08:47:54 -0400112 $(call banner-leave,$@)
113
Joey Armstrong811e9542022-12-25 21:45:27 -0500114## -----------------------------------------------------------------------
Joey Armstrong5f51f2e2023-01-17 17:06:26 -0500115## Intent:
Joey Armstrong811e9542022-12-25 21:45:27 -0500116## -----------------------------------------------------------------------
David K. Bainbridge41835142021-04-01 17:26:12 +0000117docker-push: ## Push the docker images to an external repository
Joey Armstrong393daca2023-07-06 08:47:54 -0400118
119 $(call banner-enter,$@)
120
121docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}
Matteo Scandolo8ed186a2020-05-04 10:52:42 -0700122ifdef BUILD_PROFILED
123 docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}-profile
124endif
David K. Bainbridge3768fde2020-07-29 21:15:35 -0700125ifdef BUILD_RACE
126 docker push ${RWCORE_IMAGENAME}:${DOCKER_TAG}-rd
127endif
David K. Bainbridge41835142021-04-01 17:26:12 +0000128docker-kind-load: ## Load docker images into a KinD cluster
Andrea Campanella7a6cce22019-12-17 14:10:27 -0800129 @if [ "`kind get clusters | grep voltha-$(TYPE)`" = '' ]; then echo "no voltha-$(TYPE) cluster found" && exit 1; fi
130 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 -0800131
Joey Armstrong393daca2023-07-06 08:47:54 -0400132 $(call banner-leave,$@)
133
134## -----------------------------------------------------------------------
Matt Jeanneret2e3051a2019-05-11 15:01:46 -0400135## lint and unit tests
Joey Armstrong393daca2023-07-06 08:47:54 -0400136## -----------------------------------------------------------------------
137
138# [TODO]
139# o Merge lint-* targets with repo:openolt-adapter/Makefile
khenaidood2b6df92018-12-13 16:37:20 -0500140
David K. Bainbridge41835142021-04-01 17:26:12 +0000141lint-dockerfile: ## Perform static analysis on Dockerfile
Kent Hagermandcd4dcc2020-02-25 17:56:17 -0500142 @echo "Running Dockerfile lint check..."
Kent Hagerman812b2572020-01-28 11:57:51 -0500143 @${HADOLINT} $$(find . -name "Dockerfile.*")
David Bainbridge5f3619c2019-07-10 22:51:09 +0000144 @echo "Dockerfile lint check OK"
145
David K. Bainbridge41835142021-04-01 17:26:12 +0000146lint-mod: ## Verify the Go dependencies
Kent Hagermanca4c51e2019-05-02 12:28:55 -0400147 @echo "Running dependency check..."
Kent Hagerman812b2572020-01-28 11:57:51 -0500148 @${GO} mod verify
Scott Bakere4c2a982019-11-21 16:32:03 -0800149 @echo "Dependency check OK. Running vendor check..."
150 @git status > /dev/null
Kent Hagermandcd4dcc2020-02-25 17:56:17 -0500151 @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)
152 @[[ `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 -0500153
Joey Armstrong393daca2023-07-06 08:47:54 -0400154 $(MAKE) mod-update
Joey Armstrong811e9542022-12-25 21:45:27 -0500155
Scott Bakere4c2a982019-11-21 16:32:03 -0800156 @git status > /dev/null
Kent Hagermandcd4dcc2020-02-25 17:56:17 -0500157 @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)
158 @[[ `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 -0800159 @echo "Vendor check OK."
160
Joey Armstrong811e9542022-12-25 21:45:27 -0500161## -----------------------------------------------------------------------
162## -----------------------------------------------------------------------
David K. Bainbridge41835142021-04-01 17:26:12 +0000163lint: lint-mod lint-dockerfile ## Run all lint targets
Kent Hagerman074d0e02019-04-24 17:58:16 -0400164
Joey Armstrong393daca2023-07-06 08:47:54 -0400165include $(MAKEDIR)/analysis/include.mk
Scott Baker18ea5a42019-10-15 16:08:42 -0700166
Joey Armstrong811e9542022-12-25 21:45:27 -0500167## -----------------------------------------------------------------------
168## -----------------------------------------------------------------------
169tests-dir := ./tests/results
170tests-coverage := $(tests-dir)/go-test-coverage
171tests-results := $(tests-dir)/go-test-results
Joey Armstrong393daca2023-07-06 08:47:54 -0400172
173test :: test-coverage ## Run unit tests
174test-coverage : local-lib-go
Kent Hagerman074d0e02019-04-24 17:58:16 -0400175
Joey Armstrong367d76b2023-06-08 17:16:46 -0400176clean :: distclean ## Removes any local filesystem artifacts generated by a build
David K. Bainbridge1678e192019-05-17 11:48:29 -0700177
Joey Armstrong367d76b2023-06-08 17:16:46 -0400178distclean sterile :: ## Removes any local filesystem artifacts generated by a build or test run
Joey Armstrong811e9542022-12-25 21:45:27 -0500179 $(RM) -r ./sca-report
David K. Bainbridge1678e192019-05-17 11:48:29 -0700180
David K. Bainbridge41835142021-04-01 17:26:12 +0000181fmt: ## Formats the soure code to go best practice style
Joey Armstrong393daca2023-07-06 08:47:54 -0400182# gofmt -s -w $(PACKAGES)
Maninder9a1bc0d2020-10-26 11:34:02 +0530183 @go fmt ${PACKAGES}
David K. Bainbridge41835142021-04-01 17:26:12 +0000184
Joey Armstrong393daca2023-07-06 08:47:54 -0400185## -----------------------------------------------------------------------
186## -----------------------------------------------------------------------
187.PHONY: mod-update
188mod-update: mod-tidy mod-vendor
189
190## -----------------------------------------------------------------------
191## -----------------------------------------------------------------------
192.PHONY: mod-tidy
193mod-tidy:
194 $(call banner-enter,$@)
195 ${GO} mod tidy
196 $(call banner-leave,$@)
197
198## -----------------------------------------------------------------------
199## Intent: Refresh vendor/ directory package source
200## -----------------------------------------------------------------------
201## Note: This target is destructive, vendor/ directory will be removed.
202## Todo: Update logic to checkout version on demand VS checkin a static
203## copy of vendor/ sources then augment. Logically removal of
204## files under revision control is strange.
205## -----------------------------------------------------------------------
206.PHONY: mod-vendor
207mod-vendor:
208 $(call banner-enter,$@)
209 @$(if $(LOCAL_FIX_PERMS),chmod 777 .)
210 ${GO} mod vendor
211 @$(if $(LOCAL_FIX_PERMS),chmod 755 .)
212 $(call banner-leave,$@)
213
214## -----------------------------------------------------------------------
215## -----------------------------------------------------------------------
216help ::
217 @echo '[MOD UPDATE]'
218 @echo ' mod-update'
219 @echo ' LOCAL_FIX_PERMS=1 Hack to fix docker filesystem access problems'
220 @echo ' mod-tidy'
221 @echo ' mod-vendor'
Joey Armstrong811e9542022-12-25 21:45:27 -0500222
223## ---------------------------------------------------------------------------
David K. Bainbridge41835142021-04-01 17:26:12 +0000224# 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 -0500225## ---------------------------------------------------------------------------
Joey Armstrong367d76b2023-06-08 17:16:46 -0400226help :: ## Print help for each Makefile target
David K. Bainbridge41835142021-04-01 17:26:12 +0000227 @echo "Usage: make [<target>]"
Joey Armstrong811e9542022-12-25 21:45:27 -0500228 @echo " help This message"
229 @echo " todo Future enhancements"
230 @echo " versions Display version-by-tool used while building"
231 ifdef VERBOSE
David K. Bainbridge41835142021-04-01 17:26:12 +0000232 @echo
Joey Armstrong811e9542022-12-25 21:45:27 -0500233 endif
234 @echo
235 @grep --no-filename '^[[:alpha:]_-]*:.* ##' $(MAKEFILE_LIST) \
236 | sort \
237 | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s : %s\n", $$1, $$2};'
238
Joey Armstrong393daca2023-07-06 08:47:54 -0400239$(if $(DEBUG),$(warning LEAVE))
240
Joey Armstrong811e9542022-12-25 21:45:27 -0500241# [EOF]