Skip to content

Commit 3f64d3e

Browse files
authored
Merge pull request #619 from sysprog21/refine-configurations
Refine build system for efficient configurations
2 parents 994257a + 18f6db1 commit 3f64d3e

File tree

10 files changed

+151
-31
lines changed

10 files changed

+151
-31
lines changed

.github/workflows/main.yml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,13 @@ jobs:
284284
env:
285285
CC: ${{ steps.install_cc.outputs.cc }}
286286
run: |
287+
. .ci/common.sh
288+
export LATEST_RELEASE=$(download_with_headers "https://api.github.com/repos/sysprog21/rv32emu-prebuilt/releases" \
289+
"Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
290+
| grep '"tag_name"' \
291+
| grep "sail" \
292+
| head -n 1 \
293+
| sed -E 's/.*"tag_name": "([^"]+)".*/\1/')
287294
.ci/riscv-tests.sh
288295
if: ${{ always() }}
289296

@@ -341,7 +348,8 @@ jobs:
341348
- uses: actions/checkout@v4
342349
- name: install-dependencies
343350
run: |
344-
brew install make dtc expect sdl2 sdl2_mixer bc e2fsprogs p7zip llvm@18 dcfldd
351+
brew install make dtc expect sdl2 bc e2fsprogs p7zip llvm@18 dcfldd
352+
brew install sdl2_mixer || echo "Warning: sdl2_mixer installation failed, continuing without SDL_MIXER support"
345353
.ci/riscv-toolchain-install.sh
346354
echo "${{ github.workspace }}/toolchain/bin" >> $GITHUB_PATH
347355
echo "$(brew --prefix llvm@18)/bin" >> $GITHUB_PATH
@@ -493,6 +501,13 @@ jobs:
493501
env:
494502
CC: ${{ steps.install_cc.outputs.cc }}
495503
run: |
504+
. .ci/common.sh
505+
export LATEST_RELEASE=$(download_with_headers "https://api.github.com/repos/sysprog21/rv32emu-prebuilt/releases" \
506+
"Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
507+
| grep '"tag_name"' \
508+
| grep "sail" \
509+
| head -n 1 \
510+
| sed -E 's/.*"tag_name": "([^"]+)".*/\1/')
496511
python3 -m venv venv
497512
. venv/bin/activate
498513
.ci/riscv-tests.sh
@@ -529,8 +544,12 @@ jobs:
529544
sudo apt-get install -q=2 clang-18 clang-tools-18
530545
shell: bash
531546
- name: run scan-build without JIT
547+
env:
548+
LATEST_RELEASE: dummy
532549
run: make distclean && scan-build-18 -v -o ~/scan-build --status-bugs --use-cc=clang-18 --force-analyze-debug-code --show-description -analyzer-config stable-report-filename=true -enable-checker valist,nullability make ENABLE_EXT_F=0 ENABLE_SDL=0 ENABLE_JIT=0
533550
- name: run scan-build with JIT
551+
env:
552+
LATEST_RELEASE: dummy
534553
run: |
535554
make ENABLE_JIT=1 distclean && scan-build-18 -v -o ~/scan-build --status-bugs --use-cc=clang-18 --force-analyze-debug-code --show-description -analyzer-config stable-report-filename=true -enable-checker valist,nullability make ENABLE_EXT_F=0 ENABLE_SDL=0 ENABLE_JIT=1
536555

Makefile

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
include mk/common.mk
22
include mk/toolchain.mk
33

4+
# Verify GNU make version (3.80+ required for order-only prerequisites)
5+
ifeq ($(filter 3.80 3.81 3.82 3.83 3.84 4.% 5.%,$(MAKE_VERSION)),)
6+
$(error GNU make 3.80 or higher is required. Current version: $(MAKE_VERSION))
7+
endif
8+
49
OUT ?= build
510
BIN := $(OUT)/rv32emu
611

@@ -46,6 +51,7 @@ endif
4651
# Device Tree(initrd, memory range)
4752
# src/io.c(memory init)
4853
# src/riscv.c(system emulation layout init)
54+
# Note: These memory settings are for SYSTEM mode only (when ELF_LOADER=0)
4955
ifeq ($(call has, SYSTEM), 1)
5056
ifeq ($(call has, ELF_LOADER), 0)
5157
MiB = 1024*1024
@@ -66,6 +72,7 @@ CFLAGS_dt += -DMEM_START=0x$(MEM_START) \
6672
-DINITRD_END=0x$(shell echo "obase=16; ibase=16; \
6773
$(REAL_MEM_SIZE) - $(call compute_size, $(DTB_SIZE)) - 1" | bc)
6874

75+
# Memory size for SYSTEM mode (may be overridden by FULL4G if ENABLE_ELF_LOADER=1)
6976
CFLAGS += -DMEM_SIZE=0x$(REAL_MEM_SIZE) -DDTB_SIZE=0x$(REAL_DTB_SIZE) -DINITRD_SIZE=0x$(REAL_INITRD_SIZE)
7077
endif
7178
endif
@@ -192,27 +199,35 @@ ENABLE_FULL4G ?= 0
192199

193200
# Experimental SDL oriented system calls
194201
ENABLE_SDL ?= 1
202+
ENABLE_SDL_MIXER ?= 1
195203
ifneq ("$(CC_IS_EMCC)", "1") # note that emcc generates port SDL headers/library, so it does not requires system SDL headers/library
196204
ifeq ($(call has, SDL), 1)
197205
ifeq (, $(shell which sdl2-config))
198206
$(warning No sdl2-config in $$PATH. Check SDL2 installation in advance)
199207
override ENABLE_SDL := 0
200208
endif
201209
ifeq (1, $(shell pkg-config --exists SDL2_mixer; echo $$?))
202-
$(warning No SDL2_mixer lib installed. Check SDL2_mixer installation in advance)
203-
override ENABLE_SDL := 0
210+
$(warning No SDL2_mixer lib installed. SDL2_mixer support will be disabled)
211+
override ENABLE_SDL_MIXER := 0
212+
endif
204213
endif
205214
endif
206215
$(call set-feature, SDL)
216+
$(call set-feature, SDL_MIXER)
207217
ifeq ($(call has, SDL), 1)
208218
OBJS_EXT += syscall_sdl.o
219+
ifneq ("$(CC_IS_EMCC)", "1")
209220
$(OUT)/syscall_sdl.o: CFLAGS += $(shell sdl2-config --cflags)
221+
endif
210222
# 4 GiB of memory is required to run video games.
211223
ENABLE_FULL4G := 1
224+
ifneq ("$(CC_IS_EMCC)", "1")
212225
LDFLAGS += $(shell sdl2-config --libs) -pthread
226+
ifeq ($(call has, SDL_MIXER), 1)
213227
LDFLAGS += $(shell pkg-config --libs SDL2_mixer)
214228
endif
215229
endif
230+
endif
216231

217232
# If SYSTEM is enabled and ELF_LOADER is not, then skip FULL4G bacause guestOS
218233
# has dedicated memory mapping range.
@@ -225,7 +240,22 @@ endif
225240
# Full access to a 4 GiB address space, necessitating more memory mapping
226241
# during emulator initialization.
227242
$(call set-feature, FULL4G)
243+
244+
# Configuration validation and conflict detection
245+
ifeq ($(call has, SDL), 1)
246+
ifeq ($(call has, SYSTEM), 1)
247+
ifeq ($(call has, ELF_LOADER), 0)
248+
ifeq ($(call has, FULL4G), 0)
249+
$(warning SDL requires FULL4G=1 but SYSTEM forces FULL4G=0. Set ENABLE_ELF_LOADER=1 or disable SDL/SYSTEM)
250+
endif
251+
endif
252+
endif
253+
endif
254+
228255
ifeq ($(call has, FULL4G), 1)
256+
# Note: If both SYSTEM and FULL4G are enabled with ELF_LOADER=1,
257+
# this MEM_SIZE definition will override the SYSTEM mode definition.
258+
# This is intentional for ELF loader use cases.
229259
CFLAGS += -DMEM_SIZE=0xFFFFFFFFULL # 2^{32} - 1
230260
endif
231261

@@ -278,6 +308,8 @@ ifeq ($(call has, JIT), 1)
278308
else
279309
$(error No llvm-config-18 installed. Check llvm-config-18 installation in advance, or use "ENABLE_T2C=0" to disable tier-2 LLVM compiler)
280310
endif
311+
else
312+
$(warning T2C (tier-2 compiler) is disabled. Using tier-1 JIT only.)
281313
endif
282314
ifneq ($(processor),$(filter $(processor),x86_64 aarch64 arm64))
283315
$(error JIT mode only supports for x64 and arm64 target currently.)
@@ -318,7 +350,7 @@ DTB_DEPS := $(BUILD_DTB) $(BUILD_DTB2C)
318350
endif
319351
endif
320352

321-
all: config $(DTB_DEPS) $(BIN)
353+
all: config $(DTB_DEPS) $(BUILD_DTB) $(BUILD_DTB2C) $(BIN)
322354

323355
OBJS := \
324356
map.o \
@@ -353,19 +385,31 @@ ifeq ($(call has, GDBSTUB), 1)
353385
$(OBJS): $(GDBSTUB_LIB)
354386
endif
355387

356-
$(OUT)/%.o: src/%.c $(deps_emcc)
357-
$(Q)mkdir -p $(shell dirname $@)
388+
$(OUT)/%.o: src/%.c $(CONFIG_FILE) $(deps_emcc) | $(OUT)
389+
$(Q)mkdir -p $(dir $@)
358390
$(VECHO) " CC\t$@\n"
359391
$(Q)$(CC) -o $@ $(CFLAGS) $(CFLAGS_emcc) -c -MMD -MF $@.d $<
360392

361-
$(BIN): $(OBJS) $(DEV_OBJS)
393+
$(OUT):
394+
$(Q)mkdir -p $@
395+
396+
$(BIN): $(OBJS) $(DEV_OBJS) | $(OUT)
362397
$(VECHO) " LD\t$@\n"
363398
$(Q)$(CC) -o $@ $(CFLAGS_emcc) $^ $(LDFLAGS)
364399

400+
$(CONFIG_FILE): FORCE
401+
$(Q)mkdir -p $(OUT)
402+
$(Q)echo "$(CFLAGS)" | xargs -n1 | sort | sed -n 's/^RV32_FEATURE/ENABLE/p' > $@.tmp
403+
$(Q)if ! cmp -s $@ $@.tmp 2>/dev/null; then \
404+
mv $@.tmp $@; \
405+
$(PRINTF) "Configuration updated. Check $(OUT)/.config for configured items.\n"; \
406+
else \
407+
$(RM) $@.tmp; \
408+
fi
409+
410+
.PHONY: FORCE config
411+
FORCE:
365412
config: $(CONFIG_FILE)
366-
$(CONFIG_FILE):
367-
$(Q)echo "$(CFLAGS)" | xargs -n1 | sort | sed -n 's/^RV32_FEATURE/ENABLE/p' > $@
368-
$(VECHO) "Check the file $(OUT)/.config for configured items.\n"
369413

370414
# Tools
371415
include mk/tools.mk
@@ -460,4 +504,7 @@ distclean: clean
460504
$(Q)-$(RM) -r $(SOFTFLOAT_DUMMY_PLAT) $(OUT)/softfloat
461505
$(Q)$(call notice, [OK])
462506

507+
.PHONY: all config tool check check-hello misalign misalign-in-blk-emu mmu-test
508+
.PHONY: gdbstub-test doom quake clean distclean artifact
509+
463510
-include $(deps)

mk/external.mk

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ COMPRESSED_IS_DIR :=
44
EXTRACTOR :=
55
VERIFIER :=
66

7-
# temporarily files to store correct SHA value and computed SHA value
7+
# Temporary files to store correct SHA value and computed SHA value
88
# respectively for verification of directory source
9-
$(eval SHA_FILE1 := $(shell mktemp))
10-
$(eval SHA_FILE2 := $(shell mktemp))
9+
SHA_FILE1 := $(shell mktemp)
10+
SHA_FILE2 := $(shell mktemp)
1111

1212
# $(1): compressed source
1313
define prologue
@@ -17,7 +17,7 @@ endef
1717

1818
# $(1), $(2), $(3): files to be deleted
1919
define epilogue
20-
$(eval _ := $(shell $(RM) $(1) $(2) $(3)))
20+
$(RM) $(1) $(2) $(3)
2121
endef
2222

2323
# $(1): compressed source URL

mk/riscv-arch-test.mk

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
riscof-check:
22
$(Q)if [ "$(shell pip show riscof 2>&1 | head -n 1 | cut -d' ' -f1)" = "WARNING:" ]; then \
3-
$(PRINTF) "Run 'pip3 install -r requirements.txt to install dependencies.\n"; \
4-
exit 1; \
5-
fi;
3+
$(PRINTF) "Run 'pip3 install -r requirements.txt' to install dependencies.\n"; \
4+
exit 1; \
5+
fi
66

77
ARCH_TEST_DIR ?= tests/riscv-arch-test
88
ARCH_TEST_SUITE ?= $(ARCH_TEST_DIR)/riscv-test-suite
@@ -27,3 +27,5 @@ endif
2727
--config=$(RISCV_TARGET)/config.ini \
2828
--suite=$(ARCH_TEST_SUITE) \
2929
--env=$(ARCH_TEST_DIR)/riscv-test-suite/env
30+
31+
.PHONY: riscof-check arch-test

mk/softfloat.mk

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,13 @@ $(SOFTFLOAT_DUMMY_PLAT):
334334
$(Q)touch $@
335335

336336
$(SOFTFLOAT_FILES): $(SOFTFLOAT_SENTINEL)
337-
$(OUT)/softfloat/%.o: $(SOFTFLOAT_DIR)/%.c $(SOFTFLOAT_SENTINEL) $(SOFTFLOAT_DUMMY_PLAT)
338-
$(Q)mkdir -p $(shell dirname $@)
337+
$(OUT)/softfloat/%.o: $(SOFTFLOAT_DIR)/%.c $(CONFIG_FILE) $(SOFTFLOAT_SENTINEL) $(SOFTFLOAT_DUMMY_PLAT) | $(OUT)/softfloat $(OUT)/softfloat/RISCV
339338
$(VECHO) " CC\t$@\n"
340339
$(Q)$(CC) -o $@ $(CFLAGS) $(CFLAGS_softfloat) -c -MMD -MF $@.d $<
341340

341+
$(OUT)/softfloat $(OUT)/softfloat/RISCV:
342+
$(Q)mkdir -p $@
343+
342344
SOFTFLOAT_LIB := $(OUT)/softfloat/softfloat.a
343345
$(SOFTFLOAT_LIB): $(SOFTFLOAT_OBJS)
344346
$(VECHO) " AR\t$@\n"

mk/system.mk

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ $(BUILD_DTB2C): $(BIN_TO_C) $(BUILD_DTB)
2929
$(VECHO) " BIN2C\t$@\n"
3030
$(Q)$(BIN_TO_C) $(BUILD_DTB) > $@
3131

32-
$(DEV_OUT)/%.o: $(DEV_SRC)/%.c $(deps_emcc)
33-
$(Q)mkdir -p $(DEV_OUT)
32+
$(DEV_OUT)/%.o: $(DEV_SRC)/%.c $(CONFIG_FILE) $(deps_emcc) | $(DEV_OUT)
3433
$(VECHO) " CC\t$@\n"
3534
$(Q)$(CC) -o $@ $(CFLAGS) $(CFLAGS_emcc) -c -MMD -MF $@.d $<
35+
36+
$(DEV_OUT):
37+
$(Q)mkdir -p $@
38+
3639
DEV_OBJS := $(patsubst $(DEV_SRC)/%.c, $(DEV_OUT)/%.o, $(wildcard $(DEV_SRC)/*.c))
3740
deps := $(DEV_OBJS:%.o=%.o.d)
3841

@@ -46,4 +49,6 @@ system_deps += artifact $(BUILD_DTB) $(BUILD_DTB2C) $(BIN)
4649
system: $(system_deps)
4750
$(system_action)
4851

52+
.PHONY: system
53+
4954
endif

mk/tests.mk

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,31 +85,39 @@ $(CACHE_TEST_TARGET): $(CACHE_TEST_OBJS)
8585
$(VECHO) " CC\t$@\n"
8686
$(Q)$(CC) $^ -o $@ $(LDFLAGS)
8787

88-
$(CACHE_TEST_OUTDIR)/%.o: $(CACHE_TEST_SRCDIR)/%.c
88+
$(CACHE_TEST_OUTDIR)/%.o: $(CACHE_TEST_SRCDIR)/%.c $(CONFIG_FILE) | $(CACHE_TEST_OUTDIR) $(CACHE_TEST_OUTDIR)/lfu
8989
$(VECHO) " CC\t$@\n"
90-
$(Q)mkdir -p $(dir $@)/lfu
9190
$(Q)$(CC) -o $@ $(CFLAGS) -I./src -c -MMD -MF $@.d $<
9291

92+
$(CACHE_TEST_OUTDIR) $(CACHE_TEST_OUTDIR)/lfu:
93+
$(Q)mkdir -p $@
94+
9395
$(MAP_TEST_OUT): $(MAP_TEST_TARGET)
9496
$(Q)touch $@
9597

9698
$(MAP_TEST_TARGET): $(MAP_TEST_OBJS)
9799
$(VECHO) " CC\t$@\n"
98100
$(Q)$(CC) $^ -o $@ $(LDFLAGS)
99101

100-
$(MAP_TEST_OUTDIR)/%.o: $(MAP_TEST_SRCDIR)/%.c
102+
$(MAP_TEST_OUTDIR)/%.o: $(MAP_TEST_SRCDIR)/%.c $(CONFIG_FILE) | $(MAP_TEST_OUTDIR)
101103
$(VECHO) " CC\t$@\n"
102-
$(Q)mkdir -p $(dir $@)
103104
$(Q)$(CC) -o $@ $(CFLAGS) -I./src -c -MMD -MF $@.d $<
104105

106+
$(MAP_TEST_OUTDIR):
107+
$(Q)mkdir -p $@
108+
105109
$(PATH_TEST_OUT): $(PATH_TEST_TARGET)
106110
$(Q)touch $@
107111

108112
$(PATH_TEST_TARGET): $(PATH_TEST_OBJS)
109113
$(VECHO) " CC\t$@\n"
110114
$(Q)$(CC) $^ -o $@ $(LDFLAGS)
111115

112-
$(PATH_TEST_OUTDIR)/%.o: $(PATH_TEST_SRCDIR)/%.c
116+
$(PATH_TEST_OUTDIR)/%.o: $(PATH_TEST_SRCDIR)/%.c $(CONFIG_FILE) | $(PATH_TEST_OUTDIR)
113117
$(VECHO) " CC\t$@\n"
114-
$(Q)mkdir -p $(dir $@)
115118
$(Q)$(CC) -o $@ $(CFLAGS) -I./src -c -MMD -MF $@.d $<
119+
120+
$(PATH_TEST_OUTDIR):
121+
$(Q)mkdir -p $@
122+
123+
.PHONY: tests run-test-cache run-test-map run-test-path

mk/tools.mk

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ HIST_OBJS := \
2323
HIST_OBJS := $(addprefix $(OUT)/, $(HIST_OBJS))
2424
deps += $(HIST_OBJS:%.o=%.o.d)
2525

26-
$(OUT)/%.o: tools/%.c
26+
$(OUT)/%.o: tools/%.c | $(OUT)
2727
$(VECHO) " CC\t$@\n"
2828
$(Q)$(CC) -o $@ $(CFLAGS) -Wno-missing-field-initializers -Isrc -c -MMD -MF $@.d $<
2929

@@ -39,3 +39,5 @@ LINUX_IMAGE_SRC = $(BUILDROOT_DATA) $(LINUX_DATA)
3939
build-linux-image: $(LINUX_IMAGE_SRC)
4040
$(Q)./tools/build-linux-image.sh
4141
$(Q)$(PRINTF) "Build done.\n"
42+
43+
.PHONY: build-linux-image

mk/wasm.mk

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ CFLAGS += -mtail-call
1818

1919
# Build emscripten-port SDL
2020
ifeq ($(call has, SDL), 1)
21-
CFLAGS_emcc += -sUSE_SDL=2 -sSDL2_MIXER_FORMATS=wav,mid -sUSE_SDL_MIXER=2
21+
# Disable STRICT mode to avoid -Werror in SDL2_mixer port compilation.
22+
# The emscripten-ports/SDL2_mixer was archived in Jan 2024 and has warnings
23+
# in music_modplug.c that become fatal errors under STRICT mode.
24+
CFLAGS_emcc += -sSTRICT=0 -sUSE_SDL=2 -sSDL2_MIXER_FORMATS=wav,mid -sUSE_SDL_MIXER=2
2225
OBJS_EXT += syscall_sdl.o
2326
LDFLAGS += -pthread
2427
endif
@@ -159,4 +162,7 @@ start-web: $(start_web_deps)
159162
$(foreach T, $(STATIC_WEB_FILES), $(call cp-web-file, $(T)))
160163
$(Q)mv $(DEMO_DIR)/*.html $(DEMO_DIR)/index.html
161164
$(Q)python3 -m http.server --bind $(DEMO_IP) $(DEMO_PORT) --directory $(DEMO_DIR)
165+
166+
.PHONY: check-demo-dir-exist start-web
167+
162168
endif

0 commit comments

Comments
 (0)