|
1 | | -include ../../mk/toolchain.mk |
| 1 | +RV32_EXT ?= rv32i |
| 2 | +OPT_LEVEL ?= 0 1 2 |
2 | 3 |
|
3 | | -ASFLAGS := -march=rv32i -mabi=ilp32 |
| 4 | +ASFLAGS := -march=$(RV32_EXT) -mabi=ilp32 |
4 | 5 | LDFLAGS := --oformat=elf32-littleriscv |
5 | 6 |
|
6 | | -.PHONY: all clean |
| 7 | +CURDIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST)))) |
| 8 | +SRC_DIR := $(CURDIR) |
| 9 | + |
| 10 | +# prevent lazy set if not providing initial value |
| 11 | +ifeq ($(BUILD_DIR),) |
| 12 | + BUILD_DIR := $(CURDIR)/build |
| 13 | +endif |
| 14 | + |
| 15 | +SRCS := hello.S |
| 16 | +EXECUTABLES := hello.elf |
| 17 | + |
| 18 | +SHELL_HACK := $(shell mkdir -p $(foreach LEVEL,$(OPT_LEVEL),$(BUILD_DIR)/$(RV32_EXT)/O$(LEVEL)/asm-hello)) |
| 19 | + |
| 20 | +.PHONY: all clean build-O0 build-O1 build-O2 |
| 21 | + |
| 22 | +ifneq ($(filter $(OPT_LEVEL), 0),) |
| 23 | +all: build-O0 |
| 24 | +endif |
| 25 | + |
| 26 | +ifneq ($(filter $(OPT_LEVEL), 1),) |
| 27 | +all: build-O1 |
| 28 | +endif |
| 29 | + |
| 30 | +ifneq ($(filter $(OPT_LEVEL), 2),) |
| 31 | +all: build-O2 |
| 32 | +endif |
| 33 | + |
| 34 | +include ../../mk/common.mk |
| 35 | +include ../../mk/toolchain.mk |
| 36 | + |
| 37 | +# RISC-V assembler doesn't support "-O" option currently |
| 38 | + |
| 39 | +build-O0: $(addprefix $(BUILD_DIR)/$(RV32_EXT)/O0/asm-hello/,$(EXECUTABLES)) |
| 40 | + |
| 41 | +$(BUILD_DIR)/$(RV32_EXT)/O0/asm-hello/%.elf: $(SRC_DIR)/%.S | build-toolchain |
| 42 | + $(VECHO) " RISCVAS\t$(patsubst $(abspath $(BUILD_DIR)/..)/%.elf,%.o,$@)\n" |
| 43 | + $(Q)$(RV32_PREFIX)as -o $(patsubst %.elf,%.o,$@) -R $(ASFLAGS) $< |
| 44 | + $(VECHO) " RISCVLD\t$(patsubst $(abspath $(BUILD_DIR)/..)/%,%,$@)\n" |
| 45 | + $(Q)$(RV32_PREFIX)ld -o $@ -T $(SRC_DIR)/hello.ld -O0 $(LDFLAGS) $(patsubst %.elf,%.o,$@) |
| 46 | + |
| 47 | +build-O1: $(addprefix $(BUILD_DIR)/$(RV32_EXT)/O1/asm-hello/,$(EXECUTABLES)) |
7 | 48 |
|
8 | | -%.o: %.S |
9 | | - $(CROSS_COMPILE)as -R $(ASFLAGS) -o $@ $< |
| 49 | +$(BUILD_DIR)/$(RV32_EXT)/O1/asm-hello/%.elf: $(SRC_DIR)/%.S | build-toolchain |
| 50 | + $(VECHO) " RISCVAS\t$(patsubst $(abspath $(BUILD_DIR)/..)/%.elf,%.o,$@)\n" |
| 51 | + $(Q)$(RV32_PREFIX)as -o $(patsubst %.elf,%.o,$@) -R $(ASFLAGS) $< |
| 52 | + $(VECHO) " RISCVLD\t$(patsubst $(abspath $(BUILD_DIR)/..)/%,%,$@)\n" |
| 53 | + $(Q)$(RV32_PREFIX)ld -o $@ -T $(SRC_DIR)/hello.ld -O1 $(LDFLAGS) $(patsubst %.elf,%.o,$@) |
10 | 54 |
|
11 | | -all: hello.elf |
| 55 | +build-O2: $(addprefix $(BUILD_DIR)/$(RV32_EXT)/O2/asm-hello/,$(EXECUTABLES)) |
12 | 56 |
|
13 | | -hello.elf: hello.o |
14 | | - $(CROSS_COMPILE)ld -o $@ -T hello.ld $(LDFLAGS) $< |
| 57 | +$(BUILD_DIR)/$(RV32_EXT)/O2/asm-hello/%.elf: $(SRC_DIR)/%.S | build-toolchain |
| 58 | + $(VECHO) " RISCVAS\t$(patsubst $(abspath $(BUILD_DIR)/..)/%.elf,%.o,$@)\n" |
| 59 | + $(Q)$(RV32_PREFIX)as -o $(patsubst %.elf,%.o,$@) -R $(ASFLAGS) $< |
| 60 | + $(VECHO) " RISCVLD\t$(patsubst $(abspath $(BUILD_DIR)/..)/%,%,$@)\n" |
| 61 | + $(Q)$(RV32_PREFIX)ld -o $@ -T $(SRC_DIR)/hello.ld -O2 $(LDFLAGS) $(patsubst %.elf,%.o,$@) |
15 | 62 |
|
16 | 63 | clean: |
17 | | - $(RM) hello.elf hello.o |
| 64 | + $(RM) $(foreach LEVEL,$(OPT_LEVEL),$(BUILD_DIR)/$(RV32_EXT)/O$(LEVEL)/asm-hello/*) |
0 commit comments