2016-02-22 21:07:19 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2016-09-01 17:04:00 +00:00
|
|
|
# Copyright (c) 2016 Google Inc.
|
2016-02-22 21:07:19 +00:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2016-04-22 00:50:11 +00:00
|
|
|
# Updates an output file with version info unless the new content is the same
|
|
|
|
# as the existing content.
|
2016-02-22 21:07:19 +00:00
|
|
|
#
|
2016-04-22 00:50:11 +00:00
|
|
|
# Args: <spirv-tools_dir> <output-file>
|
2016-02-22 21:07:19 +00:00
|
|
|
#
|
2016-04-22 00:50:11 +00:00
|
|
|
# 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"
|
2016-08-04 22:25:42 +00:00
|
|
|
# if that succeeds, or otherwise a message containing the phrase
|
|
|
|
# "unknown hash".
|
2016-04-22 00:50:11 +00:00
|
|
|
# The string contents are escaped as necessary.
|
2016-02-22 21:07:19 +00:00
|
|
|
|
2016-03-06 16:44:21 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2016-02-22 21:07:19 +00:00
|
|
|
import datetime
|
2016-08-12 18:19:17 +00:00
|
|
|
import errno
|
2017-01-17 18:32:28 +00:00
|
|
|
import os
|
2016-02-22 21:07:19 +00:00
|
|
|
import os.path
|
2016-04-22 00:50:11 +00:00
|
|
|
import re
|
2016-02-22 21:07:19 +00:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2017-01-17 18:32:28 +00:00
|
|
|
import time
|
2016-02-22 21:07:19 +00:00
|
|
|
|
2016-03-06 18:00:58 +00:00
|
|
|
|
2016-08-12 18:19:17 +00:00
|
|
|
def mkdir_p(directory):
|
|
|
|
"""Make the directory, and all its ancestors as required. Any of the
|
|
|
|
directories are allowed to already exist."""
|
|
|
|
|
2017-03-20 15:23:35 +00:00
|
|
|
if directory == "":
|
|
|
|
# We're being asked to make the current directory.
|
|
|
|
return
|
|
|
|
|
2016-08-12 18:19:17 +00:00
|
|
|
try:
|
|
|
|
os.makedirs(directory)
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno == errno.EEXIST and os.path.isdir(directory):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
2016-08-04 22:25:42 +00:00
|
|
|
def command_output(cmd, directory):
|
2016-02-24 17:41:29 +00:00
|
|
|
"""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,
|
2016-08-04 22:25:42 +00:00
|
|
|
cwd=directory,
|
2016-02-24 17:41:29 +00:00
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
|
|
|
(stdout, _) = p.communicate()
|
2016-03-06 18:00:58 +00:00
|
|
|
if p.returncode != 0:
|
2016-08-04 22:25:42 +00:00
|
|
|
raise RuntimeError('Failed to run %s in %s' % (cmd, directory))
|
2016-02-24 17:41:29 +00:00
|
|
|
return stdout
|
|
|
|
|
|
|
|
|
2016-08-04 22:25:42 +00:00
|
|
|
def deduce_software_version(directory):
|
2016-04-22 00:50:11 +00:00
|
|
|
"""Returns a software version number parsed from the CHANGES file
|
2016-08-04 22:25:42 +00:00
|
|
|
in the given directory.
|
2016-04-22 00:50:11 +00:00
|
|
|
|
|
|
|
The CHANGES file describes most recent versions first.
|
|
|
|
"""
|
|
|
|
|
2017-01-04 16:04:47 +00:00
|
|
|
# Match the first well-formed version-and-date line.
|
|
|
|
# Allow trailing whitespace in the checked-out source code has
|
|
|
|
# unexpected carriage returns on a linefeed-only system such as
|
|
|
|
# Linux.
|
|
|
|
pattern = re.compile(r'^(v\d+\.\d+(-dev)?) \d\d\d\d-\d\d-\d\d\s*$')
|
2016-08-04 22:25:42 +00:00
|
|
|
changes_file = os.path.join(directory, 'CHANGES')
|
2016-12-19 18:26:42 +00:00
|
|
|
with open(changes_file, mode='rU') as f:
|
2016-04-22 00:50:11 +00:00
|
|
|
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))
|
|
|
|
|
|
|
|
|
2016-08-04 22:25:42 +00:00
|
|
|
def describe(directory):
|
2016-02-24 17:41:29 +00:00
|
|
|
"""Returns a string describing the current Git HEAD version as descriptively
|
|
|
|
as possible.
|
|
|
|
|
2016-08-04 22:25:42 +00:00
|
|
|
Runs 'git describe', or alternately 'git rev-parse HEAD', in directory. If
|
2016-02-24 17:41:29 +00:00
|
|
|
successful, returns the output; otherwise returns 'unknown hash, <date>'."""
|
2016-02-22 21:07:19 +00:00
|
|
|
try:
|
2016-03-11 20:24:41 +00:00
|
|
|
# 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.
|
2016-08-04 22:25:42 +00:00
|
|
|
return command_output(['git', 'describe'], directory).rstrip().decode()
|
2016-02-23 22:58:31 +00:00
|
|
|
except:
|
2016-02-24 17:41:29 +00:00
|
|
|
try:
|
2016-03-11 20:24:41 +00:00
|
|
|
return command_output(
|
2016-08-04 22:25:42 +00:00
|
|
|
['git', 'rev-parse', 'HEAD'], directory).rstrip().decode()
|
2016-02-24 17:41:29 +00:00
|
|
|
except:
|
2017-01-17 20:41:23 +00:00
|
|
|
# This is the fallback case where git gives us no information,
|
|
|
|
# e.g. because the source tree might not be in a git tree.
|
|
|
|
# In this case, usually use a timestamp. However, to ensure
|
|
|
|
# reproducible builds, allow the builder to override the wall
|
|
|
|
# clock time with enviornment variable SOURCE_DATE_EPOCH
|
|
|
|
# containing a (presumably) fixed timestamp.
|
|
|
|
timestamp = int(os.environ.get('SOURCE_DATE_EPOCH', time.time()))
|
|
|
|
formatted = datetime.date.fromtimestamp(timestamp).isoformat()
|
|
|
|
return 'unknown hash, {}'.format(formatted)
|
2016-02-24 17:41:29 +00:00
|
|
|
|
2016-02-22 21:07:19 +00:00
|
|
|
|
|
|
|
def main():
|
2016-04-22 00:50:11 +00:00
|
|
|
if len(sys.argv) != 3:
|
2016-08-04 22:25:42 +00:00
|
|
|
print('usage: {} <spirv-tools-dir> <output-file>'.format(sys.argv[0]))
|
2016-02-22 21:07:19 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
2016-04-22 00:50:11 +00:00
|
|
|
output_file = sys.argv[2]
|
2016-08-12 18:19:17 +00:00
|
|
|
mkdir_p(os.path.dirname(output_file))
|
2016-04-22 00:50:11 +00:00
|
|
|
|
2016-06-02 19:32:28 +00:00
|
|
|
software_version = deduce_software_version(sys.argv[1])
|
2016-04-22 00:50:11 +00:00
|
|
|
new_content = '"{}", "SPIRV-Tools {} {}"\n'.format(
|
|
|
|
software_version, software_version,
|
2016-03-11 20:24:41 +00:00
|
|
|
describe(sys.argv[1]).replace('"', '\\"'))
|
2016-08-04 22:26:22 +00:00
|
|
|
|
|
|
|
if os.path.isfile(output_file):
|
|
|
|
with open(output_file, 'r') as f:
|
|
|
|
if new_content == f.read():
|
|
|
|
return
|
|
|
|
|
|
|
|
with open(output_file, 'w') as f:
|
|
|
|
f.write(new_content)
|
2016-02-22 21:07:19 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|