|
| 1 | +# Programs built by this makefile |
| 2 | +RUN_PROGRAM = rocket.bin |
| 3 | +TEST_PROGRAM = |
| 4 | + |
| 5 | +MY_MODULE_SOURCES = i2s.c audio.c dma.c |
| 6 | +# MY_MODULE_SOURCES = printf.c |
| 7 | + |
| 8 | +# MY_MODULE_SOURCES is a list of those library modules (such as gpio.c) |
| 9 | +# for which you intend to use your own code. The reference implementation |
| 10 | +# from our libraries will be used for any module you do not name in this list. |
| 11 | +# Editing this list allows you to control whether the program being |
| 12 | +# built is using your code or the reference implementation for each module |
| 13 | +# on a per-module basis. Great for testing! |
| 14 | +# |
| 15 | +# NOTE: when you name a module in this list, it must provide definitions |
| 16 | +# for all of the symbols in the entire module. For example, if you list |
| 17 | +# gpio.c as one of your modules, your gpio.c must define gpio_set_function, |
| 18 | +# gpio_get_function, ... and so on for all functions declared in the gpio.h |
| 19 | +# header file. If your module forgets to implement any of the needed |
| 20 | +# functions, the linker will bring in gpio.o from reference libmango to |
| 21 | +# resolve the missing definition. But you can't have both gpio modules! |
| 22 | +# The linker will report multiple definition errors for every function |
| 23 | +# that occurs in both your gpio.c and the reference gpio.o. No bueno! |
| 24 | +# |
| 25 | +# You shouldn't need to modify anything below this line. |
| 26 | +######################################################## |
| 27 | + |
| 28 | +PROGRAMS = $(RUN_PROGRAM) $(TEST_PROGRAM) |
| 29 | + |
| 30 | +all: $(PROGRAMS) |
| 31 | + |
| 32 | +# Flags for compile and link |
| 33 | +ARCH = -march=rv64im -mabi=lp64 |
| 34 | +ASFLAGS = $(ARCH) |
| 35 | +CFLAGS = $(ARCH) -g -Og -I$$CS107E/include $$warn $$freestanding |
| 36 | +LDFLAGS = -nostdlib -L$$CS107E/lib -T memmap.ld |
| 37 | +LDLIBS = -lmango -lmango_gcc |
| 38 | + |
| 39 | +# Common objects for the programs built by this makefile |
| 40 | +SOURCES = $(MY_MODULE_SOURCES) |
| 41 | +OBJECTS = $(addsuffix .o, $(basename $(SOURCES))) |
| 42 | + |
| 43 | +# Rules and recipes for all build steps |
| 44 | + |
| 45 | +# Extract raw binary from elf executable |
| 46 | +%.bin: %.elf |
| 47 | + riscv64-unknown-elf-objcopy $< -O binary $@ |
| 48 | + |
| 49 | +# Link program executable from program.o and all common objects |
| 50 | +%.elf: $(OBJECTS) %.o |
| 51 | + riscv64-unknown-elf-gcc $(LDFLAGS) $^ $(LDLIBS) -o $@ |
| 52 | + |
| 53 | +# Compile C source to object file |
| 54 | +%.o: %.c *.h |
| 55 | + riscv64-unknown-elf-gcc $(CFLAGS) -c $< -o $@ |
| 56 | + |
| 57 | +# Assemble asm source to object file |
| 58 | +%.o: %.s |
| 59 | + riscv64-unknown-elf-as $(ASFLAGS) $< -o $@ |
| 60 | + |
| 61 | +# Disassemble object file to asm listing |
| 62 | +%.list: %.o |
| 63 | + riscv64-unknown-elf-objdump $(OBJDUMP_FLAGS) $< |
| 64 | + |
| 65 | +# Build and run the application binary |
| 66 | +run: $(RUN_PROGRAM) |
| 67 | + mango-run $< |
| 68 | + |
| 69 | +# Build and run the test binary |
| 70 | +test: $(TEST_PROGRAM) |
| 71 | + mango-run $< |
| 72 | + |
| 73 | +# run the test program under gdb |
| 74 | +debug: $(TEST_PROGRAM:.bin=.elf) |
| 75 | + riscv64-unknown-elf-gdb $(GDB_FLAGS) $< |
| 76 | + |
| 77 | +# Remove all build products |
| 78 | +clean: |
| 79 | + rm -f *.o *.bin *.elf *.list |
| 80 | + |
| 81 | +# this rule will provide better error message when |
| 82 | +# a source file cannot be found (missing, misnamed) |
| 83 | +$(SOURCES) $(PROGRAMS:.bin=.c): |
| 84 | + $(error cannot find source file `$@` needed for build) |
| 85 | + |
| 86 | +# Access .c and .s source files within shared mylib directory using vpath |
| 87 | +# https://www.cmcrossroads.com/article/basics-vpath-and-vpath |
| 88 | +vpath %.c ../mylib |
| 89 | +vpath %.s ../mylib |
| 90 | + |
| 91 | +.PHONY: all clean run test |
| 92 | +.PRECIOUS: %.elf %.o |
| 93 | + |
| 94 | +# disable built-in rules (they are not used) |
| 95 | +.SUFFIXES: |
| 96 | + |
| 97 | +export warn = -Wall -Wpointer-arith -Wwrite-strings -Werror \ |
| 98 | + -Wno-error=unused-function -Wno-error=unused-variable \ |
| 99 | + -fno-diagnostics-show-option |
| 100 | +export freestanding = -ffreestanding -nostdinc \ |
| 101 | + -isystem $(shell riscv64-unknown-elf-gcc -print-file-name=include) |
| 102 | +OBJDUMP_FLAGS = -d --no-show-raw-insn --no-addresses --disassembler-color=terminal --visualize-jumps |
| 103 | +GDB_FLAGS = -q --command=$$CS107E/other/gdbsim.commands |
0 commit comments