• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2
3# Copyright 2018 The Amber 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# Generates build-versions.h in the src/ directory.
18#
19# Args:  <output_dir> <amber-dir> <spirv-tools-dir> <spirv-headers> <glslang-dir> <shaderc-dir>
20
21from __future__ import print_function
22
23import datetime
24import os.path
25import re
26import subprocess
27import sys
28import time
29
30OUTFILE = 'src/build-versions.h'
31
32
33def command_output(cmd, directory):
34  p = subprocess.Popen(cmd,
35                       cwd=directory,
36                       stdout=subprocess.PIPE,
37                       stderr=subprocess.PIPE)
38  (stdout, _) = p.communicate()
39  if p.returncode != 0:
40    raise RuntimeError('Failed to run {} in {}'.format(cmd, directory))
41  return stdout
42
43
44def describe(directory):
45  if not os.path.exists(directory):
46    return "-"
47  return command_output(
48      ['git', 'log', '-1', '--format=%h'], directory).rstrip().decode()
49
50
51def get_version_string(project, directory):
52  name = project.upper().replace('-', '_')
53  return "#define {}_VERSION \"{}\"".format(name, describe(directory))
54
55
56def main():
57  if len(sys.argv) != 4:
58    print('usage: {} <outdir> <amber-dir> <third_party>'.format(
59      sys.argv[0]))
60    sys.exit(1)
61
62  outdir = sys.argv[1]
63  srcdir = sys.argv[3]
64
65  projects = ['spirv-tools', 'spirv-headers', 'glslang', 'shaderc']
66  new_content = get_version_string('amber', sys.argv[2]) + "\n"
67  new_content = new_content + ''.join([
68    '{}\n'.format(get_version_string(p, os.path.join(srcdir, p)))
69    for p in projects
70  ])
71
72  file = outdir + "/" + OUTFILE
73  if os.path.isfile(file):
74    with open(file, 'r') as f:
75      if new_content == f.read():
76        return
77  with open(file, 'w') as f:
78    f.write(new_content)
79
80
81if __name__ == '__main__':
82  main()
83