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