[PATCH] D84380: [lit] Support running tests on Windows without GnuWin32.

Zachary Turner via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 22 19:14:15 PDT 2020


zturner created this revision.
zturner added reviewers: amccarth, akhuang.
Herald added a subscriber: delcypher.
Herald added a project: LLVM.

  Historically, we have told contributors that GnuWin32 is a pre-requisite
  because our tests depend on utilities such as sed, grep, diff, and more.
  However, Git on Windows includes versions of these utilities in its
  installation.  Furthermore, GnuWin32 has not been updated in many years.
  For these reasons, it makes sense to have the ability to run llvm tests
  in a way that is both:
    a) Easier on the user (less stuff to install)
    b) More up-to-date (The verions that ship with git are at least as
       new, if not newer, than the versions in GnuWin32.
  
  We add support for this here by attempting to detect where Git is
  installed using the Windows registry, confirming the existence of
  several common Unix tools, and then adding this location to lit's PATH
  environment.


https://reviews.llvm.org/D84380

Files:
  llvm/utils/lit/lit/llvm/config.py


Index: llvm/utils/lit/lit/llvm/config.py
===================================================================
--- llvm/utils/lit/lit/llvm/config.py
+++ llvm/utils/lit/lit/llvm/config.py
@@ -1,3 +1,4 @@
+import itertools
 import os
 import platform
 import re
@@ -24,9 +25,15 @@
             features.add('system-windows')
 
             # Seek sane tools in directories and set to $PATH.
-            path = self.lit_config.getToolsPath(config.lit_tools_dir,
-                                                config.environment['PATH'],
-                                                ['cmp.exe', 'grep.exe', 'sed.exe'])
+            path = None
+            lit_tools_dir = getattr(config, 'lit_tools_dir', None)
+            if lit_tools_dir:
+                path = self.lit_config.getToolsPath(lit_tools_dir,
+                                                    config.environment['PATH'],
+                                                    ['cmp.exe', 'grep.exe', 'sed.exe',
+                                                    'diff.exe', 'echo.exe'])
+            if path is None:
+                path = self._find_git_windows_unix_tools()
             if path is not None:
                 self.with_environment('PATH', path, append_path=True)
             # Many tools behave strangely if these environment variables aren't set.
@@ -115,6 +122,36 @@
                 self.with_environment(
                     'DYLD_INSERT_LIBRARIES', gmalloc_path_str)
 
+    def _find_git_windows_unix_tools(self):
+        assert(sys.platform == 'win32')
+        import winreg
+
+        # Search both the 64 and 32-bit hives, as well as HKLM + HKCU
+        masks = [winreg.KEY_WOW64_32KEY, winreg.KEY_WOW64_64KEY]
+        hives = [winreg.HKEY_LOCAL_MACHINE, winreg.HKEY_CURRENT_USER]
+        for mask, hive in itertools.product(masks, hives):
+            try:
+                with winreg.OpenKey(hive, r"SOFTWARE\GitForWindows", access=winreg.KEY_READ | mask) as key:
+                    try:
+                        install_root, _ = winreg.QueryValueEx(key, 'InstallPath')
+                    except:
+                        continue
+
+                    if not install_root:
+                        continue
+                    candidate_path = os.path.join(install_root, 'usr', 'bin')
+                    files = [os.path.join(candidate_path, x) for x in ['echo.exe', 'grep.exe', 'sed.exe', 'diff.exe']]
+                    # If any of the files doesn't exist, keep searching.
+                    if any(not os.path.exists(x) for x in files):
+                        continue
+
+                    # We found it, stop enumerating.
+                    return candidate_path
+            except:
+                continue
+
+        return None
+
     def with_environment(self, variable, value, append_path=False):
         if append_path:
             # For paths, we should be able to take a list of them and process all


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D84380.280004.patch
Type: text/x-patch
Size: 2945 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200723/5fc4932a/attachment.bin>


More information about the llvm-commits mailing list