Path: blob/main/utils/update_build_version.py
1560 views
#!/usr/bin/env python12# Copyright 2016 The Shaderc Authors. All rights reserved.3#4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this file except in compliance with the License.6# You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14# limitations under the License.1516# Updates build-version.inc in the current directory, unless the update is17# identical to the existing content.18#19# Args: <shaderc-dir> <spirv-tools-dir> <glslang-dir>20#21# For each directory, there will be a line in build-version.inc containing that22# directory's "git describe" output enclosed in double quotes and appropriately23# escaped.2425import datetime26import errno27import os.path28import re29import subprocess30import sys31import time3233def mkdir_p(directory):34"""Make the directory, and all its ancestors as required. Any of the35directories are allowed to already exist."""3637if directory == "":38# We're being asked to make the current directory.39return4041try:42os.makedirs(directory)43except OSError as e:44if e.errno == errno.EEXIST and os.path.isdir(directory):45pass46else:47raise4849def command_output(cmd, directory):50"""Runs a command in a directory and returns its standard output stream.5152Captures the standard error stream.5354Raises a RuntimeError if the command fails to launch or otherwise fails.55"""56p = subprocess.Popen(cmd,57cwd=directory,58stdout=subprocess.PIPE,59stderr=subprocess.PIPE)60(stdout, _) = p.communicate()61if p.returncode != 0:62raise RuntimeError('Failed to run {} in {}'.format(cmd, directory))63return stdout646566def deduce_software_version(directory):67"""Returns a software version number parsed from the CHANGES file in the68given directory.6970The CHANGES file describes most recent versions first.71"""7273# Match the first well-formed version-and-date line.74# Allow trailing whitespace in the checked-out source code has75# unexpected carriage returns on a linefeed-only system such as76# Linux.77pattern = re.compile(r'^(v\d+\.\d+(-dev|[\.-]rc\d+)?) \d\d\d\d-\d\d-\d\d\s*$')78changes_file = os.path.join(directory, 'CHANGES')79with open(changes_file, errors='replace') as f:80for line in f.readlines():81match = pattern.match(line)82if match:83return match.group(1)84raise Exception('No version number found in {}'.format(changes_file))858687def describe(directory):88"""Returns a string describing the current Git HEAD version as89descriptively as possible.9091Runs 'git describe', or alternately 'git rev-parse HEAD', in92directory. If successful, returns the output; otherwise returns93'unknown hash, <date>'.94"""95try:96# decode() is needed here for Python3 compatibility. In Python2,97# str and bytes are the same type, but not in Python3.98# Popen.communicate() returns a bytes instance, which needs to be99# decoded into text data first in Python3. And this decode() won't100# hurt Python2.101return command_output(['git', 'describe'], directory).rstrip().decode()102except:103try:104return command_output(105['git', 'rev-parse', 'HEAD'], directory).rstrip().decode()106except:107# This is the fallback case where git gives us no information,108# e.g. because the source tree might not be in a git tree.109# In this case, usually use a timestamp. However, to ensure110# reproducible builds, allow the builder to override the wall111# clock time with enviornment variable SOURCE_DATE_EPOCH112# containing a (presumably) fixed timestamp.113timestamp = int(os.environ.get('SOURCE_DATE_EPOCH', time.time()))114formatted = datetime.date.fromtimestamp(timestamp).isoformat()115return 'unknown hash, {}'.format(formatted)116117118def get_version_string(project, directory):119"""Returns a detailed version string for a given project with its120directory, which consists of software version string and git description121string."""122detailed_version_string_lst = [project]123if project != 'glslang':124detailed_version_string_lst.append(deduce_software_version(directory))125detailed_version_string_lst.append(describe(directory).replace('"', '\\"'))126return ' '.join(detailed_version_string_lst)127128129def main():130if len(sys.argv) != 5:131print(('usage: {} <shaderc-dir> <spirv-tools-dir> <glslang-dir> <output-file>'.format(132sys.argv[0])))133sys.exit(1)134135projects = ['shaderc', 'spirv-tools', 'glslang']136new_content = ''.join([137'"{}\\n"\n'.format(get_version_string(p, d))138for (p, d) in zip(projects, sys.argv[1:])139])140141output_file = sys.argv[4]142mkdir_p(os.path.dirname(output_file))143144if os.path.isfile(output_file):145with open(output_file, 'r') as f:146if new_content == f.read():147return148with open(output_file, 'w') as f:149f.write(new_content)150151152if __name__ == '__main__':153main()154155156