blob: 20af5936d9cf533822ddd0b2959698309c344e3e [file] [log] [blame]
Joey Armstronge6cdd8e2022-12-29 11:58:15 -05001# -*- makefile -*-
2# -----------------------------------------------------------------------
3# Copyright 2017-2023 Open Networking Foundation
4#
5# 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.
16# -----------------------------------------------------------------------
17
18$(if $(DEBUG),$(warning ENTER))
19
Joey Armstronge0c1a8d2023-04-13 15:16:21 -040020##-------------------##
21##---] GLOBALS [---##
22##-------------------##
23.PHONY: lint-golang lint-golang-sca-all lint-golang-sca-modified
Joey Armstronge6cdd8e2022-12-29 11:58:15 -050024
Joey Armstronge0c1a8d2023-04-13 15:16:21 -040025GOLANG_FILES ?= $(error GOLANG_FILES= is required)
Joey Armstronge6cdd8e2022-12-29 11:58:15 -050026
Joey Armstronge0c1a8d2023-04-13 15:16:21 -040027$(info loading)
28## -----------------------------------------------------------------------
29## Intent: Use the golang command to perform syntax checking.
30## o If UNSTABLE=1 syntax check all sources
31## o else only check sources modified by the developer.
32## Usage:
33## % make lint UNSTABLE=1
34## % make lint-golang-all
35## -----------------------------------------------------------------------
36ifndef NO-LINT-GOLANG
37 lint-golang-mode := $(if $(have-golang-files),modified,all)
38 lint : lint-golang-sca-$(lint-golang-mode)
Joey Armstronge6cdd8e2022-12-29 11:58:15 -050039
Joey Armstronge0c1a8d2023-04-13 15:16:21 -040040 lint-golang-all : lint-golang-sca-all
41 lint-golang-modified : lint-golang-sca-modified
42endif# NO-LINT-GOLANG
43
44## -----------------------------------------------------------------------
45## Intent: exhaustive golint syntax checking
Joey Armstronge6cdd8e2022-12-29 11:58:15 -050046## -----------------------------------------------------------------------
47## Intent: Run goformat on files on sandbox files.
48## 1) find . -name '*.go' -print0
49## - gather all *.go sources (-name '*.go')
50## - pass as a list of null terminated items (-print0)
51## 2) xargs --null --max-args=[n] --no-run-if-empty gofmt -d
52## - Iterate over the list (xargs --null)
53## - process one item per line (--max-args=1)
54## - display filename-to-check (--verbose)
55## - display content when diffs are detected:
56## gofmt -d
57## gofmt -d -s
58## -----------------------------------------------------------------------
59lint-golang-sca-xargs := $(null)
60lint-golang-sca-xargs += --null#+ # Source paths are null terminated
61lint-golang-sca-xargs += --max-args=1#+ # Check one file at a time
62lint-golang-sca-xargs += --no-run-if-empty
63lint-golang-sca-xargs += --verbose#+ # Display source path to check
64
65## [INPLACE-EDITS] make lint-golang-sca FIX=1
66ifdef FIX
67 lint-golang-sca-args += -w
68endif
69
Joey Armstronge0c1a8d2023-04-13 15:16:21 -040070gofmt-args += -d# Do not output reformatted lines
71# gofmt-args += -e# Display all errors (including spurious)
72gofmt-args += -s# Try to simplify code
Joey Armstronge6cdd8e2022-12-29 11:58:15 -050073
Joey Armstronge0c1a8d2023-04-13 15:16:21 -040074## -----------------------------------------------------------------------
75## Intent: exhaustive golang syntax checking
76## -----------------------------------------------------------------------
77lint-golang-sca-all: $(venv-activate-script)
78# $(MAKE) --no-print-directory lint-sca-install
79
80 find . -name '*.go' -print0 \
81 | xargs $(lint-golang-sca-xargs) gofmt $(gofmt-args)
82
83## -----------------------------------------------------------------------
84## Intent: check deps for format and cleanliness
85## -----------------------------------------------------------------------
86lint-golang-sca-modified: lint-golang-all
87# $(MAKE) --no-print-directory lint-golang-sca-install
88
89 gofmt $(gofmt-args) $(GOLANG_FILES)
90
91## -----------------------------------------------------------------------
92## Intent: Tool installation as needed
93## -----------------------------------------------------------------------
94.PHONY: lint-golang-sca-install
95lint-golang-sca-install:
96 @echo
97 @echo "** -----------------------------------------------------------------------"
98 @echo "** golang syntax checking"
99 @echo "** -----------------------------------------------------------------------"
100 @echo
101
102## -----------------------------------------------------------------------
103## Intent: Display target help
104## -----------------------------------------------------------------------
105.PHONY: help-golang-sca
106help-verbose += help-golang-sca
107help-golang-sca :
108 @echo " % $(MAKE) lint-golang GOLANG_FILES=..."
109 @echo ' lint-golang-sca-modified golang checking: only modified'
110 @echo ' lint-golang-sca-all golang checking: exhaustive'
Joey Armstronge6cdd8e2022-12-29 11:58:15 -0500111
112$(if $(DEBUG),$(warning LEAVE))
113
114# [EOF]