1+ # find all packages without any dependency to underlying tiny-go packages, e.g. "machine" or "device/arm"
2+ # also exclude anything found in build output directory and "image" folder
3+ BUILD_TAGS_CHECK := m5stack_core2,microbit,xiao_ble
4+ ALL_WITHOUT_MACHINE := $(shell go list -e -tags $(BUILD_TAGS_CHECK) -f '{{.Dir}},{{.Deps}}' ./... | awk -F, '$$1 !~ /build/ && $$1 !~ /image/ && $$2 !~ /machine/ && $$2 !~ /device\/arm/ {print $$1}')
5+ # create a list of sub-dirs, relative to the root
6+ ALL_DIRS_TO_CHECK := $(subst $(CURDIR),.,$(ALL_WITHOUT_MACHINE))
7+
8+ BUILD_DIR := build
9+ LINTER_DIR := $(BUILD_DIR)/linter/drivers
10+
11+ .PHONY: clean fmt-check smoke-test unit-test test check fmt_check fmt_fix $(ALL_DIRS_TO_CHECK)
12+
13+ clean:
14+ @rm -rf $(BUILD_DIR)
15+
16+ FMT_PATHS = ./
17+
18+ fmt-check:
19+ @unformatted=$$(gofmt -l $(FMT_PATHS)); [ -z "$$unformatted" ] && exit 0; echo "Unformatted:"; for fn in $$unformatted; do echo " $$fn"; done; exit 1
20+
21+ XTENSA ?= 1
22+ smoke-test:
23+ @mkdir -p $(BUILD_DIR)
24+ @go run ./smoketest.go -xtensa=$(XTENSA) smoketest.sh
25+
26+
27+ # rwildcard is a recursive version of $(wildcard)
28+ # https://blog.jgc.org/2011/07/gnu-make-recursive-wildcard-function.html
29+ rwildcard=$(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subst *,%,$2),$d))
30+ # Recursively find all *_test.go files from cwd & reduce to unique dir names
31+ HAS_TESTS = $(sort $(dir $(call rwildcard,,*_test.go)))
32+ # Exclude anything we explicitly don't want to test for whatever reason
33+ EXCLUDE_TESTS = image waveshare-epd/epd2in66b
34+ TESTS = $(filter-out $(addsuffix /%,$(EXCLUDE_TESTS)),$(HAS_TESTS))
35+
36+ unit-test:
37+ @go test -v $(addprefix ./,$(TESTS))
38+
39+ test: clean fmt-check unit-test smoke-test
40+
41+ fmt_quick_check:
42+ @# a very fast check before build, but depends on accessibility of all imports
43+ @# switch off the "stdmethods" analyzer is needed due to finding:
44+ @# at24cx/at24cx.go:57:18: method WriteByte(eepromAddress uint16, value uint8) error should have signature WriteByte(byte) error
45+ @# at24cx/at24cx.go:67:18: method ReadByte(eepromAddress uint16) (uint8, error) should have signature ReadByte() (byte, error)
46+ @# switch off the "shift" analyzer is needed due to finding:
47+ @#tmc5160/registers.go:1939:16: m.CUR_A (16 bits) too small for shift of 16
48+ @#tmc5160/registers.go:1996:16: m.X3 (8 bits) too small for shift of 27
49+ @#tmc5160/registers.go:1996:27: m.X2 (8 bits) too small for shift of 24
50+ @#tmc5160/registers.go:1996:38: m.X1 (8 bits) too small for shift of 21
51+ @#tmc5160/registers.go:1996:49: m.W3 (8 bits) too small for shift of 18
52+ @#tmc5160/registers.go:1996:60: m.W2 (8 bits) too small for shift of 16
53+ @#tmc5160/registers.go:1996:71: m.W1 (8 bits) too small for shift of 14
54+ @#tmc5160/registers.go:1996:82: m.W0 (8 bits) too small for shift of 12
55+ go vet -tags $(BUILD_TAGS_CHECK) -stdmethods=false -shift=false $(ALL_DIRS_TO_CHECK)
56+
57+ fmt_check:
58+ @# a complete format check, but depends on accessibility of all imports
59+ @$(MAKE) linter_workspace
60+ cd $(LINTER_DIR) && golangci-lint -v run
61+
62+ fmt_fix:
63+ @# an automatic reformat and complete format check, but depends on accessibility of all imports
64+ @#TODO: activate when ready
65+ @#gofumpt -l -w $(ALL_DIRS_TO_CHECK) # TODO: test for all files, not only for filtered ones
66+ golangci-lint -v run $(ALL_DIRS_TO_CHECK) --fix
67+
68+ linter_workspace:
69+ @# creates the workspace for running the linter in one step, which is especially used for CI workflow, see ".github/workflows/golangci-lint.yml"
70+ @$(MAKE) FUNC=COPY $(filter-out .,$(ALL_DIRS_TO_CHECK))
71+ @# additionally copy go files from root for checking
72+ @cp -f $(CURDIR)/*.go $(LINTER_DIR)
73+
74+ $(ALL_DIRS_TO_CHECK):
75+ ifeq ($(FUNC),COPY)
76+ @mkdir -p $(LINTER_DIR)/$@
77+ @cp -f $(addprefix ./,$@)/*.* $(LINTER_DIR)/$@
78+ else
79+ @echo $@
80+ endif
0 commit comments