1+ # ###############################################################################
2+ # Initial setup of Makefile environment
3+
14BOARD ?= MPS2_AN385
25
36# Make the build directory reflect the board.
@@ -14,16 +17,79 @@ QSTR_DEFS = qstrdefsport.h
1417
1518# MicroPython feature configurations
1619MICROPY_ROM_TEXT_COMPRESSION ?= 1
20+
21+ ifeq ($(QEMU_ARCH ) ,arm)
1722FROZEN_MANIFEST ?= "freeze('test-frzmpy')"
23+ endif
24+ ifeq ($(QEMU_ARCH ) ,riscv32)
25+ FROZEN_MANIFEST ?= "freeze('test-frzmpy', ('frozen_const.py', 'frozen_viper.py', 'native_frozen_align.py'))"
26+ endif
1827
1928# include py core make definitions
2029include $(TOP ) /py/py.mk
2130include $(TOP ) /extmod/extmod.mk
2231
32+ # ###############################################################################
33+ # ARM specific settings
34+
35+ ifeq ($(QEMU_ARCH ) ,arm)
36+
2337CROSS_COMPILE ?= arm-none-eabi-
2438
39+ LDFLAGS += -nostdlib
40+ LIBS = $(shell $(CC ) $(CFLAGS ) -print-libgcc-file-name)
41+
42+ SRC_C += \
43+ mcu/arm/startup.c \
44+ shared/runtime/semihosting_arm.c \
45+
46+ endif
47+
48+ # ###############################################################################
49+ # RISC-V 32-bit specific settings
50+
51+ ifeq ($(QEMU_ARCH ) ,riscv32)
52+
53+ CROSS_COMPILE ?= riscv64-unknown-elf-
54+
55+ GCC_VERSION = $(word 1, $(subst ., , $(shell $(CC ) -dumpversion) ) )
56+
57+ RV32_ABI = ilp32
58+
59+ QEMU_ARGS += -bios none
60+
61+ # GCC 10 and lower do not recognise the Zicsr extension in the architecture name.
62+ ifeq ($(shell test $(GCC_VERSION ) -le 10; echo $$? ) ,0)
63+ RV32_ARCH ?= rv32imac
64+ else
65+ # Recent GCC versions explicitly require to declare extensions.
66+ RV32_ARCH ?= rv32imac_zicsr
67+ endif
68+
69+ AFLAGS += -mabi=$(RV32_ABI ) -march=$(RV32_ARCH )
70+ CFLAGS += $(AFLAGS )
71+ LDFLAGS += -mabi=$(RV32_ABI ) -march=$(RV32_ARCH ) -Wl,-EL
72+
73+ SRC_C += \
74+ mcu/rv32/interrupts.c \
75+ mcu/rv32/startup.c \
76+
77+ SRC_BOARD_O += mcu/rv32/entrypoint.o
78+
79+ endif
80+
81+ # ###############################################################################
82+ # Project specific settings and compiler/linker flags
83+
2584QEMU_SYSTEM = qemu-system-$(QEMU_ARCH )
2685QEMU_ARGS += -machine $(QEMU_MACHINE ) -nographic -monitor null -semihosting
86+ QEMU_ARGS += $(QEMU_EXTRA )
87+
88+ # Specifying QEMU_DEBUG=1 will block qemu until a debugger is connected.
89+ ifeq ($(QEMU_DEBUG ) ,1)
90+ QEMU_DEBUG_ARGS ?= -s
91+ QEMU_ARGS += -S $(QEMU_DEBUG_ARGS ) $(QEMU_DEBUG_EXTRA )
92+ endif
2793
2894INC += -I.
2995INC += -I$(TOP )
@@ -34,6 +100,8 @@ CFLAGS += $(INC) -Wall -Wpointer-arith -Wdouble-promotion -Wfloat-conversion -We
34100 -ffunction-sections -fdata-sections
35101CFLAGS += $(CFLAGS_EXTRA )
36102
103+ LDFLAGS += -T $(LDSCRIPT ) -Wl,--gc-sections -Wl,-Map=$(@:.elf=.map )
104+
37105# Debugging/Optimization
38106ifeq ($(DEBUG ) , 1)
39107CFLAGS += -g
@@ -42,25 +110,31 @@ else
42110COPT += -Os -DNDEBUG
43111endif
44112
45- # # With CoudeSourcery it's actually a little different, you just need `-T generic-m-hosted.ld`.
46- # # Although for some reason `$(LD)` will not find that linker script, it works with `$(CC)`.
47- # # It turns out that this is specific to CoudeSourcery, and ARM version of GCC ships something
48- # # else instead and according to the following files, this is what we need to pass to `$(CC).
49- # # - gcc-arm-none-eabi-4_8-2014q1/share/gcc-arm-none-eabi/samples/src/makefile.conf
50- # # - gcc-arm-none-eabi-4_8-2014q1/share/gcc-arm-none-eabi/samples/src/qemu/Makefile
51- LDFLAGS = -T $(LDSCRIPT ) --gc-sections -Map=$(@:.elf=.map )
52- LIBS = $(shell $(CC ) $(CFLAGS ) -print-libgcc-file-name)
113+ # If Picolibc is available then select it explicitly. Ubuntu 22.04 ships its
114+ # bare metal RISC-V toolchain with Picolibc rather than Newlib, and the default
115+ # is "nosys" so a value must be provided. To avoid having per-distro
116+ # workarounds, always select Picolibc if available.
117+ PICOLIBC_SPECS = $(shell $(CC ) --print-file-name=picolibc.specs)
118+ ifeq ($(PICOLIBC_SPECS ) ,picolibc.specs)
119+ # Picolibc was not found.
120+ else
121+ $(info picolibc used $(PICOLIBC_SPECS))
122+ SPECS_FRAGMENT = --specs=$(PICOLIBC_SPECS )
123+ CFLAGS += $(SPECS_FRAGMENT )
124+ LDFLAGS += $(SPECS_FRAGMENT )
125+ endif
53126
54- SRC_C = \
127+ # ###############################################################################
128+ # Source files and libraries
129+
130+ SRC_C += \
55131 main.c \
56- startup.c \
57132 uart.c \
58133 mphalport.c \
59134 shared/libc/string0.c \
60135 shared/readline/readline.c \
61136 shared/runtime/interrupt_char.c \
62137 shared/runtime/pyexec.c \
63- shared/runtime/semihosting_arm.c \
64138 shared/runtime/stdout_helpers.c \
65139 shared/runtime/sys_stdio_mphal.c \
66140
@@ -75,6 +149,9 @@ OBJ += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o))
75149# List of sources for qstr extraction
76150SRC_QSTR += $(SRC_C ) $(LIB_SRC_C )
77151
152+ # ###############################################################################
153+ # Main targets
154+
78155all : $(BUILD ) /firmware.elf
79156
80157.PHONY : repl
@@ -91,9 +168,11 @@ test: $(BUILD)/firmware.elf
91168 $(eval DIRNAME=ports/$(notdir $(CURDIR ) ) )
92169 cd $(TOP ) /tests && ./run-tests.py --target qemu-arm --device execpty:" $( QEMU_SYSTEM) $( QEMU_ARGS) -serial pty -kernel ../$( DIRNAME) /$<" $(RUN_TESTS_ARGS ) $(RUN_TESTS_EXTRA )
93170
94- # # `$(LD)` doesn't seem to like `--specs` for some reason, but we can just use `$(CC)` here.
95171$(BUILD ) /firmware.elf : $(LDSCRIPT ) $(OBJ )
96- $(Q )$(LD ) $(LDFLAGS ) -o $@ $(OBJ ) $(LIBS )
172+ $(Q )$(CC ) $(LDFLAGS ) -o $@ $(OBJ ) $(LIBS )
97173 $(Q )$(SIZE ) $@
98174
175+ # ###############################################################################
176+ # Remaining make rules
177+
99178include $(TOP ) /py/mkrules.mk
0 commit comments