SPIRV-Tools/utils/update_build_version.py
David Neto 9166854ac9 Add CHANGES file with high level software history
Add a high level version number for SPIRV-Tools, beginning
with v2016.0-dev.  The README describes the format of the
version number.

The high level version number is extracted from the CHANGES
file.  That works around:
 - stale-bait for when we don't add tags to the repository
 - our inability to add tags to the repository

Option --version causes spirv-as, spirv-dis, and spirv-val to
show the high level version number.

Add spvSoftwareVersionString to return the C-string for
the high level version number.

Add spvSoftwareVersionDetailsString() so that clients can get
more information if they want to.
Also allows us to clean up the uses in the tool executables files,
so now only one file includes build-version.inc.

Move the update-build-version logic to the only
CMakeLists file that needs it.

The update build version script takes a new argument
to name the output file.
2016-04-22 15:19:45 -04:00

112 lines
3.9 KiB
Python
Executable File

#!/usr/bin/env python
# Copyright 2016 The Shaderc Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Updates an output file with version info unless the new content is the same
# as the existing content.
#
# Args: <spirv-tools_dir> <output-file>
#
# The output file will contain a line of text consisting of two C source syntax
# string literals separated by a comma:
# - The software version deduced from the CHANGES file in the given directory.
# - A longer string with the project name, the software version number, and
# git commit information for the directory. The commit information
# is the output of "git describe" if that succeeds, or "git rev-parse HEAD"
# if that succeeds, or otherwise a message containing the phrase "unknown hash".
# The string contents are escaped as necessary.
from __future__ import print_function
import datetime
import os.path
import re
import subprocess
import sys
def command_output(cmd, dir):
"""Runs a command in a directory and returns its standard output stream.
Captures the standard error stream.
Raises a RuntimeError if the command fails to launch or otherwise fails.
"""
p = subprocess.Popen(cmd,
cwd=dir,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(stdout, _) = p.communicate()
if p.returncode != 0:
raise RuntimeError('Failed to run %s in %s' % (cmd, dir))
return stdout
def deduceSoftwareVersion(dir):
"""Returns a software version number parsed from the CHANGES file
in the given dir.
The CHANGES file describes most recent versions first.
"""
pattern = re.compile('(v\d+\.\d+(-dev)) \d\d\d\d-\d\d-\d\d$')
changes_file = os.path.join(dir, 'CHANGES')
with open(changes_file) as f:
for line in f.readlines():
match = pattern.match(line)
if match:
return match.group(1)
raise Exception('No version number found in {}'.format(changes_file))
def describe(dir):
"""Returns a string describing the current Git HEAD version as descriptively
as possible.
Runs 'git describe', or alternately 'git rev-parse HEAD', in dir. If
successful, returns the output; otherwise returns 'unknown hash, <date>'."""
try:
# decode() is needed here for Python3 compatibility. In Python2,
# str and bytes are the same type, but not in Python3.
# Popen.communicate() returns a bytes instance, which needs to be
# decoded into text data first in Python3. And this decode() won't
# hurt Python2.
return command_output(['git', 'describe'], dir).rstrip().decode()
except:
try:
return command_output(
['git', 'rev-parse', 'HEAD'], dir).rstrip().decode()
except:
return 'unknown hash, {}'.format(datetime.date.today().isoformat())
def main():
if len(sys.argv) != 3:
print('usage: {0} <spirv-tools_dir> <output-file>'.format(sys.argv[0]))
sys.exit(1)
output_file = sys.argv[2]
software_version = deduceSoftwareVersion(sys.argv[1])
new_content = '"{}", "SPIRV-Tools {} {}"\n'.format(
software_version, software_version,
describe(sys.argv[1]).replace('"', '\\"'))
if os.path.isfile(output_file) and new_content == open(output_file, 'r').read():
sys.exit(0)
open(output_file, 'w').write(new_content)
if __name__ == '__main__':
main()