Are my CMakeLists.txt files correct for my C++ Kernel?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
kanrab
Posts: 1
Joined: Tue Dec 27, 2022 7:59 am

Are my CMakeLists.txt files correct for my C++ Kernel?

Post by kanrab »

For those who built their Kernel using C++ and CMake, can someone ensure that my CMakeLists.txt's file are correct? I'm unsure if I'm invoking the linker script correctly and
I'm unsure if I have my toolchain properly configured

I plan on this being a fully-fledged OS so I set up my `CMakeLists.txt` in the top level directory like:

Code: Select all

cmake_minimum_required(VERSION 3.20)
project(NakOS)

add_subdirectory(Kernel)
Then in my `Project/Kernel/CMakeLists.txt`. This is where I add the bootstrapped assembly as a source file and where I invoke the linker script.

Code: Select all

set(KERNEL_SOURCES
    ${CMAKE_SOURCE_DIR/Boot/boot.s} # Bootstrapped Assembly
    ${CMAKE_SOURCE_DIR/Kernel/src/kernel_main.cpp}
)

add_executable(nak ${KERNEL_SOURCES})

target_include_directories(nak PUBLIC ${CMAKE_SOURCE_DIR} )

target_compile_options(nak PRIVATE 
    $<$<CONFIG:Debug>:
    -fsanitize=address,leak,undefined -pedantic -Wall -Wextra -Wundef -Werror -Wno-unused-parameter -ffreestanding -fno-exceptions -fno-rtti
    >
)

target_link_options(nak PRIVATE 
    $<$<CONFIG:Debug>:
        -fsanitize=address,leak,undefined -ffreestanding -nostdlib -T loader.ld -T ${CMAKE_CURRENT_SOURCE_DIR}/Boot/loader.ld # specifies linker script
    >
)

target_compile_features(nak
    PRIVATE
        cxx_std_20
)
In my original project, I had a bash script that looks something like

Code: Select all

#/bin/bash

export PREFIX="$HOME/opt/cross"
export TARGET=i686-elf
export PATH="$PREFIX/bin:$PATH"

# Use the cross compiler
export CXX="$HOME/opt/cross/bin/$TARGET-gcc"
But I replaced it with the following Toolchain file (and I think this is okay?)

Code: Select all

set(CMAKE_SYSTEM_PROCESSOR i686)
set(CMAKE_C_COMPILER ${CMAKE_SOURCE_DIR}/Toolchain/opt/cross/bin/i686-elf-gcc)
set(CMAKE_CXX_COMPILER ${CMAKE_SOURCE_DIR}/Toolchain/opt/cross/bin/i686-elf-g++)
I then invoke `CMake` with

Code: Select all

cmake -B build -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=1 . -GNinja -DCMAKE_TOOLCHAIN_FILE=Toolchain/os-dev-toolchain.cmake
Is there anything wrong with these files?
Post Reply