Update prebuilt logic to use more prebuilts when available.

- Use chrome_internal prebuilts for internal builds when available.
- Use daisy prebuilts for chromeos-exynos5 boards, but not for other
  arm boards.
- Document that we don't use arm-generic prebuilts for other platforms,
  since they're not binary-compatible.

BUG=chromium:222377
TEST=Verify external prebuilts are still used by default.
TEST=Verify internal prebuilts are used when USE=chrome_internal
TEST=Trybot runs of paladin and incremental builders.
Change-Id: I667c68b767c2127297080ec3c2f23e8a01894b3c
Reviewed-on: https://gerrit.chromium.org/gerrit/50058
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Commit-Queue: David James <davidjames@chromium.org>
Tested-by: David James <davidjames@chromium.org>
diff --git a/common.sh b/common.sh
index 4887321..6726885 100644
--- a/common.sh
+++ b/common.sh
@@ -176,6 +176,10 @@
   exit 1
 }
 
+# Check for a single string in a list of space-separated strings.
+# e.g. has "foo" "foo bar baz" is true, but has "f" "foo bar baz" is not.
+has() { [[ " ${*:2} " == *" $1 "* ]]; }
+
 # Directory locations inside the dev chroot; try the new default,
 # falling back to user specific paths if the upgrade has yet to
 # happen.
diff --git a/setup_board b/setup_board
index f07dfee..f5d93bb 100755
--- a/setup_board
+++ b/setup_board
@@ -193,23 +193,41 @@
   fi
 
   local binhost_dir="$CHROMIUMOS_OVERLAY/chromeos/binhost/target"
-  local preflight_binhost="$binhost_dir/$BOARD-PREFLIGHT_BINHOST.conf"
   local chrome_binhost_suffix="LATEST_RELEASE_CHROME_BINHOST.conf"
-  local chrome_binhost="$binhost_dir/$BOARD-$chrome_binhost_suffix"
-  if [[ $ARCH = x86 && ! -f "$preflight_binhost" ]]; then
-    preflight_binhost="$binhost_dir/x86-generic-PREFLIGHT_BINHOST.conf"
-  elif [[ $ARCH = amd64 && ! -f "$preflight_binhost" ]]; then
-    preflight_binhost="$binhost_dir/amd64-generic-PREFLIGHT_BINHOST.conf"
-  fi
-  if [[ $ARCH = x86 && ! -f "$chrome_binhost" ]]; then
-    chrome_binhost="$binhost_dir/x86-generic-$chrome_binhost_suffix"
-  elif [[ $ARCH = amd64 && ! -f "$chrome_binhost" ]]; then
-    chrome_binhost="$binhost_dir/amd64-generic-$chrome_binhost_suffix"
-  fi
-
   local partner_overlay="$SRC_ROOT/private-overlays/chromeos-partner-overlay"
   local internal_binhost_dir="$partner_overlay/chromeos/binhost/target"
-  local internal_binhost="$internal_binhost_dir/$BOARD-PREFLIGHT_BINHOST.conf"
+  declare -A chrome_arch_map arch_map kernel_map
+  local chrome_binhost_base kernel
+  # Arm prebuilts are kernel-specific so don't use generic prebuilts for them.
+  kernel=$(portageq-${BOARD_VARIANT} envvar CHROMEOS_KERNEL_SPLITCONFIG)
+  kernel_map=(['chromeos-exynos5']='daisy')
+  arch_map=(
+    ['amd64']='amd64-generic'
+    ['x86']='x86-generic'
+  )
+  internal_arch_map=(
+    ['amd64']='lumpy'
+    ['x86']='x86-alex'
+  )
+  if has chrome_internal $(portageq-${BOARD_VARIANT} envvar USE); then
+    chrome_binhost_base=${internal_binhost_dir}
+  else
+    chrome_binhost_base=${binhost_dir}
+  fi
+  local generic="${kernel_map[${kernel:-none}]:-${arch_map[${ARCH}]:-}}"
+  local internal_generic="${internal_arch_map[${ARCH}]:-${generic}}"
+  local board chrome_binhost internal_binhost preflight_binhost
+  for board in ${BOARD_VARIANT} ${BOARD} ${internal_generic} ${generic}; do
+    if [[ -z ${chrome_binhost} || ! -f ${chrome_binhost} ]]; then
+      chrome_binhost="${chrome_binhost_base}/${board}-${chrome_binhost_suffix}"
+    fi
+    if [[ -z ${preflight_binhost} || ! -f ${preflight_binhost} ]]; then
+      preflight_binhost="${binhost_dir}/${board}-PREFLIGHT_BINHOST.conf"
+    fi
+    if [[ -z ${internal_binhost} || ! -f ${internal_binhost} ]]; then
+      internal_binhost="${internal_binhost_dir}/${board}-PREFLIGHT_BINHOST.conf"
+    fi
+  done
 
   cat <<'EOF'
 # FULL_BINHOST is populated by the full builders. It is listed first because it