forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
219 lines (181 loc) · 6.39 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
213
214
215
216
217
218
219
# Cross-compiling (e.g., on Mac OS X)
# TOOLPREFIX = i386-jos-elf
# Using native tools (e.g., on X86 Linux)
#TOOLPREFIX =
# Try to infer the correct TOOLPREFIX if not set
ifndef TOOLPREFIX
TOOLPREFIX := $(shell if i386-jos-elf-objdump -i 2>&1 | grep '^elf32-i386$$' >/dev/null 2>&1; \
then echo 'i386-jos-elf-'; \
elif objdump -i 2>&1 | grep 'elf32-i386' >/dev/null 2>&1; \
then echo ''; \
else echo "***" 1>&2; \
echo "*** Error: Couldn't find an i386-*-elf version of GCC/binutils." 1>&2; \
echo "*** Is the directory with i386-jos-elf-gcc in your PATH?" 1>&2; \
echo "*** If your i386-*-elf toolchain is installed with a command" 1>&2; \
echo "*** prefix other than 'i386-jos-elf-', set your TOOLPREFIX" 1>&2; \
echo "*** environment variable to that prefix and run 'make' again." 1>&2; \
echo "*** To turn off this error, run 'gmake TOOLPREFIX= ...'." 1>&2; \
echo "***" 1>&2; exit 1; fi)
endif
# If the makefile can't find QEMU, specify its path here
# QEMU = qemu-system-i386
# Try to infer the correct QEMU
ifndef QEMU
QEMU = $(shell if which qemu > /dev/null; \
then echo qemu; exit; \
elif which qemu-system-i386 > /dev/null; \
then echo qemu-system-i386; exit; \
elif which qemu-system-x86_64 > /dev/null; \
then echo qemu-system-x86_64; exit; \
else \
qemu=/Applications/Q.app/Contents/MacOS/i386-softmmu.app/Contents/MacOS/i386-softmmu; \
if test -x $$qemu; then echo $$qemu; exit; fi; fi; \
echo "***" 1>&2; \
echo "*** Error: Couldn't find a working QEMU executable." 1>&2; \
echo "*** Is the directory containing the qemu binary in your PATH" 1>&2; \
echo "*** or have you tried setting the QEMU variable in Makefile?" 1>&2; \
echo "***" 1>&2; exit 1)
endif
# TODO get rid of this
WNOFLAGS = -Wno-error=infinite-recursion
ARCHNOFLAGS = -mno-sse -mno-red-zone -mno-avx -mno-avx2
CC = $(TOOLPREFIX)gcc
AS = $(TOOLPREFIX)gas
LD = $(TOOLPREFIX)ld
AR = $(TOOLPREFIX)ar
RANLIB = $(TOOLPREFIX)ranlib
OBJCOPY = $(TOOLPREFIX)objcopy
OBJDUMP = $(TOOLPREFIX)objdump
# llvm stuff
ifneq ($(LLVM),)
CC = clang
AS = $(CC) -c
LD = ld.lld
OBJCOPY = llvm-objcopy
OBJDUMP = llvm-objdump
AR = llvm-ar
RANLIB = llvm-ranlib
LINKER_FLAGS = -fuse-ld=lld
endif
# Dlang toolchain
DC ?= dmd
D_PREVIEWS = -preview=systemVariables,in,dip1000,dip1021,nosharedaccess,fixImmutableConv
DFLAGS ?= -betterC -O -m32 $(EXTRA_DFLAGS)
ifeq ($(shell $(DC) --version | sed -n 1p | awk '{print $$1}'),DMD64)
DC_type = dmd
else
DC_type = ldc2
endif
ifneq ($(DC_type),dmd)
DFLAGS += -defaultlib -mattr=-avx,-sse -mcpu=i686
else
DFLAGS += -defaultlib=none
endif
# we will support dmd-style D compilers only.
WFLAGS = -Wnonnull
CFLAGS = -std=gnu11 -pipe -pedantic -fno-pic -static -fno-builtin -ffreestanding \
-fno-strict-aliasing -nostdlib -O0 -Wall -ggdb -m32 -Wno-error -fno-omit-frame-pointer \
-nostdinc -fno-builtin $(ARCHNOFLAGS) $(WNOFLAGS) $(WFLAGS)
CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
ifneq ($(RELEASE),)
CFLAGS += -O2
endif
ASFLAGS = -m32 -gdwarf-2 -Wa,-divide --mx86-used-note=no
# FreeBSD ld wants ``elf_i386_fbsd''
ifeq ($(HOST_OS),FreeBSD)
LDFLAGS += -m $(shell $(LD) -V | grep elf_i386 2>/dev/null | head -n 1)
else
LDFLAGS += -m elf_i386
endif
LDFLAGS += -z noexecstack -O1
# Disable PIE when possible (for Ubuntu 16.10 toolchain)
ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]no-pie'),)
CFLAGS += -fno-pie -no-pie
endif
ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]nopie'),)
CFLAGS += -fno-pie -nopie
endif
CLEAN := $(filter clean, $(MAKECMDGOALS))
IVARS = -Iinclude/ -I.
# directories
TOOLSDIR = tools
BIN = bin
SYSROOT = sysroot
DIRECTORIES = $(BIN) $(SYSROOT)/$(BIN)
default: $(CLEAN)
$(MAKE) $(KERNELDIR)/include/autogenerated/compiler_information.h
$(MAKE) $(DIRECTORIES)
$(MAKE) images
images: $(BIN)/fs.img $(BIN)/xv6.img
$(DIRECTORIES):
mkdir -p $@
include d/Makefile
include userspace/Makefile
include kernel/Makefile
$(KERNELDIR)/include/autogenerated/compiler_information.h:
mkdir -p $(KERNELDIR)/include/autogenerated
printf \
"#pragma once\n \
#define XV6_COMPILER \"$(shell $(CC) --version | sed -n 1p)\"\n \
#define XV6_LINKER \"$(shell $(LD) --version | sed -n 1p)\"\n" \
> $(KERNELDIR)/include/autogenerated/compiler_information.h
$(BIN)/mkfs: $(TOOLSDIR)/mkfs.c
$(CC) $(LINKER_FLAGS) -Werror -Wall -o $@ $^ \
-I$(KERNELDIR)/include -I$(KERNELDIR)/drivers/include -I$(KERNELDIR)/drivers -I.
# Prevent deletion of intermediate files, e.g. cat.o, after first build, so
# that disk image changes after first build are persistent until clean. More
# details:
# http://www.gnu.org/software/make/manual/html_node/Chained-Rules.html
.PRECIOUS: $(BIN)/%.o
$(BIN)/fs.img: $(BIN)/mkfs $(UPROGS) $(D_PROGS)
./$(BIN)/mkfs $@ README.md sysroot/test.sh sysroot/etc/passwd $(UPROGS) $(D_PROGS)
clean:
rm -f $(BIN)/*.o $(BIN)/*.sym $(BIN)/bootblock $(BIN)/entryother \
$(SYSROOT)/bin/* \
$(SYSROOT)/mkfs \
$(BIN)/initcode \
$(BIN)/initcode.out \
$(BIN)/kernel \
$(BIN)/xv6.img \
$(BIN)/fs.img \
$(BIN)/kernelmemfs \
$(KERNELDIR)/include/autogenerated/* \
$(BIN)/xv6memfs.img mkfs $(BIN)/*
# run in emulators
bochs : fs.img xv6.img
if [ ! -e .bochsrc ]; then ln -s dot-bochsrc .bochsrc; fi
bochs -q
# try to generate a unique GDB port
GDBPORT = $(shell expr `id -u` % 5000 + 25000)
# QEMU's gdb stub command line changed in 0.11
QEMUGDB = $(shell if $(QEMU) -help | grep -q '^-gdb'; \
then echo "-gdb tcp::$(GDBPORT)"; \
else echo "-s -p $(GDBPORT)"; fi)
ifndef CPUS
CPUS := 2
endif
ifndef MEM
MEM := 224M
endif
QEMUOPTS = -drive file=$(BIN)/fs.img,index=1,media=disk,format=raw,if=ide,aio=native,cache.direct=on \
-drive file=$(BIN)/xv6.img,index=0,media=disk,format=raw,if=ide,aio=native,cache.direct=on \
-smp cpus=$(CPUS),cores=1,threads=1,sockets=$(CPUS) -m $(MEM) $(QEMUEXTRA)
ifdef CONSOLE_LOG
QEMUOPTS += -serial mon:stdio
endif
qemu: default
$(QEMU) $(QEMUOPTS)
qemu-memfs: xv6memfs.img
$(QEMU) -drive file=$(BIN)/xv6memfs.img,index=0,media=disk,format=raw -smp $(CPUS) -m $(MEM)
qemu-nox: default
$(QEMU) -nographic $(QEMUOPTS)
.gdbinit: debug/.gdbinit.tmpl
sed "s/localhost:1234/localhost:$(GDBPORT)/" < $^ > $@
qemu-gdb: default
$(QEMU) $(QEMUOPTS) -S $(QEMUGDB)
@echo "*** Now run 'gdb'." 1>&2
qemu-nox-gdb: fs.img xv6.img .gdbinit
@echo "*** Now run 'gdb'." 1>&2
$(QEMU) -nographic $(QEMUOPTS) -S $(QEMUGDB)
format:
@find . -iname *.h -o -iname *.c | xargs clang-format -style=file:.clang-format -i