build_image: cache DISK_LAYOUT_PATH early

Since get_disk_layout_path gets run in subshells, the computed value of
DISK_LAYOUT_PATH never makes it to the larger env.  Even if it did, we
currently always recompute it.  This can be a little expensive since it
involves walking all our overlays to find the json file.

Instead, let's compute this value once before we start building the image
and have the code use that value if it's been set already.  This should
be backwards compatible.

BUG=chromium:236941
TEST=`./build_image` produces the same image

Change-Id: I7360995982f35289eda0c029493d435909b587b3
Reviewed-on: https://gerrit.chromium.org/gerrit/59826
Reviewed-by: Liam McLoughlin <lmcloughlin@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 eb8900d..170f5f3 100755
--- a/build_library/base_image_util.sh
+++ b/build_library/base_image_util.sh
@@ -62,6 +62,8 @@
   check_valid_layout ${image_type}
 
   info "Using image type ${image_type}"
+  get_disk_layout_path
+  info "Using disk layout ${DISK_LAYOUT_PATH}"
 
   root_fs_dir="${BUILD_DIR}/rootfs"
   stateful_fs_dir="${BUILD_DIR}/stateful"
diff --git a/build_library/disk_layout_util.sh b/build_library/disk_layout_util.sh
index e8ed70f..a872934 100644
--- a/build_library/disk_layout_util.sh
+++ b/build_library/disk_layout_util.sh
@@ -4,6 +4,7 @@
 
 CGPT_PY="${BUILD_LIBRARY_DIR}/cgpt.py"
 PARTITION_SCRIPT_PATH="usr/sbin/write_gpt.sh"
+DISK_LAYOUT_PATH=
 
 cgpt_py() {
   if [[ -n "${FLAGS_adjust_part-}" ]]; then
@@ -19,6 +20,9 @@
 }
 
 get_disk_layout_path() {
+  if [[ -n ${DISK_LAYOUT_PATH} ]]; then
+    return 0
+  fi
   DISK_LAYOUT_PATH="${BUILD_LIBRARY_DIR}/legacy_disk_layout.json"
   local partition_script_path=$(tempfile)
   for overlay in $(cros_list_overlays --board "$BOARD"); do