Skip to content

Commit d0cafb9

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 96bbd95 commit d0cafb9

7 files changed

+128
-15
lines changed

Makefile

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
ROOT_DIR=${CURDIR}
55
LLVM_PROJ_DIR?=$(ROOT_DIR)/src/llvm-project
6+
VERSION_SCRIPT=python3 ./version.py
67

78
# Windows needs munging
89
ifeq ($(OS),Windows_NT)
@@ -45,8 +46,8 @@ endif
4546
TARGETS = wasm32-wasi wasm32-wasip1 wasm32-wasip2 wasm32-wasip1-threads wasm32-wasi-threads
4647

4748
# Only the major version is needed for Clang, see https://reviews.llvm.org/D125860.
48-
CLANG_VERSION=$(shell $(BASH) ./llvm_version_major.sh $(LLVM_PROJ_DIR))
49-
VERSION:=$(shell $(BASH) ./version.sh)
49+
CLANG_VERSION=$(shell $(VERSION_SCRIPT) llvm-major --llvm-dir=$(LLVM_PROJ_DIR))
50+
VERSION:=$(shell $(VERSION_SCRIPT))
5051
DEBUG_PREFIX_MAP=-fdebug-prefix-map=$(ROOT_DIR)=wasisdk://v$(VERSION)
5152

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

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

version.py

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#!/usr/bin/env python3
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] [--llvm-dir=<non-project dir>]
7+
8+
import sys
9+
import argparse
10+
import subprocess
11+
12+
# The number of characters to use for the abbreviated Git revision.
13+
GIT_REF_LEN = 12
14+
15+
16+
def exec(command, cwd=None):
17+
result = subprocess.run(command, stdout=subprocess.PIPE,
18+
universal_newlines=True, check=True, cwd=cwd)
19+
return result.stdout.strip()
20+
21+
22+
def git_commit(dir='.'):
23+
return exec(['git', 'rev-parse', f'--short={GIT_REF_LEN}', 'HEAD'], dir)
24+
25+
26+
def parse_git_version(version):
27+
# Parse, e.g.: wasi-sdk-21-0-g317548590b40+m
28+
parts = version.replace('+', '-').split('-')
29+
assert parts.pop(0) == 'wasi'
30+
assert parts.pop(0) == 'sdk'
31+
32+
major, minor = parts.pop(0), parts.pop(0)
33+
git = None
34+
dirty = False
35+
36+
if parts:
37+
# Check: git|dirty.
38+
next = parts.pop(0)
39+
if next == 'm':
40+
dirty = True
41+
else:
42+
git = next[1:]
43+
44+
# Check: dirty.
45+
if parts:
46+
assert parts.pop(0) == 'm', f'expected dirty flag: +m'
47+
dirty = True
48+
49+
assert not parts, f'unexpected suffixes: {parts}'
50+
return major, minor, git, dirty
51+
52+
53+
# Some inline tests to check Git version parsing:
54+
assert parse_git_version(
55+
'wasi-sdk-21-0-g317548590b40+m') == ('21', '0', '317548590b40', True)
56+
assert parse_git_version('wasi-sdk-21-2+m') == ('21', '2', None, True)
57+
assert parse_git_version(
58+
'wasi-sdk-23-0-g317548590b40') == ('23', '0', '317548590b40', False)
59+
60+
61+
def git_version():
62+
version = exec(['git', 'describe', '--long', '--candidates=999',
63+
'--match=wasi-sdk-*', '--dirty=+m', f'--abbrev={GIT_REF_LEN}'])
64+
major, minor, git, dirty = parse_git_version(version)
65+
version = f'{major}.{minor}'
66+
if git:
67+
version += f'g{git}'
68+
if dirty:
69+
version += '+m'
70+
return version
71+
72+
73+
def parse_cmake_set(line):
74+
return line.split(' ')[1].split(')')[0]
75+
76+
77+
def llvm_cmake_version(llvm_dir):
78+
with open(f'{llvm_dir}/llvm/CMakeLists.txt') as file:
79+
for line in file:
80+
line = line.strip()
81+
if line.startswith('set(LLVM_VERSION_MAJOR'):
82+
llvm_version_major = parse_cmake_set(line)
83+
elif line.startswith('set(LLVM_VERSION_MINOR'):
84+
llvm_version_minor = parse_cmake_set(line)
85+
elif line.startswith('set(LLVM_VERSION_PATCH'):
86+
llvm_version_patch = parse_cmake_set(line)
87+
return llvm_version_major, llvm_version_minor, llvm_version_patch
88+
89+
90+
def main(action, llvm_dir):
91+
if action == 'wasi-sdk':
92+
print(git_version())
93+
elif action == 'llvm':
94+
major, minor, path = llvm_cmake_version(llvm_dir)
95+
print(f'{major}.{minor}.{path}')
96+
elif action == 'llvm-major':
97+
major, _, _ = llvm_cmake_version(llvm_dir)
98+
print(major)
99+
elif action == 'dump':
100+
print(git_version())
101+
print(f'wasi-libc: {git_commit("src/wasi-libc")}')
102+
print(f'llvm: {git_commit(llvm_dir)}')
103+
major, minor, path = llvm_cmake_version(llvm_dir)
104+
print(f'llvm-version: {major}.{minor}.{path}')
105+
print(f'config: {git_commit("src/config")}')
106+
107+
108+
if __name__ == '__main__':
109+
parser = argparse.ArgumentParser(
110+
description='Print the various kinds of versions in wasi-sdk')
111+
parser.add_argument('action',
112+
choices=['wasi-sdk', 'llvm', 'llvm-major', 'dump'],
113+
nargs='?',
114+
default='wasi-sdk',
115+
help='Which kind of version to print (default: wasi-sdk).')
116+
parser.add_argument('--llvm-dir',
117+
nargs='?',
118+
default='src/llvm-project',
119+
help='Override the location of the LLVM source directory (default: src/llvm-project).')
120+
args = parser.parse_args()
121+
main(args.action, args.llvm_dir)
122+
sys.exit(0)

version.sh

-6
This file was deleted.

0 commit comments

Comments
 (0)