-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMakefile
128 lines (106 loc) · 4.25 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
CFLAGS?=-std=gnu99 -ffreestanding -ggdb -gdwarf-2 -Wall -Wextra -I./src/include -I./acpica/include -Wno-unused-parameter
CXXFLAGS?=-std=gnu++14 -ffreestanding -fno-rtti -fno-exceptions -ggdb -gdwarf-2 -Wall -Wextra -I./src/include -I./acpica/include
ARCH?=i686
TIME?=$(shell date +%s)
RELEASE?=FALSE
# Enable optimizations.
ifeq ($(RELEASE), TRUE)
CFLAGS+=-O2
endif
# Get source files.
ifeq ($(ARCH), x86_64)
IGNOREARCH = i386
else
IGNOREARCH = x86_64
endif
C_SOURCES := $(filter-out $(shell find src/arch/$(IGNOREARCH) -name '*.c'), $(shell find src -name '*.c'))
CPP_SOURCES := $(filter-out $(shell find src/arch/$(IGNOREARCH) -name '*.cpp'), $(shell find src -name '*.cpp'))
ASM_SOURCES := $(filter-out $(shell find src/arch/$(IGNOREARCH) -name '*.asm'), $(shell find src -name '*.asm'))
ACPICA_C_SOURCES := $(shell find acpica -name '*.c')
# Get object files.
C_OBJECTS = $(subst src, build, $(C_SOURCES:.c=.o))
CPP_OBJECTS = $(subst src, build, $(CPP_SOURCES:.cpp=_cpp.o))
ASM_OBJECTS = $(subst src, build, $(ASM_SOURCES:.asm=_asm.o))
ACPICA_C_OBJECTS = $(subst acpica, acpica_build, $(ACPICA_C_SOURCES:.c=.o))
all:
make clean
ARCH=i686 make build-kernel
[[ -d build ]] || mkdir build
mkdir build/$(TIME)
cp *.kernel *.sym build/$(TIME)
make test
build-kernel:
# Check if we are compiling for different arch, if so clean build dir and save.
ifneq ($(shell cat LASTARCH), $(ARCH))
rm -rfd acpica_build
$(shell echo $(ARCH) > LASTARCH)
endif
# Clean the build directory.
rm -rfd build
# Build kernel.
$(MAKE) build-kernel-main
build-kernel-main: $(ASM_OBJECTS) $(C_OBJECTS) $(CPP_OBJECTS) $(ACPICA_C_OBJECTS)
# Link objects together into binary.
ifeq ($(ARCH), x86_64)
$(ARCH)-elf-gcc -T src/arch/x86_64/kernel/linker.ld -o Star-$(ARCH).kernel -fPIC -ffreestanding -nostdlib $(ASM_OBJECTS) $(C_OBJECTS) $(CPP_OBJECTS) $(ACPICA_C_OBJECTS) -lgcc -z max-page-size=0x1000
else
$(ARCH)-elf-gcc -T src/arch/i386/kernel/linker.ld -o Star-$(ARCH).kernel -ffreestanding -nostdlib $(ASM_OBJECTS) $(C_OBJECTS) $(CPP_OBJECTS) $(ACPICA_C_OBJECTS) -lgcc
endif
# Strip out debug info into separate files.
$(ARCH)-elf-objcopy --only-keep-debug Star-$(ARCH).kernel Star-$(ARCH).sym
$(ARCH)-elf-objcopy --strip-debug Star-$(ARCH).kernel
# $(ARCH)-elf-objcopy -O binary Star-$(ARCH).kernel Star-$(ARCH).kernel.bin
# Clean the build directory.
rm -rfd build
ifeq ($(ARCH), x86_64)
cp Star-x86_64.kernel isofiles/
else
cp Star-i686.kernel isofiles/
endif
grub-mkrescue -o os.iso isofiles -d /usr/lib/grub/i386-pc
# Compile assembly source files.
$(ASM_OBJECTS):
mkdir -p $(dir $@)
ifeq ($(ARCH), x86_64)
nasm -felf64 -F dwarf $(subst build, src, $(subst _asm.o,.asm,$@)) -o $@
else
nasm -felf32 -F dwarf $(subst build, src, $(subst _asm.o,.asm,$@)) -o $@
endif
# Compile C source files.
$(C_OBJECTS):
mkdir -p $(dir $@)
ifeq ($(ARCH), x86_64)
$(ARCH)-elf-gcc -c $(subst build, src, $(subst .o,.c,$@)) -o $@ $(CFLAGS) -fPIC -mcmodel=large -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -fno-asynchronous-unwind-tables -g
else
$(ARCH)-elf-gcc -c $(subst build, src, $(subst .o,.c,$@)) -o $@ $(CFLAGS)
endif
# Compile C++ source files.
$(CPP_OBJECTS):
mkdir -p $(dir $@)
ifeq ($(ARCH), x86_64)
$(ARCH)-elf-gcc -c $(subst build, src, $(subst _cpp.o,.cpp,$@)) -o $@ $(CXXFLAGS) -fPIC -mcmodel=large -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -fno-asynchronous-unwind-tables -g
else
$(ARCH)-elf-gcc -c $(subst build, src, $(subst _cpp.o,.cpp,$@)) -o $@ $(CXXFLAGS)
endif
# Compile ACPICA C source files.
$(ACPICA_C_OBJECTS):
mkdir -p $(dir $@)
ifeq ($(ARCH), x86_64)
$(ARCH)-elf-gcc -c $(subst acpica_build, acpica, $(subst .o,.c,$@)) -o $@ $(CFLAGS) -fPIC -mcmodel=large -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -fno-asynchronous-unwind-tables -g
else
$(ARCH)-elf-gcc -c $(subst acpica_build, acpica, $(subst .o,.c,$@)) -o $@ $(CFLAGS)
endif
test:
qemu-system-x86_64 -kernel Star-i686.kernel -m 32M -d guest_errors -drive format=raw,file=fat12.img,index=0,if=floppy -serial stdio -net nic,model=rtl8139
debug:
qemu-system-i386 -kernel Star-i686.kernel -S -s & gdb Star-i686.kerel -ex 'target remote localhost:1234'
clean:
# Clean up binaries.
rm -rfd build
rm -rf *.o *.bin *.kernel *.sym *.bin
full-clean:
# Clean up binaries.
rm LASTARCH
rm -rfd build
rm -rfd acpica_build
rm -rf *.o *.bin *.kernel *.sym *.bin