Improve buildsystem by making build directory

We now create build directory for kernel and libc so that source
directories are kept more clean.
This commit is contained in:
Bananymous 2022-12-03 18:15:10 +02:00
parent c292ef5471
commit c17f61611e
3 changed files with 40 additions and 30 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
isodir isodir
sysroot sysroot
.vscode/ .vscode/
.idea/

View File

@ -13,8 +13,8 @@ EXEC_PREFIX?=$(PREFIX)
BOOTDIR?=$(EXEC_PREFIX)/boot BOOTDIR?=$(EXEC_PREFIX)/boot
INCLUDEDIR?=$(PREFIX)/include INCLUDEDIR?=$(PREFIX)/include
CFLAGS:=$(CFLAGS) -fstack-protector -ffreestanding -Wall -Wextra -Wno-unused-function CFLAGS:=$(CFLAGS) -D__is_kernel -Iinclude -fstack-protector -ffreestanding -Wall -Wextra -Wno-unused-function
CPPFLAGS:=$(CPPFLAGS) -D__is_kernel -Iinclude CPPFLAGS:=$(CPPFLAGS)
LDFLAGS:=$(LDFLAGS) LDFLAGS:=$(LDFLAGS)
LIBS:=$(LIBS) -nostdlib -lk -lgcc LIBS:=$(LIBS) -nostdlib -lk -lgcc
@ -27,6 +27,8 @@ CPPFLAGS:=$(CPPFLAGS) $(KERNEL_ARCH_CPPFLAGS)
LDFLAGS:=$(LDFLAGS) $(KERNEL_ARCH_LDFLAGS) LDFLAGS:=$(LDFLAGS) $(KERNEL_ARCH_LDFLAGS)
LIBS:=$(LIBS) $(KERNEL_ARCH_LIBS) LIBS:=$(LIBS) $(KERNEL_ARCH_LIBS)
BUILDDIR=$(abspath build)
KERNEL_OBJS=\ KERNEL_OBJS=\
$(KERNEL_ARCH_OBJS) \ $(KERNEL_ARCH_OBJS) \
kernel/GDT.o \ kernel/GDT.o \
@ -51,28 +53,30 @@ $(LIBS) \
$(ARCHDIR)/crtend.o \ $(ARCHDIR)/crtend.o \
$(ARCHDIR)/crtn.o \ $(ARCHDIR)/crtn.o \
.PHONY: all clean install install-headers install-kernel .PHONY: all always clean install install-headers install-kernel
.SUFFIXES: .o .cpp .S .SUFFIXES: .o .cpp .S
all: banan-os.kernel all: banan-os.kernel
banan-os.kernel: $(OBJS) $(ARCHDIR)/linker.ld banan-os.kernel: always $(OBJS) $(ARCHDIR)/linker.ld
$(CXX) -T $(ARCHDIR)/linker.ld -o $@ $(CFLAGS) $(CPPFLAGS) $(LINK_LIST) cd $(BUILDDIR) && $(CC) -T ../$(ARCHDIR)/linker.ld -o banan-os.kernel $(CFLAGS) $(LINK_LIST)
grub-file --is-x86-multiboot banan-os.kernel cd $(BUILDDIR) && grub-file --is-x86-multiboot banan-os.kernel
$(ARCHDIR)/crtbegin.o $(ARCHDIR)/crtend.o: $(ARCHDIR)/crtbegin.o $(ARCHDIR)/crtend.o:
OBJ=`$(CXX) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -print-file-name=$(@F)` && cp "$$OBJ" $@ OBJ=`$(CC) $(CFLAGS) $(LDFLAGS) -print-file-name=$(@F)` && cp "$$OBJ" $(BUILDDIR)/$@
.cpp.o: .cpp.o:
$(CXX) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS) $(CXX) -MD -c $< -o $(BUILDDIR)/$@ $(CFLAGS) $(CPPFLAGS)
.S.o: .S.o:
$(CXX) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS) $(CC) -MD -c $< -o $(BUILDDIR)/$@ $(CFLAGS)
always:
mkdir -p $(BUILDDIR)/arch/i386
mkdir -p $(BUILDDIR)/kernel
clean: clean:
rm -f banan-os.kernel rm -rf $(BUILDDIR)
rm -f $(OBJS) *.o */*.o */*/*.o
rm -f $(OBJS:.o=.d) *.d */*.d */*/*.d
install: install-headers install-kernel install: install-headers install-kernel
@ -82,6 +86,6 @@ install-headers:
install-kernel: banan-os.kernel install-kernel: banan-os.kernel
mkdir -p $(DESTDIR)$(BOOTDIR) mkdir -p $(DESTDIR)$(BOOTDIR)
cp banan-os.kernel $(DESTDIR)$(BOOTDIR) cp $(BUILDDIR)/banan-os.kernel $(DESTDIR)$(BOOTDIR)
-include $(OBJS:.o=.d) -include $(OBJS:.o=.d)

View File

@ -13,8 +13,8 @@ EXEC_PREFIX?=$(PREFIX)
INCLUDEDIR?=$(PREFIX)/include INCLUDEDIR?=$(PREFIX)/include
LIBDIR?=$(EXEC_PREFIX)/lib LIBDIR?=$(EXEC_PREFIX)/lib
CFLAGS:=$(CFLAGS) -ffreestanding -Wall -Wextra CFLAGS:=$(CFLAGS) -D__is_libc -Iinclude -ffreestanding -Wall -Wextra
CPPFLAGS:=$(CPPFLAGS) -D__is_libc -Iinclude CPPFLAGS:=$(CPPFLAGS)
LIBK_CFLAGS:=$(CFLAGS) LIBK_CFLAGS:=$(CFLAGS)
LIBK_CPPFLAGS:=$(CPPFLAGS) -D__is_libk LIBK_CPPFLAGS:=$(CPPFLAGS) -D__is_libk
@ -27,6 +27,8 @@ CPPFLAGS:=$(CPPFLAGS) $(ARCH_CPPFLAGS)
LIBK_CFLAGS:=$(LIBK_CFLAGS) $(KERNEL_ARCH_CFLAGS) LIBK_CFLAGS:=$(LIBK_CFLAGS) $(KERNEL_ARCH_CFLAGS)
LIBK_CPPFLAGS:=$(LIBK_CPPFLAGS) $(KERNEL_ARCH_CPPFLAGS) LIBK_CPPFLAGS:=$(LIBK_CPPFLAGS) $(KERNEL_ARCH_CPPFLAGS)
BUILDDIR=$(abspath build)
FREEOBJS=\ FREEOBJS=\
$(ARCH_FREEOBJS) \ $(ARCH_FREEOBJS) \
stdio/printf.o \ stdio/printf.o \
@ -54,33 +56,36 @@ LIBK_OBJS=$(FREEOBJS:.o=.libk.o)
#BINARIES=libc.a libk.a # Not ready for libc yet. #BINARIES=libc.a libk.a # Not ready for libc yet.
BINARIES=libk.a BINARIES=libk.a
.PHONY: all clean install install-headers install-libs .PHONY: all always clean install install-headers install-libs
.SUFFIXES: .o .libk.o .cpp .S .SUFFIXES: .o .libk.o .cpp .S
all: $(BINARIES) all: $(BINARIES)
libc.a: $(OBJS) libc.a: always $(OBJS)
$(AR) rcs $@ $(OBJS) cd $(BUILDDIR) && $(AR) rcs $@ $(OBJS)
libk.a: $(LIBK_OBJS) libk.a: always $(LIBK_OBJS)
$(AR) rcs $@ $(LIBK_OBJS) cd $(BUILDDIR) && $(AR) rcs $@ $(LIBK_OBJS)
.cpp.o: .cpp.o:
$(CXX) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS) $(CXX) -MD -c $< -o $(BUILDDIR)/$@ $(CFLAGS) $(CPPFLAGS)
.S.o: .S.o:
$(CXX) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS) $(CXX) -MD -c $< -o $(BUILDDIR)/$@ $(CFLAGS) $(CPPFLAGS)
.cpp.libk.o: .cpp.libk.o:
$(CXX) -MD -c $< -o $@ $(LIBK_CFLAGS) $(LIBK_CPPFLAGS) $(CXX) -MD -c $< -o $(BUILDDIR)/$@ $(LIBK_CFLAGS) $(LIBK_CPPFLAGS)
.S.libk.o: .S.libk.o:
$(CXX) -MD -c $< -o $@ $(LIBK_CFLAGS) $(LIBK_CPPFLAGS) $(CXX) -MD -c $< -o $(BUILDDIR)/$@ $(LIBK_CFLAGS) $(LIBK_CPPFLAGS)
clean: clean:
rm -f $(BINARIES) *.a rm -rf $(BUILDDIR)
rm -f $(OBJS) $(LIBK_OBJS) *.o */*.o */*/*.o
rm -f $(OBJS:.o=.d) $(LIBK_OBJS:.o=.d) *.d */*.d */*/*.d always:
mkdir -p $(BUILDDIR)/stdio
mkdir -p $(BUILDDIR)/stdlib
mkdir -p $(BUILDDIR)/string
install: install-headers install-libs install: install-headers install-libs
@ -90,7 +95,7 @@ install-headers:
install-libs: $(BINARIES) install-libs: $(BINARIES)
mkdir -p $(DESTDIR)$(LIBDIR) mkdir -p $(DESTDIR)$(LIBDIR)
cp $(BINARIES) $(DESTDIR)$(LIBDIR) cp $(BUILDDIR)/$(BINARIES) $(DESTDIR)$(LIBDIR)
-include $(OBJS:.o=.d) -include $(OBJS:.o=.d)
-include $(LIBK_OBJS:.o=.d) -include $(LIBK_OBJS:.o=.d)