From 177b205c48552ca43b4524b96f3d7f0873d6529f Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 11 May 2023 16:19:53 +0300 Subject: [PATCH] BuildSystem: userspace has cmake target --- CMakeLists.txt | 5 +++- kernel/kernel/kernel.cpp | 2 +- userspace/CMakeLists.txt | 20 +++++++++++++ userspace/test.c | 30 ------------------- userspace/test/CMakeLists.txt | 17 +++++++++++ userspace/test/test.cpp | 55 +++++++++++++++++++++++++++++++++++ userspace/yes/CMakeLists.txt | 17 +++++++++++ userspace/yes/main.cpp | 8 +++++ 8 files changed, 122 insertions(+), 32 deletions(-) create mode 100644 userspace/CMakeLists.txt delete mode 100644 userspace/test.c create mode 100644 userspace/test/CMakeLists.txt create mode 100644 userspace/test/test.cpp create mode 100644 userspace/yes/CMakeLists.txt create mode 100644 userspace/yes/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index cce014cb9d..179c595fa8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,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_BIN ${BANAN_SYSROOT}/usr/bin) set(BANAN_BOOT ${BANAN_SYSROOT}/boot) set(DISK_IMAGE_PATH ${CMAKE_BINARY_DIR}/banan-os.img) @@ -29,6 +30,7 @@ add_subdirectory(kernel) add_subdirectory(BAN) add_subdirectory(libc) add_subdirectory(LibELF) +add_subdirectory(userspace) add_custom_target(sysroot COMMAND mkdir -p ${BANAN_SYSROOT} @@ -52,11 +54,11 @@ add_custom_target(toolchain ) add_custom_target(image - COMMAND ${CMAKE_CXX_COMPILER} -x c ${CMAKE_SOURCE_DIR}/userspace/test.c -o ${BANAN_SYSROOT}/bin/test COMMAND ${CMAKE_COMMAND} -E env SYSROOT="${BANAN_SYSROOT}" DISK_IMAGE_PATH="${DISK_IMAGE_PATH}" ${CMAKE_SOURCE_DIR}/image.sh DEPENDS kernel-install DEPENDS ban-install DEPENDS libc-install + DEPENDS userspace-install USES_TERMINAL ) @@ -65,6 +67,7 @@ add_custom_target(image-full DEPENDS kernel-install DEPENDS ban-install DEPENDS libc-install + DEPENDS userspace-install USES_TERMINAL ) diff --git a/kernel/kernel/kernel.cpp b/kernel/kernel/kernel.cpp index e3fecf0ed5..2d238fd0d3 100644 --- a/kernel/kernel/kernel.cpp +++ b/kernel/kernel/kernel.cpp @@ -180,7 +180,7 @@ static void init2(void* tty1) ((TTY*)tty1)->initialize_device(); - MUST(Process::create_userspace("/bin/test"sv)); + MUST(Process::create_userspace("/usr/bin/test"sv)); return; Process::create_kernel( diff --git a/userspace/CMakeLists.txt b/userspace/CMakeLists.txt new file mode 100644 index 0000000000..45c236500c --- /dev/null +++ b/userspace/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.26) + +project(userspace CXX) + +set(USERSPACE_PROJECTS + test + yes +) + +foreach(USERSPACE_PROJECT ${USERSPACE_PROJECTS}) + add_subdirectory(${USERSPACE_PROJECT}) +endforeach() + +add_custom_target(userspace) +add_custom_target(userspace-install DEPENDS userspace) + +foreach(USERSPACE_PROJECT ${USERSPACE_PROJECTS}) + add_dependencies(userspace ${USERSPACE_PROJECT}) + add_dependencies(userspace-install ${USERSPACE_PROJECT}-install) +endforeach() diff --git a/userspace/test.c b/userspace/test.c deleted file mode 100644 index fe6306b508..0000000000 --- a/userspace/test.c +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include - -#define ERROR(msg) { perror(msg); return 1; } -#define BUF_SIZE 1024 - -int main() -{ - FILE* fp = fopen("/usr/include/stdio.h", "r"); - if (fp == NULL) - ERROR("fopen"); - - char* buffer = malloc(BUF_SIZE); - if (buffer == NULL) - ERROR("malloc"); - - for (;;) - { - size_t n_read = fread(buffer, 1, BUF_SIZE - 1, fp); - if (n_read == 0) - break; - buffer[n_read] = '\0'; - printf("%s", buffer); - } - - free(buffer); - fclose(fp); - - return 0; -} diff --git a/userspace/test/CMakeLists.txt b/userspace/test/CMakeLists.txt new file mode 100644 index 0000000000..88a242fb34 --- /dev/null +++ b/userspace/test/CMakeLists.txt @@ -0,0 +1,17 @@ +cmake_minimum_required(VERSION 3.26) + +project(test CXX) + +set(TEST_SOURCES + test.cpp +) + +add_executable(test ${TEST_SOURCES}) +target_compile_options(test PUBLIC -O2 -g) +add_dependencies(test libc-install) +target_link_options(test PUBLIC -nodefaultlibs -lc) + +add_custom_target(test-install + COMMAND cp ${CMAKE_CURRENT_BINARY_DIR}/test ${BANAN_BIN}/ + DEPENDS test +) diff --git a/userspace/test/test.cpp b/userspace/test/test.cpp new file mode 100644 index 0000000000..5bcb589f76 --- /dev/null +++ b/userspace/test/test.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include + +#define ERROR(msg) { perror(msg); return 1; } +#define BUF_SIZE 1024 + +int main() +{ + printf("%.2e\n", 1230.0); + printf("%.2e\n", 123.0); + printf("%.2e\n", 12.3); + printf("%.2e\n", 1.23); + printf("%.2e\n", 0.123); + printf("%.2e\n", 0.0123); + printf("%.2e\n", 0.00123); + + printf("%e\n", 123.456); + printf("%.2e\n", 123.456); + printf("%.0e\n", 123.456); + printf("%#.0e\n", 123.456); + + printf("%e\n", -123.456); + printf("%.2e\n", -123.456); + printf("%.0e\n", -123.456); + printf("%#.0e\n", -123.456); + + printf("%e\n", 0.0); + printf("%e\n", -0.0); + + return 0; + + FILE* fp = fopen("/usr/include/stdio.h", "r"); + if (fp == NULL) + ERROR("fopen"); + + char* buffer = (char*)malloc(BUF_SIZE); + if (buffer == NULL) + ERROR("malloc"); + + for (;;) + { + size_t n_read = fread(buffer, 1, BUF_SIZE - 1, fp); + if (n_read == 0) + break; + buffer[n_read] = '\0'; + printf("%s", buffer); + } + + free(buffer); + fclose(fp); + + return 0; +} diff --git a/userspace/yes/CMakeLists.txt b/userspace/yes/CMakeLists.txt new file mode 100644 index 0000000000..fd25963d98 --- /dev/null +++ b/userspace/yes/CMakeLists.txt @@ -0,0 +1,17 @@ +cmake_minimum_required(VERSION 3.26) + +project(yes CXX) + +set(SOURCES + main.cpp +) + +add_executable(yes ${SOURCES}) +target_compile_options(yes PUBLIC -O2 -g) +add_dependencies(yes libc-install) +target_link_options(yes PUBLIC -nodefaultlibs -lc) + +add_custom_target(yes-install + COMMAND cp ${CMAKE_CURRENT_BINARY_DIR}/yes ${BANAN_BIN}/ + DEPENDS yes +) diff --git a/userspace/yes/main.cpp b/userspace/yes/main.cpp new file mode 100644 index 0000000000..6ac476555b --- /dev/null +++ b/userspace/yes/main.cpp @@ -0,0 +1,8 @@ +#include + +int main() +{ + for (;;) + puts("y"); + return 0; +}