Skip to content

Commit 436edaa

Browse files
committed
Replace multiple version scripts with a Python script
In the past, I've had trouble keeping the current set of version scripts to correctly output the version, especially in light of how we append Git suffixes for a non-tagged commit and dirty state. This change replaces those scripts with a single Python one which, though much more wordy than the previous one, may be easier for contributors to read and modify. The original scripts relied on Perl; this one relies on Python, which seems like a fair exchange. Having a single script also makes it easier to solve #372.
1 parent 3175485 commit 436edaa

7 files changed

+94
-15
lines changed

Makefile

+2-2
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) ./llvm_version_major.sh $(LLVM_PROJ_DIR))
47-
VERSION:=$(shell $(BASH) ./version.sh)
46+
CLANG_VERSION=$(shell $(BASH) ./version llvm-major --llvm-dir=$(LLVM_PROJ_DIR))
47+
VERSION:=$(shell $(BASH) ./version)
4848
DEBUG_PREFIX_MAP=-fdebug-prefix-map=$(ROOT_DIR)=wasisdk://v$(VERSION)
4949

5050
default: build

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.sh` can use it for calculating the
6+
not a lightweight tag, so that `version` 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.sh`
21+
VERSION=`./version`
2222
fi
2323

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

llvm_version_major.sh

-4
This file was deleted.

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.sh`
13+
VERSION=`./version`
1414
fi
1515

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

version

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env python
2+
3+
# This script finds and prints the various versions in this project: wasi-sdk
4+
# itself, LLVM, and the Git revisions of dependencies.
5+
#
6+
# Usage: version [wasi-sdk|llvm|llvm-major|dump]?
7+
8+
import argparse
9+
import subprocess
10+
11+
# The number of characters to use for the abbreviated Git revision.
12+
GIT_REF_LEN = 12
13+
14+
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")}')
31+
32+
33+
def git_version():
34+
version = exec(['git', 'describe', '--long', '--candidates=999',
35+
'--match=wasi-sdk-*', '--dirty=+m', f'--abbrev={GIT_REF_LEN}'])
36+
# Parse, e.g.: wasi-sdk-21-0-g317548590b40+m
37+
parts = version.replace('+', '-').split('-')
38+
assert (parts[0:2] == ['wasi', 'sdk'])
39+
major, minor, git, dirty = parts[2:]
40+
version = f'{major}.{minor}'
41+
if git:
42+
assert (git.startswith('g'))
43+
version += git
44+
if dirty:
45+
assert (dirty == 'm')
46+
version += '+m'
47+
return version
48+
49+
50+
def git_commit(dir='.'):
51+
return exec(['git', 'rev-parse', f'--short={GIT_REF_LEN}', 'HEAD'], dir)
52+
53+
54+
def llvm_cmake_version(llvm_dir):
55+
with open(f'{llvm_dir}/llvm/CMakeLists.txt', 'r') as file:
56+
for line in file:
57+
line = line.strip()
58+
if line.startswith('set(LLVM_VERSION_MAJOR'):
59+
llvm_version_major = parse_cmake_set(line)
60+
elif line.startswith('set(LLVM_VERSION_MINOR'):
61+
llvm_version_minor = parse_cmake_set(line)
62+
elif line.startswith('set(LLVM_VERSION_PATCH'):
63+
llvm_version_patch = parse_cmake_set(line)
64+
return llvm_version_major, llvm_version_minor, llvm_version_patch
65+
66+
67+
def parse_cmake_set(line):
68+
return line.split(' ')[1].split(')')[0]
69+
70+
71+
def exec(command, cwd='.'):
72+
result = subprocess.run(command, capture_output=True, text=True, cwd=cwd)
73+
return result.stdout.strip()
74+
75+
76+
if __name__ == '__main__':
77+
parser = argparse.ArgumentParser(
78+
description='Print the various kinds of versions in wasi-sdk')
79+
parser.add_argument('action',
80+
choices=['wasi-sdk', 'llvm', 'llvm-major', 'dump'],
81+
nargs='?',
82+
default='wasi-sdk',
83+
help='Which kind of version to print (default: wasi-sdk).')
84+
parser.add_argument('--llvm-dir',
85+
nargs='?',
86+
default='src/llvm-project',
87+
help='Override the location of the LLVM source directory (default: src/llvm-project).')
88+
args = parser.parse_args()
89+
main(args.action, args.llvm_dir)

version.sh

-6
This file was deleted.

0 commit comments

Comments
 (0)