summaryrefslogtreecommitdiff
path: root/Makefile
blob: bd4f9ccd11b679024de02ae89e6a2afb1a8e03bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# Makefile - Main build script for sbc-harness project
#
# Copyright (C) 2024  Luke T. Shumaker <lukeshu@lukeshu.com>
# SPDX-Licence-Identifier: AGPL-3.0-or-later

linux.git = $(HOME)/src/github.com/torvalds/linux

all: build
.PHONY: all

.NOTINTERMEDIATE:
.DELETE_ON_ERROR:

################################################################################

generate/files =

generate/files += 3rd-party/linux-errno.txt
3rd-party/linux-errno.txt: %: %.gen
	./$< $(linux.git)

generate/files += lib9p/include/lib9p/linux-errno.h
lib9p/include/lib9p/linux-errno.h: %: %.gen 3rd-party/linux-errno.txt
	./$^ >$@

generate/files += lib9p/9p.generated.c lib9p/include/lib9p/9p.generated.h
lib9p/9p.generated.c lib9p/include/lib9p/9p.generated.h &: lib9p/idl.gen lib9p/idl/*.9p
	./$^

generate/files += libusb/include/libusb/tusb_helpers.h 3rd-party/MS-LCID.pdf 3rd-party/MS-LCID.txt
libusb/include/libusb/tusb_helpers.h 3rd-party/MS-LCID.pdf 3rd-party/MS-LCID.txt &: libusb/include/libusb/tusb_helpers.h.gen
	./$^

################################################################################

platforms := $(shell sed -nE 's/if *\(PICO_PLATFORM STREQUAL "(.*)"\)/\1/p' cmd/*/CMakeLists.txt)

build: $(foreach p,$(platforms),build/$p/build)
.PHONY: build

$(foreach p,$(platforms),build/$p/Makefile): build/%/Makefile:
	mkdir -p $(@D) && cd $(@D) && cmake -DCMAKE_BUILD_TYPE=Debug -DPICO_PLATFORM=$* ../..

$(foreach p,$(platforms),build/$p/build): build/%/build: build/%/Makefile
	$(MAKE) -C $(<D)
.PHONY: $(foreach p,$(platforms),build/$p/build)

generate: $(generate/files)
.PHONY: generate

generate-clean:
	rm -f -- $(generate/files)
.PHONY: generate-clean

################################################################################

.PHONY: FORCE

# List of sources, by type.
.sources.mk: $(if $(wildcard .git),FORCE)
	git ls-files | grep -vFx $(foreach f,$(generate/files),-e $f) \
	| sed 's,^,$(CURDIR)/,' | xargs editorconfig                  \
	| sed -nE -e 's,\[$(CURDIR)/(.*)\],\1,p' -e 's/^_mode=//p'    \
	| sed -E '{N;s/(.*)\n(.*)/sources_\2 += \1/;}'                \
	| sort                                                        \
	>$@.tmp
	if ! cmp -s $@.tmp $@; then mv $@.tmp $@; fi
-include .sources.mk
sources_all := $(foreach v,$(filter sources_%,$(.VARIABLES)),$($v))

# lint #############

get_dscname = sed -n '1,3{ /^\#!/d; /^<!--$$/d; /-\*- .* -\*-/d; s,[/*\# ]*,,; s/ - .*//;p; q; }'

lint:
	$(MAKE) -k $(patsubst sources_%,lint/%,$(filter sources_%,$(.VARIABLES)))
lint/sh lint/bash: lint/%:
	shellcheck $(sources_$*)
lint/python3: lint/%:
	mypy --strict --scripts-are-modules $(sources_$*)
	black --check $(sources_$*)
	isort --check $(sources_$*)
lint/c: lint/%:
	@for filename in $(filter %.h,$(sources_$*)); do \
	    dscname=$$($(get_dscname) $$filename); \
	    guard=$${dscname//'/'/'_'}; \
	    guard=$${guard//'.'/'_'}; \
	    guard="_$${guard^^}_"; \
	    if ! { grep -Fxq "#ifndef $${guard}"      "$$filename" && \
	           grep -Fxq "#define $${guard}"      "$$filename" && \
	           grep -Fxq "#endif /* $${guard} */" "$$filename"; }; then \
	        echo "$$filename does not have $${guard} guard"; r=1; \
	    fi; \
	done
lint/make lint/cmake lint/gitignore lint/ini lint/9p lint/markdown: lint/%:
	@:
lint/unknown: lint/%:
	@printf "%s: cannot lint unknown file type\n" $(sources_$*) >&2
lint/all: lint/%:
	$(eval export sources_$*)
	@find $$(printf '%s\n' $${sources_$*} | grep -vE '^cmd/[^/]+/static/') \
	    -maxdepth 0 -type f | \
	{ r=0; while read -r filename; do \
	    if ! grep -q 'Copyright (C) 2024  Luke T. Shumaker' $$filename; then \
	        echo "$$filename is missing a copyright statement"; r=1; \
	    fi; \
	    dscname=$$($(get_dscname) $$filename); \
	    filename_alt1=$$(echo "$$filename" | sed \
	        -e 's,^cmd/,,' \
	        -e 's,.*/config/,,' \
	        -e 's,.*/include/,,' \
	        -e 's,^lib9p/idl/,,' \
	        -e 's/\.wip$$//'); \
	    filename_alt2=$$(echo "$$filename_alt1" | sed \
	        -e 's,^sbc_harness/hw/,hw/,'); \
	    if ! { [ "$$dscname" == "$$filename" ] || [ "$$dscname" == "$$filename_alt1" ] || [ "$$dscname" == "$$filename_alt2" ]; }; then \
	        echo "$$filename self-identifies as $$dscname"; r=1; \
	    fi; \
	    if grep -n --color=auto "$$(printf '\\S\t')" $$filename; then \
	        echo "$$filename uses tabs for alignment"; r=1; \
	    fi; \
	done; exit $$r; }
.PHONY: lint lint/%

# format ###########
format:
	$(MAKE) -k $(patsubst sources_%,format/%,$(filter-out sources_all,$(filter sources_%,$(.VARIABLES))))
format/python3: format/%:
	black $(sources_$*)
	isort $(sources_$*)
format/sh format/bash: format/%
	@:
format/c: format/%:
	@: TODO
format/make format/cmake format/gitignore format/ini format/9p format/markdown: format/%:
	@:
format/unknown: format/%:
	@:
.PHONY: format format/%