Skip to content

Commit 86d4dd1

Browse files
committed
Improve Makefile for recursive invocation
1 parent 25aa104 commit 86d4dd1

File tree

2 files changed

+67
-16
lines changed

2 files changed

+67
-16
lines changed

Makefile

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ OPT_LEVEL := 0 1 2
55
CFLAGS := -std=c99
66
LDFLAGS := -lsemihost -lm
77

8-
SRC_DIR := src
9-
BUILD_DIR := build
8+
CURDIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
9+
SRC_DIR := $(CURDIR)/src
10+
BUILD_DIR := $(CURDIR)/build
1011

1112
SRCS := \
1213
captcha.c \
@@ -43,31 +44,34 @@ ifneq ($(filter $(OPT_LEVEL), 2),)
4344
all: build-O2
4445
endif
4546

47+
all:
48+
$(Q)$(MAKE) -C src/asm-hello BUILD_DIR="$(BUILD_DIR)" RV32_EXT=$(RV32_EXT) OPT_LEVEL="$(OPT_LEVEL)" all
49+
4650
include mk/common.mk
4751
include mk/toolchain.mk
4852

4953
build-O0: $(addprefix $(BUILD_DIR)/$(RV32_EXT)/O0/,$(EXECUTABLES))
5054

5155
$(BUILD_DIR)/$(RV32_EXT)/O0/%.elf: $(SRC_DIR)/%.c | build-toolchain
52-
$(VECHO) " RISCVCC\t$@\n"
56+
$(VECHO) " RISCVCC\t$(patsubst $(abspath $(SRC_DIR)/..)/%,%,$@)\n"
5357
$(Q)$(CROSS_COMPILE) -o $@ -O0 $(CFLAGS) $< $(LDFLAGS)
5458

5559
build-O1: $(addprefix $(BUILD_DIR)/$(RV32_EXT)/O1/,$(EXECUTABLES))
5660

5761
$(BUILD_DIR)/$(RV32_EXT)/O1/%.elf: $(SRC_DIR)/%.c | build-toolchain
58-
$(VECHO) " RISCVCC\t$@\n"
62+
$(VECHO) " RISCVCC\t$(patsubst $(abspath $(SRC_DIR)/..)/%,%,$@)\n"
5963
$(Q)$(CROSS_COMPILE) -o $@ -O1 $(CFLAGS) $< $(LDFLAGS)
6064

6165
build-O2: $(addprefix $(BUILD_DIR)/$(RV32_EXT)/O2/,$(EXECUTABLES))
6266

6367
$(BUILD_DIR)/$(RV32_EXT)/O2/%.elf: $(SRC_DIR)/%.c | build-toolchain
64-
$(VECHO) " RISCVCC\t$@\n"
68+
$(VECHO) " RISCVCC\t$(patsubst $(abspath $(SRC_DIR)/..)/%,%,$@)\n"
6569
$(Q)$(CROSS_COMPILE) -o $@ -O2 $(CFLAGS) $< $(LDFLAGS)
6670

6771
clean:
68-
$(RM) -r $(BUILD_DIR)/$(RV32_EXT)/*
72+
$(RM) -r $(foreach LEVEL,$(OPT_LEVEL),$(BUILD_DIR)/$(RV32_EXT)/O$(LEVEL)/*)
6973

7074
distclean: clean
7175
$(RM) -r $(BUILD_DIR)/*
7276
$(RM) -r $(RISCV_TOOLCHAIN_DIR)/build/*
73-
$(MAKE) -C $(RISCV_TOOLCHAIN_DIR) distclean
77+
$(Q)$(MAKE) -C $(RISCV_TOOLCHAIN_DIR) distclean

src/asm-hello/Makefile

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,64 @@
1-
include ../../mk/toolchain.mk
1+
RV32_EXT ?= rv32i
2+
OPT_LEVEL ?= 0 1 2
23

3-
ASFLAGS := -march=rv32i -mabi=ilp32
4+
ASFLAGS := -march=$(RV32_EXT) -mabi=ilp32
45
LDFLAGS := --oformat=elf32-littleriscv
56

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))
748

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,$@)
1054

11-
all: hello.elf
55+
build-O2: $(addprefix $(BUILD_DIR)/$(RV32_EXT)/O2/asm-hello/,$(EXECUTABLES))
1256

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,$@)
1562

1663
clean:
17-
$(RM) hello.elf hello.o
64+
$(RM) $(foreach LEVEL,$(OPT_LEVEL),$(BUILD_DIR)/$(RV32_EXT)/O$(LEVEL)/asm-hello/*)

0 commit comments

Comments
 (0)