-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
104 lines (79 loc) · 3.29 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
arch ?= x86_64
boot := build/boot-$(arch).bin
loader := build/loader-$(arch).elf
kernel := build/kernel-$(arch).bin
kernel_elf := build/kernel-$(arch).elf
img := build/os-$(arch).img
iso := build/os-$(arch).iso
linker_script := src/arch/$(arch)/linker.ld
boot_source_file := src/arch/$(arch)/boot.asm
loader_source_file := src/arch/$(arch)/loader.asm
stdlib_c_source_files := $(shell find src/stdlib -name "*.c")
stdlib_c_object_files := $(patsubst src/stdlib/%.c, \
build/stdlib/%.o, $(stdlib_c_source_files))
stdlib_as_source_files := $(shell find src/stdlib -name "*.S")
stdlib_as_object_files := $(patsubst src/stdlib/%.S, \
build/stdlib/%.o, $(stdlib_as_source_files))
swift_source_files := src/kernel/main.swift $(shell find src -name "*.swift" ! -name "main.swift")
swift_bc_files := $(patsubst src/%.swift, \
build/%.bc, $(swift_source_files))
swift_object_files := $(patsubst src/%.swift, \
build/%.o, $(swift_source_files))
libc_source_files := $(shell find src/libc -name "*.c")
libc_object_files := $(patsubst src/libc/%.c, \
build/libc/%.o, $(libc_source_files))
SWIFT = swiftc
SWIFTFLAGS = -emit-library -emit-bc -static-stdlib -module-name SnowWhiteOS -import-objc-header src/stdlib/public/SwiftShims/shim.h -I $(shell echo $$LLVM_INCLUDE_PATH)
CXX = clang++
CXXFLAGS = -target x86_64-pc-linux-gnu -ffreestanding -mcmodel=large -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -nostdlib -std=c++11 -Isrc/include
CC = clang
CFLAGS = -target x86_64-pc-linux-gnu -ffreestanding -mcmodel=large -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -nostdlib -Isrc/include/libc
NASM = nasm
LD = $(arch)-elf-ld
AS = $(arch)-elf-as
OBJCOPY = $(arch)-elf-objcopy
.PHONY: all clean run img
all: $(boot) $(kernel)
clean:
@rm -r build
run: $(img)
@qemu-system-x86_64 $(img)
img: $(img)
iso: $(iso)
$(img): $(boot) $(kernel)
@mkdir -p $(shell dirname $@)
@dd if=/dev/zero of=$(img) bs=512 count=2880
@dd if=$(boot) of=$(img) conv=notrunc
@dd if=$(kernel) of=$(img) conv=notrunc bs=512 seek=1
$(iso): $(img)
@mkdir -p build/cdcontents
@cp $(img) build/cdcontents
@mkisofs -o $(iso) -V SnowWhiteOS -b $(img) build/cdcontents
$(boot):
@mkdir -p $(shell dirname $@)
@$(NASM) -f bin $(boot_source_file) -o $(boot)
$(loader):
@mkdir -p $(shell dirname $@)
@$(NASM) -f elf64 $(loader_source_file) -o $(loader)
$(kernel): $(loader) $(libc_object_files) $(stdlib_c_object_files) $(stdlib_as_object_files) $(swift_object_files) $(linker_script)
@$(LD) -T $(linker_script) -o $(kernel_elf) $(loader) $(libc_object_files) $(stdlib_c_object_files) $(stdlib_as_object_files) $(swift_object_files)
@$(OBJCOPY) $(kernel_elf) -O binary $(kernel) # debug
# compile stdlib c files
build/stdlib/%.o: src/stdlib/%.c
@mkdir -p $(shell dirname $@)
@$(CC) $(CFLAGS) -c $< -o $@
# compile stdlib as files
build/stdlib/%.o: src/stdlib/%.S
@mkdir -p $(shell dirname $@)
@$(AS) $< -o $@
# compile swift files
$(swift_object_files):
@$(foreach var,$(swift_bc_files),mkdir -p $(shell dirname $(var));)
$(SWIFT) $(SWIFTFLAGS) $(swift_source_files)
@$(foreach var,$(swift_bc_files),mv $(shell basename $(var)) $(var);)
@$(CC) $(CFLAGS) -c $(swift_bc_files)
@$(foreach var,$(swift_object_files),mv $(shell basename $(var)) $(var);)
# compile libc files
build/libc/%.o: src/libc/%.c
@mkdir -p $(shell dirname $@)
@$(CC) $(CFLAGS) -c $< -o $@