Page 1 of 1

How can I replace meaty skeleton's build process with cmake?

Posted: Fri Jun 21, 2024 1:48 pm
by mediocrevegetable1
I'm attempting to write a new operating system with a similar, but not identical, structure to Meaty Skeleton. I would like to use CMake only to build the operating system as opposed to Makefiles and shell scripts. This is my directory structure:

Code: Select all

MyOS
│   .clang-format
│   CMakeLists.txt
│   foo.txt
│
├───kernel
│   │   CMakeLists.txt
│   │
│   ├───include
│   │   └───vga
│   │           text.h
│   │
│   └───src
│       │   boot.asm
│       │   CMakeLists.txt
│       │   crti.asm
│       │   crti.o
│       │   crtn.asm
│       │   crtn.o
│       │   linker.ld
│       │   main.c
│       │
│       └───vga
│           └───text
│                   text.c
│
└───libc
    │   CMakeLists.txt
    │
    ├───include
    │   │   stdio.h
    │   │   string.h
    │   │
    │   └───sys
    │           cdefs.h
    │
    └───src
        │   CMakeLists.txt
        │
        ├───stdio
        │       fputs.c
        │       putchar.c
        │       puts.c
        │
        └───string
                memmove.c
(Note: boot.asm, crti.asm and crtn.asm are to be assembled using NASM).

These are the relevent CMake files:

Code: Select all

# MyOS/CMakeLists.txt

cmake_minimum_required(VERSION 3.14)

project(
    MyOS
    VERSION 0.1
    DESCRIPTION "A Hobby OS written in C"
    LANGUAGES C ASM_NASM)

set(CMAKE_C_COMPILER i686-elf-gcc)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)

if(MSVC)
    set(${PROJECT_NAME}_C_WARNINGS "/W4 /WX")
else()
    set(${PROJECT_NAME}_C_WARNINGS "-Wall -Wextra -pedantic-errors")
endif()
set(${PROJECT_NAME}_NASM_WARNINGS "-w+all")

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

add_subdirectory(kernel)
add_subdirectory(libc)

Code: Select all

# MyOS/kernel/CMakeLists.txt

add_subdirectory(src)

Code: Select all

# MyOS/kernel/src/CMakeLists.txt

file(GLOB_RECURSE HEADER_LIST ../include/*.h ../../libc/include)

add_library(kernel OBJECT
    vga/text/text.c
    ${HEADER_LIST})
target_include_directories(src
    PRIVATE
        ${CMAKE_SOURCE_DIR}/libc/include
    PUBLIC
        ../include)
target_link_libraries(src ${PROJECT_NAME}-libc)
target_compile_options(src PRIVATE ${${PROJECT_NAME}_C_WARNINGS})

Code: Select all

# MyOS/libc/CMakeLists.txt

configure_file(include/stdio.h "${CMAKE_SOURCE_DIR}/sysroot/usr/include/stdio.h" COPYONLY)
configure_file(include/stdio.h "${CMAKE_SOURCE_DIR}/sysroot/usr/include/string.h" COPYONLY)
configure_file(include/sys/cdefs.h "${CMAKE_SOURCE_DIR}/sysroot/usr/include/sys/cdefs.h" COPYONLY)

add_subdirectory(src)

Code: Select all

# MyOS/libc/src/CMakeLists.txt

file(GLOB_RECURSE HEADER_LIST "../include/*.h" "${CMAKE_SOURCE_DIR}/kernel/include/*.h")

add_library(${PROJECT_NAME}-libc STATIC
    stdio/fputs.c
    stdio/putchar.c
    stdio/puts.c
    
    string/memmove.c

    ${HEADER_LIST})
target_include_directories(${PROJECT_NAME}-libc
    PUBLIC
        ../include
        ${CMAKE_SOURCE_DIR})
target_compile_options(${PROJECT_NAME}-libc PRIVATE ${${PROJECT_NAME}_C_WARNINGS})
I'm not really sure what else to do from here, though. Files like *.asm and linker.ld still aren't dealt with and the whole thing just needs to be brought together, and I'm not entirely sure how to do that with CMake (I also find meaty skeleton's build files a bit hard to understand in general, which isn't making my life any easier).

What I would like to know is, are the CMakeLists.txt files I've written thus far okay, and what else do I need to add to complete the build? I'd be happy to share any other files or details if needed. As far as I've checked, I cannot find a similar question on this forum. I found one where someone claimed to have ported the build process to CMake, but didn't share any files. Thank you in advance.