From 4bf9eded3a5d6b007ba79a5716143fa8b3a5aaf6 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 12 Dec 2025 14:26:16 +0100 Subject: kapi: move platform independent implementation --- CMakeLists.txt | 4 ++++ kapi/CMakeLists.txt | 11 +++-------- kapi/src/cio.cpp | 57 ----------------------------------------------------- kapi/src/system.cpp | 20 ------------------- src/kapi/cio.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/kapi/system.cpp | 20 +++++++++++++++++++ 6 files changed, 84 insertions(+), 85 deletions(-) delete mode 100644 kapi/src/cio.cpp delete mode 100644 kapi/src/system.cpp create mode 100644 src/kapi/cio.cpp create mode 100644 src/kapi/system.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c348d0..d973268 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,6 +73,10 @@ add_subdirectory("libs") add_executable("kernel" "src/kstd.cpp" "src/main.cpp" + + # Platform Independent KAPI implementation + "src/kapi/cio.cpp" + "src/kapi/system.cpp" ) target_link_libraries("kernel" PRIVATE diff --git a/kapi/CMakeLists.txt b/kapi/CMakeLists.txt index d0ecb70..e513dba 100644 --- a/kapi/CMakeLists.txt +++ b/kapi/CMakeLists.txt @@ -1,4 +1,4 @@ -add_library("kapi" OBJECT) +add_library("kapi" INTERFACE) add_library("os::kapi" ALIAS "kapi") target_sources("kapi" PUBLIC @@ -16,16 +16,11 @@ target_sources("kapi" PUBLIC "include/kapi/system.hpp" ) -target_sources("kapi" PRIVATE - "src/cio.cpp" - "src/system.cpp" -) - -target_include_directories("kapi" PUBLIC +target_include_directories("kapi" INTERFACE "include" ) -target_link_libraries("kapi" PUBLIC +target_link_libraries("kapi" INTERFACE "libs::kstd" "gcc" diff --git a/kapi/src/cio.cpp b/kapi/src/cio.cpp deleted file mode 100644 index 66493b6..0000000 --- a/kapi/src/cio.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include "kapi/cio.hpp" - -#include -#include -#include - -namespace teachos::cio -{ - namespace - { - struct null_device final : public output_device - { - null_device static instance; - - auto write(std::string_view) -> void override {} - auto writeln(std::string_view) -> void override {} - - auto write_error(std::string_view) -> void override {} - auto writeln_error(std::string_view) -> void override {} - }; - - constinit null_device null_device::instance; - } // namespace - - // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) - constinit auto active_device = static_cast(&null_device::instance); - - auto set_output_device(output_device & device) -> std::optional - { - if (&device == active_device) - { - return {}; - } - return std::exchange(active_device, &device); - } - - auto print(std::string_view text) -> void - { - active_device->write(text); - } - - auto println(std::string_view text) -> void - { - active_device->writeln(text); - } - - auto print_error(std::string_view text) -> void - { - active_device->write_error(text); - } - - auto println_error(std::string_view text) -> void - { - active_device->writeln_error(text); - } - -} // namespace teachos::cio \ No newline at end of file diff --git a/kapi/src/system.cpp b/kapi/src/system.cpp deleted file mode 100644 index 3ae3f29..0000000 --- a/kapi/src/system.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include "kapi/system.hpp" - -#include "kapi/cio.hpp" -#include "kapi/cpu.hpp" - -namespace teachos::system -{ - - [[gnu::weak]] - auto panic(std::string_view message, std::source_location location) -> void - { - cio::println_error("!!!Kernel Panic!!! "); - cio::println_error(message); - cio::println_error(location.file_name()); - cio::println_error(location.function_name()); - - cpu::halt(); - } - -} // namespace teachos::system diff --git a/src/kapi/cio.cpp b/src/kapi/cio.cpp new file mode 100644 index 0000000..66493b6 --- /dev/null +++ b/src/kapi/cio.cpp @@ -0,0 +1,57 @@ +#include "kapi/cio.hpp" + +#include +#include +#include + +namespace teachos::cio +{ + namespace + { + struct null_device final : public output_device + { + null_device static instance; + + auto write(std::string_view) -> void override {} + auto writeln(std::string_view) -> void override {} + + auto write_error(std::string_view) -> void override {} + auto writeln_error(std::string_view) -> void override {} + }; + + constinit null_device null_device::instance; + } // namespace + + // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) + constinit auto active_device = static_cast(&null_device::instance); + + auto set_output_device(output_device & device) -> std::optional + { + if (&device == active_device) + { + return {}; + } + return std::exchange(active_device, &device); + } + + auto print(std::string_view text) -> void + { + active_device->write(text); + } + + auto println(std::string_view text) -> void + { + active_device->writeln(text); + } + + auto print_error(std::string_view text) -> void + { + active_device->write_error(text); + } + + auto println_error(std::string_view text) -> void + { + active_device->writeln_error(text); + } + +} // namespace teachos::cio \ No newline at end of file diff --git a/src/kapi/system.cpp b/src/kapi/system.cpp new file mode 100644 index 0000000..3ae3f29 --- /dev/null +++ b/src/kapi/system.cpp @@ -0,0 +1,20 @@ +#include "kapi/system.hpp" + +#include "kapi/cio.hpp" +#include "kapi/cpu.hpp" + +namespace teachos::system +{ + + [[gnu::weak]] + auto panic(std::string_view message, std::source_location location) -> void + { + cio::println_error("!!!Kernel Panic!!! "); + cio::println_error(message); + cio::println_error(location.file_name()); + cio::println_error(location.function_name()); + + cpu::halt(); + } + +} // namespace teachos::system -- cgit v1.2.3