From 3c81186daf97cf7011fd2806dfd0bc3b875e92f3 Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Thu, 31 Oct 2024 15:39:37 -0600 Subject: Clean up the build system --- .editorconfig | 4 +- .gitignore | 5 +- 3rd-party/linux-errno.txt | 2 +- 3rd-party/linux-errno.txt.gen | 14 --- CMakeLists.txt | 48 +++++++++- GNUmakefile | 146 ++++++++++++++++++++++++++++++ Makefile | 139 ---------------------------- build-aux/embed-sources.h.gen | 10 +++ build-aux/linux-errno.txt.gen | 17 ++++ build-aux/stack.c.gen | 9 ++ build-aux/stack.py | 184 ++++++++++++++++++++++++++++++++++++++ build-aux/stack.sh | 2 + cmd/sbc_harness/CMakeLists.txt | 16 +--- cmd/srv9p/CMakeLists.txt | 38 +------- cmd/srv9p/static.h.gen | 10 --- lib9p/include/lib9p/linux-errno.h | 4 +- pico_sdk_import.cmake | 1 - stack.py | 184 -------------------------------------- stack.sh | 2 - 19 files changed, 428 insertions(+), 407 deletions(-) delete mode 100755 3rd-party/linux-errno.txt.gen create mode 100644 GNUmakefile delete mode 100644 Makefile create mode 100755 build-aux/embed-sources.h.gen create mode 100755 build-aux/linux-errno.txt.gen create mode 100755 build-aux/stack.c.gen create mode 100644 build-aux/stack.py create mode 100755 build-aux/stack.sh delete mode 100755 cmd/srv9p/static.h.gen delete mode 120000 pico_sdk_import.cmake delete mode 100644 stack.py delete mode 100755 stack.sh diff --git a/.editorconfig b/.editorconfig index b2ffbdd..e44781e 100644 --- a/.editorconfig +++ b/.editorconfig @@ -30,10 +30,10 @@ _mode = markdown [*.9p{,.wip}] _mode = 9p -[{3rd-party/linux-errno.txt.gen,cmd/srv9p/static.h.gen}] +[cmd/srv9p/static.h.gen] _mode = sh -[libusb/include/libusb/tusb_helpers.h.gen] +[{build-aux/linux-errno.txt.gen,libusb/include/libusb/tusb_helpers.h.gen}] _mode = bash [{lib9p/idl.gen,lib9p/include/lib9p/linux-errno.h.gen,stack.py}] diff --git a/.gitignore b/.gitignore index 4e3ccaa..ee30cb2 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,5 @@ *.tmp .mypy_cache/ -build/ - -/.sources.mk +/build/ +/build-aux/sources.mk diff --git a/3rd-party/linux-errno.txt b/3rd-party/linux-errno.txt index d2f6585..839efc2 100644 --- a/3rd-party/linux-errno.txt +++ b/3rd-party/linux-errno.txt @@ -1,4 +1,4 @@ -# ./3rd-party/linux-errno.txt - Generated from linux.git v6.7. DO NOT EDIT! +# 3rd-party/linux-errno.txt - Generated from build-aux/linux-errno.txt.gen and linux.git v6.7. DO NOT EDIT! 1 EPERM Operation not permitted 2 ENOENT No such file or directory 3 ESRCH No such process diff --git a/3rd-party/linux-errno.txt.gen b/3rd-party/linux-errno.txt.gen deleted file mode 100755 index abfbe95..0000000 --- a/3rd-party/linux-errno.txt.gen +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/sh -# 3rd-party/linux-errno.txt.gen - Generate a listing of Linux kernel errnos -# -# Copyright (C) 2024 Luke T. Shumaker -# SPDX-License-Identifier: AGPL-3.0-or-later - -set -e -( - cd "$1" - echo "# ${0%.gen} - Generated from linux.git $(git describe). DO NOT EDIT!" - git ls-files include/uapi/ | grep errno | - xargs sed -nE 's,#\s*define\s+(E[A-Z0-9]+)\s+([0-9]+)\s+/\* (.*) \*/,\2 \1 \3,p' | - sort --numeric-sort -) >"${0%.gen}" diff --git a/CMakeLists.txt b/CMakeLists.txt index 6655b0a..100214c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,11 +6,11 @@ cmake_minimum_required(VERSION 3.30) if (NOT PICO_PLATFORM) - message(FATAL_ERROR "PICO_PLATFORM must be specified") + message(FATAL_ERROR "PICO_PLATFORM must be specified; use the GNUmakefile to set this") endif() set(PICO_SDK_PATH "${CMAKE_SOURCE_DIR}/3rd-party/pico-sdk") -include(pico_sdk_import.cmake) +include("${PICO_SDK_PATH}/external/pico_sdk_import.cmake") project(sbc_harness) @@ -18,6 +18,50 @@ pico_sdk_init() add_compile_options(-Wall -Wextra -Werror) +function(target_embed_sources arg_target arg_hdrname) + set(embed_objs) + foreach(embed_src IN LISTS ARGN) + add_custom_command( + OUTPUT "${embed_src}.obj" + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + COMMAND mkdir -p -- "$" && + ${CMAKE_LINKER} -r -b binary -o "${CMAKE_CURRENT_BINARY_DIR}/${embed_src}.obj" "${embed_src}" + DEPENDS "${embed_src}" + ) + list(APPEND embed_objs "${embed_src}.obj") + endforeach() + set_source_files_properties("${embed_objs}" PROPERTIES + EXTERNAL_OBJECT true + GENERATED true + ) + + set_source_files_properties("${arg_hdrname}" PROPERTIES + GENERATED true + ) + add_custom_command( + OUTPUT "${arg_hdrname}" + COMMAND "${CMAKE_SOURCE_DIR}/build-aux/embed-sources.h.gen" "${embed_objs}" >"${arg_hdrname}" + COMMAND_EXPAND_LISTS + DEPENDS "${embed_objs}" "${CMAKE_SOURCE_DIR}/build-aux/embed-sources.h.gen" + ) + + target_sources("${arg_target}" PRIVATE "${embed_objs}" "${arg_hdrname}") +endfunction() + +function(add_stack_analysis arg_outfile arg_objlib_target) + target_compile_options("${arg_objlib_target}" PUBLIC "-fcallgraph-info=su,da") + set_source_files_properties("${arg_outfile}" PROPERTIES + GENERATED true + ) + add_custom_command( + OUTPUT "${arg_outfile}" + COMMAND "${CMAKE_SOURCE_DIR}/build-aux/stack.c.gen" "$" >"${arg_outfile}" + COMMAND_EXPAND_LISTS + DEPENDS "$" "${CMAKE_SOURCE_DIR}/build-aux/stack.c.gen" + COMMENT "Calculating ${arg_objlib_target} required stack sizes" + ) +endfunction() + add_subdirectory(libcr) add_subdirectory(libcr_ipc) add_subdirectory(libmisc) diff --git a/GNUmakefile b/GNUmakefile new file mode 100644 index 0000000..28f287a --- /dev/null +++ b/GNUmakefile @@ -0,0 +1,146 @@ +# GNUmakefile - Main build script for sbc-harness project +# +# Copyright (C) 2024 Luke T. Shumaker +# 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 += 3rd-party/linux-errno.txt +3rd-party/linux-errno.txt: build-aux/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 + $^ + +generate/files += build-aux/sources.mk +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 + +generate: $(generate/files) +.PHONY: generate + +generate-clean: + rm -f -- $(generate/files) +.PHONY: generate-clean + +# `build` ###################################################################### + +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 generate + $(MAKE) -C $(&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; \ + if ! grep -q ' SPDX-License-Identifier[:] ' $$filename; then \ + echo "$$filename is missing an SPDX-License-Identifier"; r=1; \ + fi; \ + dscname_act=$$($(get_dscname) $$filename); \ + dscname_exp=$$(echo "$$filename" | sed \ + -e 's,.*/config/,,' \ + -e 's,.*include/,,' \ + -e 's,^lib9p/idl/,,' \ + -e 's/\.wip$$//'); \ + if [ "$$dscname_act" != "$$dscname_exp" ] && [ "cmd/$$dscname_act" != "$$dscname_exp" ]; then \ + echo "$$filename self-identifies as $$dscname_act (expected $$dscname_exp)"; 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/% diff --git a/Makefile b/Makefile deleted file mode 100644 index 26ce8ba..0000000 --- a/Makefile +++ /dev/null @@ -1,139 +0,0 @@ -# Makefile - Main build script for sbc-harness project -# -# Copyright (C) 2024 Luke T. Shumaker -# SPDX-License-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 $($@.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; /^