Skip to content

Commit 5f97107

Browse files
committed
Validate the prerequisites before building
To prevent users from building shecc without certain required packages, these changes modify the build system to check for prerequisites when building. The build system now lists all necessary tools, verify the existence of their executables, and prints warning messages if any required tool is missing. If qemu is required but missing, the build process will proceed only up to stage 0, print warning messages and stop at the beginning of stage 1. Close #44
1 parent eb7375b commit 5f97107

File tree

4 files changed

+50
-42
lines changed

4 files changed

+50
-42
lines changed

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ CFLAGS := -O -g \
1616

1717
BUILD_SESSION := .session.mk
1818

19-
include mk/common.mk
2019
-include $(BUILD_SESSION)
2120

2221
STAGE0 := shecc
@@ -26,6 +25,7 @@ STAGE2 := shecc-stage2.elf
2625
OUT ?= out
2726
ARCHS = arm riscv
2827
ARCH ?= $(firstword $(ARCHS))
28+
HOST_ARCH = $(shell arch 2>/dev/null)
2929
SRCDIR := $(shell find src -type d)
3030
LIBDIR := $(shell find lib -type d)
3131

@@ -42,12 +42,14 @@ ifeq (,$(filter $(ARCH),$(ARCHS)))
4242
$(error Support ARM and RISC-V only. Select the target with "ARCH=arm" or "ARCH=riscv")
4343
endif
4444
include mk/$(ARCH).mk
45+
include mk/common.mk
4546

4647
config:
4748
$(Q)ln -s $(PWD)/$(SRCDIR)/$(ARCH)-codegen.c $(SRCDIR)/codegen.c
48-
$(call $(ARCH)-specific-defs) > $@
49+
$(Q)$(PRINTF) $(ARCH_DEFS) > $@
4950
$(VECHO) "Target machine code switch to %s\n" $(ARCH)
5051
$(Q)$(MAKE) $(BUILD_SESSION) --silent
52+
$(Q)$(CONFIG_CHECK_CMD)
5153

5254
$(OUT)/tests/%.elf: tests/%.c $(OUT)/$(STAGE0)
5355
$(VECHO) " SHECC\t$@\n"
@@ -103,6 +105,7 @@ $(OUT)/$(STAGE0): $(OUT)/libc.inc $(OBJS)
103105
$(Q)$(CC) $(OBJS) -o $@
104106

105107
$(OUT)/$(STAGE1): $(OUT)/$(STAGE0)
108+
$(Q)$(STAGE1_CHECK_CMD)
106109
$(VECHO) " SHECC\t$@\n"
107110
$(Q)$(OUT)/$(STAGE0) --dump-ir -o $@ $(SRCDIR)/main.c > $(OUT)/shecc-stage1.log
108111
$(Q)chmod a+x $@

mk/arm.mk

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
1-
ifeq ($(HOST_ARCH),armv7l) # detect ARMv7-A only and assume Linux-compatible
2-
TARGET_EXEC :=
3-
else
4-
TARGET_EXEC = qemu-arm
5-
TARGET_EXEC := $(shell which $(TARGET_EXEC))
6-
ifndef TARGET_EXEC
7-
$(warning "no qemu-arm found. Please check package installation")
8-
ARM_EXEC = echo WARN: unable to run
9-
endif
10-
endif
11-
12-
export TARGET_EXEC
13-
14-
arm-specific-defs = \
15-
$(Q)$(PRINTF) \
16-
"/* target: ARM */\n$\
17-
\#pragma once\n$\
18-
\#define ARCH_PREDEFINED \"__arm__\" /* defined by GNU C and RealView */\n$\
19-
\#define ELF_MACHINE 0x28 /* up to ARMv7/Aarch32 */\n$\
20-
\#define ELF_FLAGS 0x5000200\n$\
21-
"
1+
ARCH_NAME = armv7l
2+
ARCH_RUNNER = qemu-arm
3+
ARCH_DEFS = \
4+
"/* target: ARM */\n$\
5+
\#pragma once\n$\
6+
\#define ARCH_PREDEFINED \"__arm__\" /* defined by GNU C and RealView */\n$\
7+
\#define ELF_MACHINE 0x28 /* up to ARMv7/Aarch32 */\n$\
8+
\#define ELF_FLAGS 0x5000200\n$\
9+
"

mk/common.mk

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ else
55
PRINTF = env printf
66
endif
77

8-
HOST_ARCH = $(shell arch 2>/dev/null)
9-
108
# Control the build verbosity
119
ifeq ("$(VERBOSE)","1")
1210
Q :=
@@ -23,3 +21,29 @@ PASS_COLOR = \e[32;01m
2321
NO_COLOR = \e[0m
2422

2523
pass = $(PRINTF) "$(PASS_COLOR)$1 Passed$(NO_COLOR)\n"
24+
25+
# Check the prerequisites
26+
PREREQ_LIST := dot jq
27+
TARGET_EXEC ?=
28+
ifneq ($(HOST_ARCH),$(ARCH_NAME))
29+
# Add qemu to the list if the host and target architectures differ
30+
PREREQ_LIST += $(ARCH_RUNNER)
31+
ifeq ($(filter $(ARCH_RUNNER),$(notdir $(shell which $(ARCH_RUNNER)))),)
32+
STAGE1_WARN_MSG := "Warning: failed to build the stage 1 and $\
33+
stage 2 compilers due to missing $(ARCH_RUNNER)\n"
34+
STAGE1_CHECK_CMD := $(VECHO) $(STAGE1_WARN_MSG) && exit 1
35+
endif
36+
37+
# Generate the path to the architecture-specific qemu
38+
TARGET_EXEC = $(shell which $(ARCH_RUNNER))
39+
endif
40+
export TARGET_EXEC
41+
42+
PREREQ_EXEC := $(shell which $(PREREQ_LIST))
43+
PREREQ_MISSING := $(filter-out $(notdir $(PREREQ_EXEC)),$(PREREQ_LIST))
44+
45+
ifdef PREREQ_MISSING
46+
CONFIG_WARN_MSG := "Warning: missing packages: $(PREREQ_MISSING)\n$\
47+
Warning: Please check package installation\n"
48+
CONFIG_CHECK_CMD := $(VECHO) $(CONFIG_WARN_MSG)
49+
endif

mk/riscv.mk

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
TARGET_EXEC = qemu-riscv32
2-
TARGET_EXEC := $(shell which $(TARGET_EXEC))
3-
ifndef TARGET_EXEC
4-
$(warning "no qemu-riscv32 found. Please check package installation")
5-
TARGET_EXEC = echo WARN: unable to run
6-
endif
7-
8-
export TARGET_EXEC
9-
10-
riscv-specific-defs = \
11-
$(Q)$(PRINTF) \
12-
"/* target: RISCV */\n$\
13-
\#pragma once\n$\
14-
\#define ARCH_PREDEFINED \"__riscv\" /* Older versions of the GCC toolchain defined __riscv__ */\n$\
15-
\#define ELF_MACHINE 0xf3\n$\
16-
\#define ELF_FLAGS 0\n$\
17-
"
1+
# Enforce the use qemu of by setting the ARCH_NAME variable to empty
2+
ARCH_NAME =
3+
ARCH_RUNNER = qemu-riscv32
4+
ARCH_DEFS = \
5+
"/* target: RISCV */\n$\
6+
\#pragma once\n$\
7+
\#define ARCH_PREDEFINED \"__riscv\" /* Older versions of the GCC toolchain defined __riscv__ */\n$\
8+
\#define ELF_MACHINE 0xf3\n$\
9+
\#define ELF_FLAGS 0\n$\
10+
"

0 commit comments

Comments
 (0)