[llvm] 3308fc4 - [gn build] Use shutil.which to find git in write_vcsrevision.py (#142570)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 28 02:55:22 PDT 2025


Author: David Spickett
Date: 2025-07-28T10:55:19+01:00
New Revision: 3308fc4acda46af459b9fe69bab2f011a19d2286

URL: https://github.com/llvm/llvm-project/commit/3308fc4acda46af459b9fe69bab2f011a19d2286
DIFF: https://github.com/llvm/llvm-project/commit/3308fc4acda46af459b9fe69bab2f011a19d2286.diff

LOG: [gn build] Use shutil.which to find git in write_vcsrevision.py (#142570)

Relates to https://github.com/llvm/llvm-project/issues/54337

This is just a comment referencing distutils but even so, we can ditch
the custom which and use the one Python 3.3 added. Which has the .bat
bug fixed:
https://docs.python.org/3.3/library/shutil.html#shutil.which

I tested this on Windows:
```
C:\Users\tcwg>touch foo.bat

C:\Users\tcwg>python
Python 3.11.9 (tags/v3.11.9:de54cf5, Apr  2 2024, 12:24:25) [MSC v.1938 64 bit (ARM64)] on win32 Type "help", "copyright", "credits" or "license" for more information.
>>> import shutil
>>> shutil.which("foo")
'.\\foo.BAT'
```

I just ran the script manually and got reasonable results, I haven't
done a GN build.

Added: 
    

Modified: 
    llvm/utils/gn/build/write_vcsrevision.py

Removed: 
    


################################################################################
diff  --git a/llvm/utils/gn/build/write_vcsrevision.py b/llvm/utils/gn/build/write_vcsrevision.py
index afd6aae60f6d7..3a627eea9b97f 100755
--- a/llvm/utils/gn/build/write_vcsrevision.py
+++ b/llvm/utils/gn/build/write_vcsrevision.py
@@ -6,22 +6,13 @@
 import os
 import subprocess
 import sys
+import shutil
 
 
 THIS_DIR = os.path.abspath(os.path.dirname(__file__))
 LLVM_DIR = os.path.dirname(os.path.dirname(os.path.dirname(THIS_DIR)))
 
 
-def which(program):
-    # distutils.spawn.which() doesn't find .bat files,
-    # https://bugs.python.org/issue2200
-    for path in os.environ["PATH"].split(os.pathsep):
-        candidate = os.path.join(path, program)
-        if os.path.isfile(candidate) and os.access(candidate, os.X_OK):
-            return candidate
-    return None
-
-
 def main():
     parser = argparse.ArgumentParser(description=__doc__)
     parser.add_argument(
@@ -46,11 +37,11 @@ def main():
 
     vcsrevision_contents = ""
     if args.write_git_rev:
-        git, use_shell = which("git"), False
+        git, use_shell = shutil.which("git"), False
         if not git:
-            git = which("git.exe")
+            git = shutil.which("git.exe")
         if not git:
-            git, use_shell = which("git.bat"), True
+            git, use_shell = shutil.which("git.bat"), True
         git_dir = (
             subprocess.check_output(
                 [git, "rev-parse", "--git-dir"], cwd=LLVM_DIR, shell=use_shell


        


More information about the llvm-commits mailing list