Skip to content

Commit 58b1e67

Browse files
committed
Add LLVM toolchain support to RISC-V build system
- Detects `TOOLCHAIN_TYPE=llvm` from the environment - Fixes build errors for LLVM: - Removes unused `coalesced` variable in `malloc.c` - Replaces `__maybe_unused` with portable `__attribute__((__unused__))`
1 parent ceee53c commit 58b1e67

File tree

3 files changed

+41
-20
lines changed

3 files changed

+41
-20
lines changed

arch/riscv/build.mk

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,50 @@ DEFINES := -DF_CPU=$(F_CLK) \
1717
-DF_TIMER=$(F_TICK) \
1818
-include config.h
1919

20-
ASFLAGS = -march=rv32imzicsr -mabi=ilp32
21-
CFLAGS += -Wall -Wextra -Wshadow -Wno-unused-parameter -Werror
20+
# Toolchain override (default to GNU)
21+
CROSS_COMPILE ?= riscv32-unknown-elf-
22+
TOOLCHAIN_TYPE ?= gnu
23+
24+
# Architecture flags
25+
ARCH_FLAGS = -march=rv32imzicsr -mabi=ilp32
26+
27+
# Common compiler flags
28+
CFLAGS += -Wall -Wextra -Werror -Wshadow -Wno-unused-parameter
2229
CFLAGS += -O2 -std=gnu99
23-
CFLAGS += -march=rv32imzicsr -mabi=ilp32
30+
CFLAGS += $(ARCH_FLAGS)
2431
CFLAGS += -mstrict-align -ffreestanding -nostdlib -fomit-frame-pointer
2532
CFLAGS += $(INC_DIRS) $(DEFINES) -fdata-sections -ffunction-sections
26-
ARFLAGS = r
2733

28-
# Linker flags
29-
LDFLAGS = -melf32lriscv --gc-sections
30-
LDSCRIPT = $(ARCH_DIR)/riscv32-qemu.ld
34+
ifeq ($(TOOLCHAIN_TYPE),llvm)
35+
CC = $(CROSS_COMPILE)clang
36+
AS = $(CROSS_COMPILE)clang
37+
LD = $(CROSS_COMPILE)ld.lld
38+
DUMP = $(CROSS_COMPILE)llvm-objdump -M no-aliases
39+
READ = $(CROSS_COMPILE)llvm-readelf
40+
OBJ = $(CROSS_COMPILE)llvm-objcopy
41+
SIZE = $(CROSS_COMPILE)llvm-size
42+
AR = $(CROSS_COMPILE)ar
3143

32-
CROSS_COMPILE ?= riscv-none-elf-
33-
CC = $(CROSS_COMPILE)gcc
34-
AS = $(CROSS_COMPILE)as
35-
LD = $(CROSS_COMPILE)ld
36-
DUMP = $(CROSS_COMPILE)objdump -Mno-aliases
37-
READ = $(CROSS_COMPILE)readelf
38-
OBJ = $(CROSS_COMPILE)objcopy
39-
SIZE = $(CROSS_COMPILE)size
40-
AR = $(CROSS_COMPILE)ar
44+
CFLAGS += --target=riscv32-unknown-elf
45+
CFLAGS += -Wno-unused-command-line-argument
46+
ASFLAGS = --target=riscv32-unknown-elf $(ARCH_FLAGS)
47+
LDFLAGS = -m elf32lriscv --gc-sections
48+
else
49+
CC = $(CROSS_COMPILE)gcc
50+
AS = $(CROSS_COMPILE)as
51+
LD = $(CROSS_COMPILE)ld
52+
DUMP = $(CROSS_COMPILE)objdump -Mno-aliases
53+
READ = $(CROSS_COMPILE)readelf
54+
OBJ = $(CROSS_COMPILE)objcopy
55+
SIZE = $(CROSS_COMPILE)size
56+
AR = $(CROSS_COMPILE)ar
57+
58+
ASFLAGS = $(ARCH_FLAGS)
59+
LDFLAGS = -melf32lriscv --gc-sections
60+
endif
61+
62+
ARFLAGS = r
63+
LDSCRIPT = $(ARCH_DIR)/riscv32-qemu.ld
4164

4265
HAL_OBJS := boot.o hal.o muldiv.o
4366
HAL_OBJS := $(addprefix $(BUILD_KERNEL_DIR)/,$(HAL_OBJS))

kernel/pipe.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ static inline uint16_t pipe_free_space_internal(const pipe_t *p)
3434
return (p->mask + 1) - p->used;
3535
}
3636

37-
static inline char pipe_get_byte(pipe_t *p)
37+
static inline __attribute__((__unused__)) char pipe_get_byte(pipe_t *p)
3838
{
3939
char val = p->buf[p->head];
4040
p->head = (p->head + 1) & p->mask;
4141
p->used--;
4242
return val;
4343
}
4444

45-
static inline void pipe_put_byte(pipe_t *p, char c)
45+
static inline __attribute__((__unused__)) void pipe_put_byte(pipe_t *p, char c)
4646
{
4747
p->buf[p->tail] = c;
4848
p->tail = (p->tail + 1) & p->mask;

lib/malloc.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,13 @@ void free(void *ptr)
109109
static void selective_coalesce(void)
110110
{
111111
memblock_t *p = first_free;
112-
uint32_t coalesced = 0;
113112

114113
while (p && p->next) {
115114
/* Merge only when blocks are FREE *and* adjacent in memory */
116115
uint8_t *pend = (uint8_t *) p + sizeof(memblock_t) + GET_SIZE(p);
117116
if (!IS_USED(p) && !IS_USED(p->next) && pend == (uint8_t *) p->next) {
118117
p->size = GET_SIZE(p) + sizeof(memblock_t) + GET_SIZE(p->next);
119118
p->next = p->next->next;
120-
coalesced++;
121119
free_blocks_count--;
122120
} else {
123121
p = p->next;

0 commit comments

Comments
 (0)