-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
263 lines (217 loc) · 6.54 KB
/
Copy pathMakefile
File metadata and controls
263 lines (217 loc) · 6.54 KB
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
254
255
256
257
258
259
260
261
262
263
# =========================================================
# Libra Kernel Build System
# =========================================================
# -------------------------
# Toolchain (CI + Local)
# -------------------------
ACTIONS ?= 0
DETECTED_OS := $(shell uname -s)
ifeq ($(ACTIONS),1)
TOOLCHAIN_DIR := $(shell pwd)/toolchain
CROSS_COMPILE := $(TOOLCHAIN_DIR)/
else
CROSS_COMPILE ?= toolchain/
endif
CC := x86_64-elf-gcc
LD := x86_64-elf-ld
AR := x86_64-elf-ar
HCC = gcc
HCFLAGS := -Wall -Wextra -O2
NCURSES_LIBS:= -lncursesw # Required to link the terminal interface
AS := nasm
QEMU := qemu-system-x86_64
XORRISO := xorriso
export srctree := .
export SRCARCH := x86
# Directory references
SRC_DIR := scripts/kbuild-standalone
BIN_DIR := tools/bin
# Build targets for compiled configuration utilities
CONF_BIN := $(BIN_DIR)/conf
MCONF_BIN := $(BIN_DIR)/mconf
# -------------------------
# Flags
# -------------------------
CFLAGS := \
-ffreestanding \
-fno-stack-protector \
-fno-pic \
-mno-red-zone \
-m64 \
-march=x86-64 \
-mcmodel=kernel \
-O0 \
-Wall -Wextra \
-g \
-I./inc -I. \
-mno-sse -mno-mmx -mno-3dnow -fpermissive -Wno-error=int-conversion -Wno-error=incompatible-pointer-types -Wno-error=implicit-function-declaration
LDFLAGS := \
-T linker.ld \
-nostdlib \
-z max-page-size=0x1000
ASFLAGS := -f elf64
# -------------------------
# Directories
# -------------------------
SRC_DIR := src
OBJ_DIR := objects
DEP_DIR := dependencies
ISO_DIR := iso
# -------------------------
# Sources
# -------------------------
SRCS_C := $(shell find $(SRC_DIR) -type f -name '*.c')
SRCS_S := $(shell find $(SRC_DIR) -type f -name '*.s')
SRCS_ASM := $(shell find $(SRC_DIR) -type f -name '*.asm')
OBJS_C := $(patsubst $(SRC_DIR)/%.c, $(OBJ_DIR)/%.o, $(SRCS_C))
OBJS_S := $(patsubst $(SRC_DIR)/%.s, $(OBJ_DIR)/%_s.o, $(SRCS_S))
OBJS_ASM := $(patsubst $(SRC_DIR)/%.asm, $(OBJ_DIR)/%_asm.o, $(SRCS_ASM))
OBJS := $(OBJS_C) $(OBJS_S) $(OBJS_ASM)
DEPS := $(patsubst $(SRC_DIR)/%.c, $(DEP_DIR)/%.d, $(SRCS_C))
# -------------------------
# Output
# -------------------------
KERNEL := $(ISO_DIR)/kernel.elf
ISO := custom_os.iso
INITRAMFS := $(ISO_DIR)/initramfs.cpio
# -------------------------
# Targets
# -------------------------
.PHONY: all clean iso run dirs initramfs
all: $(KERNEL)
# -------------------------
# Link Kernel
# -------------------------
$(KERNEL): genconfig $(OBJS) linker.ld | dirs
@echo "[LD] Linking kernel"
@mkdir -p $(dir $@)
@$(LD) $(LDFLAGS) $(OBJS) -o $@
# -------------------------
# Compile C
# -------------------------
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | dirs
@echo "[CC] $<"
@mkdir -p $(dir $@)
@$(CC) $(CFLAGS) -c $< -o $@
# -------------------------
# Assemble .s
# -------------------------
$(OBJ_DIR)/%_s.o: $(SRC_DIR)/%.s | dirs
@echo "[AS] $<"
@mkdir -p $(dir $@)
@$(AS) $(ASFLAGS) $< -o $@
# -------------------------
# Assemble .asm
# -------------------------
$(OBJ_DIR)/%_asm.o: $(SRC_DIR)/%.asm | dirs
@echo "[AS] $<"
@mkdir -p $(dir $@)
@$(AS) $(ASFLAGS) $< -o $@
# -------------------------
# Directories
# -------------------------
dirs:
@mkdir -p $(OBJ_DIR) $(DEP_DIR) $(ISO_DIR)
# -------------------------
# ISO Build (UEFI)
# -------------------------
initramfs: | dirs
@echo "[CPIO] Packing initramfs"
@cd rootfs && find . -type f | cpio -o -H newc --quiet > ../$(INITRAMFS)
iso: $(KERNEL) initramfs
@echo "[ISO] building..."
@if [ ! -f $(ISO_DIR)/limine.conf ]; then \
echo "ERROR: missing limine.conf"; exit 1; \
fi
@cp limine/limine-uefi-cd.bin $(ISO_DIR)/
@mkdir -p $(ISO_DIR)/EFI/BOOT
@cp limine/BOOTX64.EFI $(ISO_DIR)/EFI/BOOT/
@$(XORRISO) -as mkisofs \
-e limine-uefi-cd.bin \
-no-emul-boot \
-efi-boot-part \
--efi-boot-image \
--protective-msdos-label \
$(ISO_DIR) -o $(ISO)
# -------------------------
# Run in QEMU
# -------------------------
run: iso
@echo "[QEMU] booting..."
@$(QEMU) \
-bios ./prebuilt/OVMF.fd \
-m 4G \
-M q35 \
-serial stdio \
-D qemu.log \
-d int \
-drive id=disk0,file=my_disk.qcow2,if=none,format=qcow2 \
-device ide-hd,drive=disk0,bus=ide.0 \
-cdrom $(ISO) \
-netdev user,id=net0 \
-device rtl8139,netdev=net0 \
-object filter-dump,id=f1,netdev=net0,file=packets.pcap \
-audiodev alsa,id=snd0 -device AC97,audiodev=snd0 \
-drive file=nvme.img,if=none,id=nvme0,format=raw \
-device nvme,drive=nvme0,serial=deadbeef \
-device qemu-xhci,id=xhci \
-smp 4 -device virtio-gpu-pci,xres=1280,yres=720 \
-trace "virtio_*" \
-trace "virtio_gpu_*" \
-trace "pci_*"
# -------------------------
# Python-based Kconfiglib Environment (PEP 668 Compliant)
# -------------------------
VENV_DIR := .venv
VENV_ACTIVATE := $(VENV_DIR)/bin/activate
VENV_PIP := $(VENV_DIR)/bin/pip
MENUCONFIG_PY := $(VENV_DIR)/bin/menuconfig
OLDCONFIG_PY := $(VENV_DIR)/bin/oldconfig
# Virtual environment prerequisite target
$(VENV_ACTIVATE):
@echo "[VENV] Creating localized Python environment..."
@python3 -m venv $(VENV_DIR)
@echo "[VENV] Installing kconfiglib package..."
@$(VENV_PIP) install --upgrade pip > /dev/null
@$(VENV_PIP) install kconfiglib > /dev/null
# Replaces compile_tools entirely
compile_tools: $(VENV_ACTIVATE)
@mkdir -p $(BIN_DIR)
@echo "[VENV] Python engine ready."
# Interactive visual configuration shell menu
# Interactive visual configuration shell menu (Dark Aqua Theme)
menuconfig: compile_tools
@echo "[KCONFIG] Launching Python menuconfig with Dark Aqua Theme..."
@export MENUCONFIG_STYLE="\
body=fg:darkcyan bg:default;\
main_frame=fg:cyan bg:default;\
frame_title=fg:brightcyan bg:default bold;\
cursor=fg:black bg:cyan bold;\
selected_item=fg:brightcyan bg:default bold;\
unselected_item=fg:white bg:default;\
help=fg:darkcyan bg:default;\
separator=fg:cyan bg:default;\
shadow=fg:black bg:default;\
menu=fg:white bg:default;\
text=fg:white bg:default;\
jumps=fg:brightcyan bg:default bold;\
inputs=fg:white bg:darkcyan;\
buttons=fg:black bg:cyan;\
selected_button=fg:black bg:brightcyan bold;\
unselected_button=fg:white bg:darkcyan"; \
. $(VENV_ACTIVATE) && $(MENUCONFIG_PY) Kconfig
# Automatic header compiler generator
genconfig: compile_tools
@echo "[KCONFIG] Generating configuration header..."
@mkdir -p inc
@export srctree=.; \
export SRCARCH=x86; \
export KCONFIG_AUTOHEADER=inc/config.h; \
. $(VENV_ACTIVATE) && python3 $(VENV_DIR)/bin/genconfig Kconfig
clean:
@echo "[CLEAN]"
@rm -rf $(OBJ_DIR) $(DEP_DIR) $(ISO) $(ISO_DIR)/kernel.elf
@rm -rf inc/config.h
@rm -rf $(BIN_DIR)/*
@rm -rf qemu.log .config .config.old
-include $(DEPS)