lakitu: Add buildtime package dependencies not installed in sysroot.

This CL is a follow-up of CL: http://go/cos-rev/10002
In the previous CL, the buildtime dependencies which were not
installed in SYSROOT were not included. In addition, it also
included SDK depencies which are not listed as part of emerge-${BOARD}.

BUG=b/176256229
TEST=presubmit
RELEASE_NOTE=None

Change-Id: Ifcc96da243b5c99d183d474d17392ab2b9284511
Reviewed-on: https://cos-review.googlesource.com/c/cos/overlays/board-overlays/+/10260
Reviewed-by: Robert Kolchmeyer <rkolchmeyer@google.com>
Reviewed-by: Edward Jee <edjee@google.com>
Tested-by: Cusky Presubmit Bot <presubmit@cos-infra-prod.iam.gserviceaccount.com>
diff --git a/project-lakitu/scripts/board_specific_setup.sh b/project-lakitu/scripts/board_specific_setup.sh
index 96307ea..43df2bd 100644
--- a/project-lakitu/scripts/board_specific_setup.sh
+++ b/project-lakitu/scripts/board_specific_setup.sh
@@ -51,10 +51,7 @@
   # This command line is similar to what ListInstalledPackage function (in
   # chromite/licensing/licenses_lib.py) does.
   #
-  # The following "grep" command filters out extra messages to leave the package
-  # list only.
-  #
-  # And the "sed" command extracts the category name, the package name, and the
+  # The "sed" command extracts the category name, the package name, and the
   # version from each line. With that, the example above is converted to
   #
   #   ...
@@ -63,10 +60,10 @@
   #   app-emulation/docker-credential-helpers-0.6.3-r1
   #   ...
   "emerge-${BOARD}" \
-      --with-bdeps=n --with-bdeps-auto=n --usepkgonly --emptytree --pretend \
+      --quiet --with-bdeps=n --with-bdeps-auto=n \
+      --usepkgonly --emptytree --pretend \
       --color=n virtual/target-os | \
-      grep --color=never "^\[" | \
-      sed -E 's/\[[^]]+R[^]]+\] (.+) to \/build\/.*/\1/' \
+      sed -E 's/\[[^]]+\] (.+) to \/build\/.*/\1/' \
       > "${runtime_pkg_file}"
 
   echo "${runtime_pkg_file}"
@@ -85,10 +82,7 @@
   #   [binary   R    ] app-arch/gzip-1.9 to /build/lakitu/
   #   ...
   #
-  # The following "grep" command filters out extra messages to leave the package
-  # list only.
-
-  # And the "sed" command extracts the category name, the package name, and the
+  # The "sed" command extracts the category name, the package name, and the
   # version from each line. With that, the example above is converted to
   #
   #   ...
@@ -96,39 +90,42 @@
   #   dev-go/protobuf-1.3.2-r1
   #   app-arch/gzip-1.9
   #   ...
+  #
+  # The "emerge-${BOARD}" command list packages that are either a dependency
+  # due to DEPEND or BDEPEND or both. In case of both, multiple entries are
+  # present and `sort` and `uniq` are used to avoid that.
+  #
   "emerge-${BOARD}" \
-      --with-bdeps=y --with-bdeps-auto=y \
+      --quiet --with-bdeps=y --with-bdeps-auto=y \
       --emptytree --pretend --color=n \
       virtual/target-os | \
-      grep --color=never "/build/" | \
-      sed -E 's/\[[^]]+R[^]]+\] (.+) to \/build\/.*/\1/' \
+      sed -E 's/\[[^]]+\] ([[:alnum:]_.//-]*).*/\1/' | \
+      sort | uniq \
       > "${all_pkg_file}"
 
+  # Package listed above does not include some of the SDK related
+  # dependencies such as glibc and gcc. To add those packages,
+  # use the package list present at /etc/portage/profile/package.provided
+  # under sysroot. It is the same location from where cros_extract_deps
+  # gets those package for generation of CPE file.
+  local -r pkg_list_file="/build/${BOARD}/etc/portage/profile/package.provided"
+
+  while IFS= read -r line
+  do
+    # Check for empty line and comment.
+    [[ -z "${line}" || "${line}" =~ ^#.* ]] && continue
+    echo "${line}" >> "${all_pkg_file}"
+  done < "${pkg_list_file}"
+
   # Find the buildtime dependent packages.
-  # Args $2 represent the file containing the installed packages.
+  # Argument $2 represent the file containing the installed packages.
+  # All the ebuild files should have a GPL license and therefore,
+  # should be distributed in the image. So, there is no need
+  # to check whether the package belongs to a public overlay or not.
   local -r buildtime_pkg_file="$1/buildtime_pkg_info"
   fgrep -v -f "$2" "${all_pkg_file}" > "${buildtime_pkg_file}"
 
-  # Find all the buildtime packages that are present in public
-  # overlays. This is needed because he buildtime packages will
-  # be exposed in cos-package-info.json file and it should only
-  # contain packages from public overlay if it is build time
-  # dependency.
-  local -r input_file="${buildtime_pkg_file}"
-  local -r buildtime_public_pkg_file="$1/buildtime_public_pkg_info"
-
-  while IFS= read -r pkg
-  do
-    # Output of qlist-${BOARD} -R virtual/pkgconfig is in the below format:
-    # virtual/pkgconfig::portage-stable
-    # Below command gets portage-stable for the above output.
-    overlay=$(qlist-"${BOARD}" -R "${pkg}" | awk -F'::' '{print $2}')
-    if [[ "${overlay}" != *"-private"* ]]; then
-      echo "${pkg}" >> "${buildtime_public_pkg_file}"
-    fi
-  done < "${input_file}"
-
-  echo "${buildtime_public_pkg_file}"
+  echo "${buildtime_pkg_file}"
 }
 
 create_package_info() {