build_packages: add a --autosetgov_sticky option

Allow users to opt-in to --autosetgov behavior all the time, even if
they forget to specify the flag.

BUG=b:187789305
TEST=Ran series of commands and checked state
	# Warns about powersave, but leaves it alone.
	./build_packages ...
	# Automatically switches to performance.
	./build_packages ... --autosetgov
	# Still warns about powersave, but leaves it alone (good).
	./build_packages ...
	# Tells user they're opting in, and automatically switches to performance.
	./build_packages ... --autosetgov --autosetgov_sticky
	# Automatically switches to performance.
	./build_packages ...
	# Tells user they're opting out, and warns about powersave, and leaves it alone.
	./build_packages ... --autosetgov_sticky
	# Warns about powersave, but leaves it alone.
	./build_packages ...

Change-Id: Ic80ff701225730736820e1271a0b1174e4cf253f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosutils/+/3417385
Tested-by: Mike Frysinger <vapier@chromium.org>
Reviewed-by: Alex Klein <saklein@chromium.org>
diff --git a/build_packages b/build_packages
index 6a0ddb9..a103c31 100755
--- a/build_packages
+++ b/build_packages
@@ -30,6 +30,8 @@
   "Calculate reverse dependencies on changed ebuilds."
 DEFINE_boolean autosetgov "${FLAGS_FALSE}" \
   "Automatically set cpu governor to 'performance'."
+DEFINE_boolean autosetgov_sticky "${FLAGS_FALSE}" \
+  "Remember --autosetgov setting for future runs."
 DEFINE_boolean use_any_chrome "${FLAGS_TRUE}" \
   "Use any Chrome prebuilt available, even if the prebuilt doesn't match exactly."
 DEFINE_boolean cleanbuild "${FLAGS_FALSE}" \
@@ -149,6 +151,23 @@
 
 # On some systems, powersave can take a long time to ramp up.  Inform the user
 # so they can get faster builds.  https://crbug.com/1008932
+CHROMITE_CONFIG_DIR=$(
+  python -c 'from chromite.lib.chromite_config import DIR; print(DIR)')
+CONFIG_AUTOSETGOV="${CHROMITE_CONFIG_DIR}/autosetgov"
+if [[ "${FLAGS_autosetgov_sticky}" -eq "${FLAGS_TRUE}" ]]; then
+  mkdir -p "${CHROMITE_CONFIG_DIR}"
+  if [[ "${FLAGS_autosetgov}" -eq "${FLAGS_TRUE}" ]]; then
+    info "Future runs of build_packages will *always* use --autosetgov"
+    echo "# Delete this file to turn off automatic build_packages" \
+      "--autosetgov." >"${CONFIG_AUTOSETGOV}"
+  else
+    info "Future runs of build_packages will respect --autosetgov"
+    rm -f "${CONFIG_AUTOSETGOV}"
+  fi
+fi
+if [[ -e "${CONFIG_AUTOSETGOV}" ]]; then
+  FLAGS_autosetgov="${FLAGS_TRUE}"
+fi
 # Make sure we can actually support "performance".
 if grep -qs performance \
      /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors; then