-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
212 lines (178 loc) · 4.91 KB
/
Makefile
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
GIT_BRANCH = $(shell git branch --show-current)
IMAGE_NAME = menios
DOCKER = $(shell which docker)
DOCKER_IMAGE = $(IMAGE_NAME):$(GIT_BRANCH)
ARCH ?= x86-64
GCC_DIR = /usr/bin
LIB_DIR = src/libc
CINCLUDE = \
-I./include
OUTPUT_DIR = bin
KERNEL_DIR = src/kernel
KERNEL = $(OUTPUT_DIR)/kernel.elf
OBJDIR = obj
LIBDIR = lib
UACPI_OBJ = $(OBJDIR)/uacpi
KERNEL_OBJ = $(OBJDIR)/kernel
KERNEL_SRC = $(shell find -L src -type f -name '*.c')
KERNEL_ASM = $(shell find -L src/kernel -type f -name '*.s')
KERNEL_OBJS = $(patsubst %.c, %.o, $(KERNEL_SRC))
UACPI_SRC = $(shell find -L vendor/uacpi -type f -name '*.c')
UACPI_OBJS := $(patsubst %.c, %.o, $(UACPI_SRC))
OBJS = $(KERNEL_OBJS) $(UACPI_OBJS)
override CFLAGS += \
-Wall \
-Wextra \
-Winline \
-Wfatal-errors \
-Wno-unused-parameter \
-static \
-std=gnu11 \
-nostdinc \
-nostdlib \
-ffreestanding \
-fno-lto \
-fno-stack-check \
-fno-stack-protector \
-m64 \
-march=x86-64 \
-mno-80387 \
-mno-mmx \
-mno-red-zone \
-mno-sse \
-mno-sse2 \
-DMENIOS_KERNEL \
-DACPI_DEBUG_OUTPUT \
-DUACPI_KERNEL_INITIALIZATION
override CPPFLAGS := \
$(CINCLUDE) \
$(CPPFLAGS) \
-MMD \
-MP
override LDFLAGS += \
-static \
--no-dynamic-linker \
-L$(LIBDIR) \
-z noexecstack \
-T linker.ld \
-m elf_x86_64 \
-nostdlib \
-z max-page-size=0x1000 \
-z text
override NASMFLAGS += \
-Wall \
-f elf64
GCC_KERNEL_OPTS = \
$(CFLAGS) \
$(CINCLUDE)
GCC = $(GCC_DIR)/gcc
LD = $(GCC_DIR)/ld
NASM = $(GCC_DIR)/nasm
QEMU_MEMORY = size=2G,maxmem=2G
QEMU_X86_64 = qemu-system-x86_64
QEMU_LOG_FILE=com1.log
QEMU_OPTS = -smp cpus=2,maxcpus=4,sockets=1,dies=1,clusters=1,cores=2 \
-vga std \
-no-reboot \
--no-shutdown \
-M q35 \
-m $(QEMU_MEMORY) \
-hda $(IMAGE_NAME).hdd \
-serial file:$(QEMU_LOG_FILE) \
-monitor stdio \
-d int \
-M hpet=on \
-usb \
-device usb-ehci,id=ehci \
-device usb-mouse \
-device usb-kbd \
-rtc base=utc,clock=host
OS_NAME = $(shell uname -s | tr A-Z a-z)
.PHONY: clean
all: build
.PHONY: check
check:
cppcheck --force -q --enable=performance,information,missingInclude -I./include -I/Library/Developer/CommandLineTools/usr/lib/clang/14.0.3/include --error-exitcode=3 ./src/kernel
.PHONY: clean
clean:
rm -rf $(OUTPUT_DIR) || true
mkdir $(OUTPUT_DIR) || true
.PHONY: docker
docker:
ifeq ($(OS_NAME),linux)
@echo Skipping Docker
else
ifeq ($(shell docker images $(DOCKER_IMAGE) -q), )
docker rmi -f $(DOCKER_IMAGE) && \
docker build --platform linux/amd64 -t $(DOCKER_IMAGE) .
else
@echo "The Docker image for meniOS already exists"
endif
endif
%.o: %.c
ifeq ($(OS_NAME),linux)
$(GCC) $(GCC_KERNEL_OPTS) -c $< -o $@
else
$(DOCKER) run --rm -it --mount type=bind,source=$$(pwd),target=/mnt $(DOCKER_IMAGE) /bin/sh -c "cd /mnt && make build"
endif
.PHONY: build
build: docker $(OBJS)
ifeq ($(OS_NAME),linux)
@set -eux
rm -rf $(KERNEL_OBJ)/*
$(NASM) -f elf64 ./src/kernel/lgdt.s
$(NASM) -f elf64 ./src/kernel/pit.s
$(NASM) -f elf64 ./src/kernel/lidt.s
cp ./src/kernel/lgdt.o $(KERNEL_OBJ)
cp ./src/kernel/pit.o $(KERNEL_OBJ)
cp ./src/kernel/lidt.o $(KERNEL_OBJ)
cp $(OBJS) $(KERNEL_OBJ)
$(LD) $(LDFLAGS) -o $(KERNEL) $(shell find -L $(KERNEL_OBJ) -type f -name '*.o')
@echo Building image
rm -f $(IMAGE_NAME).hdd
dd if=/dev/zero bs=1M count=0 seek=64 of=$(IMAGE_NAME).hdd
sgdisk $(IMAGE_NAME).hdd -n 1:2048 -t 1:ef00
./bin/limine bios-install $(IMAGE_NAME).hdd
mformat -F -i $(IMAGE_NAME).hdd@@1M
mmd -i $(IMAGE_NAME).hdd@@1M ::/EFI ::/EFI/BOOT
mcopy -i $(IMAGE_NAME).hdd@@1M $(KERNEL) limine.conf ./bin/limine-bios.sys ::/
mcopy -i $(IMAGE_NAME).hdd@@1M ./bin/EFI/BOOT/BOOTX64.EFI ::/EFI/BOOT
@echo Building ISO
# cp /limine/bin/*.bin bin/
xorriso -as mkisofs -b limine-bios-cd.bin \
-no-emul-boot -boot-load-size 4 -boot-info-table \
--efi-boot limine-uefi-cd.bin \
-efi-boot-part --efi-boot-image --protective-msdos-label \
./bin -o $(IMAGE_NAME).iso
./bin/limine bios-install $(IMAGE_NAME).iso
else
$(DOCKER) run --rm -it --mount type=bind,source=$$(pwd),target=/mnt $(DOCKER_IMAGE) /bin/sh -c "cd /mnt && make build"
endif
.PHONY: run
run:
$(QEMU_X86_64) $(QEMU_OPTS)
.PHONY: test
test: docker
@set -eux
ifeq ($(OS_NAME),linux)
@echo "Testing inside Linux"
for file in $(shell find -L test -type f -name 'test_*.c'); do \
gcc -DMENIOS_NO_DEBUG -I./include \
$$file \
test/unity.c \
test/stubs.c \
src/kernel/console/vprintk.c \
src/kernel/mem/kmalloc.c \
src/libc/itoa.c \
src/libc/string.c \
-o "$$file".bin ; \
echo "Testing $(.c:.bin=$$file)" ; \
"$$file".bin ; \
rm "$$file".bin ; \
done;
else
@echo "Testing inside Docker"
$(DOCKER) run --rm -it --mount type=bind,source=$$(pwd),target=/mnt $(DOCKER_IMAGE) /bin/sh -c "cd /mnt && make test"
endif
shell:
$(DOCKER) run --rm -it --mount type=bind,source=$$(pwd),target=/mnt $(DOCKER_IMAGE) /bin/sh -c "cd /mnt && /bin/bash"