From 6ffe96d1e3f0289c2070e050cf8e1a30288e6d91 Mon Sep 17 00:00:00 2001 From: Alexey Gavrilov Date: Fri, 27 Mar 2026 14:03:55 +0700 Subject: Cmake build generator support: build stage Multi-target architecture implementation: each root source code directory presents the one *INTERFACE* target to it's subdirectory subtargets that link only as *STATIC* library. For instance, *arch* directory presents *Architecture* target then *Architecture* target has *CPUID*, *IO*, *PORT* and etc. dependencies Exact order of linking targets is: boot.o CRTIBUNDLE CRTNBUNDLE. Sequence destruction cause to UB. Please to pay attention to this. TODO - Debug information - Build type - Toolchain file - Image creation - Build by WinGW compiler on Windows platform (optional) Signed-off-by: Alexey Gavrilov --- arch/CMakeLists.txt | 17 +++++++++++++++++ arch/SecondaryInit.h | 2 +- arch/x86/CMakeLists.txt | 34 ++++++++++++++++++++++++++++++++++ arch/x86/boot.nasm | 16 ++++++++++++++++ arch/x86/cpuid/CMakeLists.txt | 25 +++++++++++++++++++++++++ arch/x86/cpuid/Cpuid.h | 2 +- arch/x86/cpuid/CpuidCheck.nasm | 21 +++++++++++++++++++++ arch/x86/cpuid/CpuidCheck.s | 21 --------------------- arch/x86/gdt/CMakeLists.txt | 23 +++++++++++++++++++++++ arch/x86/gdt/Gdt.h | 2 +- arch/x86/gdt/SwitchToGdt.nasm | 13 +++++++++++++ arch/x86/gdt/SwitchToGdt.s | 13 ------------- arch/x86/ic/CMakeLists.txt | 16 ++++++++++++++++ arch/x86/ic/IPic.h | 2 +- arch/x86/idt/CMakeLists.txt | 15 +++++++++++++++ arch/x86/io/CMakeLists.txt | 16 ++++++++++++++++ arch/x86/io/VGADisplayInfo.h | 2 +- arch/x86/msr/CMakeLists.txt | 15 +++++++++++++++ arch/x86/port/CMakeLists.txt | 16 ++++++++++++++++ arch/x86/port/PortOps.h | 2 +- 20 files changed, 233 insertions(+), 40 deletions(-) create mode 100644 arch/CMakeLists.txt create mode 100644 arch/x86/CMakeLists.txt create mode 100644 arch/x86/boot.nasm create mode 100644 arch/x86/cpuid/CMakeLists.txt create mode 100644 arch/x86/cpuid/CpuidCheck.nasm delete mode 100644 arch/x86/cpuid/CpuidCheck.s create mode 100644 arch/x86/gdt/CMakeLists.txt create mode 100644 arch/x86/gdt/SwitchToGdt.nasm delete mode 100644 arch/x86/gdt/SwitchToGdt.s create mode 100644 arch/x86/ic/CMakeLists.txt create mode 100644 arch/x86/idt/CMakeLists.txt create mode 100644 arch/x86/io/CMakeLists.txt create mode 100644 arch/x86/msr/CMakeLists.txt create mode 100644 arch/x86/port/CMakeLists.txt (limited to 'arch') diff --git a/arch/CMakeLists.txt b/arch/CMakeLists.txt new file mode 100644 index 0000000..c8719ce --- /dev/null +++ b/arch/CMakeLists.txt @@ -0,0 +1,17 @@ +add_library(Architecture INTERFACE) + +add_library(Initialization STATIC) +target_link_libraries(Initialization PUBLIC CXX_COMPILER_REQUIREMENTS) + +target_sources(Initialization PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/SecondaryInit.cpp +) + +target_sources(Initialization + PUBLIC + FILE_SET HEADERS + FILES + ${CMAKE_CURRENT_LIST_DIR}/SecondaryInit.h +) + +add_subdirectory(${ARCH}) diff --git a/arch/SecondaryInit.h b/arch/SecondaryInit.h index 53ff191..104f44c 100644 --- a/arch/SecondaryInit.h +++ b/arch/SecondaryInit.h @@ -1,6 +1,6 @@ -#include #include "../kernel/mem/Segments.h" #include "../kernel/mem/Kheap.h" +#include #if defined(__IAPC) #include "x86/PrimaryInit.h" diff --git a/arch/x86/CMakeLists.txt b/arch/x86/CMakeLists.txt new file mode 100644 index 0000000..5601efa --- /dev/null +++ b/arch/x86/CMakeLists.txt @@ -0,0 +1,34 @@ +target_link_options(CXX_LINKER_REQUIREMENTS INTERFACE + LINKER:-L${CMAKE_CURRENT_LIST_DIR} +) + +target_sources(Initialization PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/PrimaryInit.cpp +) + +target_sources(Initialization + PUBLIC + FILE_SET HEADERS + FILES + ${CMAKE_CURRENT_LIST_DIR}/PrimaryInit.h +) + +target_link_libraries(Architecture INTERFACE Initialization) + +# Create library for assembler build +add_library(ArchitectureAssemblerSources STATIC) + +add_library(Boot OBJECT) +target_sources(Boot PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/boot.nasm +) + +add_subdirectory(cpuid) +add_subdirectory(idt) +add_subdirectory(gdt) +add_subdirectory(ic) +add_subdirectory(io) +add_subdirectory(msr) +add_subdirectory(port) + +target_link_libraries(Architecture INTERFACE ArchitectureAssemblerSources) diff --git a/arch/x86/boot.nasm b/arch/x86/boot.nasm new file mode 100644 index 0000000..11175af --- /dev/null +++ b/arch/x86/boot.nasm @@ -0,0 +1,16 @@ +section .text +extern main, _estack, _init +global _start:function +_start: + ; Stack setup + mov eax, _estack + ; Align stack to odd address + and eax, 0xFFFFFFFE + mov esp, eax + + call _init + call main + cli +.hang: hlt + jmp .hang +; .end: diff --git a/arch/x86/cpuid/CMakeLists.txt b/arch/x86/cpuid/CMakeLists.txt new file mode 100644 index 0000000..e8528e8 --- /dev/null +++ b/arch/x86/cpuid/CMakeLists.txt @@ -0,0 +1,25 @@ +add_library(CPUID STATIC) +target_link_libraries(CPUID PUBLIC CXX_COMPILER_REQUIREMENTS) + +target_sources(CPUID PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/Cpuid.cpp +) + +target_sources(CPUID + PUBLIC + FILE_SET HEADERS + BASE_DIRS + ${CMAKE_CURRENT_LIST_DIR} + FILES + ${CMAKE_CURRENT_LIST_DIR}/Cpuid.h +) + +target_link_libraries(Architecture INTERFACE CPUID) + +add_library(CPUIDASM OBJECT) + +target_sources(CPUIDASM PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/CpuidCheck.nasm +) + +target_sources(ArchitectureAssemblerSources PRIVATE $) diff --git a/arch/x86/cpuid/Cpuid.h b/arch/x86/cpuid/Cpuid.h index a65cdc8..c8ad4aa 100644 --- a/arch/x86/cpuid/Cpuid.h +++ b/arch/x86/cpuid/Cpuid.h @@ -1,6 +1,6 @@ #ifndef CPUID_H #define CPUID_H -#include +#include #include // To declare cpuid::Executor::Executor() diff --git a/arch/x86/cpuid/CpuidCheck.nasm b/arch/x86/cpuid/CpuidCheck.nasm new file mode 100644 index 0000000..67bc9eb --- /dev/null +++ b/arch/x86/cpuid/CpuidCheck.nasm @@ -0,0 +1,21 @@ +global CPUIDCHK:function +CPUIDCHK: + push ebx + pushfd + pop eax + mov ebx, eax + xor eax, 0x200000 + push eax + popfd + pushfd + pop eax + xor eax, ebx + je nocpuid + mov eax, 1 + jmp restore +nocpuid: + mov eax, 0 +restore: + pop ebx + ret +.endchk: diff --git a/arch/x86/cpuid/CpuidCheck.s b/arch/x86/cpuid/CpuidCheck.s deleted file mode 100644 index 67bc9eb..0000000 --- a/arch/x86/cpuid/CpuidCheck.s +++ /dev/null @@ -1,21 +0,0 @@ -global CPUIDCHK:function -CPUIDCHK: - push ebx - pushfd - pop eax - mov ebx, eax - xor eax, 0x200000 - push eax - popfd - pushfd - pop eax - xor eax, ebx - je nocpuid - mov eax, 1 - jmp restore -nocpuid: - mov eax, 0 -restore: - pop ebx - ret -.endchk: diff --git a/arch/x86/gdt/CMakeLists.txt b/arch/x86/gdt/CMakeLists.txt new file mode 100644 index 0000000..57e3c26 --- /dev/null +++ b/arch/x86/gdt/CMakeLists.txt @@ -0,0 +1,23 @@ +add_library(GDT STATIC) +target_link_libraries(GDT PUBLIC CXX_COMPILER_REQUIREMENTS) + +target_sources(GDT PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/Gdt.cpp +) + +target_sources(GDT + PUBLIC + FILE_SET HEADERS + FILES + ${CMAKE_CURRENT_LIST_DIR}/Gdt.h +) + +target_link_libraries(Architecture INTERFACE GDT) + +add_library(GDTASM OBJECT) + +target_sources(GDTASM PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/SwitchToGdt.nasm +) + +target_sources(ArchitectureAssemblerSources PRIVATE $) diff --git a/arch/x86/gdt/Gdt.h b/arch/x86/gdt/Gdt.h index 321d67c..8f1d967 100755 --- a/arch/x86/gdt/Gdt.h +++ b/arch/x86/gdt/Gdt.h @@ -1,7 +1,7 @@ #ifndef GDT_H #define GDT_H -#include +#include #include diff --git a/arch/x86/gdt/SwitchToGdt.nasm b/arch/x86/gdt/SwitchToGdt.nasm new file mode 100644 index 0000000..52ebbc7 --- /dev/null +++ b/arch/x86/gdt/SwitchToGdt.nasm @@ -0,0 +1,13 @@ +global SWITCHTOGDT +SWITCHTOGDT: + lgdt [esp + 4] + jmp 0x08:.reloadCS +.reloadCS: + mov ax, 0x10 + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + mov ss, ax + ret +.end: diff --git a/arch/x86/gdt/SwitchToGdt.s b/arch/x86/gdt/SwitchToGdt.s deleted file mode 100644 index 52ebbc7..0000000 --- a/arch/x86/gdt/SwitchToGdt.s +++ /dev/null @@ -1,13 +0,0 @@ -global SWITCHTOGDT -SWITCHTOGDT: - lgdt [esp + 4] - jmp 0x08:.reloadCS -.reloadCS: - mov ax, 0x10 - mov ds, ax - mov es, ax - mov fs, ax - mov gs, ax - mov ss, ax - ret -.end: diff --git a/arch/x86/ic/CMakeLists.txt b/arch/x86/ic/CMakeLists.txt new file mode 100644 index 0000000..e6130d7 --- /dev/null +++ b/arch/x86/ic/CMakeLists.txt @@ -0,0 +1,16 @@ +add_library(IC STATIC) +target_link_libraries(IC PUBLIC CXX_COMPILER_REQUIREMENTS) + +target_sources(IC PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/Pic.cpp +) + +target_sources(IC + PUBLIC + FILE_SET HEADERS + FILES + ${CMAKE_CURRENT_LIST_DIR}/Pic.h + ${CMAKE_CURRENT_LIST_DIR}/IPic.h +) + +target_link_libraries(Architecture INTERFACE IC) diff --git a/arch/x86/ic/IPic.h b/arch/x86/ic/IPic.h index 9bb1b4f..5e0449b 100644 --- a/arch/x86/ic/IPic.h +++ b/arch/x86/ic/IPic.h @@ -1,7 +1,7 @@ #ifndef IPIC_H #define IPIC_H -#include +#include // Defines diff --git a/arch/x86/idt/CMakeLists.txt b/arch/x86/idt/CMakeLists.txt new file mode 100644 index 0000000..da7bdae --- /dev/null +++ b/arch/x86/idt/CMakeLists.txt @@ -0,0 +1,15 @@ +add_library(IDT STATIC) +target_link_libraries(IDT PUBLIC CXX_COMPILER_REQUIREMENTS) + +target_sources(IDT PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/Idt.cpp +) + +target_sources(IDT + PUBLIC + FILE_SET HEADERS + FILES + ${CMAKE_CURRENT_LIST_DIR}/Idt.h +) + +target_link_libraries(Architecture INTERFACE IDT) diff --git a/arch/x86/io/CMakeLists.txt b/arch/x86/io/CMakeLists.txt new file mode 100644 index 0000000..9c2dd33 --- /dev/null +++ b/arch/x86/io/CMakeLists.txt @@ -0,0 +1,16 @@ +add_library(IO STATIC) +target_link_libraries(IO PUBLIC CXX_COMPILER_REQUIREMENTS) + +target_sources(IO PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/VGADisplay.cpp +) + +target_sources(IO + PUBLIC + FILE_SET HEADERS + FILES + ${CMAKE_CURRENT_LIST_DIR}/VGADisplay.h + ${CMAKE_CURRENT_LIST_DIR}/VGADisplayInfo.h +) + +target_link_libraries(Architecture INTERFACE IO) diff --git a/arch/x86/io/VGADisplayInfo.h b/arch/x86/io/VGADisplayInfo.h index 875e7e5..55303dd 100644 --- a/arch/x86/io/VGADisplayInfo.h +++ b/arch/x86/io/VGADisplayInfo.h @@ -1,7 +1,7 @@ #ifndef VGAINFOCLASS_H #define VGAINFOCLASS_H -#include +#include #define TOBG(attr) attr << 4 diff --git a/arch/x86/msr/CMakeLists.txt b/arch/x86/msr/CMakeLists.txt new file mode 100644 index 0000000..86d3330 --- /dev/null +++ b/arch/x86/msr/CMakeLists.txt @@ -0,0 +1,15 @@ +add_library(MSR STATIC) +target_link_libraries(MSR PUBLIC CXX_COMPILER_REQUIREMENTS) + +target_sources(MSR PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/Msr.cpp +) + +target_sources(MSR + PUBLIC + FILE_SET HEADERS + FILES + ${CMAKE_CURRENT_LIST_DIR}/Msr.h +) + +target_link_libraries(Architecture INTERFACE MSR) diff --git a/arch/x86/port/CMakeLists.txt b/arch/x86/port/CMakeLists.txt new file mode 100644 index 0000000..6ad61ef --- /dev/null +++ b/arch/x86/port/CMakeLists.txt @@ -0,0 +1,16 @@ +add_library(Port STATIC) +target_link_libraries(Port PUBLIC CXX_COMPILER_REQUIREMENTS) + +target_sources(Port PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/PortOps.cpp +) + +target_sources(Port + PUBLIC + FILE_SET HEADERS + FILES + ${CMAKE_CURRENT_LIST_DIR}/PortOps.h + ${CMAKE_CURRENT_LIST_DIR}/Operations +) + +target_link_libraries(Architecture INTERFACE Port) diff --git a/arch/x86/port/PortOps.h b/arch/x86/port/PortOps.h index 7db3a5f..f8dac63 100644 --- a/arch/x86/port/PortOps.h +++ b/arch/x86/port/PortOps.h @@ -1,7 +1,7 @@ #ifndef PORT_H #define PORT_H -#include +#include using regb = uint8; -- cgit v1.2.3