Skip to content

Commit 4218524

Browse files
committed
Avoid re-fetching binaries when they exist
We fetch the SHA1 first and verify the binaries. If any verification fails, the returned value of the "verify" macro becomes non-zero. Only if the verification fails, the download begins. Close #491
1 parent abb1135 commit 4218524

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

.github/workflows/build-artifact.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ jobs:
5959
run: |
6060
make artifact ENABLE_PREBUILT=0
6161
mkdir -p /tmp/rv32emu-prebuilt
62+
mv build/sha1sum-linux-x86-softfp /tmp
63+
mv build/sha1sum-riscv32 /tmp
6264
mv build/linux-x86-softfp build/riscv32 /tmp/rv32emu-prebuilt
6365
- name: Build Sail model
6466
run: |
@@ -88,4 +90,6 @@ jobs:
8890
--title "$RELEASE_TAG""-nightly"
8991
gh release upload $RELEASE_TAG \
9092
rv32emu-prebuilt.tar.gz \
93+
sha1sum-linux-x86-softfp \
94+
sha1sum-riscv32 \
9195
--repo sysprog21/rv32emu-prebuilt

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ build/path/
1919
build/linux-x86-softfp/
2020
build/riscv32/
2121
build/sail_cSim/
22+
build/sha1sum-*
2223
*.a
2324
*.o
2425
*.o.d

mk/artifact.mk

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,29 @@ else
4848
LDFLAGS_CROSS := -lm -lsemihost
4949
endif
5050

51-
.PHONY: artifact scimark2 ieeelib
51+
.PHONY: artifact fetch-checksum scimark2 ieeelib
5252

53-
artifact: ieeelib scimark2
53+
artifact: fetch-checksum ieeelib scimark2
5454
ifeq ($(call has, PREBUILT), 1)
55-
$(Q)$(PRINTF) "Fetching prebuilt executables from \"rv32emu-prebuilt\" ...\n"
56-
$(Q)wget -q --show-progress https://github.com/sysprog21/rv32emu-prebuilt/releases/download/$(LATEST_RELEASE)/rv32emu-prebuilt.tar.gz -O- | tar -C build --strip-components=1 -xz
55+
$(Q)$(PRINTF) "Checking SHA1 of binaries ...\n"
56+
57+
$(Q)$(eval PREBUILT_X86_FILENAME := $(shell cat $(BIN_DIR)/sha1sum-linux-x86-softfp | awk '{ print $$2 };'))
58+
$(Q)$(eval PREBUILT_RV32_FILENAME := $(shell cat $(BIN_DIR)/sha1sum-riscv32 | awk '{ print $$2 };'))
59+
60+
$(Q)$(eval RES := 0)
61+
$(Q)$(eval $(foreach FILE,$(PREBUILT_X86_FILENAME), \
62+
$(call verify,$(shell grep -w $(FILE) $(BIN_DIR)/sha1sum-linux-x86-softfp | awk '{ print $$1 };'),$(BIN_DIR)/linux-x86-softfp/$(FILE),RES) \
63+
))
64+
$(Q)$(eval $(foreach FILE,$(PREBUILT_RV32_FILENAME), \
65+
$(call verify,$(shell grep -w $(FILE) $(BIN_DIR)/sha1sum-riscv32 | awk '{ print $$1 };'),$(BIN_DIR)/riscv32/$(FILE),RES) \
66+
))
67+
68+
$(Q)if [ "$(RES)" = "1" ]; then \
69+
$(PRINTF) "$(YELLOW)SHA1 verifications fail! Re-fetching prebuilt binaries from \"rv32emu-prebuilt\" ...\n$(NO_COLOR)"; \
70+
wget -q --show-progress https://github.com/sysprog21/rv32emu-prebuilt/releases/download/$(LATEST_RELEASE)/rv32emu-prebuilt.tar.gz -O- | tar -C build --strip-components=1 -xz; \
71+
else \
72+
$(PRINTF) "$(PASS_COLOR)SHA1 verifications succeed!\n$(NO_COLOR)"; \
73+
fi
5774
else
5875
git submodule update --init $(addprefix ./tests/,$(foreach tb,$(TEST_SUITES),$(tb)))
5976
$(Q)for tb in $(TEST_SUITES); do \
@@ -82,6 +99,16 @@ else
8299
-DCMAKE_BUILD_TYPE=RELEASE -DBOARD_NAME=rv32emu .. && \
83100
make
84101
$(Q)cp ./tests/quake/build/port/boards/rv32emu/quake $(BIN_DIR)/riscv32/quake
102+
103+
$(Q)(cd $(BIN_DIR)/linux-x86-softfp; for fd in *; do $(SHA1SUM) "$$fd"; done) >> $(BIN_DIR)/sha1sum-linux-x86-softfp
104+
$(Q)(cd $(BIN_DIR)/riscv32; for fd in *; do $(SHA1SUM) "$$fd"; done) >> $(BIN_DIR)/sha1sum-riscv32
105+
endif
106+
107+
fetch-checksum:
108+
ifeq ($(call has, PREBUILT), 1)
109+
$(Q)$(PRINTF) "Fetching SHA1 of binaries ...\n"
110+
$(Q)wget -q --show-progress -O $(BIN_DIR)/sha1sum-linux-x86-softfp https://github.com/sysprog21/rv32emu-prebuilt/releases/download/$(LATEST_RELEASE)/sha1sum-linux-x86-softfp
111+
$(Q)wget -q --show-progress -O $(BIN_DIR)/sha1sum-riscv32 https://github.com/sysprog21/rv32emu-prebuilt/releases/download/$(LATEST_RELEASE)/sha1sum-riscv32
85112
endif
86113

87114
scimark2:

mk/external.mk

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,16 @@ endef
4040

4141
# $(1): correct SHA1 value
4242
# $(2): filename or directory path
43+
# $(3): (optional) returned result
4344
#
4445
# Note:
4546
# 1. for regular file, $(SHA1SUM) command's -c option generates keyword "FAILED" for indicating an unmatch
4647
# 2. for directory, cmp command outputs keyword "differ" for indicating an unmatch
4748
define verify
4849
$(eval COMPRESSED_IS_DIR := $(if $(wildcard $(2)/*),1,0))
4950
$(eval _ := \
50-
$(if $(filter 1,$(COMPRESSED_IS_DIR)), \
51-
($(eval VERIFIER := \
51+
$(if $(filter 1,$(COMPRESSED_IS_DIR)), \
52+
($(eval VERIFIER := \
5253
echo $(1) > $(SHA1_FILE1) \
5354
| find $(2) -type f -print0 \
5455
| sort -z \
@@ -58,10 +59,13 @@ define verify
5859
| cut -f 1 -d ' ' > $(SHA1_FILE2) && cmp $(SHA1_FILE1) $(SHA1_FILE2))), \
5960
($(eval VERIFIER := echo "$(strip $(1)) $(strip $(2))" | $(SHA1SUM) -c)) \
6061
))
61-
$(eval _ := $(shell $(VERIFIER)))
62+
$(eval _ := $(shell $(VERIFIER) 2>&1))
6263
$(eval _ := \
6364
$(if $(filter FAILED differ:,$(_)), \
64-
($(error $(_))), \
65+
($(if $(3), \
66+
$(eval $(3) := 1), \
67+
$(error $(_)) \
68+
)), \
6569
(# SHA1 value match, do nothing) \
6670
))
6771
endef
@@ -92,7 +96,7 @@ $($(T)_DATA):
9296
$(Q)$$(call prologue,$$@)
9397
$(Q)$$(call download,$(strip $($(T)_DATA_URL)))
9498
$(Q)$$(call extract,$(OUT),$(notdir $($(T)_DATA_URL)))
95-
$(Q)$$(call verify,$($(T)_DATA_SHA1), $($(T)_DATA))
99+
$(Q)$$(call verify,$($(T)_DATA_SHA1),$($(T)_DATA))
96100
$(Q)$$(call epilogue,$(notdir $($(T)_DATA_URL)),$(SHA1_FILE1),$(SHA1_FILE2))
97101
endef
98102

0 commit comments

Comments
 (0)