Joey Armstrong | e6cdd8e | 2022-12-29 11:58:15 -0500 | [diff] [blame] | 1 | # -*- 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 Armstrong | e0c1a8d | 2023-04-13 15:16:21 -0400 | [diff] [blame] | 20 | ##-------------------## |
| 21 | ##---] GLOBALS [---## |
| 22 | ##-------------------## |
| 23 | .PHONY: lint-golang lint-golang-sca-all lint-golang-sca-modified |
Joey Armstrong | e6cdd8e | 2022-12-29 11:58:15 -0500 | [diff] [blame] | 24 | |
Joey Armstrong | e0c1a8d | 2023-04-13 15:16:21 -0400 | [diff] [blame] | 25 | GOLANG_FILES ?= $(error GOLANG_FILES= is required) |
Joey Armstrong | e6cdd8e | 2022-12-29 11:58:15 -0500 | [diff] [blame] | 26 | |
Joey Armstrong | e0c1a8d | 2023-04-13 15:16:21 -0400 | [diff] [blame] | 27 | $(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 | ## ----------------------------------------------------------------------- |
| 36 | ifndef NO-LINT-GOLANG |
| 37 | lint-golang-mode := $(if $(have-golang-files),modified,all) |
| 38 | lint : lint-golang-sca-$(lint-golang-mode) |
Joey Armstrong | e6cdd8e | 2022-12-29 11:58:15 -0500 | [diff] [blame] | 39 | |
Joey Armstrong | e0c1a8d | 2023-04-13 15:16:21 -0400 | [diff] [blame] | 40 | lint-golang-all : lint-golang-sca-all |
| 41 | lint-golang-modified : lint-golang-sca-modified |
| 42 | endif# NO-LINT-GOLANG |
| 43 | |
| 44 | ## ----------------------------------------------------------------------- |
| 45 | ## Intent: exhaustive golint syntax checking |
Joey Armstrong | e6cdd8e | 2022-12-29 11:58:15 -0500 | [diff] [blame] | 46 | ## ----------------------------------------------------------------------- |
| 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 | ## ----------------------------------------------------------------------- |
| 59 | lint-golang-sca-xargs := $(null) |
| 60 | lint-golang-sca-xargs += --null#+ # Source paths are null terminated |
| 61 | lint-golang-sca-xargs += --max-args=1#+ # Check one file at a time |
| 62 | lint-golang-sca-xargs += --no-run-if-empty |
| 63 | lint-golang-sca-xargs += --verbose#+ # Display source path to check |
| 64 | |
| 65 | ## [INPLACE-EDITS] make lint-golang-sca FIX=1 |
| 66 | ifdef FIX |
| 67 | lint-golang-sca-args += -w |
| 68 | endif |
| 69 | |
Joey Armstrong | e0c1a8d | 2023-04-13 15:16:21 -0400 | [diff] [blame] | 70 | gofmt-args += -d# Do not output reformatted lines |
| 71 | # gofmt-args += -e# Display all errors (including spurious) |
| 72 | gofmt-args += -s# Try to simplify code |
Joey Armstrong | e6cdd8e | 2022-12-29 11:58:15 -0500 | [diff] [blame] | 73 | |
Joey Armstrong | e0c1a8d | 2023-04-13 15:16:21 -0400 | [diff] [blame] | 74 | ## ----------------------------------------------------------------------- |
| 75 | ## Intent: exhaustive golang syntax checking |
| 76 | ## ----------------------------------------------------------------------- |
| 77 | lint-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 | ## ----------------------------------------------------------------------- |
| 86 | lint-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 |
| 95 | lint-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 |
| 106 | help-verbose += help-golang-sca |
| 107 | help-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 Armstrong | e6cdd8e | 2022-12-29 11:58:15 -0500 | [diff] [blame] | 111 | |
| 112 | $(if $(DEBUG),$(warning LEAVE)) |
| 113 | |
| 114 | # [EOF] |