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