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