-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
253 lines (218 loc) · 6.73 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
cmake_minimum_required(VERSION 3.10)
project(ByteOS C ASM)
message(STATUS "==========================")
message(STATUS " Compiling ByteOS ")
message(STATUS "==========================")
# ----------------------------------------------------------------
# Set Kernel Options
# ----------------------------------------------------------------
set(KernelArch ${KERNEL_TARGET})
# Declare Variables
set(LINKER_SCRIPT_DIR "${PROJECT_SOURCE_DIR}/linker")
set(LINKER_FILE "${LINKER_SCRIPT_DIR}/linker-${KernelArch}.ld")
set(CMAKE_SYSTEM_PROCESSOR ${KernelArch})
# Use c23 standard
set(CMAKE_C_STANDARD 23)
set(CMAKE_OBJCOPY llvm-objcopy)
# set(TOOLS_PREFIX "${KernelArch}-linux-gnu-")
message(STATUS "TOOLS_PREFIX ${TOOLS_PREFIX}")
set(CMAKE_ASM_COMPILER ${TOOLS_PREFIX}gcc)
set(CMAKE_C_COMPILER ${TOOLS_PREFIX}gcc)
# set(CMAKE_ASM_COMPILER clang)
# set(CMAKE_C_COMPILER clang)
# set(CMAKE_EXE_LINKER_FLAGS -fuse-ld=lld)
# Set compile options
set(CMAKE_C_FLAGS "")
set(CMAKE_EXE_LINKER_FLAGS "-T ${LINKER_FILE} -nostdlib -nostartfiles -ffreestanding")
list(REMOVE_ITEM CMAKE_C_IMPLICIT_LINK_LIBRARIES stdc)
add_compile_options(
# -------------------------------------------
# Compile Options for Warnings
# -------------------------------------------
# -Wall
-Werror
# -Wmissing-declarations
-Wundef
-Wpointer-arith
-Wno-nonnull
# -------------------------------------------
# Compile Options for Machine
# -------------------------------------------
-O2
-fno-builtin
-ffreestanding
# -------------------------------------------
# Compile Options for architecture
# -------------------------------------------
# -target ${KernelArch}-elf
)
# Aarch64 options
if(KernelArch STREQUAL "aarch64")
add_compile_options(
-mgeneral-regs-only
)
elseif(KernelArch STREQUAL "riscv64")
add_compile_options(
-mcmodel=medany
# -march=rv64gc
# -march=rv64imac
# -mabi=lp64
# -mfloat-abi=hard
# -msoft-float
)
endif()
# For vscode
add_definitions(-DCMAKE_EXPORT_COMPILE_COMMANDS=ON)
include(config.cmake)
# Macro to add source files to Variables
# WARN: Dont use function to replace macro.
macro(add_source_directories)
FOREACH(dir_name ${SOURCE_DIRS})
file(GLOB_RECURSE FUNC_ASM_SOURCES "${CMAKE_SOURCE_DIR}/${dir_name}/*.S")
file(GLOB_RECURSE FUNC_C_SOURCES "${CMAKE_SOURCE_DIR}/${dir_name}/*.c")
list(APPEND ASM_SOURCES ${FUNC_ASM_SOURCES})
list(APPEND C_SOURCES ${FUNC_C_SOURCES})
ENDFOREACH()
endmacro()
# find all source files
set(SOURCE_DIRS
arch/${KernelArch}
drivers
kernel
libs/buddy_alloc
libs/smoldtb
libs/std_impl
libs/lwext4
libs/elf_parser
)
add_source_directories()
# Set include folders
include_directories(
includes
arch/${KernelArch}/includes
drivers/includes
libs/smoldtb
libs/memory
libs/std_impl
libs/buddy_alloc
${CMAKE_CURRENT_BINARY_DIR}/generated
libs/lwext4/includes
libs/elf_parser/includes
)
# --------------------------------------
# Generate executable files
# --------------------------------------
add_executable(byteos ${ASM_SOURCES} ${C_SOURCES} ${CPP_SOURCES}
drivers/driver.c
drivers/includes/driver-header.h
drivers/rtc/pl031.c
drivers/block/virtio-blk.c
drivers/virtio.c
drivers/includes/rtc.h
includes/inttypes.h
includes/stdio.h
includes/stdlib.h
libs/lwext4/lwext4.c
libs/elf_parser/includes/elf_parser.h
libs/std_impl/tinyprintf.c
)
target_link_libraries(byteos -static)
# No Stdlib and ignore stdlib headers
target_compile_options(
byteos
PRIVATE
-nostdlib
)
add_custom_target(byteos_bin
COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:byteos> ${PROJECT_BINARY_DIR}/byteos.bin
)
# --------------------------------------
# QEMU Execution Options
# --------------------------------------
set(QEMU_EXEC qemu-system-${CMAKE_SYSTEM_PROCESSOR})
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/arch/config-include.h.in
${CMAKE_CURRENT_BINARY_DIR}/generated/config-include.h
)
if(KernelArch STREQUAL "aarch64")
list(APPEND QEMU_EXEC -machine virt -cpu cortex-a72)
elseif(KernelArch STREQUAL "riscv64")
list(APPEND QEMU_EXEC -machine virt)
endif()
set(QEMU_EXEC_CMD ${QEMU_EXEC}
-kernel ${PROJECT_BINARY_DIR}/byteos.bin
-nographic
-D qemu.log
-d in_asm,int,pcall,cpu_reset,guest_errors
# --------------------------------------
# Machine Config
# --------------------------------------
-usb
-device usb-ehci,id=ehci
# -device usb-tablet,bus=usb-bus.0
-device usb-host,bus=ehci.0
# -device usb-storage,bus=ehci.0,drive=x0
# --------------------------------------
# Virtio Devices
# --------------------------------------
-global virtio-mmio.force-legacy=false
-drive
file=mount.img,if=none,format=raw,id=x0
-device
virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0
)
add_custom_target(fdt
COMMAND ${QEMU_EXEC_CMD}
-machine virt,dumpdtb=virt.out
&&
fdtdump virt.out
# --------------------------------------
# Other Arguments
# --------------------------------------
COMMENT "Display Device Tree For the qemu configuration"
USES_TERMINAL
)
add_custom_target(run
COMMAND ${QEMU_EXEC_CMD}
# --------------------------------------
# Other Arguments
# --------------------------------------
DEPENDS byteos_bin
COMMENT "Running byteos in QEMU"
USES_TERMINAL
)
add_custom_target(dbg
COMMAND ${QEMU_EXEC_CMD} -s -S
# --------------------------------------
# Other Arguments
# --------------------------------------
DEPENDS byteos_bin
COMMENT "Running byteos in QEMU"
USES_TERMINAL
)
add_custom_target(gdb
COMMAND gdb byteos -ex 'target remote localhost:1234' -ex 'disp /16i $pc'
# --------------------------------------
# Other Arguments
# --------------------------------------
DEPENDS byteos
COMMENT "Debugging byteos through gdb"
USES_TERMINAL
)
file(GLOB_RECURSE FMT_C_FILES "${CMAKE_SOURCE_DIR}/*.c")
list(FILTER FMT_C_FILES EXCLUDE REGEX ".*/build/.*")
file(GLOB_RECURSE FMT_HEADER_FILES "${CMAKE_SOURCE_DIR}/*.h" EXCLUDE REGEX ".*/build/.*")
list(FILTER FMT_HEADER_FILES EXCLUDE REGEX ".*/build/.*")
add_custom_target(fmt
COMMAND clang-format
-i
${FMT_C_FILES}
${FUNC_HEADER_FILES}
)
# TODO: Generate config header from the toml file.
add_custom_target(gen_config
COMMAND ${CMAKE_SOURCE_DIR}/utils/gen_config.py
${CMAKE_SOURCE_DIR}/configs/${CONFIG_FILE}
${CMAKE_BINARY_DIR}/generated/gen_config.h
)
add_dependencies(byteos gen_config)