run bp2build sparingly

Rerun bp2build less eagerly
1. avoids overriding metrics files
2. enables running pretty.sh without having to acquire a lock; useful to view intermediate results during incremental_build.sh

Bug: NA
Test: NA
Change-Id: I196e7057290e29e9aac9e936d56c3c2757d254e7
diff --git a/scripts/incremental_build/clone.sh b/scripts/incremental_build/clone.sh
index 0acdfc4..79913d0 100755
--- a/scripts/incremental_build/clone.sh
+++ b/scripts/incremental_build/clone.sh
@@ -14,17 +14,19 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-#
-# Gather and print top-line performance metrics for the android build
-#
 readonly TOP="$(realpath "$(dirname "$0")/../../../..")"
+if [[ -z ${OUT_DIR+x} ]]; then
+  OUT_DIR="$TOP/out"
+fi
 
-"$TOP/build/soong/soong_ui.bash" \
-  --build-mode \
-  --all-modules \
-  --dir="$(pwd)" \
-  --skip-soong-tests \
-  bp2build
+if [[ ! -f ${OUT_DIR}/soong/workspace/WORKSPACE ]]; then
+  "$TOP/build/soong/soong_ui.bash" \
+    --build-mode \
+    --all-modules \
+    --dir="$(pwd)" \
+    --skip-soong-tests \
+    bp2build
+fi
 
 ANDROID_BUILD_TOP=$TOP "$TOP/build/bazel/bin/bazel" \
   run --config=bp2build --verbose_failures \
diff --git a/scripts/incremental_build/incremental_build.sh b/scripts/incremental_build/incremental_build.sh
index d8e4634..8c7cd8f 100755
--- a/scripts/incremental_build/incremental_build.sh
+++ b/scripts/incremental_build/incremental_build.sh
@@ -14,17 +14,19 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-#
-# Gather and print top-line performance metrics for the android build
-#
 readonly TOP="$(realpath "$(dirname "$0")/../../../..")"
+if [[ -z ${OUT_DIR+x} ]]; then
+  OUT_DIR="$TOP/out"
+fi
 
-"$TOP/build/soong/soong_ui.bash" \
-  --build-mode \
-  --all-modules \
-  --dir="$(pwd)" \
-  --skip-soong-tests \
-  bp2build
+if [[ ! -f ${OUT_DIR}/soong/workspace/WORKSPACE ]]; then
+  "$TOP/build/soong/soong_ui.bash" \
+    --build-mode \
+    --all-modules \
+    --dir="$(pwd)" \
+    --skip-soong-tests \
+    bp2build
+fi
 
 # to debug use `--run_under /usr/lib/python3.10/pdb.py`
 # or simply add `breakpoint()` in the code somewhere
diff --git a/scripts/incremental_build/pretty.sh b/scripts/incremental_build/pretty.sh
index 48f3783..6a7b01e 100755
--- a/scripts/incremental_build/pretty.sh
+++ b/scripts/incremental_build/pretty.sh
@@ -14,17 +14,19 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-#
-# Gather and print top-line performance metrics for the android build
-#
 readonly TOP="$(realpath "$(dirname "$0")/../../../..")"
+if [[ -z ${OUT_DIR+x} ]]; then
+  OUT_DIR="$TOP/out"
+fi
 
-"$TOP/build/soong/soong_ui.bash" \
-  --build-mode \
-  --all-modules \
-  --dir="$(pwd)" \
-  --skip-soong-tests \
-  bp2build
+if [[ ! -f ${OUT_DIR}/soong/workspace/WORKSPACE ]]; then
+  "$TOP/build/soong/soong_ui.bash" \
+    --build-mode \
+    --all-modules \
+    --dir="$(pwd)" \
+    --skip-soong-tests \
+    bp2build
+fi
 
 ANDROID_BUILD_TOP=$TOP "$TOP/build/bazel/bin/bazel" \
   run --config=bp2build --verbose_failures \