wip: vectors, panics and lsp fix

This commit is contained in:
karina
2026-04-22 09:32:01 +04:00
parent 7aa49b37f5
commit 97538aa098
11 changed files with 234 additions and 11 deletions
+42 -10
View File
@@ -2,6 +2,12 @@ cmake_minimum_required(VERSION 3.20)
project(ksOSKernel LANGUAGES ASM C)
set(KERNEL_MODULE_NAME "Kernel")
# Where to emit compile_commands.json for SourceKit-LSP. Empty: Kernel/
# (ancestor of SWIFT_SOURCES). Override: -DKERNEL_COMPILE_COMMANDS_DIR=/path
# or env KERNEL_COMPILE_COMMANDS_DIR.
set(KERNEL_COMPILE_COMMANDS_DIR "" CACHE PATH
"Directory for Swift compile_commands.json (SourceKit-LSP)")
# --- Locate Swift toolchain with Embedded Swift stdlib ---
# Priority: cmake var > env var > auto-detect
if(NOT SWIFT_TOOLCHAIN AND DEFINED ENV{SWIFT_TOOLCHAIN})
@@ -52,6 +58,18 @@ endif()
message(STATUS "Swift: ${SWIFTC}")
message(STATUS "Swift resource dir: ${SWIFT_RESOURCE_DIR}")
# Hint for SourceKit: VSCode must use this toolchain (same as compile_commands),
# not Xcode's default; otherwise: "Loading the standard library failed" with Embedded.
get_filename_component(_SWIFT_IDE_PATH "${SWIFTC}" DIRECTORY)
set(_ide_hint
"# If SourceKit reports \"Loading the standard library failed\", set the Swift
# extension's swift.path to the directory on the line below and restart
# \"Swift: Restart SourceKit LSP\". (Embedded Swift — must match this toolchain, not Xcode.)
# Regenerated on each CMake configure.
")
string(APPEND _ide_hint "${_SWIFT_IDE_PATH}\n")
file(WRITE "${CMAKE_CURRENT_SOURCE_DIR}/../ide-swift-toolchain.txt" "${_ide_hint}")
# --- Build ---
add_compile_options(-ffreestanding -nostdlib -O0 -g)
@@ -64,9 +82,11 @@ add_link_options(
)
set(SWIFT_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/Source/kernel.swift
${CMAKE_CURRENT_SOURCE_DIR}/Source/Arch/dtb.swift
${CMAKE_CURRENT_SOURCE_DIR}/Source/IO/uart.swift
${CMAKE_CURRENT_SOURCE_DIR}/Source/Kernel.swift
${CMAKE_CURRENT_SOURCE_DIR}/Source/Arch/DTB.swift
${CMAKE_CURRENT_SOURCE_DIR}/Source/IO/UART.swift
${CMAKE_CURRENT_SOURCE_DIR}/Source/Arch/ExceptionContext.swift
${CMAKE_CURRENT_SOURCE_DIR}/Source/OS/panic.swift # idfk why but sourcekit-lsp dont see Panic.swift but see panic.swift perfectly
)
set(SWIFT_OBJ ${CMAKE_CURRENT_BINARY_DIR}/kernel_swift.o)
@@ -94,7 +114,7 @@ set_source_files_properties(${SWIFT_OBJ} PROPERTIES
GENERATED TRUE
)
add_executable(kernel.elf Source/Arch/entry.S Source/Support/stubs.c ${SWIFT_OBJ})
add_executable(kernel.elf Source/Arch/entry.S Source/Support/stubs.c Source/Arch/vectors.S ${SWIFT_OBJ})
add_custom_command(TARGET kernel.elf POST_BUILD
COMMAND ${LLVM_OBJCOPY} -O binary kernel.elf kernel.bin
@@ -109,8 +129,10 @@ set(SWIFT_ARGS
"\"-enable-experimental-feature\"" "\"Embedded\""
"\"-module-name\"" "\"${KERNEL_MODULE_NAME}\""
"\"-parse-as-library\""
"\"-wmo\""
"\"-import-bridging-header\"" "\"${_BRIDGING_HEADER}\""
"\"-Xcc\"" "\"-I${CMAKE_CURRENT_SOURCE_DIR}/../Common\""
"\"-Xcc\"" "\"-fno-stack-protector\""
"\"-resource-dir\"" "\"${SWIFT_RESOURCE_DIR}\""
)
foreach(_src IN LISTS SWIFT_SOURCES)
@@ -137,14 +159,24 @@ foreach(_src IN LISTS SWIFT_SOURCES)
math(EXPR _idx "${_idx} + 1")
endforeach()
if(DEFINED ENV{TEMP_DIR})
set(COMPDB_OUTPUT_DIR "$ENV{TEMP_DIR}/Kernel")
elseif(DEFINED ENV{BUILD_DIR})
set(COMPDB_OUTPUT_DIR "$ENV{BUILD_DIR}/temp/Kernel")
# Precedence: CMake -D > env > Kernel/ (ancestor of all SWIFT_SOURCES).
if(NOT KERNEL_COMPILE_COMMANDS_DIR STREQUAL "")
set(COMPDB_OUTPUT_DIR "${KERNEL_COMPILE_COMMANDS_DIR}")
elseif(DEFINED ENV{KERNEL_COMPILE_COMMANDS_DIR} AND NOT "$ENV{KERNEL_COMPILE_COMMANDS_DIR}" STREQUAL "")
set(COMPDB_OUTPUT_DIR "$ENV{KERNEL_COMPILE_COMMANDS_DIR}")
else()
set(COMPDB_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}")
set(COMPDB_OUTPUT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
endif()
# Same JSON twice: (1) optional Kernel/ or KERNEL_COMPILE_COMMANDS_DIR, (2) repo
# root. SourceKit/Cursor/VSCode Swift extension resolve compile_commands at the
# workspace root first; a DB only under Kernel/ often yields no language service
# and broken cross-file resolution (WMO) even though the build works.
set(KERNEL_COMPDB_CONTENT "[\n${COMPDB_ENTRIES}]\n")
file(GENERATE OUTPUT "${COMPDB_OUTPUT_DIR}/compile_commands.json"
CONTENT "[\n${COMPDB_ENTRIES}]\n"
CONTENT "${KERNEL_COMPDB_CONTENT}"
)
set(_REPO_COMPDB "${CMAKE_CURRENT_SOURCE_DIR}/../compile_commands.json")
file(GENERATE OUTPUT "${_REPO_COMPDB}"
CONTENT "${KERNEL_COMPDB_CONTENT}"
)