Home | History | Annotate | Download | only in utils
      1 #!/usr/bin/env python
      2 
      3 # Copyright 2016 The Shaderc Authors. All rights reserved.
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License");
      6 # you may not use this file except in compliance with the License.
      7 # You may obtain a copy of the License at
      8 #
      9 #     http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 # Unless required by applicable law or agreed to in writing, software
     12 # distributed under the License is distributed on an "AS IS" BASIS,
     13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 # See the License for the specific language governing permissions and
     15 # limitations under the License.
     16 
     17 # Updates build-version.inc in the current directory, unless the update is
     18 # identical to the existing content.
     19 #
     20 # Args: <shaderc-dir> <spirv-tools-dir> <glslang-dir>
     21 #
     22 # For each directory, there will be a line in build-version.inc containing that
     23 # directory's "git describe" output enclosed in double quotes and appropriately
     24 # escaped.
     25 
     26 from __future__ import print_function
     27 
     28 import datetime
     29 import os.path
     30 import re
     31 import subprocess
     32 import sys
     33 import time
     34 
     35 OUTFILE = 'build-version.inc'
     36 
     37 
     38 def command_output(cmd, directory):
     39     """Runs a command in a directory and returns its standard output stream.
     40 
     41     Captures the standard error stream.
     42 
     43     Raises a RuntimeError if the command fails to launch or otherwise fails.
     44     """
     45     p = subprocess.Popen(cmd,
     46                          cwd=directory,
     47                          stdout=subprocess.PIPE,
     48                          stderr=subprocess.PIPE)
     49     (stdout, _) = p.communicate()
     50     if p.returncode != 0:
     51         raise RuntimeError('Failed to run {} in {}'.format(cmd, directory))
     52     return stdout
     53 
     54 
     55 def deduce_software_version(directory):
     56     """Returns a software version number parsed from the CHANGES file
     57     in the given directory.
     58 
     59     The CHANGES file describes most recent versions first.
     60     """
     61 
     62     # Match the first well-formed version-and-date line.
     63     # Allow trailing whitespace in the checked-out source code has
     64     # unexpected carriage returns on a linefeed-only system such as
     65     # Linux.
     66     pattern = re.compile(r'^(v\d+\.\d+(-dev)?) \d\d\d\d-\d\d-\d\d\s*$')
     67     changes_file = os.path.join(directory, 'CHANGES')
     68     with open(changes_file) as f:
     69         for line in f.readlines():
     70             match = pattern.match(line)
     71             if match:
     72                 return match.group(1)
     73     raise Exception('No version number found in {}'.format(changes_file))
     74 
     75 
     76 def describe(directory):
     77     """Returns a string describing the current Git HEAD version as descriptively
     78     as possible.
     79 
     80     Runs 'git describe', or alternately 'git rev-parse HEAD', in directory.  If
     81     successful, returns the output; otherwise returns 'unknown hash, <date>'."""
     82     try:
     83         # decode() is needed here for Python3 compatibility. In Python2,
     84         # str and bytes are the same type, but not in Python3.
     85         # Popen.communicate() returns a bytes instance, which needs to be
     86         # decoded into text data first in Python3. And this decode() won't
     87         # hurt Python2.
     88         return command_output(['git', 'describe'], directory).rstrip().decode()
     89     except:
     90         try:
     91             return command_output(
     92                 ['git', 'rev-parse', 'HEAD'], directory).rstrip().decode()
     93         except:
     94             # This is the fallback case where git gives us no information,
     95             # e.g. because the source tree might not be in a git tree.
     96             # In this case, usually use a timestamp.  However, to ensure
     97             # reproducible builds, allow the builder to override the wall
     98             # clock time with enviornment variable SOURCE_DATE_EPOCH
     99             # containing a (presumably) fixed timestamp.
    100             timestamp = int(os.environ.get('SOURCE_DATE_EPOCH', time.time()))
    101             formatted = datetime.date.fromtimestamp(timestamp).isoformat()
    102             return 'unknown hash, {}'.format(formatted)
    103 
    104 
    105 def get_version_string(project, directory):
    106     """Returns a detailed version string for a given project with its directory,
    107     which consists of software version string and git description string."""
    108     detailed_version_string_lst = [project]
    109     if project != 'glslang':
    110         detailed_version_string_lst.append(deduce_software_version(directory))
    111     detailed_version_string_lst.append(describe(directory).replace('"', '\\"'))
    112     return ' '.join(detailed_version_string_lst)
    113 
    114 
    115 def main():
    116     if len(sys.argv) != 4:
    117         print('usage: {} <shaderc-dir> <spirv-tools-dir> <glslang-dir>'.format(
    118             sys.argv[0]))
    119         sys.exit(1)
    120 
    121     projects = ['shaderc', 'spirv-tools', 'glslang']
    122     new_content = ''.join([
    123         '"{}\\n"\n'.format(get_version_string(p, d))
    124         for (p, d) in zip(projects, sys.argv[1:])
    125     ])
    126 
    127     if os.path.isfile(OUTFILE):
    128         with open(OUTFILE, 'r') as f:
    129             if new_content == f.read():
    130                 return
    131     with open(OUTFILE, 'w') as f:
    132         f.write(new_content)
    133 
    134 
    135 if __name__ == '__main__':
    136     main()
    137