From 9f977488fa2e3b8e615e72c9fdffcb82a99191ec Mon Sep 17 00:00:00 2001 From: Bananymous Date: Fri, 7 Apr 2023 02:27:15 +0300 Subject: [PATCH] BuildSystem: cmake can now build out libc I can't seem to get libc++ build to work... --- CMakeLists.txt | 4 +++- kernel/CMakeLists.txt | 6 +++++- kernel/arch/i386/crt0.S | 23 +++++++++++++++++++++++ kernel/arch/x86_64/crt0.S | 31 +++++++++++++++++++++++++++++++ libc/CMakeLists.txt | 15 ++++++++++++++- toolchain/build.sh | 2 -- toolchain/gcc-12.2.0.patch | 11 +++++------ 7 files changed, 81 insertions(+), 11 deletions(-) create mode 100644 kernel/arch/i386/crt0.S create mode 100644 kernel/arch/x86_64/crt0.S diff --git a/CMakeLists.txt b/CMakeLists.txt index 1aa85da7..830dec19 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,7 @@ project(banan-os CXX) set(BANAN_SYSROOT ${CMAKE_BINARY_DIR}/sysroot) set(BANAN_INCLUDE ${BANAN_SYSROOT}/usr/include) +set(BANAN_LIB ${BANAN_SYSROOT}/usr/lib) set(BANAN_BOOT ${BANAN_SYSROOT}/boot) set(DISK_IMAGE_PATH ${CMAKE_BINARY_DIR}/banan-os.img) @@ -30,8 +31,9 @@ add_subdirectory(libc) add_custom_target(sysroot COMMAND mkdir -p ${BANAN_SYSROOT} - COMMAND mkdir -p ${BANAN_BOOT} COMMAND mkdir -p ${BANAN_INCLUDE} + COMMAND mkdir -p ${BANAN_LIB} + COMMAND mkdir -p ${BANAN_BOOT} COMMAND cp -r ${CMAKE_SOURCE_DIR}/base/* ${BANAN_SYSROOT}/ ) diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index 9db93ade..2fd9cb3d 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -120,6 +120,11 @@ endif() target_link_options(kernel PUBLIC -ffreestanding -nostdlib -O2) +add_custom_target(crt0 + COMMAND ${CMAKE_CXX_COMPILER} -c ${CMAKE_CURRENT_SOURCE_DIR}/arch/${BANAN_ARCH}/crt0.S -o ${BANAN_LIB}/crt0.o + DEPENDS headers +) + add_custom_target(kernel-headers COMMAND cp -r ${CMAKE_CURRENT_SOURCE_DIR}/include/* ${BANAN_INCLUDE}/ DEPENDS sysroot @@ -148,4 +153,3 @@ add_custom_command( ) set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_COMPILER} -o ${CMAKE_CURRENT_BINARY_DIR}/crti.o ${CMAKE_CURRENT_BINARY_DIR}/crtbegin.o ${CMAKE_CURRENT_BINARY_DIR}/crtend.o ${CMAKE_CURRENT_BINARY_DIR}/crtn.o -lgcc ") - diff --git a/kernel/arch/i386/crt0.S b/kernel/arch/i386/crt0.S new file mode 100644 index 00000000..127309fc --- /dev/null +++ b/kernel/arch/i386/crt0.S @@ -0,0 +1,23 @@ +.section .text + +.global _start +_start: + # Set up end of the stack frame linked list. + movl $0, %ebp + pushl %ebp # rip=0 + pushl %ebp # rbp=0 + movl %esp, %ebp + + # Prepare signals, memory allocation, stdio and such. + call initialize_standard_library + + # Run the global constructors. + call _init + + # Run main + call main + + # Terminate the process with the exit code. + movl %eax, %edi + call exit +.size _start, . - _start diff --git a/kernel/arch/x86_64/crt0.S b/kernel/arch/x86_64/crt0.S new file mode 100644 index 00000000..bdc038e6 --- /dev/null +++ b/kernel/arch/x86_64/crt0.S @@ -0,0 +1,31 @@ +.section .text + +.global _start +_start: + # Set up end of the stack frame linked list. + movq $0, %rbp + pushq %rbp # rip=0 + pushq %rbp # rbp=0 + movq %rsp, %rbp + + # We need those in a moment when we call main. + pushq %rsi + pushq %rdi + + # Prepare signals, memory allocation, stdio and such. + call initialize_standard_library + + # Run the global constructors. + call _init + + # Restore argc and argv. + popq %rdi + popq %rsi + + # Run main + call main + + # Terminate the process with the exit code. + movl %eax, %edi + call exit +.size _start, . - _start diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt index 0205a5ad..1ce5ef81 100644 --- a/libc/CMakeLists.txt +++ b/libc/CMakeLists.txt @@ -2,12 +2,25 @@ cmake_minimum_required(VERSION 3.26) project(libc CXX) +set(LIBC_SOURCES + ctype.cpp + stdio.cpp + stdlib.cpp + string.cpp + unistd.cpp +) + add_custom_target(libc-headers COMMAND cp -r ${CMAKE_CURRENT_SOURCE_DIR}/include/* ${BANAN_INCLUDE} DEPENDS sysroot ) +add_library(libc ${LIBC_SOURCES}) +add_dependencies(libc crt0) + add_custom_target(libc-install - DEPENDS libc-headers + COMMAND cp ${CMAKE_CURRENT_BINARY_DIR}/libc.a ${BANAN_LIB}/ + DEPENDS libc ) +set(CMAKE_STATIC_LIBRARY_PREFIX "") diff --git a/toolchain/build.sh b/toolchain/build.sh index 2012acde..ccc5b48a 100755 --- a/toolchain/build.sh +++ b/toolchain/build.sh @@ -43,7 +43,6 @@ if [ ! -f ${PREFIX}/bin/${TARGET}-ld ]; then --target="$TARGET" \ --prefix="$PREFIX" \ --with-sysroot="$SYSROOT" \ - --enable-shared \ --disable-werror make -j $(nproc) @@ -73,7 +72,6 @@ if [ ! -f ${PREFIX}/bin/${TARGET}-g++ ]; then --target="$TARGET" \ --prefix="$PREFIX" \ --with-sysroot="$SYSROOT" \ - --enable-shared \ --enable-languages=c,c++ make -j $(nproc) all-gcc all-target-libgcc diff --git a/toolchain/gcc-12.2.0.patch b/toolchain/gcc-12.2.0.patch index f10b22f3..b4b421e5 100644 --- a/toolchain/gcc-12.2.0.patch +++ b/toolchain/gcc-12.2.0.patch @@ -1,4 +1,4 @@ -diff -ruN gcc-12.2.0/config.sub gcc-12.2.0-banan/config.sub +/diff -ruN gcc-12.2.0/config.sub gcc-12.2.0-banan/config.sub --- gcc-12.2.0/config.sub 2022-08-19 11:09:52.128656687 +0300 +++ gcc-12.2.0-banan/config.sub 2023-04-05 21:48:39.434844559 +0300 @@ -1749,7 +1749,7 @@ @@ -25,7 +25,7 @@ diff -ruN gcc-12.2.0/fixincludes/mkfixinc.sh gcc-12.2.0-banan/fixincludes/mkfixi diff -ruN gcc-12.2.0/gcc/config/banan_os.h gcc-12.2.0-banan/gcc/config/banan_os.h --- gcc-12.2.0/gcc/config/banan_os.h 1970-01-01 02:00:00.000000000 +0200 +++ gcc-12.2.0-banan/gcc/config/banan_os.h 2023-04-05 22:03:20.133753757 +0300 -@@ -0,0 +1,32 @@ +@@ -0,0 +1,31 @@ +/* Useful if you wish to make target-specific GCC changes. */ +#undef TARGET_BANAN_OS +#define TARGET_BANAN_OS 1 @@ -44,9 +44,8 @@ diff -ruN gcc-12.2.0/gcc/config/banan_os.h gcc-12.2.0-banan/gcc/config/banan_os. +#undef ENDFILE_SPEC +#define ENDFILE_SPEC "crtend.o%s crtn.o%s" + -+/* Linker struff */ -+#undef LINK_SPEC -+#define LINK_SPEC "%{shared:-shared} %{static:-static} %{!shared: %{!static: %{rdynamic:-export-dynamic}}} -z max-page-size=4096" ++/* Don't use separate math library */ ++#define MATH_LIBRARY "" + +/* Additional predefined macros. */ +#undef TARGET_OS_CPP_BUILTINS @@ -54,7 +53,7 @@ diff -ruN gcc-12.2.0/gcc/config/banan_os.h gcc-12.2.0-banan/gcc/config/banan_os. + do { \ + builtin_define ("__banan_os__"); \ + builtin_define ("__unix__"); \ -+ builtin_assert ("system=myos"); \ ++ builtin_assert ("system=banan_os"); \ + builtin_assert ("system=unix"); \ + builtin_assert ("system=posix"); \ + } while(0);