Skip to content

Commit 8f2b234

Browse files
authored
Merge pull request #203 from DrXiao/fix-build-sys
Refine the build system
2 parents 68ef318 + 5f97107 commit 8f2b234

File tree

4 files changed

+51
-49
lines changed

4 files changed

+51
-49
lines changed

Makefile

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

1717
BUILD_SESSION := .session.mk
1818

19-
include mk/common.mk
20-
include mk/arm.mk
21-
include mk/riscv.mk
2219
-include $(BUILD_SESSION)
2320

2421
STAGE0 := shecc
@@ -28,6 +25,7 @@ STAGE2 := shecc-stage2.elf
2825
OUT ?= out
2926
ARCHS = arm riscv
3027
ARCH ?= $(firstword $(ARCHS))
28+
HOST_ARCH = $(shell arch 2>/dev/null)
3129
SRCDIR := $(shell find src -type d)
3230
LIBDIR := $(shell find lib -type d)
3331

@@ -43,17 +41,15 @@ all: config bootstrap
4341
ifeq (,$(filter $(ARCH),$(ARCHS)))
4442
$(error Support ARM and RISC-V only. Select the target with "ARCH=arm" or "ARCH=riscv")
4543
endif
46-
47-
ifneq ("$(wildcard $(PWD)/config)","")
48-
TARGET_EXEC := $($(shell head -1 config | sed 's/.*: \([^ ]*\).*/\1/')_EXEC)
49-
endif
50-
export TARGET_EXEC
44+
include mk/$(ARCH).mk
45+
include mk/common.mk
5146

5247
config:
5348
$(Q)ln -s $(PWD)/$(SRCDIR)/$(ARCH)-codegen.c $(SRCDIR)/codegen.c
54-
$(call $(ARCH)-specific-defs) > $@
49+
$(Q)$(PRINTF) $(ARCH_DEFS) > $@
5550
$(VECHO) "Target machine code switch to %s\n" $(ARCH)
5651
$(Q)$(MAKE) $(BUILD_SESSION) --silent
52+
$(Q)$(CONFIG_CHECK_CMD)
5753

5854
$(OUT)/tests/%.elf: tests/%.c $(OUT)/$(STAGE0)
5955
$(VECHO) " SHECC\t$@\n"
@@ -109,6 +105,7 @@ $(OUT)/$(STAGE0): $(OUT)/libc.inc $(OBJS)
109105
$(Q)$(CC) $(OBJS) -o $@
110106

111107
$(OUT)/$(STAGE1): $(OUT)/$(STAGE0)
108+
$(Q)$(STAGE1_CHECK_CMD)
112109
$(VECHO) " SHECC\t$@\n"
113110
$(Q)$(OUT)/$(STAGE0) --dump-ir -o $@ $(SRCDIR)/main.c > $(OUT)/shecc-stage1.log
114111
$(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-
ARM_EXEC :=
3-
else
4-
ARM_EXEC = qemu-arm
5-
ARM_EXEC := $(shell which $(ARM_EXEC))
6-
ifndef ARM_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 ARM_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-
RISCV_EXEC = qemu-riscv32
2-
RISCV_EXEC := $(shell which $(RISCV_EXEC))
3-
ifndef RISCV_EXEC
4-
$(warning "no qemu-riscv32 found. Please check package installation")
5-
RISCV_EXEC = echo WARN: unable to run
6-
endif
7-
8-
export RISCV_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)