-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
198 lines (162 loc) · 5.59 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
include config.def
-include config.mk
V = @
Q = $(V:1=)
QUIET_CC = $(Q:@=@echo ' CC '$@;)
QUIET_CXX = $(Q:@=@echo ' CXX '$@;)
QUIET_AR = $(Q:@=@echo ' AR '$@;)
QUIET_LD = $(Q:@=@echo ' LD '$@;)
QUIET_GEN = $(Q:@=@echo ' GEN '$@;)
QUIET_PY2 = $(Q:@=@echo ' PY2 '$@;)
QUIET_PY3 = $(Q:@=@echo ' PY3 '$@;)
MKDIR_P := mkdir -p
LN_S := ln -s
UNAME_S := $(shell uname -s)
TOP := $(shell echo $${PWD-`pwd`})
HOST_CC := cc
PY2 := python2
PY3 := python3
# try the latest version first
LLVM_CONFIG := llvm-config-5.0
ifeq ($(shell which $(LLVM_CONFIG) 2> /dev/null),)
LLVM_CONFIG := llvm-config
endif
# Homebrew hides llvm here
ifeq ($(shell which $(LLVM_CONFIG) 2> /dev/null),)
LLVM_CONFIG := /usr/local/opt/llvm/bin/llvm-config
endif
ifeq ($(shell which $(LLVM_CONFIG) 2> /dev/null),)
LLVM_PREFIX :=
else
LLVM_PREFIX := "$(shell $(LLVM_CONFIG) --bindir)/"
endif
LLVM_CC := $(LLVM_PREFIX)clang -target $(ARCH)-linux-gnu
LLVM_HOST_CC := $(LLVM_PREFIX)clang
LLVM_LINK := $(LLVM_PREFIX)llvm-link
ifeq ($(UNAME_S),Darwin)
USE_CLANG ?= 1
TOOLPREFIX ?= $(ARCH)-linux-gnu-
endif
ifeq ($(USE_CLANG),1)
CC := $(LLVM_CC)
else
CC := $(TOOLPREFIX)gcc
endif
LD := $(TOOLPREFIX)ld
AR := $(TOOLPREFIX)ar
RANLIB := $(TOOLPREFIX)ranlib
NM := $(TOOLPREFIX)nm
OBJCOPY := $(TOOLPREFIX)objcopy
OBJDUMP := $(TOOLPREFIX)objdump
GDB := $(TOOLPREFIX)gdb
BOCHS := bochs
QEMU := qemu-system-$(ARCH)
BASE_CFLAGS += -ffreestanding
BASE_CFLAGS += -fno-stack-protector
BASE_CFLAGS += -ffunction-sections -fdata-sections
BASE_CFLAGS += -g
BASE_CFLAGS += -Wall -MD -MP
BASE_CFLAGS += -Wno-initializer-overrides
CFLAGS += $(BASE_CFLAGS)
CFLAGS += -fno-PIE
CFLAGS += -fwrapv
CFLAGS += -mno-red-zone
CFLAGS += -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx
CFLAGS += -I include
CFLAGS += -DNR_CPUS=1
USER_CFLAGS += $(BASE_CFLAGS)
USER_CFLAGS += -fno-PIE -fwrapv
LDFLAGS += --gc-sections
object = $(addprefix $(O)/,$(patsubst %.c,%.o,$(patsubst %.S,%.o,$(filter-out %/asm-offsets.c %.lds.S,$(1)))))
include kernel/kernel.mk
include firmware/firmware.mk
include vmm/vmm.mk
include tests/tests.mk
all: $(ALL)
.DEFAULT_GOAL = all
# Default sed regexp - multiline due to syntax constraints
#
# Use [:space:] because LLVM's integrated assembler inserts <tab> around
# the .ascii directive whereas GCC keeps the <space> as-is.
define sed-offsets
's:^[[:space:]]*\.ascii[[:space:]]*"\(.*\)".*:\1:; \
/^->/{s:->#\(.*\):/* \1 */:; \
s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
s:->::; p;}'
endef
define gen-offsets
(set -e; \
echo "#pragma once"; \
echo ""; \
sed -ne $(sed-offsets) )
endef
%/asm-offsets.h: $(O)/%/asm-offsets.S
$(QUIET_GEN)$(call gen-offsets) < $< > $@~
$(Q)mv $@~ $@
%.bin: %.elf
$(QUIET_GEN)$(OBJCOPY) -O binary $< $@
$(Q)$(OBJDUMP) -S $< > $(basename $@).asm
$(O)/%.lds: %.lds.S
$(Q)$(MKDIR_P) $(@D)
$(QUIET_GEN)$(CPP) -o $@ -P $(CFLAGS) $<
$(O)/%.S: %.c
$(Q)$(MKDIR_P) $(@D)
$(QUIET_CC)$(CC) -o $@ -S $(CFLAGS) $<
$(O)/%.o: %.S
$(Q)$(MKDIR_P) $(@D)
$(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) $<
$(O)/%.o: %.c
$(Q)$(MKDIR_P) $(@D)
$(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) -DCONFIG_SAFE_TYPE -D__MODULE__='"$(basename $(notdir $<))"' $<
clean:
-rm -rf $(O)
check: all $(TESTS)
ifneq ($(UNAME_S),Linux)
$(error works on Linux only)
else
$(Q)tests/test_vmm.py -v
endif
# usage: $(call try-run,cmd,option-ok,otherwise)
__try-run = set -e; \
if ($(1)) >/dev/null 2>&1; \
then echo "$(2)"; \
else echo "$(3)"; \
fi
try-run = $(shell $(__try-run))
# usage: $(call bochs-option,option,option-ok,otherwise)
bochs-option = $(call try-run,$(BOCHS) --help 2>&1 | grep -- $(1),$(2),$(3))
define run-bochs
CPU=$(BOCHS_CPU) NR_CPUS=1 ISO=$< \
$(BOCHS) -q -f boot/bochsrc $(call bochs-option,-rc,-rc boot/bochs.dbg) || true
endef
bochs: $(VMM_ISO)
$(call run-bochs)
bochs-kernel: $(KERNEL_ISO)
$(call run-bochs)
define gen-iso
cp -r boot/isolinux $(@D)/iso/
$(LN_S) $(realpath $(KERNEL))$(if $(INITRD), $(realpath $(INITRD))) $(@D)/iso/
mkisofs -r -f -o $@~ -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table $(@D)/iso
boot/isohybrid.pl $@~
mv $@~ $@
endef
$(KERNEL_ISO): $(KERNEL)
$(Q)-rm -rf $(@D)/iso
$(Q)$(MKDIR_P) $(@D)/iso/
$(Q)echo 'default mboot.c32 /$(notdir $(KERNEL))$(if $(APPEND), $(APPEND))$(if $(INITRD), --- /$(notdir $(INITRD)))' > $(@D)/iso/isolinux.cfg
$(QUIET_GEN)$(call gen-iso)
QEMUOPTS += -machine q35,accel=kvm:tcg -cpu $(QEMU_CPU)
QEMUOPTS += -m 1G
QEMUOPTS += -device isa-debug-exit
QEMUOPTS += -debugcon file:/dev/stdout
QEMUOPTS += -serial mon:stdio -display none
qemu: $(VMM_BIN) $(KERNEL)
ifneq ($(UNAME_S),Linux)
$(error works on Linux only)
else
$(QEMU) $(QEMUOPTS) -kernel $(VMM_BIN) -initrd "$(KERNEL)$(if $(APPEND), $(APPEND)),$(if $(INITRD),$(INITRD),/dev/null)" || true
endif
qemu-kernel: $(KERNEL)
$(QEMU) $(QEMUOPTS) -kernel $(KERNEL)$(if $(APPEND), -append $(APPEND))$(if $(INITRD), -initrd $(INITRD)) || true
.PRECIOUS: $(O)/%.o $(O)/%.S
.PHONY: all clean check bochs bochs-kernel qemu qemu-kernel