# The miscellaneous library shared between the Luna kernel and userspace, with stuff such as custom types, common routines and data structures.

file(GLOB HEADERS include/luna/*.h)

set(FREESTANDING_SOURCES
    ${HEADERS}
    src/CRC32.cpp
    src/EscapeSequence.cpp
    src/Format.cpp
    src/Sort.cpp
    src/NumberParsing.cpp
    src/CString.cpp
    src/CPath.cpp
    src/Units.cpp
    src/SystemError.cpp
    src/Bitmap.cpp
    src/Buffer.cpp
    src/Scanf.cpp
    src/Stack.cpp
    src/String.cpp
    src/StringBuilder.cpp
    src/StringView.cpp
    src/Utf8.cpp
    src/TarStream.cpp
    src/DebugLog.cpp
    src/Heap.cpp
    src/Spinlock.cpp
    src/PathParser.cpp
    src/UBSAN.cpp
    src/Base64.cpp
    src/Hash.cpp
)

set(SOURCES
    ${FREESTANDING_SOURCES}
    src/CppABI.cpp
    src/ImplPOSIX.cpp
)

add_library(luna-freestanding ${FREESTANDING_SOURCES})
target_compile_definitions(luna-freestanding PRIVATE USE_FREESTANDING)
target_compile_options(luna-freestanding PRIVATE ${COMMON_FLAGS})
target_compile_options(luna-freestanding PRIVATE -nostdlib -mcmodel=kernel -ffreestanding)

target_include_directories(luna-freestanding PUBLIC include/)
target_include_directories(luna-freestanding PRIVATE ${LUNA_ROOT}/kernel/src)
set_target_properties(luna-freestanding PROPERTIES CXX_STANDARD 20)

add_library(luna ${SOURCES})
target_compile_options(luna PRIVATE ${COMMON_FLAGS})
target_include_directories(luna PUBLIC include/)
target_include_directories(luna PUBLIC ${LUNA_BASE}/usr/include)

if("${LUNA_ARCH}" MATCHES "x86_64")
target_compile_options(luna-freestanding PRIVATE -mno-red-zone)
target_compile_options(luna-freestanding PRIVATE -mno-80387 -mno-mmx -mno-sse -mno-sse2)
target_compile_definitions(luna-freestanding PUBLIC ARCH_X86_64)
target_compile_definitions(luna PUBLIC ARCH_X86_64)
endif()

add_custom_command(
    TARGET luna
    COMMAND "${CMAKE_COMMAND}" -E copy ${CMAKE_CURRENT_BINARY_DIR}/libluna.a ${LUNA_BASE}/usr/lib/libluna.a
)