From a4463244a84fb02bc1e902d934660bc502906cfb Mon Sep 17 00:00:00 2001 From: Johan Manuel Date: Mon, 23 Nov 2020 19:10:39 +0100 Subject: [PATCH] Make the buildsystem more consistent Used to be, when you made a change in the libc, the kernel didn't get rebuilt by a call to `make`. Now it does! I _will_ get the hang of Makefile good practices. Still an inconvenience in the way modules (aka apps) are built: they each have all of the other as dependencies, so a change in one triggers a rebuild of all of them. --- .gitignore | 6 ++---- Makefile | 18 +++++++--------- kernel/Makefile | 28 ++++++++++++------------ libc/Makefile | 46 +++++++++++++++++----------------------- libc/src/dirent.c | 4 +++- libc/src/stdlib/malloc.c | 4 ---- libc/src/string/memset.c | 3 ++- modules/Makefile | 29 +++++++++++++------------ modules/src/terminal.c | 2 +- snow/Makefile | 24 +++++++++------------ ui/Makefile | 24 +++++++++------------ 11 files changed, 85 insertions(+), 103 deletions(-) diff --git a/.gitignore b/.gitignore index 0645d67..9065691 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,4 @@ -* -!*.* -!*/ - +*.mod *.iso *.o *.pyc @@ -13,6 +10,7 @@ *.map *.img misc/grub.cfg +misc/root isodir sysroot toolchain/build diff --git a/Makefile b/Makefile index 09ed905..3052b3a 100644 --- a/Makefile +++ b/Makefile @@ -5,14 +5,15 @@ LC_ALL=C HOST=i686-elf PREFIX=usr BOOTDIR=boot -LIBDIR=$(PREFIX)/lib -INCLUDEDIR=$(PREFIX)/include ISODIR=isodir ISO=$(PWD)/$(ISODIR) TARGETROOT=$(PWD)/misc/root SYSROOTDIR=sysroot SYSROOT=$(PWD)/$(SYSROOTDIR) +INCLUDEDIR=$(SYSROOT)/$(PREFIX)/include +LIBDIR=$(SYSROOT)/$(PREFIX)/lib + PATH:=$(PATH):$(PWD)/toolchain/compiler/bin MAKE:=$(MAKE) -s @@ -21,7 +22,7 @@ AR=$(HOST)-ar AS=$(HOST)-as CC=$(HOST)-gcc -CFLAGS=-Og -std=gnu11 -ffreestanding -Wall -Wextra +CFLAGS=-O1 -std=gnu11 -ffreestanding -Wall -Wextra ASFLAGS=--32 LDFLAGS=-nostdlib -L$(SYSROOT)/usr/lib -m elf_i386 @@ -39,7 +40,7 @@ endif # CFLAGS+=-target i386-pc-none-eabi -m32 # CFLAGS+=-mno-mmx -mno-sse -mno-sse2 -CC+=--sysroot=$(SYSROOT) -isystem=/$(INCLUDEDIR) +CC+=--sysroot=$(SYSROOT) -isystem=/$(PREFIX)/include # Make will be called on these folders PROJECTS=libc snow kernel modules ui @@ -64,8 +65,6 @@ $(PROJECTS): $(PROJECT_HEADERS) # Specify dependencies kernel: libc -snow: libc -ui: libc snow modules: libc snow ui qemu: SnowflakeOS.iso @@ -113,14 +112,13 @@ misc/disk.img: assets modules @echo "hello ext2 world" > misc/root/motd @echo "version: 0.5" > misc/root/etc/config @mv misc/*.rgb misc/root/ - @mkfs.ext2 misc/disk.img -d misc/root 2> /dev/null - @rm -r misc/root + @mkfs.ext2 misc/disk.img -d misc/root > /dev/null 2>&1 misc/disk2.img: assets modules $(info [all] writing disk2 image) @touch misc/disk2.img - @dd if=/dev/zero of=misc/disk2.img bs=1024 count=1000 - @mkfs.ext2 misc/disk2.img -d sysroot 2> /dev/null + @dd if=/dev/zero of=misc/disk2.img bs=1024 count=1000 2> /dev/null + @mkfs.ext2 misc/disk2.img -d sysroot > /dev/null 2>&1 toolchain: @env -i toolchain/build-toolchain.sh diff --git a/kernel/Makefile b/kernel/Makefile index 2fc0f0f..e242a23 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -5,34 +5,36 @@ LIBS=-lk OBJS:=$(patsubst %.c,%.o,$(shell find src -name '*.c')) OBJS+=$(patsubst %.S,%.o,$(shell find src -name '*.S')) +LIBK_DEP=$(LIBDIR)/libk.a + +KERNEL=$(ISO)/$(BOOTDIR)/SnowflakeOS.kernel +SYMBOLS=$(ISO)/modules/symbols.map + .PHONY: all clean build install-headers install-kernel -SnowflakeOS.kernel symbols.map: $(OBJS) linker.ld - $(info [kernel] linking kernel) - @$(LD) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) +$(KERNEL) $(SYMBOLS): $(OBJS) $(LIBK_DEP) linker.ld + $(info [kernel] linking) + @mkdir -p $(ISO)/$(BOOTDIR) + @$(LD) $(LDFLAGS) -o $(KERNEL) $(OBJS) $(LIBS) $(info [kernel] generating symbol table) - @awk '$$1 ~ /0x[0-9a-f]{16}/ {print substr($$1, 3), $$2}' linker.map > symbols.map @mkdir -p $(ISO)/modules - @cp symbols.map $(ISO)/modules/ + @awk '$$1 ~ /0x[0-9a-f]{16}/ {print substr($$1, 3), $$2}' linker.map > $(SYMBOLS) @rm linker.map %.o: %.c - $(info [kernel] building $@) + $(info [kernel] $@) @$(CC) -c $< -o $@ $(CFLAGS) %.o: %.S - $(info [kernel] building $@) + $(info [kernel] $@) @$(CC) -c $< -o $@ $(CFLAGS) -build: SnowflakeOS.kernel - $(info [kernel] installing) - @mkdir -p $(ISO)/$(BOOTDIR) - @cp SnowflakeOS.kernel $(ISO)/$(BOOTDIR) +build: $(KERNEL) install-headers: $(info [kernel] installing headers) - @mkdir -p $(SYSROOT)/$(INCLUDEDIR) - @cp -rT include $(SYSROOT)/$(INCLUDEDIR) + @mkdir -p $(INCLUDEDIR) + @cp -rT include $(INCLUDEDIR) clean: $(info [kernel] cleaning) diff --git a/libc/Makefile b/libc/Makefile index e0677c9..8a0103b 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -1,58 +1,50 @@ LIBK_CFLAGS:=$(CFLAGS) -D_KERNEL_ CFLAGS+=-Wno-format -OBJS:=$(patsubst %.c,%.o,$(wildcard src/*/*.c)) -OBJS+=$(patsubst %.c,%.o,$(wildcard src/*.c)) - - -OBJS+=$(patsubst %.S,%.o,$(wildcard src/*/*.S)) -OBJS+=$(patsubst %.S,%.o,$(wildcard src/*.S)) +OBJS+=$(patsubst %.c,%.o,$(shell find src/ -name '*.c')) +OBJS+=$(patsubst %.S,%.o,$(shell find src/ -name '*.S')) LIBK_OBJS:=$(OBJS:.o=.libk.o) # libk is libc but compiled with _KERNEL_ defined -BINARIES=libc.a libk.a +LIBC=$(LIBDIR)/libc.a +LIBK=$(LIBDIR)/libk.a -all: $(BINARIES) +.PHONY: all clean build install-headers -.PHONY: all clean build install-headers install-libs - -libc.a: $(OBJS) - $(info [libc] linking $@) +$(LIBC): $(OBJS) + $(info [libc] linking $(notdir $@)) + @mkdir -p $(LIBDIR) @$(AR) rcs $@ $(OBJS) -libk.a: $(LIBK_OBJS) - $(info [libc] linking $@) +$(LIBK): $(LIBK_OBJS) + $(info [libc] linking $(notdir $@)) + @mkdir -p $(LIBDIR) @$(AR) rcs $@ $(LIBK_OBJS) %.o: %.c - $(info [libc] building $@) + $(info [libc] $@) @$(CC) -c $< -o $@ $(CFLAGS) %.o: %.S - $(info [libc] building $@) + $(info [libc] $@) @$(CC) -c $< -o $@ $(CFLAGS) %.libk.o: %.c - $(info [libc] building $@) + $(info [libc] $@) @$(CC) -c $< -o $@ $(LIBK_CFLAGS) %.libk.o: %.S - $(info [libc] building $@) + $(info [libc] $@) @$(CC) -c $< -o $@ $(LIBK_CFLAGS) clean: $(info [libc] cleaning up) - @rm -f $(BINARIES) $(OBJS) $(LIBK_OBJS) *.o */*.o */*/*.o + @rm -f $(OBJS) $(LIBK_OBJS) *.o */*.o */*/*.o -build: install-libs +build: $(LIBC) $(LIBK) install-headers: $(info [libc] installing headers) - @mkdir -p $(SYSROOT)/$(INCLUDEDIR) - @cp -rT include $(SYSROOT)/$(INCLUDEDIR) - -install-libs: $(BINARIES) - $(info [libc] installing shared libraries) - @mkdir -p $(SYSROOT)/$(LIBDIR) - @cp $(BINARIES) $(SYSROOT)/$(LIBDIR) + @mkdir -p $(INCLUDEDIR) + @cp -rT include $(INCLUDEDIR) \ No newline at end of file diff --git a/libc/src/dirent.c b/libc/src/dirent.c index bbd6190..622d2b1 100644 --- a/libc/src/dirent.c +++ b/libc/src/dirent.c @@ -1,13 +1,15 @@ #ifndef _KERNEL_ #include +#include #include #include #include #include -#include +extern int32_t syscall1(uint32_t eax, uint32_t ebx); +extern int32_t syscall2(uint32_t eax, uint32_t ebx, uint32_t ecx); /* Opens the directory pointed to by `path` and returns a directory handle. * This handle can later be freed by calling `closedir`. diff --git a/libc/src/stdlib/malloc.c b/libc/src/stdlib/malloc.c index 22319c5..87d79b6 100644 --- a/libc/src/stdlib/malloc.c +++ b/libc/src/stdlib/malloc.c @@ -9,10 +9,6 @@ #include #endif -#ifndef _KERNEL_ -#include -#endif - #define MIN_ALIGN 4 typedef struct _mem_block_t { diff --git a/libc/src/string/memset.c b/libc/src/string/memset.c index 444abef..d0e4ea8 100644 --- a/libc/src/string/memset.c +++ b/libc/src/string/memset.c @@ -3,8 +3,9 @@ void* memset(void* bufptr, int value, size_t size) { unsigned char* buf = (unsigned char*) bufptr; - for (size_t i = 0; i < size; i++) + for (size_t i = 0; i < size; i++) { buf[i] = (unsigned char) value; + } return bufptr; } diff --git a/modules/Makefile b/modules/Makefile index 28ebb07..53a49e9 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -2,15 +2,17 @@ CFLAGS:=$(CFLAGS) LDFLAGS:=$(LDFLAGS) -Tmod.ld LIBS=-lui -lsnow -lc +LIB_DEPS=$(LIBDIR)/libc.a $(LIBDIR)/libui.a $(LIBDIR)/libsnow.a + MODS=$(patsubst %.c,%,$(wildcard src/*.c)) -MODS_BUILD=$(patsubst %.c,%.mod,$(wildcard src/*.c)) +MODS:=$(notdir $(MODS)) +MODS:=$(MODS:%=$(TARGETROOT)/%) +EXECUTABLES=$(notdir $(MODS)) +OBJS=$(EXECUTABLES:%=src/%.o) .PHONY: build install-headers clean build: $(MODS) - $(info [modules] copying modules) - @mkdir -p $(TARGETROOT) - @cp $(MODS) $(TARGETROOT) install-headers: @@ -19,16 +21,15 @@ clean: @rm -f */*.o @find . -executable -type f -delete -$(MODS): % : %.mod - $(info [modules] building $@) - @mv $^ $@ +# TODO: every module depends on every other, a change in one rebuilds all of them +$(MODS): $(OBJS) $(LIB_DEPS) src/start.o + $(info [modules] $(notdir $@)) + @mkdir -p $(TARGETROOT) + @$(LD) src/start.o src/$(@F).o -o $@ $(LDFLAGS) $(LIBS) -%.mod: %.c mod.ld src/start.o FORCE - @$(CC) -c $< -o $*.o $(CFLAGS) - @$(LD) src/start.o $*.o -o $@ $(LDFLAGS) $(LIBS) +%.o: %.c + @$(CC) -c $< -o $@ $(CFLAGS) %.o: %.S - $(info [modules] building $@) - @$(AS) $(ASFLAGS) $< -o $@ - -FORCE: \ No newline at end of file + $(info [modules] $@) + @$(AS) $(ASFLAGS) $< -o $@ \ No newline at end of file diff --git a/modules/src/terminal.c b/modules/src/terminal.c index 38b096a..261421a 100644 --- a/modules/src/terminal.c +++ b/modules/src/terminal.c @@ -114,7 +114,7 @@ int main() { str_free(text_buf); str_free(input_buf); - // snow_close_window(win); + snow_close_window(win); return 0; } diff --git a/snow/Makefile b/snow/Makefile index 59e0bf0..43be21c 100644 --- a/snow/Makefile +++ b/snow/Makefile @@ -1,34 +1,30 @@ OBJS=$(patsubst %.c,%.o,$(wildcard src/*.c)) OBJS+=$(patsubst %.S,%.o,$(wildcard src/*.S)) -all: libsnow.a +LIBSNOW=$(LIBDIR)/libsnow.a -.PHONY: all clean build install-headers install-libs +.PHONY: all clean build install-headers -libsnow.a: $(OBJS) - $(info [snow] linking $@) +$(LIBSNOW): $(OBJS) + $(info [snow] linking $(notdir $@)) + mkdir -p $(LIBDIR) @$(AR) rcs $@ $(OBJS) %.o: %.c - $(info [snow] building $@) + $(info [snow] $@) @$(CC) -c $< -o $@ $(CFLAGS) %.o: %.S - $(info [snow] building $@) + $(info [snow] $@) @$(CC) -c $< -o $@ $(CFLAGS) clean: $(info [snow] cleaning up) @rm -f *.a $(OBJS) -build: install-libs +build: $(LIBSNOW) install-headers: $(info [snow] installing headers) - @mkdir -p $(SYSROOT)/$(INCLUDEDIR) - @cp -rT include $(SYSROOT)/$(INCLUDEDIR) - -install-libs: libsnow.a - $(info [snow] installing shared libraries) - @mkdir -p $(SYSROOT)/$(LIBDIR) - @cp libsnow.a $(SYSROOT)/$(LIBDIR) + @mkdir -p $(INCLUDEDIR) + @cp -rT include $(INCLUDEDIR) \ No newline at end of file diff --git a/ui/Makefile b/ui/Makefile index 6c481c2..1b41012 100644 --- a/ui/Makefile +++ b/ui/Makefile @@ -1,30 +1,26 @@ OBJS=$(patsubst %.c,%.o,$(wildcard src/*.c)) OBJS+=$(patsubst %.S,%.o,$(wildcard src/*.S)) -all: libui.a +LIBUI=$(LIBDIR)/libui.a -.PHONY: all clean build install-headers install-libs +.PHONY: all clean build install-headers -libui.a: $(OBJS) - $(info [ui] linking $@) +$(LIBUI): $(OBJS) + $(info [ui] linking $(notdir $@)) + @mkdir -p $(LIBDIR) @$(AR) rcs $@ $(OBJS) %.o: %.c - $(info [ui] building $@) + $(info [ui] $@) @$(CC) -c $< -o $@ $(CFLAGS) clean: - $(info [ui] building $@) + $(info [ui] $@) @rm -f *.a $(OBJS) -build: install-libs +build: $(LIBUI) install-headers: $(info [ui] installing headers) - @mkdir -p $(SYSROOT)/$(INCLUDEDIR) - @cp -rT include $(SYSROOT)/$(INCLUDEDIR) - -install-libs: libui.a - $(info [ui] installing shared libraries) - @mkdir -p $(SYSROOT)/$(LIBDIR) - @cp libui.a $(SYSROOT)/$(LIBDIR) + @mkdir -p $(INCLUDEDIR) + @cp -rT include $(INCLUDEDIR) \ No newline at end of file