Makefile
195 lines
1K=kernel2U=user34OBJS = \5 $K/entry.o \6 $K/start.o \7 $K/console.o \8 $K/printf.o \9 $K/uart.o \10 $K/kalloc.o \11 $K/spinlock.o \12 $K/string.o \13 $K/main.o \14 $K/vm.o \15 $K/proc.o \16 $K/swtch.o \17 $K/trampoline.o \18 $K/trap.o \19 $K/syscall.o \20 $K/sysproc.o \21 $K/bio.o \22 $K/fs.o \23 $K/log.o \24 $K/sleeplock.o \25 $K/file.o \26 $K/pipe.o \27 $K/exec.o \28 $K/sysfile.o \29 $K/kernelvec.o \30 $K/plic.o \31 $K/virtio_disk.o3233# riscv64-unknown-elf- or riscv64-linux-gnu-34# perhaps in /opt/riscv/bin35#TOOLPREFIX = 3637# Try to infer the correct TOOLPREFIX if not set38ifndef TOOLPREFIX39TOOLPREFIX := $(shell if riscv64-unknown-elf-objdump -i 2>&1 | grep 'elf64-big' >/dev/null 2>&1; \40 then echo 'riscv64-unknown-elf-'; \41 elif riscv64-elf-objdump -i 2>&1 | grep 'elf64-big' >/dev/null 2>&1; \42 then echo 'riscv64-elf-'; \43 elif riscv64-none-elf-objdump -i 2>&1 | grep 'elf64-big' >/dev/null 2>&1; \44 then echo 'riscv64-none-elf-'; \45 elif riscv64-linux-gnu-objdump -i 2>&1 | grep 'elf64-big' >/dev/null 2>&1; \46 then echo 'riscv64-linux-gnu-'; \47 elif riscv64-unknown-linux-gnu-objdump -i 2>&1 | grep 'elf64-big' >/dev/null 2>&1; \48 then echo 'riscv64-unknown-linux-gnu-'; \49 else echo "***" 1>&2; \50 echo "*** Error: Couldn't find a riscv64 version of GCC/binutils." 1>&2; \51 echo "*** To turn off this error, run 'gmake TOOLPREFIX= ...'." 1>&2; \52 echo "***" 1>&2; exit 1; fi)53endif5455QEMU = qemu-system-riscv6456MIN_QEMU_VERSION = 7.25758CC = $(TOOLPREFIX)gcc59AS = $(TOOLPREFIX)gas60LD = $(TOOLPREFIX)ld61OBJCOPY = $(TOOLPREFIX)objcopy62OBJDUMP = $(TOOLPREFIX)objdump6364CFLAGS = -Wall -Werror -Wno-unknown-attributes -O -fno-omit-frame-pointer -ggdb -gdwarf-265CFLAGS += -march=rv64gc66CFLAGS += -MD67CFLAGS += -mcmodel=medany68CFLAGS += -ffreestanding69CFLAGS += -fno-common -nostdlib70CFLAGS += -fno-builtin-strncpy -fno-builtin-strncmp -fno-builtin-strlen -fno-builtin-memset71CFLAGS += -fno-builtin-memmove -fno-builtin-memcmp -fno-builtin-log -fno-builtin-bzero72CFLAGS += -fno-builtin-strchr -fno-builtin-exit -fno-builtin-malloc -fno-builtin-putc73CFLAGS += -fno-builtin-free74CFLAGS += -fno-builtin-memcpy -Wno-main75CFLAGS += -fno-builtin-printf -fno-builtin-fprintf -fno-builtin-vprintf76CFLAGS += -I.77CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)7879# Disable PIE when possible (for Ubuntu 16.10 toolchain)80ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]no-pie'),)81CFLAGS += -fno-pie -no-pie82endif83ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]nopie'),)84CFLAGS += -fno-pie -nopie85endif8687LDFLAGS = -z max-page-size=40968889$K/kernel: $(OBJS) $K/kernel.ld90 $(LD) $(LDFLAGS) -T $K/kernel.ld -o $K/kernel $(OBJS) 91 $(OBJDUMP) -S $K/kernel > $K/kernel.asm92 $(OBJDUMP) -t $K/kernel | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $K/kernel.sym9394$K/%.o: $K/%.S95 $(CC) -march=rv64gc -g -c -o $@ $<9697tags: $(OBJS)98 etags kernel/*.S kernel/*.c99100ULIB = $U/ulib.o $U/usys.o $U/printf.o $U/umalloc.o101102_%: %.o $(ULIB) $U/user.ld103 $(LD) $(LDFLAGS) -T $U/user.ld -o $@ $< $(ULIB)104 $(OBJDUMP) -S $@ > $*.asm105 $(OBJDUMP) -t $@ | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $*.sym106107$U/usys.S : $U/usys.pl108 perl $U/usys.pl > $U/usys.S109110$U/usys.o : $U/usys.S111 $(CC) $(CFLAGS) -c -o $U/usys.o $U/usys.S112113$U/_forktest: $U/forktest.o $(ULIB)114 # forktest has less library code linked in - needs to be small115 # in order to be able to max out the proc table.116 $(LD) $(LDFLAGS) -N -e main -Ttext 0 -o $U/_forktest $U/forktest.o $U/ulib.o $U/usys.o117 $(OBJDUMP) -S $U/_forktest > $U/forktest.asm118119mkfs/mkfs: mkfs/mkfs.c $K/fs.h $K/param.h120 gcc -Wno-unknown-attributes -I. -o mkfs/mkfs mkfs/mkfs.c121122# Prevent deletion of intermediate files, e.g. cat.o, after first build, so123# that disk image changes after first build are persistent until clean. More124# details:125# http://www.gnu.org/software/make/manual/html_node/Chained-Rules.html126.PRECIOUS: %.o127128UPROGS=\129 $U/_cat\130 $U/_echo\131 $U/_forktest\132 $U/_grep\133 $U/_init\134 $U/_kill\135 $U/_ln\136 $U/_ls\137 $U/_mkdir\138 $U/_rm\139 $U/_sh\140 $U/_stressfs\141 $U/_usertests\142 $U/_grind\143 $U/_wc\144 $U/_zombie\145 $U/_logstress\146 $U/_forphan\147 $U/_dorphan\148149fs.img: mkfs/mkfs README $(UPROGS)150 mkfs/mkfs fs.img README $(UPROGS)151152-include kernel/*.d user/*.d153154clean: 155 rm -f *.tex *.dvi *.idx *.aux *.log *.ind *.ilg \156 */*.o */*.d */*.asm */*.sym \157 $K/kernel fs.img \158 mkfs/mkfs .gdbinit \159 $U/usys.S \160 $(UPROGS)161162# try to generate a unique GDB port163GDBPORT = $(shell expr `id -u` % 5000 + 25000)164# QEMU's gdb stub command line changed in 0.11165QEMUGDB = $(shell if $(QEMU) -help | grep -q '^-gdb'; \166 then echo "-gdb tcp::$(GDBPORT)"; \167 else echo "-s -p $(GDBPORT)"; fi)168ifndef CPUS169CPUS := 3170endif171172QEMUOPTS = -machine virt -bios none -kernel $K/kernel -m 128M -smp $(CPUS) -nographic173QEMUOPTS += -global virtio-mmio.force-legacy=false174QEMUOPTS += -drive file=fs.img,if=none,format=raw,id=x0175QEMUOPTS += -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0176177qemu: check-qemu-version $K/kernel fs.img178 $(QEMU) $(QEMUOPTS)179180.gdbinit: .gdbinit.tmpl-riscv181 sed "s/:1234/:$(GDBPORT)/" < $^ > $@182183qemu-gdb: $K/kernel .gdbinit fs.img184 @echo "*** Now run 'gdb' in another window." 1>&2185 $(QEMU) $(QEMUOPTS) -S $(QEMUGDB)186187print-gdbport:188 @echo $(GDBPORT)189190QEMU_VERSION := $(shell $(QEMU) --version | head -n 1 | sed -E 's/^QEMU emulator version ([0-9]+\.[0-9]+)\..*/\1/')191check-qemu-version:192 @if [ "$(shell echo "$(QEMU_VERSION) >= $(MIN_QEMU_VERSION)" | bc)" -eq 0 ]; then \193 echo "ERROR: Need qemu version >= $(MIN_QEMU_VERSION)"; \194 exit 1; \195 fi196