build_image: detect compression used with toolchain packages

We'll be switching from bzip2 to zstd at some point, so have this code
detect & use the right tool automatically.

BUG=b:187789829
TEST=CQ passes

Change-Id: Ifcf14ceac0b5d79836fa61b8807496929dff9f6b
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosutils/+/3582874
Reviewed-by: Chris McDonald <cjmcdonald@chromium.org>
Commit-Queue: Mike Frysinger <vapier@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
diff --git a/build_library/base_image_util.sh b/build_library/base_image_util.sh
index fe22d29..d9bd4ea 100755
--- a/build_library/base_image_util.sh
+++ b/build_library/base_image_util.sh
@@ -217,19 +217,21 @@
     exclude_gconv=true
   fi
 
-  local idx pkg pkg_version pkg_atom pkg_path pkg_excludes
+  local idx pkg pkg_version pkg_atom pkg_path pkg_excludes pkg_comp
   for (( idx = 0; idx < ${#pkgs[@]}; idx++ )); do
     pkg="${pkgs[${idx}]}"
     pkg_version="$(_get_variable "${BOARD_ROOT}/${SYSROOT_SETTINGS_FILE}" \
       "${pkg_ver_str[${idx}]}")"
     pkg_atom="cross-${abi}/${pkg}-${pkg_version}"
     pkg_path="${PKGDIR}/${pkg_atom}.tbz2"
+    pkg_decompressor="$(detect_decompression_tool "${pkg_path}")"
     if [[ "${pkg}" == "glibc" ]]; then
       # LIBC_PATH needs to be export for later use in dev_image_util.sh
       # to extract debug symbols.
       # TODO(b/223285139): Change to export an array of packages instead
       # since we should have really extract debug info for all of them.
       export LIBC_PATH="${pkg_path}"
+      export LIBC_DECOMPRESSOR="${pkg_decompressor}"
     fi
 
     if [[ ! -e "${pkg_path}" ]]; then
@@ -257,7 +259,7 @@
         'usr/lib64/gconv'
       )
     fi
-    info_run sudo tar -Ilbzip2 -xpf "${pkg_path}" \
+    info_run sudo tar -I"${pkg_decompressor}" -xpf "${pkg_path}" \
       -C "${root_fs_dir}" "./usr/${abi}" \
       --strip-components=3 "${pkg_excludes[@]/#/--exclude=}"
   done
diff --git a/build_library/build_common.sh b/build_library/build_common.sh
index 38eb1ec..87ef6b2 100644
--- a/build_library/build_common.sh
+++ b/build_library/build_common.sh
@@ -103,3 +103,18 @@
   sudo -E PKGDIR="${tmp_pkgdir}" ${EMERGE_BOARD_CMD} --usepkgonly \
     --root=${install_root} ${kernel} || die "Cannot emerge kernel to root"
 }
+
+# Detect the decompression tool to use for |file|.
+detect_decompression_tool() {
+  local file="$1"
+
+  if [[ "$(od -An -tx1 -N4 "${file}")" == " 28 b5 2f fd" ]]; then
+    # Since the Gentoo binpkg has trailing garbage, tell zstd to ignore it.
+    echo "zstd -f"
+  elif [[ "$(od -An -tx1 -N3 "${file}")" == " 42 5a 68" ]]; then
+    echo "lbzip2"
+  else
+    echo "$1: unknown compression type" >&2
+    return 1
+  fi
+}
diff --git a/build_library/dev_image_util.sh b/build_library/dev_image_util.sh
index 0538048..09c4723 100755
--- a/build_library/dev_image_util.sh
+++ b/build_library/dev_image_util.sh
@@ -47,7 +47,7 @@
   # Copy over the libc debug info so that gdb
   # works with threads and also for a better debugging experience.
   sudo mkdir -p "${root_fs_dir}/usr/local/usr/lib/debug"
-  info_run sudo tar -Ilbzip2 -xpf "${LIBC_PATH}" \
+  info_run sudo tar -I"${LIBC_DECOMPRESSOR}" -xpf "${LIBC_PATH}" \
     -C "${root_fs_dir}/usr/local/usr/lib/debug" \
     ./usr/lib/debug/usr/${CHOST} --strip-components=6
   # Since gdb only looks in /usr/lib/debug, symlink the /usr/local