aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoraqua <aqua@iserlohn-fortress.net>2023-02-12 17:28:33 +0200
committeraqua <aqua@iserlohn-fortress.net>2023-02-12 17:28:33 +0200
commit878cdffb3f69c780cb55f1fbb54747d9066a385a (patch)
treecd8bb7352bdb6d14ed20f5aa2d3d0e649f0f0427
parentMake shadowing variables an error (diff)
downloadkernel-878cdffb3f69c780cb55f1fbb54747d9066a385a.tar.xz
Generate dependency files for source code
Use the compiler to generate dependency files for all C, C++ and .S assembly source code files. These are included in the makefiles where the files are used.
-rw-r--r--.gitignore1
-rw-r--r--Makefile6
-rw-r--r--Makefile.all5
-rw-r--r--Makefile.config2
-rw-r--r--doxygen.config2
-rw-r--r--i686/toolchain.mk2
-rw-r--r--lib/Makefile4
-rw-r--r--lib/stdlib/memset.c2
-rw-r--r--lib/tst/linked_list_allocator.cc4
-rw-r--r--rules.mk93
-rw-r--r--src/Makefile2
-rw-r--r--src/task.h2
12 files changed, 88 insertions, 37 deletions
diff --git a/.gitignore b/.gitignore
index 2bc2d47..19429b0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,6 @@
lib/musl*
build*
+*.d
*.o
*.a
*.elf
diff --git a/Makefile b/Makefile
index 0e0474f..33b5f76 100644
--- a/Makefile
+++ b/Makefile
@@ -30,8 +30,8 @@ info: Makefile.config
run: Makefile.config
@${MAKE} -f Makefile.all $@
-doc: FORCE
- doxygen doxygen.config
+doc: doxygen.config
+ doxygen $<
clean:
@${MAKE} -C lib clean
@@ -40,8 +40,6 @@ clean:
@${MAKE} -C src clean
test:
- @echo " -> Looking for inline assembly in src/ and lib/"
- @grep -rn 'asm' devices src lib
@echo " -> Running tests in src"
@make -C src test > /dev/null
@echo " -> Running tests in lib"
diff --git a/Makefile.all b/Makefile.all
index efd7386..ae7bbba 100644
--- a/Makefile.all
+++ b/Makefile.all
@@ -7,8 +7,8 @@ include Makefile.config
LDFLAGS += -T ${ARCH}/linker.ld
-all: glitch.elf
- @echo "built $^"
+TARGETS += glitch.elf
+include rules.mk
.PHONY: run info
run: glitch.iso
@@ -56,4 +56,3 @@ devices/devs.a: FORCE
src/kernel.a: FORCE
@${MAKE} -C src kernel.a
-include rules.mk
diff --git a/Makefile.config b/Makefile.config
index 5252c0d..b5a5ab3 100644
--- a/Makefile.config
+++ b/Makefile.config
@@ -56,7 +56,7 @@ STRIP := i686-elf-strip
# define compiler and flags for test targets
TEST_CXX := g++
-TEST_CXXFLAGS := -Wall -Wextra -Wpedantic -Werror=shadow -Werror=conversion -g -Og \
+TEST_CXXFLAGS := -Wall -Wextra -Wpedantic -Werror=shadow -Wconversion -g -Og \
$(shell pkg-config --cflags --libs gtest gtest_main gmock)
# emulator name and flags
diff --git a/doxygen.config b/doxygen.config
index d5b45f6..0d6ed08 100644
--- a/doxygen.config
+++ b/doxygen.config
@@ -908,7 +908,7 @@ WARN_LOGFILE =
# spaces. See also FILE_PATTERNS and EXTENSION_MAPPING
# Note: If this tag is empty the current directory is searched.
-INPUT = src lib i686 devices README.md
+INPUT = README.md src lib i686 devices
# This tag can be used to specify the character encoding of the source files
# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses
diff --git a/i686/toolchain.mk b/i686/toolchain.mk
index 8294660..93c5f6d 100644
--- a/i686/toolchain.mk
+++ b/i686/toolchain.mk
@@ -28,7 +28,7 @@ STRIP := i686-elf-strip
# define compiler and flags for test targets
TEST_CXX := g++
-TEST_CXXFLAGS := -Wall -Wextra -Wpedantic -Werror=shadow -Werror=conversion -g -Og \
+TEST_CXXFLAGS := -Wall -Wextra -Wpedantic -Werror=shadow -Wconversion -g -Og \
$(shell pkg-config --cflags --libs gtest gtest_main gmock)
# emulator name and flags
diff --git a/lib/Makefile b/lib/Makefile
index 385e5ba..cace677 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -6,10 +6,10 @@ libk.SRCS = \
stdlib/memcpy.c stdlib/memset.c stdlib/linked_list_allocator.c \
string/itoa.c
-TESTS += tst/test_endian_little tst/test_mem tst/test_string tst/test_linked_list_allocator
+TESTS += tst/endian_little tst/mem tst/string tst/linked_list_allocator
blake2.SRCS = blake2/blake2s.c
-TESTS += tst/test_blake2s_selftest
+TESTS += tst/blake2s_selftest
include ../rules.mk
diff --git a/lib/stdlib/memset.c b/lib/stdlib/memset.c
index ccd46dd..a16bd05 100644
--- a/lib/stdlib/memset.c
+++ b/lib/stdlib/memset.c
@@ -2,6 +2,6 @@ void *
memset(void *s, int c, long unsigned n)
{
char *pDest = (char *)s;
- for (unsigned i = 0; i < n; ++i) pDest[i] = c;
+ for (unsigned i = 0; i < n; ++i) pDest[i] = (char)c;
return s;
}
diff --git a/lib/tst/linked_list_allocator.cc b/lib/tst/linked_list_allocator.cc
index f3af8e2..280a6d5 100644
--- a/lib/tst/linked_list_allocator.cc
+++ b/lib/tst/linked_list_allocator.cc
@@ -6,9 +6,9 @@ namespace libk {
#include "../stdlib/linked_list_allocator.c"
std::ostream &
-operator<<(std::ostream &os, const Chunk &begin)
+operator<<(std::ostream &os, const Chunk &b)
{
- for (const Chunk *iter = &begin; iter != nullptr; iter = iter->next) {
+ for (const Chunk *iter = &b; iter != nullptr; iter = iter->next) {
os << iter << " used=" << iter->used << " size=" << std::setw(4) << iter->size << " next=" << iter->next
<< std::endl;
}
diff --git a/rules.mk b/rules.mk
index bfaf2e1..a3a2b52 100644
--- a/rules.mk
+++ b/rules.mk
@@ -1,36 +1,88 @@
-# for each target.SRCS, generate target.OBJS, target.a and add target.OBJS to all
+# generate objects from sources
+# args 1: target.SRCS
+# returns: target.OBJS
+objects = $(foreach f,$($1),$(addsuffix _${ARCH}.o,$(basename $f)))
+
+# generate depends from .S .c .cpp sources
+# args 1: target.SRCS
+# returns: target.DEPS
+depends = $(foreach f,$(filter %.S %.c %.cpp,$($1)),$(addsuffix .d,$(basename $f)))
+
+# default target
+all: targets
+ @echo " -> Built all in $(shell pwd | xargs basename)"
+
+# for each target.SRCS
+# - generate target.OBJS
+# - generate target.DEPS and include them
+# - generate target.a that depends on target.OBJS
+# - add target.a to TARGETS
$(foreach V,$(filter %.SRCS, ${.VARIABLES}),\
- $(eval $(V:%.SRCS=%.OBJS) += $(foreach f,$($(V)),$(addsuffix .o,$(basename $(f))))) \
+ $(eval $(V:%.SRCS=%.OBJS) := $(call objects,$V)) \
+ $(eval TARGET.OBJS += $(V:%.SRCS=%.OBJS)) \
+ $(eval $(V:%.SRCS=%.DEPS) := $(call depends,$V)) \
+ $(eval include $($(V:%.SRCS=%.DEPS))) \
+ $(eval TARGET.DEPS += $(V:%.SRCS=%.DEPS)) \
$(eval $(V:%.SRCS=%.a): $($(V:%.SRCS=%.OBJS))) \
- $(eval all: $($(V:%.SRCS=%.OBJS))) \
+ $(eval TARGETS += $(V:%.SRCS=%.a)) \
)
+TESTS.DEPS = $(foreach F,${TESTS},$(addsuffix .d,$F))
+include ${TESTS.DEPS}
+
+debug:
+ @echo "targets: ${TARGETS}"
+ @echo " OBJS: ${TARGET.OBJS}"
+ @echo " DEPS: ${TARGET.DEPS}"
+ @echo "tests : ${TESTS}"
+ @echo " DEPS: ${TESTS.DEPS}"
+
+%.info:
+ @echo "Target: $(basename $@)"
+ @echo " SRCS: ${$(basename $@).SRCS}"
+ @echo " OBJS: ${$(basename $@).OBJS}"
+ @echo " DEPS: ${$(basename $@).DEPS}"
+
+targets: ${TARGETS}
+
# extra flags
CFLAGS += -I../lib \
-Werror=implicit-function-declaration
CXXFLAGS += -I../lib -Drestrict=__restrict__
+# Depndency rules
+%.d: %.S
+ @${CC} ${CFLAGS} -M -MT $(<:.S=_${ARCH}.o) $< -MF $@
+
+%.d: %.c
+ @${CC} ${CFLAGS} -M -MT $(<:.c=_${ARCH}.o) $< -MF $@
+
+%.d: %.cpp
+ @${CXX} ${CXXFLAGS} -M -MT $(<:.cpp=_${ARCH}.o) $< -MF $@
+
+%.d: %.cc
+ @${TEST_CXX} ${TEST_CXXFLAGS} -M -MT $(<:.cc=_${ARCH}.o) $< -MF $@
+
# Suffix rules
-.SUFFIXES: .s .S .c .cpp .o
%.a:
@echo ' AR $@'
@${AR} ${ARFLAGS} $@ $(filter %.o,$^)
-.s.o:
- @echo ' AS $^'
- @$(AS) $(ASFLAGS) -c -o $@ $^
+%_${ARCH}.o: %.s
+ @echo ' AS $<'
+ @$(AS) $(ASFLAGS) -c -o $@ $<
-.S.o:
- @echo ' CC $^'
- @$(CC) $(CFLAGS) -c -o $@ $^
+%_${ARCH}.o: %.S
+ @echo ' CC $<'
+ @$(CC) $(CFLAGS) -c -o $@ $<
-.c.o:
- @echo ' CC $^'
- @$(CC) $(CFLAGS) -c -o $@ $^
+%_${ARCH}.o: %.c
+ @echo ' CC $<'
+ @$(CC) $(CFLAGS) -c -o $@ $<
-.cpp.o:
- @echo ' CXX $^'
- @$(CXX) $(CXXFLAGS) -c -o $@ $^
+%_${ARCH}.o: %.cpp
+ @echo ' CXX $<'
+ @$(CXX) $(CXXFLAGS) -c -o $@ $<
%.elf:
@echo ' LD $@'
@@ -39,24 +91,25 @@ CXXFLAGS += -I../lib -Drestrict=__restrict__
@b2sum $@ | cut -d' ' -f1
# test rules
-tst/test_%: tst/%.cc
+tst/%: tst/%.cc
@echo ' CXX TEST $@'
@$(TEST_CXX) $(TEST_CXXFLAGS) $< -o $@
.PHONY: test.base valgrind.base clean.base FORCE
-test.base: $(TESTS)
+test.base: ${TESTS}
@echo " -> Running tests in $(shell pwd | xargs basename)"
@$(foreach f,$^,echo " -> $f"; ./$f &&) echo "Done"
-valgrind.base: $(TESTS)
+valgrind.base: ${TESTS}
@echo " -> Running valgrind on tests in $(shell pwd | xargs basename)"
@$(foreach f,$^,echo " -> $f"; valgrind --leak-check=full ./$f;)
clean.base: FORCE
@echo " -> Cleaning $(shell pwd | xargs basename)"
@$(foreach V,$(filter %.OBJS, ${.VARIABLES}), rm -rf $($(V)))
+ @$(foreach V,$(filter %.DEPS, ${.VARIABLES}), rm -rf $($(V)))
@rm -rf *.a
- @rm -rf tst/test_*
+ @rm -rf ${TESTS}
%: %.base ;
diff --git a/src/Makefile b/src/Makefile
index 1aa8617..02ff120 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -7,7 +7,7 @@ CXXFLAGS += ${INCLUDES}
kernel.SRCS := multiboot2.c mmap.c kernel.cpp mem/vmm.c
kernel.OBJS := conf.h
-TESTS += tst/test_taskqueue tst/test_roundrobin
+TESTS += tst/taskqueue tst/roundrobin
include ../rules.mk
diff --git a/src/task.h b/src/task.h
index 381f628..0d59bb1 100644
--- a/src/task.h
+++ b/src/task.h
@@ -13,7 +13,7 @@ struct Task {
#ifdef __cplusplus
template <typename T> struct Queue {
struct Item {
- Item(T *node) : node(node) {}
+ Item(T *p_node) : node(p_node) {}
T *node;
Item *next = nullptr;