Skip to content

Commit

Permalink
Refactor snapshot updating/checking scripts
Browse files Browse the repository at this point in the history
- Adds .session.mk generation for consistent
  makefile execution behavior.
  • Loading branch information
ChAoSUnItY committed Dec 8, 2024
1 parent b310322 commit ef7d273
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 12 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ jobs:
strategy:
matrix:
compiler: [gcc-12, clang]
architecture: [arm, riscv]
steps:
- name: checkout code
uses: actions/checkout@v4
Expand All @@ -19,11 +20,8 @@ jobs:
sudo apt-get install -q -y graphviz jq
sudo apt-get install -q -y qemu-user
sudo apt-get install -q -y build-essential
make clean config
make check-snapshots || exit 1
make distclean config ARCH=arm
make check || exit 1
make distclean config ARCH=riscv
make distclean config ARCH=${{ matrix.architecture }}
make check-snapshot || exit 1
make check || exit 1
host-arm:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ a.out
*.dot
config
src/codegen.c
.session.mk
35 changes: 30 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ CFLAGS := -O -g \
-Wno-format \
-Wno-format-pedantic

BUILD_SESSION := .session.mk

include mk/common.mk
include mk/arm.mk
include mk/riscv.mk
-include $(BUILD_SESSION)

STAGE0 := shecc
STAGE1 := shecc-stage1.elf
STAGE2 := shecc-stage2.elf

OUT ?= out
ARCH ?= arm
ARCHS = arm riscv
ARCH ?= $(firstword $(ARCHS))
SRCDIR := $(shell find src -type d)
LIBDIR := $(shell find lib -type d)

Expand All @@ -28,11 +32,11 @@ OBJS := $(SRCS:%.c=$(OUT)/%.o)
deps := $(OBJS:%.o=%.o.d)
TESTS := $(wildcard tests/*.c)
TESTBINS := $(TESTS:%.c=$(OUT)/%.elf)
SNAPSHOTS := $(patsubst tests/%.c, tests/snapshots/%.json, $(TESTS))
SNAPSHOTS := $(foreach SNAPSHOT_ARCH,$(ARCHS), $(patsubst tests/%.c, tests/snapshots/%-$(SNAPSHOT_ARCH).json, $(TESTS)))

all: config bootstrap

ifeq (,$(filter $(ARCH),arm riscv))
ifeq (,$(filter $(ARCH),$(ARCHS)))
$(error Support ARM and RISC-V only. Select the target with "ARCH=arm" or "ARCH=riscv")
endif

Expand All @@ -45,6 +49,7 @@ config:
$(Q)ln -s $(PWD)/$(SRCDIR)/$(ARCH)-codegen.c $(SRCDIR)/codegen.c
$(call $(ARCH)-specific-defs) > $@
$(VECHO) "Target machine code switch to %s\n" $(ARCH)
$(Q)$(MAKE) $(BUILD_SESSION) --silent

$(OUT)/tests/%.elf: tests/%.c $(OUT)/$(STAGE0)
$(VECHO) " SHECC\t$@\n"
Expand All @@ -56,7 +61,24 @@ check: $(TESTBINS) tests/driver.sh
tests/driver.sh

check-snapshots: $(OUT)/$(STAGE0) $(SNAPSHOTS) tests/check-snapshots.sh
tests/check-snapshots.sh
$(Q)$(foreach SNAPSHOT_ARCH, $(ARCHS), $(MAKE) distclean config check-snapshot ARCH=$(SNAPSHOT_ARCH) --silent;)
$(VECHO) "Switching backend back to %s\n" $(ARCH)
$(Q)$(MAKE) distclean config ARCH=$(ARCH) --silent

check-snapshot: $(OUT)/$(STAGE0) tests/check-snapshots.sh
$(VECHO) "Checking snapshot for %s\n" $(ARCH)
tests/check-snapshots.sh $(ARCH)
$(VECHO) " OK\n"

update-snapshots: tests/update-snapshots.sh
$(Q)$(foreach SNAPSHOT_ARCH, $(ARCHS), $(MAKE) distclean config update-snapshot ARCH=$(SNAPSHOT_ARCH) --silent;)
$(VECHO) "Switching backend back to %s\n" $(ARCH)
$(Q)$(MAKE) distclean config ARCH=$(ARCH) --silent

update-snapshot: $(OUT)/$(STAGE0) tests/update-snapshots.sh
$(VECHO) "Updating snapshot for %s\n" $(ARCH)
tests/update-snapshots.sh $(ARCH)
$(VECHO) " OK\n"

$(OUT)/%.o: %.c
$(VECHO) " CC\t$@\n"
Expand Down Expand Up @@ -90,6 +112,9 @@ bootstrap: $(OUT)/$(STAGE2)
echo "Unable to bootstrap. Aborting"; false; \
fi

$(BUILD_SESSION):
$(PRINTF) "ARCH=$(ARCH)" > $@

.PHONY: clean
clean:
-$(RM) $(OUT)/$(STAGE0) $(OUT)/$(STAGE1) $(OUT)/$(STAGE2)
Expand All @@ -99,6 +124,6 @@ clean:
-$(RM) $(OUT)/libc.inc

distclean: clean
-$(RM) $(OUT)/inliner $(OUT)/target $(SRCDIR)/codegen.c config
-$(RM) $(OUT)/inliner $(OUT)/target $(SRCDIR)/codegen.c config $(BUILD_SESSION)

-include $(deps)
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,23 @@ $ chmod +x fib
$ qemu-arm fib
```

### IR Regression Tests

Verify that the emitted IRs are identical to the snapshots by specifying `check-snapshots` target when invoking `make`:
```shell
$ make check-snapshots
```

If the IR emission algorithm has been updated, you can update snapshots by specifying `update-snapshots` target when invoking `make`:
```shell
$ make update-snapshots
```

Notice that the above 2 targets will update all backend snapshots at once, to update/check current backend's snapshot,
use `update-snapshot` / `check-snapshot` instead.

### Unit Tests

`shecc` comes with unit tests. To run the tests, give `check` as an argument:
```shell
$ make check
Expand Down
9 changes: 8 additions & 1 deletion tests/check-snapshots.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ set -u

readonly SHECC="$PWD/out/shecc"

if [ "$#" != 1 ]; then
echo "Usage: $0 <architecture>"
exit 1
fi

readonly ARCH="$1"

function check_snapshot() {
local source="$1"
local ref="tests/snapshots/$(basename $source .c).json"
local ref="tests/snapshots/$(basename $source .c)-$ARCH.json"
local temp_exe=$(mktemp)
local temp_json=$(mktemp --suffix .json)

Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions tests/snapshots/fib-riscv.json

Large diffs are not rendered by default.

File renamed without changes.
1 change: 1 addition & 0 deletions tests/snapshots/hello-riscv.json

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion tests/update-snapshots.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ set -u

readonly SHECC="$PWD/out/shecc"

if [ "$#" != 1 ]; then
echo "Usage: $0 <architecture>"
exit 1
fi

readonly ARCH="$1"

function update_snapshot() {
local source="$1"
local dest="tests/snapshots/$(basename $source .c).json"
local dest="tests/snapshots/$(basename $source .c)-$ARCH.json"
local temp_exe=$(mktemp)
local temp_json=$(mktemp --suffix .json)

Expand Down

0 comments on commit ef7d273

Please sign in to comment.