2 # Generate version information for a program
4 # Copyright (C) 2015 Kevin O'Connor <kevin@koconnor.net>
6 # This file may be distributed under the terms of the GNU GPLv3 license.
7 import sys, os, subprocess, shlex, time, socket, optparse, logging, traceback
10 /* DO NOT EDIT! This is an autogenerated file. See scripts/buildversion.py. */
11 #define BUILD_VERSION "%s"
12 #define BUILD_TOOLS "%s"
15 # Run program and return the specified output
16 def check_output(prog):
17 logging.debug("Running %s" % (repr(prog),))
19 process = subprocess.Popen(shlex.split(prog), stdout=subprocess.PIPE)
20 output = process.communicate()[0]
21 retcode = process.poll()
23 logging.debug("Exception on run: %s" % (traceback.format_exc(),))
25 logging.debug("Got (code=%s): %s" % (retcode, repr(output)))
29 return output.decode()
31 logging.debug("Exception on decode: %s" % (traceback.format_exc(),))
34 # Obtain version info from "git" program
36 if not os.path.exists('.git'):
37 logging.debug("No '.git' file/directory found")
39 ver = check_output("git describe --tags --long --dirty").strip()
40 logging.debug("Got git version: %s" % (repr(ver),))
43 # Look for version in a ".version" file. Official release tarballs
44 # have this file (see scripts/tarball.sh).
46 if not os.path.isfile('.version'):
47 logging.debug("No '.version' file found")
50 f = open('.version', 'r')
51 ver = f.readline().strip()
54 logging.debug("Exception on read: %s" % (traceback.format_exc(),))
56 logging.debug("Got .version: %s" % (repr(ver),))
59 # Generate an output file with the version information
60 def write_version(outfile, version, toolstr):
61 logging.debug("Write file %s and %s" % (repr(version), repr(toolstr)))
62 sys.stdout.write("Version: %s\n" % (version,))
63 f = open(outfile, 'w')
64 f.write(VERSION_FORMAT % (version, toolstr))
67 # Run "tool --version" for each specified tool and extract versions
68 def tool_versions(tools):
69 tools = [t.strip() for t in tools.split(';')]
73 # Extract first line from "tool --version" output
74 verstr = check_output("%s --version" % (tool,)).split('\n')[0]
75 # Check if this tool looks like a binutils program
77 if verstr.startswith('GNU '):
80 # Extract version information and exclude program name
83 prog, ver = verstr.split(' ', 1)
84 if not prog or not ver:
86 # Check for any version conflicts
87 if versions[isbinutils] and versions[isbinutils] != ver:
88 logging.debug("Mixed version %s vs %s" % (
89 repr(versions[isbinutils]), repr(ver)))
90 versions[isbinutils] = "mixed"
92 versions[isbinutils] = ver
94 cleanbuild = versions[0] and versions[1] and success == len(tools)
95 return cleanbuild, "gcc: %s binutils: %s" % (versions[0], versions[1])
98 usage = "%prog [options] <outputheader.h>"
99 opts = optparse.OptionParser(usage)
100 opts.add_option("-e", "--extra", dest="extra", default="",
101 help="extra version string to append to version")
102 opts.add_option("-t", "--tools", dest="tools", default="",
103 help="list of build programs to extract version from")
104 opts.add_option("-v", action="store_true", dest="verbose",
105 help="enable debug messages")
107 options, args = opts.parse_args()
109 opts.error("Incorrect arguments")
112 logging.basicConfig(level=logging.DEBUG)
114 cleanbuild, toolstr = tool_versions(options.tools)
117 cleanbuild = cleanbuild and 'dirty' not in ver
120 # We expect the "extra version" to contain information on the
121 # distributor and distribution package version (if
122 # applicable). It is a "clean" build if this is a build from
123 # an official release tarball and the above info is present.
124 cleanbuild = cleanbuild and ver and options.extra != ""
128 btime = time.strftime("%Y%m%d_%H%M%S")
129 hostname = socket.gethostname()
130 ver = "%s-%s-%s" % (ver, btime, hostname)
131 write_version(outfile, ver + options.extra, toolstr)
133 if __name__ == '__main__':