Skip to content

Commit 13a6531

Browse files
authored
Merge pull request #393 from ChinYikMing/sha1
Fix hardcoded sha1sum command and compare sha1 value
2 parents 68301c3 + 74f5f18 commit 13a6531

File tree

1 file changed

+75
-17
lines changed

1 file changed

+75
-17
lines changed

mk/external.mk

Lines changed: 75 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,99 @@
1+
COMPRESSED_SUFFIX :=
2+
COMPRESSED_IS_ZIP :=
3+
COMPRESSED_IS_DIR :=
4+
EXTRACTOR :=
5+
VERIFIER :=
6+
7+
# temporarily files to store correct SHA1 value and computed SHA1 value
8+
# respectively for verification of directory source
9+
$(eval SHA1_FILE1 := $(shell mktemp))
10+
$(eval SHA1_FILE2 := $(shell mktemp))
11+
12+
# $(1): compressed source
13+
define prologue
14+
$(eval _ := $(shell echo " GET\t$(1)\n"))
15+
$(info $(_))
16+
endef
17+
18+
# $(1), $(2), $(3): files to be deleted
19+
define epilogue
20+
$(eval _ := $(shell $(RM) $(1) $(2) $(3)))
21+
endef
22+
23+
# $(1): compressed source URL
24+
define download
25+
$(eval _ := $(shell curl --progress-bar -O -L -C - "$(strip $(1))"))
26+
endef
27+
28+
# $(1): compressed source(.zip or.gz)
29+
define extract
30+
$(eval COMPRESSED_SUFFIX := $(suffix $(1)))
31+
$(eval COMPRESSED_IS_ZIP := $(filter $(COMPRESSED_SUFFIX),.zip))
32+
$(eval _ := \
33+
$(if $(COMPRESSED_IS_ZIP), \
34+
($(eval EXTRACTOR := unzip -d $(OUT) $(1))), \
35+
($(eval EXTRACTOR := tar -xf $(1) -C $(OUT))) \
36+
))
37+
$(eval _ := $(shell $(EXTRACTOR)))
38+
endef
39+
40+
# $(1): correct SHA1 value
41+
# $(2): filename or directory path
42+
#
43+
# Note:
44+
# 1. for regular file, $(SHA1SUM) command's -c option generates keyword "FAILED" for indicating an unmatch
45+
# 2. for directory, cmp command outputs keyword "differ" for indicating an unmatch
46+
define verify
47+
$(eval COMPRESSED_IS_DIR := $(if $(wildcard $(2)/*),1,0))
48+
$(eval _ := \
49+
$(if $(filter 1,$(COMPRESSED_IS_DIR)), \
50+
($(eval VERIFIER := \
51+
| echo $(1) > $(SHA1_FILE1) \
52+
| find $(2) -type f -print0 \
53+
| sort -z \
54+
| xargs -0 $(SHA1SUM) \
55+
| sort \
56+
| $(SHA1SUM) \
57+
| cut -f 1 -d ' ' > $(SHA1_FILE2) && cmp $(SHA1_FILE1) $(SHA1_FILE2))), \
58+
($(eval VERIFIER := echo "$(strip $(1)) $(strip $(2))" | $(SHA1SUM) -c)) \
59+
))
60+
$(eval _ := $(shell $(VERIFIER)))
61+
$(eval _ := \
62+
$(if $(filter FAILED differ:,$(_)), \
63+
($(error $(_))), \
64+
(# SHA1 value match, do nothing) \
65+
))
66+
endef
67+
168
# For each external target, the following must be defined in advance:
269
# _DATA_URL : the hyperlink which points to archive.
370
# _DATA : the file to be read by specific executable.
471
# _DATA_SHA1 : the checksum of the content in _DATA
5-
# _DATA_EXTRACT : the way to extract content from compressed file
6-
# _DATA_VERIFY : the way to verify the checksum of extracted file
772

873
# Doom
974
# https://tipsmake.com/how-to-run-doom-on-raspberry-pi-without-emulator
1075
DOOM_DATA_URL = http://www.doomworld.com/3ddownloads/ports/shareware_doom_iwad.zip
1176
DOOM_DATA = $(OUT)/DOOM1.WAD
1277
DOOM_DATA_SHA1 = 5b2e249b9c5133ec987b3ea77596381dc0d6bc1d
13-
DOOM_DATA_EXTRACT = unzip -d $(OUT) $(notdir $($(T)_DATA_URL))
14-
DOOM_DATA_VERIFY = echo "$(strip $$($(T)_DATA_SHA1)) $$@" | $(SHA1SUM) -c
1578

1679
# Quake
1780
QUAKE_DATA_URL = https://www.libsdl.org/projects/quake/data/quakesw-1.0.6.zip
1881
QUAKE_DATA = $(OUT)/id1/pak0.pak
1982
QUAKE_DATA_SHA1 = 36b42dc7b6313fd9cabc0be8b9e9864840929735
20-
QUAKE_DATA_EXTRACT = unzip -d $(OUT) $(notdir $($(T)_DATA_URL))
21-
QUAKE_DATA_VERIFY = echo "$(strip $$($(T)_DATA_SHA1)) $$@" | $(SHA1SUM) -c
2283

2384
# Timidity software synthesizer configuration for SDL2_mixer
2485
TIMIDITY_DATA_URL = http://www.libsdl.org/projects/mixer/timidity/timidity.tar.gz
2586
TIMIDITY_DATA = $(OUT)/timidity
26-
TIMIDITY_DATA_SHA1 = cdd30736508d26968222a6414f3beabc3b7a0725
27-
TIMIDITY_DATA_EXTRACT = tar -xf $(notdir $($(T)_DATA_URL)) -C $(OUT)
28-
TIMIDITY_TMP_FILE = /tmp/timidity_sha1.txt
29-
TIMIDITY_DATA_VERIFY = echo "$(TIMIDITY_DATA_SHA1)" > $(TIMIDITY_TMP_FILE) | find $(TIMIDITY_DATA) -type f -print0 | sort -z | xargs -0 shasum | shasum | cut -f 1 -d ' '
87+
TIMIDITY_DATA_SHA1 = cf6217a5d824b717ec4a07e15e6c129a4657ca25
3088

31-
define download-n-extract
89+
define download-extract-verify
3290
$($(T)_DATA):
33-
$(VECHO) " GET\t$$@\n"
34-
$(Q)curl --progress-bar -O -L -C - "$(strip $($(T)_DATA_URL))"
35-
$(Q)$($(T)_DATA_EXTRACT)
36-
$(Q)$($(T)_DATA_VERIFY)
37-
$(Q)$(RM) $(notdir $($(T)_DATA_URL)) $($(T)_TMP_FILE)
91+
$(Q)$$(call prologue,$$@)
92+
$(Q)$$(call download,$(strip $($(T)_DATA_URL)))
93+
$(Q)$$(call extract,$(notdir $($(T)_DATA_URL)))
94+
$(Q)$$(call verify,$($(T)_DATA_SHA1), $($(T)_DATA))
95+
$(Q)$$(call epilogue,$(notdir $($(T)_DATA_URL)),$(SHA1_FILE1),$(SHA1_FILE2))
3896
endef
3997

4098
EXTERNAL_DATA = DOOM QUAKE TIMIDITY
41-
$(foreach T,$(EXTERNAL_DATA),$(eval $(download-n-extract)))
99+
$(foreach T,$(EXTERNAL_DATA),$(eval $(download-extract-verify)))

0 commit comments

Comments
 (0)