Skip to content

Commit

Permalink
Make the buildsystem more consistent
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
29jm committed Nov 23, 2020
1 parent 39c0d60 commit a446324
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 103 deletions.
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
*
!*.*
!*/

*.mod
*.iso
*.o
*.pyc
Expand All @@ -13,6 +10,7 @@
*.map
*.img
misc/grub.cfg
misc/root
isodir
sysroot
toolchain/build
Expand Down
18 changes: 8 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -64,8 +65,6 @@ $(PROJECTS): $(PROJECT_HEADERS)

# Specify dependencies
kernel: libc
snow: libc
ui: libc snow
modules: libc snow ui

qemu: SnowflakeOS.iso
Expand Down Expand Up @@ -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
Expand Down
28 changes: 15 additions & 13 deletions kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
46 changes: 19 additions & 27 deletions libc/Makefile
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 3 additions & 1 deletion libc/src/dirent.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#ifndef _KERNEL_

#include <kernel/uapi/uapi_fs.h>
#include <kernel/uapi/uapi_syscall.h>

#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include <snow.h>
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`.
Expand Down
4 changes: 0 additions & 4 deletions libc/src/stdlib/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
#include <kernel/sys.h>
#endif

#ifndef _KERNEL_
#include <snow.h>
#endif

#define MIN_ALIGN 4

typedef struct _mem_block_t {
Expand Down
3 changes: 2 additions & 1 deletion libc/src/string/memset.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
29 changes: 15 additions & 14 deletions modules/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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:
$(info [modules] $@)
@$(AS) $(ASFLAGS) $< -o $@
2 changes: 1 addition & 1 deletion modules/src/terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ int main() {
str_free(text_buf);
str_free(input_buf);

// snow_close_window(win);
snow_close_window(win);

return 0;
}
Expand Down
24 changes: 10 additions & 14 deletions snow/Makefile
Original file line number Diff line number Diff line change
@@ -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)
24 changes: 10 additions & 14 deletions ui/Makefile
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit a446324

Please sign in to comment.