1
- #!/usr/bin/env python
1
+ #!/usr/bin/env python3
2
2
3
3
# This script finds and prints the various versions in this project: wasi-sdk
4
4
# itself, LLVM, and the Git revisions of dependencies.
5
5
#
6
- # Usage: version [wasi-sdk|llvm|llvm-major|dump]?
6
+ # Usage: version [wasi-sdk|llvm|llvm-major|dump] [--llvm-dir=<non-project dir>]
7
7
8
+ import sys
8
9
import argparse
9
10
import subprocess
10
11
11
12
# The number of characters to use for the abbreviated Git revision.
12
13
GIT_REF_LEN = 12
13
14
14
15
15
- def main (action , llvm_dir ):
16
- if action == 'wasi-sdk' :
17
- print (git_version ())
18
- elif action == 'llvm' :
19
- major , minor , path = llvm_cmake_version (llvm_dir )
20
- print (f'{ major } .{ minor } .{ path } ' )
21
- elif action == 'llvm-major' :
22
- major , _ , _ = llvm_cmake_version (llvm_dir )
23
- print (major )
24
- elif action == 'dump' :
25
- print (git_version ())
26
- print (f'wasi-libc: { git_commit ("src/wasi-libc" )} ' )
27
- print (f'llvm: { git_commit (llvm_dir )} ' )
28
- major , minor , path = llvm_cmake_version (llvm_dir )
29
- print (f'llvm-version: { major } .{ minor } .{ path } ' )
30
- print (f'config: { git_commit ("src/config" )} ' )
16
+ def exec (command , cwd = None ):
17
+ result = subprocess .run (command , capture_output = True , text = True , cwd = cwd )
18
+ return result .stdout .strip ()
31
19
32
20
33
- def git_version ():
34
- version = exec (['git' , 'describe' , '--long' , '--candidates=999' ,
35
- '--match=wasi-sdk-*' , '--dirty=+m' , f'--abbrev={ GIT_REF_LEN } ' ])
36
- major , minor , git , dirty = parse_git_version (version )
37
- version = f'{ major } .{ minor } '
38
- if git :
39
- version += f'g{ git } '
40
- if dirty :
41
- version += '+m'
42
- return version
21
+ def git_commit (dir = '.' ):
22
+ return exec (['git' , 'rev-parse' , f'--short={ GIT_REF_LEN } ' , 'HEAD' ], dir )
43
23
44
24
45
25
def parse_git_version (version ):
@@ -77,12 +57,24 @@ def parse_git_version(version):
77
57
'wasi-sdk-23-0-g317548590b40' ) == ('23' , '0' , '317548590b40' , False )
78
58
79
59
80
- def git_commit (dir = '.' ):
81
- return exec (['git' , 'rev-parse' , f'--short={ GIT_REF_LEN } ' , 'HEAD' ], dir )
60
+ def git_version ():
61
+ version = exec (['git' , 'describe' , '--long' , '--candidates=999' ,
62
+ '--match=wasi-sdk-*' , '--dirty=+m' , f'--abbrev={ GIT_REF_LEN } ' ])
63
+ major , minor , git , dirty = parse_git_version (version )
64
+ version = f'{ major } .{ minor } '
65
+ if git :
66
+ version += f'g{ git } '
67
+ if dirty :
68
+ version += '+m'
69
+ return version
70
+
71
+
72
+ def parse_cmake_set (line ):
73
+ return line .split (' ' )[1 ].split (')' )[0 ]
82
74
83
75
84
76
def llvm_cmake_version (llvm_dir ):
85
- with open (f'{ llvm_dir } /llvm/CMakeLists.txt' , 'r' ) as file :
77
+ with open (f'{ llvm_dir } /llvm/CMakeLists.txt' ) as file :
86
78
for line in file :
87
79
line = line .strip ()
88
80
if line .startswith ('set(LLVM_VERSION_MAJOR' ):
@@ -94,13 +86,22 @@ def llvm_cmake_version(llvm_dir):
94
86
return llvm_version_major , llvm_version_minor , llvm_version_patch
95
87
96
88
97
- def parse_cmake_set (line ):
98
- return line .split (' ' )[1 ].split (')' )[0 ]
99
-
100
-
101
- def exec (command , cwd = '.' ):
102
- result = subprocess .run (command , capture_output = True , text = True , cwd = cwd )
103
- return result .stdout .strip ()
89
+ def main (action , llvm_dir ):
90
+ if action == 'wasi-sdk' :
91
+ print (git_version ())
92
+ elif action == 'llvm' :
93
+ major , minor , path = llvm_cmake_version (llvm_dir )
94
+ print (f'{ major } .{ minor } .{ path } ' )
95
+ elif action == 'llvm-major' :
96
+ major , _ , _ = llvm_cmake_version (llvm_dir )
97
+ print (major )
98
+ elif action == 'dump' :
99
+ print (git_version ())
100
+ print (f'wasi-libc: { git_commit ("src/wasi-libc" )} ' )
101
+ print (f'llvm: { git_commit (llvm_dir )} ' )
102
+ major , minor , path = llvm_cmake_version (llvm_dir )
103
+ print (f'llvm-version: { major } .{ minor } .{ path } ' )
104
+ print (f'config: { git_commit ("src/config" )} ' )
104
105
105
106
106
107
if __name__ == '__main__' :
@@ -117,3 +118,4 @@ def exec(command, cwd='.'):
117
118
help = 'Override the location of the LLVM source directory (default: src/llvm-project).' )
118
119
args = parser .parse_args ()
119
120
main (args .action , args .llvm_dir )
121
+ sys .exit (0 )
0 commit comments