summaryrefslogtreecommitdiff
path: root/GNUmakefile
blob: 863697389906950d0e095cc6ce2f587ed09d553a (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
140
141
142
143
144
145
146
147
148
149
150
151
152
# GNUmakefile - Main build script for sbc-harness project
#
# Copyright (C) 2024-2025  Luke T. Shumaker <lukeshu@lukeshu.com>
# SPDX-License-Identifier: AGPL-3.0-or-later

# Project configuration ########################################################

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

all: build
.PHONY: all

# Configure GNU Make itself ####################################################

ifeq ($(filter notintermediate,$(.FEATURES)),)
$(error This $(firstword $(MAKEFILE_LIST)) requies GNU Make 4.4 or later for .NOTINTERMEDIATE)
endif

.NOTINTERMEDIATE:
.DELETE_ON_ERROR:

.PHONY: FORCE

# `generate` ###################################################################

generate/files =

generate/files += COPYING.txt
COPYING.txt:
	wget --no-use-server-timestamps -O $@ https://www.gnu.org/licenses/agpl-3.0.txt

generate/files += 3rd-party/COPYING.newlib.txt
3rd-party/COPYING.newlib.txt: /usr/share/licenses/arm-none-eabi-newlib/COPYING.NEWLIB
	cp $< $@

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

generate/files += lib9p/idl/2010-9P2000.L.9p
lib9p/idl/2010-9P2000.L.9p: %: %.gen 3rd-party/linux-errno.txt
	$^ >$@

generate/files += lib9p/core_generated.c lib9p/core_include/lib9p/_core_generated.h
lib9p/core_generated.c lib9p/core_include/lib9p/_core_generated.h &: lib9p/core.gen lib9p/idl/__init__.py lib9p/core_gen lib9p/core_gen/*.py lib9p/idl lib9p/idl/2010-9P2000.L.9p lib9p/idl/*.9p
	$< $(sort $(filter %.9p,$^))

generate/files += lib9p/tests/test_compile.c
lib9p/tests/test_compile.c: %: %.gen lib9p/core_include/lib9p/_core_generated.h
	$^ $@

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
	$^

generate/files += build-aux/sources.mk
ifeq ($(INNER),)
build-aux/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
	@echo '################################################################################'
endif

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

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

# `build` and `check` ##########################################################

platforms := rp2040 host # $(shell sed -nE 's/if *\(PICO_PLATFORM STREQUAL "(.*)"\)/\1/p' cmd/*/CMakeLists.txt)
build_types = Debug Release RelWithDebInfo MinSizeRel

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

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

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

check:
	$(MAKE) -k INNER=t $(foreach t,$(build_types),$(foreach p,$(platforms),build/$p-$t/check))
.PHONY: check

$(foreach t,$(build_types),$(foreach p,$(platforms),build/$p-$t/check)): build/%/check: build/%/build
	+cd $(@D) && ctest --output-on-failure $(if $(filter --jobserver-auth=%,$(MAKEFLAGS)),--parallel)
.PHONY: $(foreach t,$(build_types),$(foreach p,$(platforms),build/$p-$t/check))

# `lint` and `format` ##########################################################

-include build-aux/sources.mk
sources_all := $(foreach v,$(filter sources_%,$(.VARIABLES)),$($v))

build-aux/venv: build-aux/requirements.txt
	python3 -m venv $@
	$@/bin/pip install -r $<
	touch --no-create $@

# `lint` ###############################
# generic ##########
lint:
	$(MAKE) -k INNER=t $(patsubst sources_%,lint/%,$(filter sources_%,$(.VARIABLES)))
# Only lint binaries if the build scripts pass lint.
	$(MAKE) INNER=t lint/bin
lint/bin: build build-aux/lint-bin
	./build-aux/lint-bin $(foreach t,$(build_types),build/rp2040-$t/cmd/sbc_harness/sbc_harness.elf)
lint/all: lint/%: build-aux/lint-generic build-aux/get-dscname
	./build-aux/lint-generic '%s\n' $(sources_$*)
lint/unknown: lint/%: build-aux/lint-unknown
	./build-aux/lint-unknown $(sources_$*)
.PHONY: lint lint/%
# specific #########
lint/c: lint/%: build-aux/lint-h build-aux/get-dscname
	./build-aux/lint-h $(filter %.h,$(sources_$*))
lint/sh lint/bash: lint/%:
	shellcheck $(sources_$*)
	shfmt --diff --case-indent --simplify $(sources_$*)
lint/python3: lint/%: build-aux/venv
	./build-aux/venv/bin/mypy --strict --scripts-are-modules $(sources_$*)
	./build-aux/venv/bin/black --check $(sources_$*)
	./build-aux/venv/bin/isort --check $(sources_$*)
	./build-aux/venv/bin/pylint $(sources_$*)
	! grep -nh 'SPECIAL$$' -- lib9p/core.gen lib9p/core_gen/*.py
	./build-aux/venv/bin/pytest $(foreach f,$(sources_python3),$(if $(filter test_%.py,$(notdir $f)),$f))
lint/make lint/cmake lint/gitignore lint/ini lint/9p-idl lint/9p-log lint/markdown lint/pip lint/man-cat: lint/%:
	@: TODO: Write/adopt linters for these file types

# `format` #############################
# generic ##########
format:
	$(MAKE) -k INNER=t $(patsubst sources_%,format/%,$(filter-out sources_all sources_unknown,$(filter sources_%,$(.VARIABLES))))
.PHONY: format format/%
# specific #########
format/c: format/%:
	@: TODO: Adopt a C code-formatter
format/sh format/bash: format/%:
	shfmt --write --case-indent --simplify $(sources_$*)
format/python3: format/%: ./build-aux/venv
	./build-aux/venv/bin/black $(sources_$*)
	./build-aux/venv/bin/isort $(sources_$*)
format/make format/cmake format/gitignore format/ini format/9p-idl format/9p-log format/markdown format/pip format/man-cat: format/%:
	@: TODO: Write/adopt formatters for these file types