update_kernel_afdo: Move away branch dependent configurations

When we add a new kernel version to afdo pipeline we don't want
to update it in the branches since it's likely that afdo was enabled
in the same milestone.
In the current design configuration changes affected all branches and
to skip the update in branches we had to use "skip branches" tricks.

The better solution which is added here is to move away branch dependent
configurations from the script and read it from remote refs.

BUG=b:272295936
TEST=tested locally

Change-Id: I7856b15c50182f4897859022906e1263e76ec54e
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/4321430
Reviewed-by: George Burgess <gbiv@chromium.org>
Tested-by: Denis Nikitin <denik@chromium.org>
Commit-Queue: Denis Nikitin <denik@chromium.org>
diff --git a/afdo_tools/update_kernel_afdo b/afdo_tools/update_kernel_afdo
index 2401b1c..3a5fd08 100755
--- a/afdo_tools/update_kernel_afdo
+++ b/afdo_tools/update_kernel_afdo
@@ -34,17 +34,18 @@
 set -eu
 set -o pipefail
 
+# Branch independent constants.
+# Changes here will affect kernel afdo update in cros branches.
+# -------------------
+ARCHS="amd arm"
 AMD_GS_BASE=gs://chromeos-prebuilt/afdo-job/vetted/kernel/amd64
 ARM_GS_BASE=gs://chromeos-prebuilt/afdo-job/vetted/kernel/arm
-AMD_KVERS="4.14 4.19 5.4 5.10 5.15"
-ARM_KVERS="5.15"
-failed_channels=""
+UPDATE_CONFIG_FILE="afdo_tools/update_kernel_afdo.cfg"
+# CL reviewers and cc.
+REVIEWERS="c-compiler-chrome@google.com"
+CC="denik@google.com"
 # Add skipped chrome branches in ascending order here.
 SKIPPED_BRANCHES="95"
-# CL reviewers and cc.
-reviewers="c-compiler-chrome@google.com"
-cc="denik@google.com"
-
 # NOTE: We enable/disable kernel AFDO starting from a particular branch.
 #   For example if we want to enable kernel AFDO in 5.15, first, we do it
 #   in main. In this case we want to disable it in beta and stable branches.
@@ -56,18 +57,21 @@
 # - key is a branch number string;
 # - value is the list of kernels separated by space.
 # Example: SKIPPED_KVERS_IN_BRANCHES["105"]="4.4 4.14"
+# -------------------
 
-# b/223115767. In M-100 there are no new profiles in 5.10. And AFDO is not
-# enabled on any 5.10 board in M-100 either.
-SKIPPED_KVERS_IN_BRANCHES["100"]="5.10"
+# Branch dependent constants were moved to UPDATE_CONFIG_FILE.
+# IMPORTANT: Starting from M-113 update_kernel_afdo reads branch-dependent
+# constants from UPDATE_CONFIG_FILE from remote refs.
+# DON'T UPDATE THESE CONSTANTS HERE, THEY WILL BE OVERWRITTEN.
+# -------------------
+AMD_KVERS="4.14 4.19 5.4 5.10"
+ARM_KVERS="5.15"
+AMD_METADATA_FILE="afdo_metadata/kernel_afdo.json"
+ARM_METADATA_FILE="afdo_metadata/kernel_arm_afdo.json"
+# -------------------
 
 script_dir=$(dirname "$0")
 tc_utils_dir="${script_dir}/.."
-metadata_dir="${tc_utils_dir}/afdo_metadata"
-amd_outfile="$(realpath --relative-to="${tc_utils_dir}" \
-  "${metadata_dir}"/kernel_afdo.json)"
-arm_outfile="$(realpath --relative-to="${tc_utils_dir}" \
-  "${metadata_dir}"/kernel_arm_afdo.json)"
 # Convert toolchain_utils into the absolute path.
 abs_tc_utils_dir="$(realpath "${tc_utils_dir}")"
 
@@ -77,16 +81,11 @@
 upload_cl=true
 # Interactive mode.
 interactive=true
+# Without arguments the script updates all branches.
+channels=""
+failed_channels=""
 
-ARCHS="amd arm"
 declare -A arch_gsbase arch_kvers arch_outfile
-arch_gsbase["amd"]="${AMD_GS_BASE}"
-arch_gsbase["arm"]="${ARM_GS_BASE}"
-arch_kvers["amd"]="${AMD_KVERS}"
-arch_kvers["arm"]="${ARM_KVERS}"
-arch_outfile["amd"]="${amd_outfile}"
-arch_outfile["arm"]="${arm_outfile}"
-
 declare -A branch branch_number commit
 remote_repo=$(git -C "${tc_utils_dir}" remote)
 canary_ref="refs/heads/main"
@@ -112,9 +111,8 @@
     ((branch_number[canary]++))
   fi
 done
+config_file="${tc_utils_dir}/${UPDATE_CONFIG_FILE}"
 
-# Without arguments the script updates all branches.
-channels=""
 for arg in "$@"
 do
   case "${arg}" in
@@ -193,6 +191,21 @@
   git reset --hard HEAD
   git checkout -b "${channel}" "${remote_repo}/${curr_branch}"
 
+  # Read branch-dependent constants from $remote_repo.
+  # shellcheck source=afdo_tools/update_kernel_afdo.cfg
+  [[ -e "${config_file}" ]] && source "${config_file}"
+
+  amd_outfile="$(realpath --relative-to="${tc_utils_dir}" \
+    "${tc_utils_dir}/${AMD_METADATA_FILE}")"
+  arm_outfile="$(realpath --relative-to="${tc_utils_dir}" \
+    "${tc_utils_dir}/${ARM_METADATA_FILE}")"
+  arch_gsbase["amd"]="${AMD_GS_BASE}"
+  arch_gsbase["arm"]="${ARM_GS_BASE}"
+  arch_kvers["amd"]="${AMD_KVERS}"
+  arch_kvers["arm"]="${ARM_KVERS}"
+  arch_outfile["amd"]="${amd_outfile}"
+  arch_outfile["arm"]="${arm_outfile}"
+
   new_changes=false
   for arch in ${ARCHS}
   do
@@ -358,11 +371,11 @@
       if ${interactive}
       then
         (cd "${tc_utils_dir}" && \
-          repo upload --br="${channel}" --re="${reviewers}" --cc="${cc}" .)
+          repo upload --br="${channel}" --re="${REVIEWERS}" --cc="${CC}" .)
       else
         (cd "${tc_utils_dir}" && \
-          repo upload --br="${channel}" --no-verify -y --re="${reviewers}" \
-                      --cc="${cc}" .)
+          repo upload --br="${channel}" --no-verify -y --re="${REVIEWERS}" \
+                      --cc="${CC}" .)
       fi
     done
   else
diff --git a/afdo_tools/update_kernel_afdo.cfg b/afdo_tools/update_kernel_afdo.cfg
new file mode 100644
index 0000000..66a6fd1
--- /dev/null
+++ b/afdo_tools/update_kernel_afdo.cfg
@@ -0,0 +1,8 @@
+# Add a new supported kernel version in this file.
+# All changes here won't affect kernel afdo update in branches.
+# WARNING: Changes must be submitted to have effect.
+
+AMD_KVERS="4.14 4.19 5.4 5.10 5.15"
+ARM_KVERS="5.15"
+AMD_METADATA_FILE="afdo_metadata/kernel_afdo.json"
+ARM_METADATA_FILE="afdo_metadata/kernel_arm_afdo.json"