summaryrefslogtreecommitdiff
path: root/makefile
diff options
context:
space:
mode:
Diffstat (limited to 'makefile')
-rw-r--r--makefile253
1 files changed, 108 insertions, 145 deletions
diff --git a/makefile b/makefile
index 3151d4d..5ba2931 100644
--- a/makefile
+++ b/makefile
@@ -1,165 +1,118 @@
-LIMINE_DIR = $(abspath dependencies/limine)
-MINTSUKI_HEADERS_DIR = $(abspath dependencies/mintsuki-headers)
-TOOLCHAIN_DIR = $(abspath toolchain)
+# arguments are not strictly necessary to build
-EXTRA_CC_ARGS = -Wall -Wextra -Og -ggdb -fno-exceptions
+CC_EXTRA_ARGS = -Wall -Wextra -Og -ggdb
-HILBERT_NASM = nasm -f elf64
-HILBERT_CC = ${TOOLCHAIN_DIR}/usr/bin/x86_64-elf-c++ -std=c++20 \
- ${EXTRA_CC_ARGS} -static -mno-sse -I include -I $(abspath euler/include) \
- -I $(abspath libraries/daguerre/include) -I ${MINTSUKI_HEADERS_DIR} \
- -I $(abspath libraries/pake/include)
-HILBERT_AR = ${TOOLCHAIN_DIR}/usr/bin/x86_64-elf-ar
-HILBERT_LD = ${TOOLCHAIN_DIR}/usr/bin/x86_64-elf-ld -z noexecstack
+.PHONY: default
+default: build/disk.iso
-.EXPORT_ALL_VARIABLES:
+# command and arguments that you should be careful about changing
-LIB_DIR = ${TOOLCHAIN_DIR}/usr/x86_64-elf/lib
+NASM = nasm
+CC = toolchain/usr/bin/x86_64-elf-c++
+AR = toolchain/usr/bin/x86_64-elf-ar
+LD = toolchain/usr/bin/x86_64-elf-ld
-LIMINE_DEP = dependencies/.limine-done
-MINTSUKI_HEADERS_DEP = dependencies/.mintsuki-headers-done
-BINUTILS_DEP = toolchain/.binutils-done
-GCC_DEP = toolchain/.gcc-done
-LIBGCC_DEP = toolchain/.libgcc-done
-LIBSTDCPP_DEP = toolchain/.libstdcpp-done
+KERNEL_INCLUDES = \
+ dependencies/limine \
+ dependencies/mintsuki-headers \
+ kernel/include
-EULER_DEP = toolchain/.euler-done
-DAGUERRE_DEP = toolchain/.daguerre-done
-PAKE_DEP = toolchain/.pake-done
+USER_INCLUDES = \
+ dependencies/mintsuki-headers \
+ euler/include \
+ $(wildcard libraries/*/include)
-APP_DEPS = ${EULER_DEP}
-LIBRARY_DEPS = ${LIBSTDCPP_DEP}
+NASM_ARGS = -f elf64
-.PHONY: default run clean clean-dependencies
+CC_ARGS_COMMON = -std=c++20 -static -mno-sse -Iinclude ${CC_EXTRA_ARGS}
+CC_ARGS_KERNEL = \
+ ${CC_ARGS_COMMON} -ffreestanding -fno-exceptions \
+ -fno-rtti -mcmodel=kernel ${KERNEL_INCLUDES:%=-I%}
+CC_ARGS_USER = ${CC_ARGS_COMMON} ${USER_INCLUDES:%=-I%}
-default: build/disk.iso
+LD_ARGS = -z noexecstack
-run: build/disk.iso
- gdb -x qemu.gdb
+LIBDIR = toolchain/usr/x86_64-elf/lib
-clean:
- rm -rf build ${EULER_DEP} ${DAGUERRE_DEP} ${PAKE_DEP}
- make -C euler clean
- make -C kernel clean
- make -C applications/init clean
- make -C applications/goldman clean
- make -C applications/hello clean
- make -C applications/clock clean
- make -C libraries/daguerre clean
- make -C libraries/pake clean
-
-clean-dependencies: clean
- rm -rf toolchain dependencies
-
-${LIMINE_DEP}:
- mkdir -p dependencies
- test -e dependencies/limine || git clone --depth 1 -b v7.5.1 https://github.com/limine-bootloader/limine dependencies/limine
- cd ${LIMINE_DIR} && ./bootstrap
- cd ${LIMINE_DIR} && ./configure --enable-bios --enable-bios-cd
- +make -C ${LIMINE_DIR}
- touch $@
-
-${MINTSUKI_HEADERS_DEP}:
- mkdir -p dependencies
- test -e dependencies/mintsuki-headers || git clone --depth 1 https://github.com/osdev0/freestanding-headers dependencies/mintsuki-headers
- cd dependencies/mintsuki-headers && git fetch --depth=1 origin dd3abd2d7147efc4170dff478d3b7730bed14147
- cd dependencies/mintsuki-headers && git checkout dd3abd2d7147efc4170dff478d3b7730bed14147
- patch dependencies/mintsuki-headers/stddef.h patches/mintsuki-stddef.patch
- touch $@
-
-${BINUTILS_DEP}:
- mkdir -p dependencies toolchain/usr
- test -e dependencies/binutils || git clone --depth 1 -b binutils-2_42 https://sourceware.org/git/binutils-gdb dependencies/binutils
- mkdir -p dependencies/binutils/build
- cd dependencies/binutils/build && ../configure --disable-gdb \
- --target=x86_64-elf --prefix=${TOOLCHAIN_DIR}/usr
- +make -C dependencies/binutils/build
- +make -C dependencies/binutils/build install
- touch $@
-
-${GCC_DEP}: ${BINUTILS_DEP}
- mkdir -p toolchain/usr/include
- test -e dependencies/gcc || git clone --depth 1 -b releases/gcc-14.1.0 https://gcc.gnu.org/git/gcc dependencies/gcc
- mkdir -p dependencies/gcc/build
- cd dependencies/gcc/build && ../configure --disable-fixed-point \
- --disable-gcov --disable-multilib --disable-shared \
- --disable-hosted-libstdcxx \
- --enable-languages=c++ --target=x86_64-elf --enable-cstdio=stdio_pure \
- --prefix=${TOOLCHAIN_DIR}/usr --without-headers --enable-cxx-flags=-mno-sse
- +make -C dependencies/gcc/build all-gcc
- +make -C dependencies/gcc/build install-gcc
- touch $@
-
-${LIBGCC_DEP}: ${GCC_DEP}
- +make -C dependencies/gcc/build all-target-libgcc
- +make -C dependencies/gcc/build install-target-libgcc
- touch $@
-
-${LIBSTDCPP_DEP}: ${LIBGCC_DEP}
- +make -C dependencies/gcc/build all-target-libstdc++-v3
- +make -C dependencies/gcc/build install-target-libstdc++-v3
- patch toolchain/usr/x86_64-elf/include/c++/14.1.0/memory patches/gcc-memory.patch
- touch $@
-
-.PHONY: ${EULER_DEP}
-${EULER_DEP}: ${LIBRARY_DEPS}
- +make -C euler build/crt0.o build/libc.a build/libg.a build/libm.a
- mkdir -p ${LIB_DIR}
- cp euler/build/crt0.o euler/build/libc.a \
- euler/build/libg.a euler/build/libm.a ${LIB_DIR}/
- touch $@
-
-.PHONY: ${DAGUERRE_DEP}
-${DAGUERRE_DEP}: ${LIBRARY_DEPS}
- +make -C libraries/daguerre build/libdaguerre.a
- cp libraries/daguerre/build/libdaguerre.a ${LIB_DIR}/
- touch $@
-
-.PHONY: ${PAKE_DEP}
-${PAKE_DEP}: ${LIBRARY_DEPS}
- +make -C libraries/pake build/libpake.a
- cp libraries/pake/build/libpake.a ${LIB_DIR}/
- touch $@
-
-.PHONY: kernel/build/kernel.elf
-kernel/build/kernel.elf: ${GCC_DEP} ${MINTSUKI_HEADERS_DEP} ${LIMINE_DEP}
- +make -C kernel build/kernel.elf
-
-.PHONY: applications/init/build/init.elf
-applications/init/build/init.elf: ${APP_DEPS}
- +make -C applications/init build/init.elf
-
-.PHONY: applications/goldman/build/goldman.elf
-applications/goldman/build/goldman.elf: ${APP_DEPS} ${DAGUERRE_DEP}
- +make -C applications/goldman build/goldman.elf
-
-.PHONY: applications/hello/build/hello.elf
-applications/hello/build/hello.elf: ${APP_DEPS} ${DAGUERRE_DEP} ${PAKE_DEP}
- +make -C applications/hello build/hello.elf
-
-.PHONY: applications/clock/build/clock.elf
-applications/clock/build/clock.elf: ${APP_DEPS} ${DAGUERRE_DEP} ${PAKE_DEP}
- +make -C applications/clock build/clock.elf
-
-build/initfs.tgz: applications/init/build/init.elf \
- applications/goldman/build/goldman.elf \
- applications/hello/build/hello.elf \
- applications/clock/build/clock.elf
- @mkdir -p build
+SOURCES_FIND = -type f -regex '.*\.\(asm\|cpp\)'
+
+# kernel section
+
+build/kernel/%.asm.o: kernel/%.asm
+ @mkdir -p ${@D}
+ ${NASM} ${NASM_ARGS} $^ -o $@
+
+build/kernel/%.cpp.o: kernel/%.cpp
+ @mkdir -p ${@D}
+ ${CC} -c ${CC_ARGS_KERNEL} $^ -o $@
+
+KERNEL_SOURCES = $(shell find kernel/source ${SOURCES_FIND})
+build/kernel.elf: ${KERNEL_SOURCES:%=build/%.o}
+ ${LD} ${LD_ARGS} -T kernel/link.ld $^ -o $@
+
+# euler section
+
+build/euler/%.asm.o: euler/%.asm
+ @mkdir -p ${@D}
+ ${NASM} ${NASM_ARGS} $^ -o $@
+
+build/euler/%.cpp.o: euler/%.cpp
+ @mkdir -p ${@D}
+ ${CC} -c ${CC_ARGS_USER} -ffreestanding $^ -o $@
+
+EULER_SOURCES = $(shell find euler/source ${SOURCES_FIND})
+${LIBDIR}/crt0.o ${LIBDIR}/libc.a ${LIBDIR}/libg.a ${LIBDIR}/libm.a build/euler.a&: ${EULER_SOURCES:%=build/%.o}
+ ${AR} rcs build/euler.a $^
+ cp build/euler.a ${LIBDIR}/libc.a
+ ${NASM} ${NASM_ARGS} /dev/null -o ${LIBDIR}/crt0.o
+ ${AR} rcs ${LIBDIR}/libg.a ${LIBDIR}/crt0.o
+ ${AR} rcs ${LIBDIR}/libm.a ${LIBDIR}/crt0.o
+
+# libraries and applications section
+
+ALL_LIBRARIES = daguerre pake
+ALL_APPLICATIONS = clock goldman hello init
+
+ clock_LIBRARIES = daguerre pake
+goldman_LIBRARIES = daguerre
+ hello_LIBRARIES = daguerre pake
+ init_LIBRARIES =
+
+build/%.cpp.o: %.cpp
+ @mkdir -p ${@D}
+ ${CC} -c ${CC_ARGS_USER} $^ -o $@
+
+# ${1} = library name
+define LIBRARY_TEMPLATE =
+${1}_SOURCES = $$(shell find libraries/${1}/source $${SOURCES_FIND})
+$${LIBDIR}/lib${1}.a build/libraries/lib${1}.a&: $${${1}_SOURCES:%=build/%.o}
+ $${AR} rcs build/libraries/lib${1}.a $$^
+ cp build/libraries/lib${1}.a $${LIBDIR}/lib${1}.a
+endef
+
+# ${1} = application name
+define APPLICATION_TEMPLATE =
+${1}_SOURCES = $$(shell find applications/${1}/source $${SOURCES_FIND})
+build/applications/${1}.elf: $${${1}_SOURCES:%=build/%.o} $${${1}_LIBRARIES:%=$${LIBDIR}/lib%.a} $${LIBDIR}/libc.a
+ $${CC} ${CC_ARGS_USER} $${${1}_SOURCES:%=build/%.o} $$(patsubst %,-l%,$${${1}_LIBRARIES}) -o $$@
+endef
+
+$(foreach library,${ALL_LIBRARIES},$(eval $(call LIBRARY_TEMPLATE,${library})))
+$(foreach application,${ALL_APPLICATIONS},$(eval $(call APPLICATION_TEMPLATE,${application})))
+
+# initfs and disk section
+
+build/initfs.tgz: ${ALL_APPLICATIONS:%=build/applications/%.elf}
rm -rf build/initfs
cp -r skeleton build/initfs
- cp applications/init/build/init.elf build/initfs/bin/init
- cp applications/goldman/build/goldman.elf build/initfs/bin/goldman
- cp applications/hello/build/hello.elf build/initfs/bin/hello
- cp applications/clock/build/clock.elf build/initfs/bin/clock
+ $(foreach application,${ALL_APPLICATIONS},cp build/applications/${application}.elf build/initfs/bin/${application}; )
cd build/initfs && tar czf ../initfs.tgz .
-build/disk.iso: kernel/build/kernel.elf build/initfs.tgz ${LIMINE_DEP}
- @mkdir -p build
+build/disk.iso: build/kernel.elf build/initfs.tgz
rm -rf build/iso
mkdir build/iso
- cp kernel/build/kernel.elf ${LIMINE_DIR}/bin/limine-bios.sys \
- ${LIMINE_DIR}/bin/limine-bios-cd.bin build/initfs.tgz build/iso/
+ cp dependencies/limine/bin/limine-bios.sys dependencies/limine/bin/limine-bios-cd.bin \
+ build/kernel.elf build/initfs.tgz build/iso/
echo 'TIMEOUT=0' > build/iso/limine.cfg
echo ':Hilbert OS' >> build/iso/limine.cfg
echo 'PROTOCOL=limine' >> build/iso/limine.cfg
@@ -168,3 +121,13 @@ build/disk.iso: kernel/build/kernel.elf build/initfs.tgz ${LIMINE_DEP}
echo 'MODULE_CMDLINE=initfs' >> build/iso/limine.cfg
xorriso -as mkisofs -b limine-bios-cd.bin -no-emul-boot -boot-load-size 4 \
-boot-info-table --protective-msdos-label build/iso -o $@
+
+# phony targets
+
+.PHONY: run
+run: build/disk.iso
+ gdb -x qemu.gdb
+
+.PHONY: clean
+clean:
+ rm -rf build ${LIBDIR}/crt0.o ${LIBDIR}/libc.a ${LIBDIR}/libg.a ${LIBDIR}/libm.a $(foreach library,${ALL_LIBRARIES},${LIBDIR}/lib${library}.a)