Skip to content

Commit 7a4f8f6

Browse files
committed
Apply review suggestions
- finish with a `sys.exit(0)` - elide default `r` in `open()` - specify `python3` - default to `cwd=None` - change file name to `version.py` - reorder functions in C-like declaration order
1 parent f0ecc0a commit 7a4f8f6

5 files changed

+46
-44
lines changed

Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ override LLVM_CMAKE_FLAGS += -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" \
4343
endif
4444

4545
# Only the major version is needed for Clang, see https://reviews.llvm.org/D125860.
46-
CLANG_VERSION=$(shell $(BASH) ./version llvm-major --llvm-dir=$(LLVM_PROJ_DIR))
47-
VERSION:=$(shell $(BASH) ./version)
46+
CLANG_VERSION=$(shell $(BASH) ./version.py llvm-major --llvm-dir=$(LLVM_PROJ_DIR))
47+
VERSION:=$(shell $(BASH) ./version.py)
4848
DEBUG_PREFIX_MAP=-fdebug-prefix-map=$(ROOT_DIR)=wasisdk://v$(VERSION)
4949

5050
default: build
@@ -241,7 +241,7 @@ build/config.BUILT:
241241
touch build/config.BUILT
242242

243243
build/version.BUILT:
244-
./version dump > $(BUILD_PREFIX)/VERSION
244+
./version.py dump > $(BUILD_PREFIX)/VERSION
245245
touch build/version.BUILT
246246

247247
build: build/llvm.BUILT build/wasi-libc.BUILT build/compiler-rt.BUILT build/libcxx.BUILT build/config.BUILT build/version.BUILT

RELEASING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
To publish a new version of `wasi-sdk` as a GitHub release:
44

55
1. Tag a commit with an annotated tag. Note that this must be an annotated tag,
6-
not a lightweight tag, so that `version` can use it for calculating the
6+
not a lightweight tag, so that `version.py` can use it for calculating the
77
package version (use `git show wasi-sdk-...` to show other tag messages).
88
Note that you may need to clear the repository cache to avoid problems with
99
cached artifacts [^cache].

deb_from_installation.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fi
1818
if [ -n "$2" ]; then
1919
VERSION="$2"
2020
else
21-
VERSION=`./version`
21+
VERSION=`./version.py`
2222
fi
2323

2424
if [ -n "$3" ]; then

tar_from_installation.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fi
1010
if [ -n "$2" ]; then
1111
VERSION="$2"
1212
else
13-
VERSION=`./version`
13+
VERSION=`./version.py`
1414
fi
1515

1616
if [ -n "$3" ]; then

version version.py

+40-38
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,25 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22

33
# This script finds and prints the various versions in this project: wasi-sdk
44
# itself, LLVM, and the Git revisions of dependencies.
55
#
6-
# Usage: version [wasi-sdk|llvm|llvm-major|dump]?
6+
# Usage: version [wasi-sdk|llvm|llvm-major|dump] [--llvm-dir=<non-project dir>]
77

8+
import sys
89
import argparse
910
import subprocess
1011

1112
# The number of characters to use for the abbreviated Git revision.
1213
GIT_REF_LEN = 12
1314

1415

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()
3119

3220

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)
4323

4424

4525
def parse_git_version(version):
@@ -77,12 +57,24 @@ def parse_git_version(version):
7757
'wasi-sdk-23-0-g317548590b40') == ('23', '0', '317548590b40', False)
7858

7959

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]
8274

8375

8476
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:
8678
for line in file:
8779
line = line.strip()
8880
if line.startswith('set(LLVM_VERSION_MAJOR'):
@@ -94,13 +86,22 @@ def llvm_cmake_version(llvm_dir):
9486
return llvm_version_major, llvm_version_minor, llvm_version_patch
9587

9688

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")}')
104105

105106

106107
if __name__ == '__main__':
@@ -117,3 +118,4 @@ def exec(command, cwd='.'):
117118
help='Override the location of the LLVM source directory (default: src/llvm-project).')
118119
args = parser.parse_args()
119120
main(args.action, args.llvm_dir)
121+
sys.exit(0)

0 commit comments

Comments
 (0)