-
Notifications
You must be signed in to change notification settings - Fork 11
/
CMakeLists.txt
143 lines (119 loc) · 4.97 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
cmake_minimum_required(VERSION 3.10)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/toolchain/CMake")
include(ExternalProject)
set(CMAKE_C_STANDARD 23)
set(CMAKE_C_FLAGS_DEBUG "-g -O0 -Werror")
set(CMAKE_C_FLAGS_RELEASE "-O3")
set(limine_cfg_file "${CMAKE_SOURCE_DIR}/kernel/limine.cfg")
set(limine_dir "${CMAKE_BINARY_DIR}/limine")
set(iso_file "${CMAKE_SOURCE_DIR}/ngos.iso")
set(iso_dir "${CMAKE_BINARY_DIR}/system_root")
set(autogen_include_dir "${CMAKE_BINARY_DIR}/autogenerated")
set(system_boot_dir "${iso_dir}/boot")
set(system_include_dir "${iso_dir}/include")
set(system_lib_dir "${iso_dir}/lib")
set(system_bin_dir "${iso_dir}/bin")
set(initrd_file ${system_boot_dir}/initrd.tar)
file(MAKE_DIRECTORY ${iso_dir}/boot/limine)
file(MAKE_DIRECTORY ${autogen_include_dir})
file(MAKE_DIRECTORY ${system_include_dir})
file(MAKE_DIRECTORY ${system_lib_dir})
file(MAKE_DIRECTORY ${system_bin_dir})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${system_bin_dir})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${system_lib_dir})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${system_lib_dir})
set(EXECUTABLE_OUTPUT_PATH ${system_bin_dir})
set(LIBRARY_OUTPUT_PATH ${system_lib_dir})
file(COPY ${limine_cfg_file} DESTINATION ${system_boot_dir})
file(COPY ${CMAKE_SOURCE_DIR}/include DESTINATION ${iso_dir})
set(CMAKE_C_FLAGS_DEBUG "-g -O0 -Werror")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "-g")
message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
# We do this as a manual git download as a workaround for the fact that ExternalProject_Add
# doesn't support shallow clones, or more accurately lies about its support for shallow clones.
# See: https://gitlab.kitware.com/cmake/cmake/-/issues/17770
if (NOT EXISTS ${limine_dir})
ExternalProject_Add(limine
SOURCE_DIR "${limine_dir}"
DOWNLOAD_COMMAND git clone --depth 1 --branch v7.x-binary https://github.com/limine-bootloader/limine.git ${limine_dir}
CONFIGURE_COMMAND ""
BUILD_IN_SOURCE 1
BUILD_COMMAND make
INSTALL_COMMAND ""
UPDATE_COMMAND ""
)
endif ()
project(nightingale C ASM)
execute_process(
COMMAND git describe --tags
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE nightingale_version
OUTPUT_STRIP_TRAILING_WHITESPACE
)
configure_file(include/version.h.in ${system_include_dir}/version.h)
configure_file(include/version.h.in ${autogen_include_dir}/version.h)
list(APPEND nightingale_autogenerated_headers
${system_include_dir}/autogenerated_errnos.h
${system_include_dir}/autogenerated_syscall_consts.h
${system_include_dir}/autogenerated_syscalls_kernel.h
${system_include_dir}/autogenerated_syscalls_user.h
${system_include_dir}/autogenerated_errnos.c
${system_include_dir}/autogenerated_syscall_names.c
${system_include_dir}/autogenerated_syscalls_kernel.c
${system_include_dir}/autogenerated_syscalls_user.c
)
add_custom_command(
OUTPUT ${nightingale_autogenerated_headers}
COMMAND ${CMAKE_SOURCE_DIR}/script/generate_syscalls.rb ${system_include_dir}
COMMAND ${CMAKE_SOURCE_DIR}/script/generate_syscalls.rb ${autogen_include_dir}
DEPENDS ${CMAKE_SOURCE_DIR}/script/generate_syscalls.rb
DEPENDS interface/SYSCALLS interface/ERRNOS
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
add_custom_target(generate_headers DEPENDS ${nightingale_autogenerated_headers})
add_compile_options(
-Wall
-Wextra
-ffreestanding
-fstrict-aliasing
-nostdlib
-nostdinc
-Wno-unused-variable
-Wno-unused-parameter
-Wno-sign-compare
-Wno-address-of-packed-member
-Wno-deprecated-non-prototype
)
add_compile_definitions(
__nightingale__=1
_NG_SOURCE=1
)
set(CMAKE_EXE_LINKER_FLAGS "-static")
include_directories(include)
include_directories(${autogen_include_dir})
add_subdirectory(kernel)
add_subdirectory(libc)
add_subdirectory(linker)
add_subdirectory(user)
add_dependencies(nightingale_kernel generate_headers)
add_dependencies(c generate_headers)
add_dependencies(elf generate_headers)
# Here we need to depend on initrd_file and not initrd because DEPENDS only directly specifies that this
# any target that uses this command must be run AFTER the dependencies and you only get an actual file
# dependency on the result if the target is an executable, library, or file.
# Essentially, having a custom_command DEPEND on a custom_target is a footgun.
add_custom_command(
DEPENDS limine nightingale_kernel ${initrd_file} kernel_modules ${limine_cfg_file}
OUTPUT ${iso_file}
COMMAND cp ${limine_dir}/limine-bios.sys ${iso_dir}/boot/limine
COMMAND cp ${limine_dir}/limine-bios-cd.bin ${iso_dir}/boot/limine
COMMAND cp ${limine_dir}/limine-uefi-cd.bin ${iso_dir}/boot/limine
COMMAND xorriso -as mkisofs -b boot/limine/limine-bios-cd.bin
-no-emul-boot -boot-load-size 4 --boot-info-table
--efi-boot boot/limine/limine-uefi-cd.bin
-efi-boot-part --efi-boot-image --protective-msdos-label
"${iso_dir}" -o "${iso_file}"
COMMAND ${limine_dir}/limine bios-install ${iso_file}
VERBATIM
)
add_custom_target(iso DEPENDS ${iso_file})