Joey Armstrong | 7614e22 | 2023-09-28 17:18:33 -0400 | [diff] [blame] | 1 | # -*- makefile -*- |
| 2 | # ----------------------------------------------------------------------- |
Joey Armstrong | dc04c93 | 2024-04-01 12:14:21 -0400 | [diff] [blame] | 3 | # Copyright 2022-2024 Open Networking Foundation Contributors |
Joey Armstrong | 7614e22 | 2023-09-28 17:18:33 -0400 | [diff] [blame] | 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 | # |
Joey Armstrong | dc04c93 | 2024-04-01 12:14:21 -0400 | [diff] [blame] | 9 | # http:#www.apache.org/licenses/LICENSE-2.0 |
Joey Armstrong | 7614e22 | 2023-09-28 17:18:33 -0400 | [diff] [blame] | 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 | # ----------------------------------------------------------------------- |
Joey Armstrong | dc04c93 | 2024-04-01 12:14:21 -0400 | [diff] [blame] | 17 | # SPDX-FileCopyrightText: 2022-2024 Open Networking Foundation Contributors |
| 18 | # SPDX-License-Identifier: Apache-2.0 |
| 19 | # ----------------------------------------------------------------------- |
| 20 | # Intent: |
| 21 | # ----------------------------------------------------------------------- |
Joey Armstrong | 7614e22 | 2023-09-28 17:18:33 -0400 | [diff] [blame] | 22 | |
| 23 | $(if $(DEBUG),$(warning ENTER)) |
| 24 | |
| 25 | ##-------------------## |
| 26 | ##---] GLOBALS [---## |
| 27 | ##-------------------## |
| 28 | .PHONY: lint-shell lint-shell-all lint-shell-mod lint-shell-src |
| 29 | |
| 30 | # Gather sources to check |
| 31 | shell-check-find := find . |
Joey Armstrong | ccab2cf | 2024-04-06 18:00:59 -0400 | [diff] [blame] | 32 | shell-check-find += $(foreach dir,$(onf-excl-dirs),-not -path './$(dir)/*') |
| 33 | shell-check-find += -not -path './vendor/*' |
| 34 | shell-check-find += -a \( -name '*.sh' -o -name '*.bash' \) |
Joey Armstrong | 7614e22 | 2023-09-28 17:18:33 -0400 | [diff] [blame] | 35 | shell-check-find += -type f -print0 |
| 36 | |
| 37 | # shell-check := $(env-clean) shellcheck |
| 38 | shell-check := shellcheck |
| 39 | |
| 40 | shell-check-args += --check-sourced |
| 41 | |
| 42 | ##------------------## |
| 43 | ##---] MACROS [---## |
| 44 | ##------------------## |
| 45 | |
| 46 | ## ----------------------------------------------------------------------- |
| 47 | ## Intent: Derive a list of dependencies when make builds a target by name |
| 48 | ## ----------------------------------------------------------------------- |
| 49 | ## Given: |
| 50 | ## target - Makefile target name to check if evaluating |
| 51 | ## listref - An indirect make variable containing a list of file paths |
| 52 | ## |
| 53 | ## Return: |
| 54 | ## target - Variable assigned a list of target dependencies |
| 55 | ## ----------------------------------------------------------------------- |
| 56 | ## Usage: $(gen-lint-shell-deps,lint-shell-mod,BY_GIT) |
| 57 | ## lint-shell-mod := foo bar tans fans |
| 58 | ## lint-shell-mod : $(lint-shell-mod) |
| 59 | ## $(lint-shell-mod): |
| 60 | ## perform-action-on-one-dependency $@ |
| 61 | ## ----------------------------------------------------------------------- |
| 62 | gen-lint-shell-deps =\ |
| 63 | $(strip \ |
| 64 | $(foreach target,$(1),\ |
| 65 | $(if $(findstring $(target),$(MAKECMDGOALS)),\ |
| 66 | $(foreach listref,$(2),\ |
| 67 | $(eval $(target) := $(null))\ |
| 68 | $(foreach path,$($(listref)),\ |
| 69 | $(if $(DEBUG),$(info ** $$(eval $(target) += $(addprefix $(target)--,$(path)))))\ |
| 70 | $(eval $(target) += $(addprefix $(target)--,$(path)))\ |
| 71 | )\ |
| 72 | )\ |
| 73 | )\ |
| 74 | )\ |
| 75 | )# gen-lint-shell-deps() |
| 76 | |
| 77 | ##-------------------## |
| 78 | ##---] TARGETS [---## |
| 79 | ##-------------------## |
| 80 | ifndef NO-LINT-SHELL # enabled(?) |
| 81 | lint : lint-shell |
| 82 | endif |
| 83 | |
| 84 | ## ----------------------------------------------------------------------- |
| 85 | ## Conditional target: lint all source or source by name |
| 86 | ## ----------------------------------------------------------------------- |
| 87 | ifdef SHELL_SRC |
| 88 | lint-shell : lint-shell-src |
| 89 | else |
| 90 | lint-shell : lint-shell-all |
| 91 | endif |
| 92 | |
| 93 | ## ----------------------------------------------------------------------- |
| 94 | ## Intent: Perform a lint check on available sources |
| 95 | ## 1) Display shellcheck version |
| 96 | ## 2) Invoke shellcheck on all files with an *.sh extension |
| 97 | ## ----------------------------------------------------------------------- |
| 98 | lint-shell-all: lint-shellcheck-cmd-version |
| 99 | |
| 100 | # $(call banner-enter,(Target $@)) |
| 101 | $(env-clean) $(shell-check-find) \ |
| 102 | | $(xargs-n1) $(shell-check) $(shell-check-args) |
| 103 | # $(call banner-leave,(Target $@)) |
| 104 | |
| 105 | ## ----------------------------------------------------------------------- |
| 106 | ## Intent: Perform lint check on a named list of files passed in |
| 107 | ## 1) Display shellcheck version |
| 108 | ## 2) Iterate and run shellcheck on each given file path. |
| 109 | ## ----------------------------------------------------------------------- |
| 110 | SHELL_SRC ?= $(error $(MAKE) $@ SHELL_SRC= is required) |
| 111 | $(call gen-lint-shell-deps,lint-shell-src,SHELL_SRC) |
| 112 | |
| 113 | lint-shell-src: \ |
| 114 | lint-shellcheck-cmd-version \ |
| 115 | $(lint-shell-src) |
| 116 | |
| 117 | ## ----------------------------------------------------------------------- |
| 118 | ## Intent: Perform lint check on locally modified files |
| 119 | ## 1) Display shellcheck version |
| 120 | ## 2) Gather a list of locally modified files (~git status) |
| 121 | ## 3) Iterate and run shellcheck on each gathered file. |
| 122 | ## ----------------------------------------------------------------------- |
| 123 | BY_GIT = $(shell git diff --name-only HEAD | grep -e '\.sh') |
| 124 | $(call gen-lint-shell-deps,lint-shell-mod,BY_GIT) |
| 125 | |
| 126 | lint-shell-mod :\ |
| 127 | lint-shellcheck-cmd-version \ |
| 128 | $(lint-shell-mod) |
| 129 | |
| 130 | ## ----------------------------------------------------------------------- |
| 131 | ## Intent: Workhorse target: |
| 132 | ## 1) gen-lint-shell-deps used to create target specific dependency lists. |
| 133 | ## 2) These dependencies are used to invoke shellcheck against a single |
| 134 | ## path from the list. |
| 135 | ## 3) lint-shell-mod : $(lint-shell-mod) |
| 136 | ## Named targets are dependent on a list of file to check. |
| 137 | ## 4) make lint-shell-mod |
| 138 | ## Building a named target will force iteration, running shellcheck |
| 139 | ## against each file path in the dependency list. |
| 140 | ## ----------------------------------------------------------------------- |
| 141 | .PHONY: $(lint-shell-mod) |
| 142 | .PHONY: $(lint-shell-src) |
| 143 | $(lint-shell-mod) $(lint-shell-src): |
| 144 | $(env-clean) $(shell-check) $(shell-check-args) $(lastword $(subst --,$(space),$@)) |
| 145 | |
| 146 | $(if $(DEBUG),$(warning LEAVE)) |
| 147 | |
| 148 | # [TODO] |
| 149 | # ----------------------------------------------------------------------- |
| 150 | # Implement exclusion lists by appling $(filter-out $(lint-shell-excl)) |
| 151 | # against the list of files passed to gen-lint-shell-deps. Not definining |
| 152 | # a target dependency will inhibit runnig shellcheck on a file. |
| 153 | |
| 154 | # Ignore vendor/ scripts but they really should be lintable |
| 155 | # lint-shell-excl += 'vendor' |
| 156 | # ----------------------------------------------------------------------- |
| 157 | |
| 158 | # [EOF] |