llvm_tools: update llvm major in non-llvm ebuilds

This CL implements updating the llvm major version in non-llvm ebuilds.
An example of the current behavior (which uprevs the major version only
for the LLVM ebuild) can be seen here:
https://crrev.com/c/3080644/2

BUG=b:196193631
TEST=./update_chromeos_llvm_hash_unittest.py

Change-Id: Icf82f0a8a4a01b255d0eeb242fe139aaa6957920
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/3088319
Reviewed-by: Manoj Gupta <manojgupta@chromium.org>
Commit-Queue: Ryan Beltran <ryanbeltran@chromium.org>
Tested-by: Ryan Beltran <ryanbeltran@chromium.org>
diff --git a/llvm_tools/update_chromeos_llvm_hash.py b/llvm_tools/update_chromeos_llvm_hash.py
index afc63f3..ab83321 100755
--- a/llvm_tools/update_chromeos_llvm_hash.py
+++ b/llvm_tools/update_chromeos_llvm_hash.py
@@ -300,13 +300,12 @@
     raise ValueError('Invalid symlink provided: %s' % symlink)
 
   ebuild = os.path.realpath(symlink)
+  llvm_major_version = get_llvm_hash.GetLLVMMajorVersion(git_hash)
   # llvm
   package = os.path.basename(os.path.dirname(symlink))
   if not package:
     raise ValueError('Tried to uprev an unknown package')
-  # llvm
   if package == 'llvm':
-    llvm_major_version = get_llvm_hash.GetLLVMMajorVersion(git_hash)
     new_ebuild, is_changed = re.subn(
         r'(\d+)\.(\d+)_pre([0-9]+)_p([0-9]+)',
         '%s.\\2_pre%s_p%s' % (llvm_major_version, svn_version,
@@ -316,7 +315,8 @@
   # any other package
   else:
     new_ebuild, is_changed = re.subn(
-        r'pre([0-9]+)', 'pre%s' % svn_version, ebuild, count=1)
+        r'(\d+)\.(\d+)_pre([0-9]+)',
+        '%s.\\2_pre%s' % (llvm_major_version, svn_version), ebuild, count=1)
 
   if not is_changed:  # failed to increment the revision number
     raise ValueError('Failed to uprev the ebuild.')
diff --git a/llvm_tools/update_chromeos_llvm_hash_unittest.py b/llvm_tools/update_chromeos_llvm_hash_unittest.py
index 01306ad..adb2059 100755
--- a/llvm_tools/update_chromeos_llvm_hash_unittest.py
+++ b/llvm_tools/update_chromeos_llvm_hash_unittest.py
@@ -311,16 +311,19 @@
     self.assertEqual(mock_command_output.call_args_list[3],
                      mock.call(expected_cmd))
 
+  @mock.patch.object(get_llvm_hash, 'GetLLVMMajorVersion')
   @mock.patch.object(os.path, 'islink', return_value=True)
   @mock.patch.object(os.path, 'realpath')
   @mock.patch.object(subprocess, 'check_output', return_value=None)
   def testSuccessfullyUprevEbuildToVersionNonLLVM(self, mock_command_output,
-                                                  mock_realpath, mock_islink):
-    symlink = '/path/to/compiler-rt/compiler-rt_pre3_p2-r10.ebuild'
-    ebuild = '/abs/path/to/compiler-rt/compiler-rt_pre3_p2.ebuild'
+                                                  mock_realpath, mock_islink,
+                                                  mock_llvm_version):
+    symlink = '/abs/path/to/compiler-rt/compiler-rt-12.0_pre314159265-r4.ebuild'
+    ebuild = '/abs/path/to/compiler-rt/compiler-rt-12.0_pre314159265.ebuild'
     mock_realpath.return_value = ebuild
+    mock_llvm_version.return_value = '1234'
     svn_version = 1000
-    git_hash = '1234'
+    git_hash = '5678'
 
     update_chromeos_llvm_hash.UprevEbuildToVersion(symlink, svn_version,
                                                    git_hash)
@@ -329,12 +332,13 @@
 
     mock_realpath.assert_called_once_with(symlink)
 
+    mock_llvm_version.assert_called_once_with(git_hash)
+
     mock_command_output.assert_called()
 
     # Verify commands
     symlink_dir = os.path.dirname(symlink)
-    new_ebuild, _ = re.subn(
-        r'pre([0-9]+)', 'pre%s' % svn_version, ebuild, count=1)
+    new_ebuild = '/abs/path/to/compiler-rt/compiler-rt-1234.0_pre1000.ebuild'
     new_symlink = new_ebuild[:-len('.ebuild')] + '-r1.ebuild'
 
     expected_cmd = ['git', '-C', symlink_dir, 'mv', ebuild, new_ebuild]