package: add support for extracting zstd archives

Teach the generic package handling code how to extract zstd (.tar.zst)
archives. When zstd is not installed on the host, host-zstd gets built
automatically.

Signed-off-by: Matt Staveley-Taylor <matt.stav.taylor@gmail.com>
Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
diff --git a/Config.in b/Config.in
index e0257ba..df43db7 100644
--- a/Config.in
+++ b/Config.in
@@ -177,6 +177,13 @@
 	  Command to be used to extract a lzip'ed file to stdout.
 	  Default is "lzip -d -c"
 
+config BR2_ZSTDCAT
+	string "zstdcat command"
+	default "zstdcat"
+	help
+	  Command to be used to extract a zstd'ed file to stdout.
+	  Default is "zstdcat"
+
 config BR2_TAR_OPTIONS
 	string "Tar options"
 	default ""
diff --git a/Makefile b/Makefile
index db5244b..a97ebe7 100644
--- a/Makefile
+++ b/Makefile
@@ -446,6 +446,7 @@
 BZCAT := $(call qstrip,$(BR2_BZCAT))
 XZCAT := $(call qstrip,$(BR2_XZCAT))
 LZCAT := $(call qstrip,$(BR2_LZCAT))
+ZSTDCAT := $(call qstrip,$(BR2_ZSTDCAT))
 TAR_OPTIONS = $(call qstrip,$(BR2_TAR_OPTIONS)) -xf
 
 ifeq ($(BR2_PER_PACKAGE_DIRECTORIES),y)
diff --git a/package/pkg-utils.mk b/package/pkg-utils.mk
index 3ab9e9fe..e8e508e 100644
--- a/package/pkg-utils.mk
+++ b/package/pkg-utils.mk
@@ -59,6 +59,7 @@
 INFLATE.tbz2 = $(BZCAT)
 INFLATE.tgz  = $(ZCAT)
 INFLATE.xz   = $(XZCAT)
+INFLATE.zst  = $(ZSTDCAT)
 INFLATE.tar  = cat
 # suitable-extractor(filename): returns extractor based on suffix
 suitable-extractor = $(INFLATE$(suffix $(1)))
@@ -66,6 +67,7 @@
 EXTRACTOR_PKG_DEPENDENCY.lzma = $(BR2_XZCAT_HOST_DEPENDENCY)
 EXTRACTOR_PKG_DEPENDENCY.xz   = $(BR2_XZCAT_HOST_DEPENDENCY)
 EXTRACTOR_PKG_DEPENDENCY.lz   = $(BR2_LZIP_HOST_DEPENDENCY)
+EXTRACTOR_PKG_DEPENDENCY.zst  = $(BR2_ZSTD_HOST_DEPENDENCY)
 
 # extractor-pkg-dependency(filename): returns a Buildroot package
 # dependency needed to extract file based on suffix
diff --git a/support/dependencies/check-host-zstd.mk b/support/dependencies/check-host-zstd.mk
new file mode 100644
index 0000000..fa595ef
--- /dev/null
+++ b/support/dependencies/check-host-zstd.mk
@@ -0,0 +1,4 @@
+ifeq (,$(call suitable-host-package,zstd,$(ZSTDCAT)))
+BR2_ZSTD_HOST_DEPENDENCY = host-zstd
+ZSTDCAT = $(HOST_DIR)/bin/zstdcat
+endif
diff --git a/support/dependencies/check-host-zstd.sh b/support/dependencies/check-host-zstd.sh
new file mode 100755
index 0000000..2f469c4
--- /dev/null
+++ b/support/dependencies/check-host-zstd.sh
@@ -0,0 +1,14 @@
+#!/bin/sh
+
+candidate="$1"
+
+zstdcat=$(which "$candidate" 2>/dev/null)
+if [ ! -x "$zstdcat" ]; then
+	zstdcat=$(which zstdcat 2>/dev/null)
+	if [ ! -x "$zstdcat" ]; then
+		# echo nothing: no suitable zstdcat found
+		exit 1
+	fi
+fi
+
+echo "$zstdcat"