xref: /xv6-public/Makefile (revision 1772853d)
1OBJS = \
2	bio.o\
3	console.o\
4	exec.o\
5	file.o\
6	fs.o\
7	ide.o\
8	ioapic.o\
9	kalloc.o\
10	kbd.o\
11	lapic.o\
12	log.o\
13	main.o\
14	mp.o\
15	picirq.o\
16	pipe.o\
17	proc.o\
18	sleeplock.o\
19	spinlock.o\
20	string.o\
21	swtch.o\
22	syscall.o\
23	sysfile.o\
24	sysproc.o\
25	timer.o\
26	trapasm.o\
27	trap.o\
28	uart.o\
29	vectors.o\
30	vm.o\
31
32# Cross-compiling (e.g., on Mac OS X)
33# TOOLPREFIX = i386-jos-elf
34
35# Using native tools (e.g., on X86 Linux)
36#TOOLPREFIX =
37
38# Try to infer the correct TOOLPREFIX if not set
39ifndef TOOLPREFIX
40TOOLPREFIX := $(shell if i386-jos-elf-objdump -i 2>&1 | grep '^elf32-i386$$' >/dev/null 2>&1; \
41	then echo 'i386-jos-elf-'; \
42	elif objdump -i 2>&1 | grep 'elf32-i386' >/dev/null 2>&1; \
43	then echo ''; \
44	else echo "***" 1>&2; \
45	echo "*** Error: Couldn't find an i386-*-elf version of GCC/binutils." 1>&2; \
46	echo "*** Is the directory with i386-jos-elf-gcc in your PATH?" 1>&2; \
47	echo "*** If your i386-*-elf toolchain is installed with a command" 1>&2; \
48	echo "*** prefix other than 'i386-jos-elf-', set your TOOLPREFIX" 1>&2; \
49	echo "*** environment variable to that prefix and run 'make' again." 1>&2; \
50	echo "*** To turn off this error, run 'gmake TOOLPREFIX= ...'." 1>&2; \
51	echo "***" 1>&2; exit 1; fi)
52endif
53
54# If the makefile can't find QEMU, specify its path here
55# QEMU = qemu-system-i386
56
57# Try to infer the correct QEMU
58ifndef QEMU
59QEMU = $(shell if which qemu > /dev/null; \
60	then echo qemu; exit; \
61	elif which qemu-system-i386 > /dev/null; \
62	then echo qemu-system-i386; exit; \
63	elif which qemu-system-x86_64 > /dev/null; \
64	then echo qemu-system-x86_64; exit; \
65	else \
66	qemu=/Applications/Q.app/Contents/MacOS/i386-softmmu.app/Contents/MacOS/i386-softmmu; \
67	if test -x $$qemu; then echo $$qemu; exit; fi; fi; \
68	echo "***" 1>&2; \
69	echo "*** Error: Couldn't find a working QEMU executable." 1>&2; \
70	echo "*** Is the directory containing the qemu binary in your PATH" 1>&2; \
71	echo "*** or have you tried setting the QEMU variable in Makefile?" 1>&2; \
72	echo "***" 1>&2; exit 1)
73endif
74
75CC = $(TOOLPREFIX)gcc
76AS = $(TOOLPREFIX)gas
77LD = $(TOOLPREFIX)ld
78OBJCOPY = $(TOOLPREFIX)objcopy
79OBJDUMP = $(TOOLPREFIX)objdump
80CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -O2 -Wall -MD -ggdb -m32 -Werror -fno-omit-frame-pointer
81#CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -fvar-tracking -fvar-tracking-assignments -O0 -g -Wall -MD -gdwarf-2 -m32 -Werror -fno-omit-frame-pointer
82CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
83ASFLAGS = -m32 -gdwarf-2 -Wa,-divide
84# FreeBSD ld wants ``elf_i386_fbsd''
85LDFLAGS += -m $(shell $(LD) -V | grep elf_i386 2>/dev/null | head -n 1)
86
87xv6.img: bootblock kernel fs.img
88	dd if=/dev/zero of=xv6.img count=10000
89	dd if=bootblock of=xv6.img conv=notrunc
90	dd if=kernel of=xv6.img seek=1 conv=notrunc
91
92xv6memfs.img: bootblock kernelmemfs
93	dd if=/dev/zero of=xv6memfs.img count=10000
94	dd if=bootblock of=xv6memfs.img conv=notrunc
95	dd if=kernelmemfs of=xv6memfs.img seek=1 conv=notrunc
96
97bootblock: bootasm.S bootmain.c
98	$(CC) $(CFLAGS) -fno-pic -O -nostdinc -I. -c bootmain.c
99	$(CC) $(CFLAGS) -fno-pic -nostdinc -I. -c bootasm.S
100	$(LD) $(LDFLAGS) -N -e start -Ttext 0x7C00 -o bootblock.o bootasm.o bootmain.o
101	$(OBJDUMP) -S bootblock.o > bootblock.asm
102	$(OBJCOPY) -S -O binary -j .text bootblock.o bootblock
103	./sign.pl bootblock
104
105entryother: entryother.S
106	$(CC) $(CFLAGS) -fno-pic -nostdinc -I. -c entryother.S
107	$(LD) $(LDFLAGS) -N -e start -Ttext 0x7000 -o bootblockother.o entryother.o
108	$(OBJCOPY) -S -O binary -j .text bootblockother.o entryother
109	$(OBJDUMP) -S bootblockother.o > entryother.asm
110
111initcode: initcode.S
112	$(CC) $(CFLAGS) -nostdinc -I. -c initcode.S
113	$(LD) $(LDFLAGS) -N -e start -Ttext 0 -o initcode.out initcode.o
114	$(OBJCOPY) -S -O binary initcode.out initcode
115	$(OBJDUMP) -S initcode.o > initcode.asm
116
117kernel: $(OBJS) entry.o entryother initcode kernel.ld
118	$(LD) $(LDFLAGS) -T kernel.ld -o kernel entry.o $(OBJS) -b binary initcode entryother
119	$(OBJDUMP) -S kernel > kernel.asm
120	$(OBJDUMP) -t kernel | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > kernel.sym
121
122# kernelmemfs is a copy of kernel that maintains the
123# disk image in memory instead of writing to a disk.
124# This is not so useful for testing persistent storage or
125# exploring disk buffering implementations, but it is
126# great for testing the kernel on real hardware without
127# needing a scratch disk.
128MEMFSOBJS = $(filter-out ide.o,$(OBJS)) memide.o
129kernelmemfs: $(MEMFSOBJS) entry.o entryother initcode kernel.ld fs.img
130	$(LD) $(LDFLAGS) -T kernel.ld -o kernelmemfs entry.o  $(MEMFSOBJS) -b binary initcode entryother fs.img
131	$(OBJDUMP) -S kernelmemfs > kernelmemfs.asm
132	$(OBJDUMP) -t kernelmemfs | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > kernelmemfs.sym
133
134tags: $(OBJS) entryother.S _init
135	etags *.S *.c
136
137vectors.S: vectors.pl
138	perl vectors.pl > vectors.S
139
140ULIB = ulib.o usys.o printf.o umalloc.o
141
142_%: %.o $(ULIB)
143	$(LD) $(LDFLAGS) -N -e main -Ttext 0 -o $@ $^
144	$(OBJDUMP) -S $@ > $*.asm
145	$(OBJDUMP) -t $@ | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $*.sym
146
147_forktest: forktest.o $(ULIB)
148	# forktest has less library code linked in - needs to be small
149	# in order to be able to max out the proc table.
150	$(LD) $(LDFLAGS) -N -e main -Ttext 0 -o _forktest forktest.o ulib.o usys.o
151	$(OBJDUMP) -S _forktest > forktest.asm
152
153mkfs: mkfs.c fs.h
154	gcc -Werror -Wall -o mkfs mkfs.c
155
156# Prevent deletion of intermediate files, e.g. cat.o, after first build, so
157# that disk image changes after first build are persistent until clean.  More
158# details:
159# http://www.gnu.org/software/make/manual/html_node/Chained-Rules.html
160.PRECIOUS: %.o
161
162UPROGS=\
163	_cat\
164	_echo\
165	_forktest\
166	_grep\
167	_init\
168	_kill\
169	_ln\
170	_ls\
171	_mkdir\
172	_rm\
173	_sh\
174	_stressfs\
175	_usertests\
176	_wc\
177	_zombie\
178
179fs.img: mkfs README $(UPROGS)
180	./mkfs fs.img README $(UPROGS)
181
182-include *.d
183
184clean:
185	rm -f *.tex *.dvi *.idx *.aux *.log *.ind *.ilg \
186	*.o *.d *.asm *.sym vectors.S bootblock entryother \
187	initcode initcode.out kernel xv6.img fs.img kernelmemfs mkfs \
188	.gdbinit \
189	$(UPROGS)
190
191# make a printout
192FILES = $(shell grep -v '^\#' runoff.list)
193PRINT = runoff.list runoff.spec README toc.hdr toc.ftr $(FILES)
194
195xv6.pdf: $(PRINT)
196	./runoff
197	ls -l xv6.pdf
198
199print: xv6.pdf
200
201# run in emulators
202
203bochs : fs.img xv6.img
204	if [ ! -e .bochsrc ]; then ln -s dot-bochsrc .bochsrc; fi
205	bochs -q
206
207# try to generate a unique GDB port
208GDBPORT = $(shell expr `id -u` % 5000 + 25000)
209# QEMU's gdb stub command line changed in 0.11
210QEMUGDB = $(shell if $(QEMU) -help | grep -q '^-gdb'; \
211	then echo "-gdb tcp::$(GDBPORT)"; \
212	else echo "-s -p $(GDBPORT)"; fi)
213ifndef CPUS
214CPUS := 2
215endif
216QEMUOPTS = -drive file=fs.img,index=1,media=disk,format=raw -drive file=xv6.img,index=0,media=disk,format=raw -smp $(CPUS) -m 512 $(QEMUEXTRA)
217
218qemu: fs.img xv6.img
219	$(QEMU) -serial mon:stdio $(QEMUOPTS)
220
221qemu-memfs: xv6memfs.img
222	$(QEMU) -drive file=xv6memfs.img,index=0,media=disk,format=raw -smp $(CPUS) -m 256
223
224qemu-nox: fs.img xv6.img
225	$(QEMU) -nographic $(QEMUOPTS)
226
227.gdbinit: .gdbinit.tmpl
228	sed "s/localhost:1234/localhost:$(GDBPORT)/" < $^ > $@
229
230qemu-gdb: fs.img xv6.img .gdbinit
231	@echo "*** Now run 'gdb'." 1>&2
232	$(QEMU) -serial mon:stdio $(QEMUOPTS) -S $(QEMUGDB)
233
234qemu-nox-gdb: fs.img xv6.img .gdbinit
235	@echo "*** Now run 'gdb'." 1>&2
236	$(QEMU) -nographic $(QEMUOPTS) -S $(QEMUGDB)
237
238# CUT HERE
239# prepare dist for students
240# after running make dist, probably want to
241# rename it to rev0 or rev1 or so on and then
242# check in that version.
243
244EXTRA=\
245	mkfs.c ulib.c user.h cat.c echo.c forktest.c grep.c kill.c\
246	ln.c ls.c mkdir.c rm.c stressfs.c usertests.c wc.c zombie.c\
247	printf.c umalloc.c\
248	README dot-bochsrc *.pl toc.* runoff runoff1 runoff.list\
249	.gdbinit.tmpl gdbutil\
250
251dist:
252	rm -rf dist
253	mkdir dist
254	for i in $(FILES); \
255	do \
256		grep -v PAGEBREAK $$i >dist/$$i; \
257	done
258	sed '/CUT HERE/,$$d' Makefile >dist/Makefile
259	echo >dist/runoff.spec
260	cp $(EXTRA) dist
261
262dist-test:
263	rm -rf dist
264	make dist
265	rm -rf dist-test
266	mkdir dist-test
267	cp dist/* dist-test
268	cd dist-test; $(MAKE) print
269	cd dist-test; $(MAKE) bochs || true
270	cd dist-test; $(MAKE) qemu
271
272# update this rule (change rev#) when it is time to
273# make a new revision.
274tar:
275	rm -rf /tmp/xv6
276	mkdir -p /tmp/xv6
277	cp dist/* dist/.gdbinit.tmpl /tmp/xv6
278	(cd /tmp; tar cf - xv6) | gzip >xv6-rev9.tar.gz  # the next one will be 9 (6/27/15)
279
280.PHONY: dist-test dist
281