Skip to content

Commit 9764cff

Browse files
committed
Refine build system for efficient configurations
This addresses build system inefficiencies and compatibility issues: 1. Configuration change detection: Use cmp-based comparison to avoid unnecessary rebuilds when configuration hasn't actually changed. Previously, every make run would touch .config and trigger full rebuilds. 2. Directory creation optimization: Replace runtime mkdir -p $(shell dirname) calls with order-only prerequisites (|). Add mkdir -p $(dir $@) to compilation rules to ensure subdirectories exist for nested object files (build/dtc/libfdt/*.o, build/devices/*.o). 3. Configuration dependency tracking: Add $(CONFIG_FILE) as explicit prerequisite to all compilation rules. This ensures object files rebuild when feature flags change (e.g., ENABLE_JIT=1 -> 0). 4. Emscripten SDL2_mixer fix: Add -sSTRICT=0 to CFLAGS_emcc to disable STRICT mode. The emscripten-ports/SDL2_mixer repository was archived in Jan 2024 and has warnings in music_modplug.c that become fatal errors under STRICT mode's -Werror flag. 5. CI scan-build fix: Set LATEST_RELEASE=dummy environment variable in scan-build steps. This prevents network-dependent prebuilt file downloads while avoiding 32-bit compilation requirements (ENABLE_PREBUILT=0 triggers -m32 flag which requires 32-bit development libraries). 6. Build system hygiene: Remove duplicate .PHONY declarations, fix shell script indentation in riscv-arch-test.mk, and add .PHONY coverage for all target declarations.
1 parent 65a1ec6 commit 9764cff

File tree

9 files changed

+94
-26
lines changed

9 files changed

+94
-26
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,8 +525,12 @@ jobs:
525525
sudo apt-get install -q=2 clang-18 clang-tools-18
526526
shell: bash
527527
- name: run scan-build without JIT
528+
env:
529+
LATEST_RELEASE: dummy
528530
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
529531
- name: run scan-build with JIT
532+
env:
533+
LATEST_RELEASE: dummy
530534
run: |
531535
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
532536

Makefile

Lines changed: 46 additions & 7 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
@@ -206,7 +213,22 @@ endif
206213
# Full access to a 4 GiB address space, necessitating more memory mapping
207214
# during emulator initialization.
208215
$(call set-feature, FULL4G)
216+
217+
# Configuration validation and conflict detection
218+
ifeq ($(call has, SDL), 1)
219+
ifeq ($(call has, SYSTEM), 1)
220+
ifeq ($(call has, ELF_LOADER), 0)
221+
ifeq ($(call has, FULL4G), 0)
222+
$(warning SDL requires FULL4G=1 but SYSTEM forces FULL4G=0. Set ENABLE_ELF_LOADER=1 or disable SDL/SYSTEM)
223+
endif
224+
endif
225+
endif
226+
endif
227+
209228
ifeq ($(call has, FULL4G), 1)
229+
# Note: If both SYSTEM and FULL4G are enabled with ELF_LOADER=1,
230+
# this MEM_SIZE definition will override the SYSTEM mode definition.
231+
# This is intentional for ELF loader use cases.
210232
CFLAGS += -DMEM_SIZE=0xFFFFFFFFULL # 2^{32} - 1
211233
endif
212234

@@ -259,6 +281,8 @@ ifeq ($(call has, JIT), 1)
259281
else
260282
$(error No llvm-config-18 installed. Check llvm-config-18 installation in advance, or use "ENABLE_T2C=0" to disable tier-2 LLVM compiler)
261283
endif
284+
else
285+
$(warning T2C (tier-2 compiler) is disabled. Using tier-1 JIT only.)
262286
endif
263287
ifneq ($(processor),$(filter $(processor),x86_64 aarch64 arm64))
264288
$(error JIT mode only supports for x64 and arm64 target currently.)
@@ -292,7 +316,7 @@ include mk/artifact.mk
292316
include mk/system.mk
293317
include mk/wasm.mk
294318

295-
all: config $(BUILD_DTB) $(BUILD_DTB2C) $(BIN)
319+
all: $(BUILD_DTB) $(BUILD_DTB2C) $(BIN)
296320

297321
OBJS := \
298322
map.o \
@@ -327,19 +351,31 @@ ifeq ($(call has, GDBSTUB), 1)
327351
$(OBJS): $(GDBSTUB_LIB)
328352
endif
329353

330-
$(OUT)/%.o: src/%.c $(deps_emcc)
331-
$(Q)mkdir -p $(shell dirname $@)
354+
$(OUT)/%.o: src/%.c $(CONFIG_FILE) $(deps_emcc) | $(OUT)
355+
$(Q)mkdir -p $(dir $@)
332356
$(VECHO) " CC\t$@\n"
333357
$(Q)$(CC) -o $@ $(CFLAGS) $(CFLAGS_emcc) -c -MMD -MF $@.d $<
334358

335-
$(BIN): $(OBJS) $(DEV_OBJS)
359+
$(OUT):
360+
$(Q)mkdir -p $@
361+
362+
$(BIN): $(OBJS) $(DEV_OBJS) | $(OUT)
336363
$(VECHO) " LD\t$@\n"
337364
$(Q)$(CC) -o $@ $(CFLAGS_emcc) $^ $(LDFLAGS)
338365

366+
$(CONFIG_FILE): FORCE
367+
$(Q)mkdir -p $(OUT)
368+
$(Q)echo "$(CFLAGS)" | xargs -n1 | sort | sed -n 's/^RV32_FEATURE/ENABLE/p' > $@.tmp
369+
$(Q)if ! cmp -s $@ $@.tmp 2>/dev/null; then \
370+
mv $@.tmp $@; \
371+
$(PRINTF) "Configuration updated. Check $(OUT)/.config for configured items.\n"; \
372+
else \
373+
$(RM) $@.tmp; \
374+
fi
375+
376+
.PHONY: FORCE config
377+
FORCE:
339378
config: $(CONFIG_FILE)
340-
$(CONFIG_FILE):
341-
$(Q)echo "$(CFLAGS)" | xargs -n1 | sort | sed -n 's/^RV32_FEATURE/ENABLE/p' > $@
342-
$(VECHO) "Check the file $(OUT)/.config for configured items.\n"
343379

344380
# Tools
345381
include mk/tools.mk
@@ -434,4 +470,7 @@ distclean: clean
434470
$(Q)-$(RM) -r $(SOFTFLOAT_DUMMY_PLAT) $(OUT)/softfloat
435471
$(Q)$(call notice, [OK])
436472

473+
.PHONY: all config tool check check-hello misalign misalign-in-blk-emu mmu-test
474+
.PHONY: gdbstub-test doom quake clean distclean artifact
475+
437476
-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)