You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.2 KiB
CMake

# Copyright (C) 2021,2022 fef <owo@fef.moe>. All rights reserved.
# Look, i have absolutely zero patience when it comes to fucking around with
# build tools. This is most probably not the correct way to do it (especially
# with the cross compiler setup in arch/<arch>/config/toolchain.cmake), but i
# literally do not care. After hours of Duck Duck Googling random shit, i
# finally got this to work (kind of). If you know your way around CMake, you
# are more than welcome to submit a PR that fixes this mess, i would appreciate
# it very much. But i certainly don't have the energy for it.
cmake_minimum_required(VERSION 3.14.0)
project(gaybsd VERSION 0.1.0 LANGUAGES C ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_EXTENSIONS ON)
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
endif()
include(cmake/git.cmake)
include(cmake/util.cmake)
include(cmake/config.cmake)
configure_file(
"${CMAKE_SOURCE_DIR}/include/gay/config.h.in"
"${CMAKE_BINARY_DIR}/include/gay/config.h"
)
include("arch/${ARCH}/config/toolchain.cmake")
gay_append_cmake_c_flags(
-ggdb
-pipe
-nostdinc
-ffreestanding
-fno-stack-protector
-Wused-but-marked-unused
# Used by the KASSERT macro for printing the filename relative to the source root
"-D__FILENAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'"
)
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -pipe -D_ASM_SOURCE")
if(DEBUG)
# -Og on gcc optimizes for the best debugging experience, but apparently this
# is treated the same as -O1 on clang for some reason. The LLVM manual says
# you're supposed to use -O0 in order to get the most submissive and debuggable
# code instead:
# <https://clang.llvm.org/docs/CommandGuide/clang.html#code-generation-options>
gay_append_cmake_c_flags(-O0)
# i don't think -O0 does anything for assembly code, but it can't hurt to
# have it in there just in case (somebody check on this at some point plz)
gay_append_cmake_asm_flags(-O0)
else()
gay_append_cmake_c_flags(-O2)
endif()
# --whole-archive ensures no (seemingly) unused symbols
# like the multiboot table are omitted from the archive
set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} -ffreestanding -static --whole-archive")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -ffreestanding -nostdlib -static")
set(GAY_INCLUDE_DIRS
"${CMAKE_SOURCE_DIR}/include"
"${CMAKE_SOURCE_DIR}/arch/${ARCH}/include"
"${CMAKE_BINARY_DIR}/include"
)
set(GAY_DEFINITIONS
"_GAY_SOURCE=202109L"
)
set(GAY_KERNEL_DEFINITIONS
"_KERNEL" # FreeBSD uses the same macro name
${GAY_DEFINITIONS}
)
include("lib/lib.cmake")
add_subdirectory("arch/${ARCH}")
add_subdirectory("kernel")
add_executable(gaybsd_image.elf)
# TODO: find a way around this hack
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/_empty.c" "")
target_sources(gaybsd_image.elf PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/_empty.c")
target_link_libraries(gaybsd_image.elf PRIVATE
gay_arch
gay_kernel
# libgcc is needed for some internal stuff like 64-bit division which
# isn't natively supported by the CPU instruction set (on i686).
# Apparently, clang uses that too.
gcc
)
add_custom_target(gaybsd ALL DEPENDS
gaybsd_image.elf
)