blob: 46928984e4d4a519e5860951765bdc1fedad74d2 [file] [log] [blame]
Kevin O'Connora6c87742015-10-13 15:09:40 -04001#!/usr/bin/env python
2# Generate version information for a program
3#
4# Copyright (C) 2015 Kevin O'Connor <kevin@koconnor.net>
5#
6# This file may be distributed under the terms of the GNU GPLv3 license.
Kevin O'Connorfa7545a2015-11-10 09:26:52 -05007import sys, os, subprocess, shlex, time, socket, optparse, logging, traceback
Kevin O'Connora6c87742015-10-13 15:09:40 -04008
9VERSION_FORMAT = """
10/* DO NOT EDIT! This is an autogenerated file. See scripts/buildversion.py. */
11#define BUILD_VERSION "%s"
Kevin O'Connorefd70a52015-10-13 15:44:25 -040012#define BUILD_TOOLS "%s"
Kevin O'Connora6c87742015-10-13 15:09:40 -040013"""
14
Kevin O'Connor8c126942015-11-09 09:23:26 -050015# Run program and return the specified output
16def check_output(prog):
Kevin O'Connorfa7545a2015-11-10 09:26:52 -050017 logging.debug("Running %s" % (repr(prog),))
Kevin O'Connor8c126942015-11-09 09:23:26 -050018 try:
19 process = subprocess.Popen(shlex.split(prog), stdout=subprocess.PIPE)
20 output = process.communicate()[0]
21 retcode = process.poll()
22 except OSError:
Kevin O'Connorfa7545a2015-11-10 09:26:52 -050023 logging.debug("Exception on run: %s" % (traceback.format_exc(),))
Kevin O'Connor8c126942015-11-09 09:23:26 -050024 return ""
Kevin O'Connorfa7545a2015-11-10 09:26:52 -050025 logging.debug("Got (code=%s): %s" % (retcode, repr(output)))
Kevin O'Connor8c126942015-11-09 09:23:26 -050026 if retcode:
27 return ""
28 try:
29 return output.decode()
30 except UnicodeError:
Kevin O'Connorfa7545a2015-11-10 09:26:52 -050031 logging.debug("Exception on decode: %s" % (traceback.format_exc(),))
Kevin O'Connor8c126942015-11-09 09:23:26 -050032 return ""
33
Kevin O'Connora6c87742015-10-13 15:09:40 -040034# Obtain version info from "git" program
35def git_version():
36 if not os.path.exists('.git'):
Kevin O'Connorfa7545a2015-11-10 09:26:52 -050037 logging.debug("No '.git' file/directory found")
Kevin O'Connora6c87742015-10-13 15:09:40 -040038 return ""
Kevin O'Connorfa7545a2015-11-10 09:26:52 -050039 ver = check_output("git describe --tags --long --dirty").strip()
40 logging.debug("Got git version: %s" % (repr(ver),))
41 return ver
Kevin O'Connora6c87742015-10-13 15:09:40 -040042
Kevin O'Connor98a100c2015-10-22 11:59:47 -040043# Look for version in a ".version" file. Official release tarballs
44# have this file (see scripts/tarball.sh).
Kevin O'Connora6c87742015-10-13 15:09:40 -040045def file_version():
46 if not os.path.isfile('.version'):
Kevin O'Connorfa7545a2015-11-10 09:26:52 -050047 logging.debug("No '.version' file found")
Kevin O'Connora6c87742015-10-13 15:09:40 -040048 return ""
49 try:
50 f = open('.version', 'r')
51 ver = f.readline().strip()
52 f.close()
Kevin O'Connor8c126942015-11-09 09:23:26 -050053 except OSError:
Kevin O'Connorfa7545a2015-11-10 09:26:52 -050054 logging.debug("Exception on read: %s" % (traceback.format_exc(),))
Kevin O'Connora6c87742015-10-13 15:09:40 -040055 return ""
Kevin O'Connorfa7545a2015-11-10 09:26:52 -050056 logging.debug("Got .version: %s" % (repr(ver),))
Kevin O'Connora6c87742015-10-13 15:09:40 -040057 return ver
58
59# Generate an output file with the version information
Kevin O'Connorefd70a52015-10-13 15:44:25 -040060def write_version(outfile, version, toolstr):
Kevin O'Connorfa7545a2015-11-10 09:26:52 -050061 logging.debug("Write file %s and %s" % (repr(version), repr(toolstr)))
Kevin O'Connora6c87742015-10-13 15:09:40 -040062 sys.stdout.write("Version: %s\n" % (version,))
63 f = open(outfile, 'w')
Kevin O'Connorefd70a52015-10-13 15:44:25 -040064 f.write(VERSION_FORMAT % (version, toolstr))
Kevin O'Connora6c87742015-10-13 15:09:40 -040065 f.close()
66
Kevin O'Connorefd70a52015-10-13 15:44:25 -040067# Run "tool --version" for each specified tool and extract versions
68def tool_versions(tools):
69 tools = [t.strip() for t in tools.split(';')]
Kevin O'Connor23421012015-10-21 20:35:50 -040070 versions = ['', '']
Kevin O'Connorefd70a52015-10-13 15:44:25 -040071 success = 0
72 for tool in tools:
Kevin O'Connor23421012015-10-21 20:35:50 -040073 # Extract first line from "tool --version" output
Kevin O'Connor8c126942015-11-09 09:23:26 -050074 verstr = check_output("%s --version" % (tool,)).split('\n')[0]
Kevin O'Connor23421012015-10-21 20:35:50 -040075 # Check if this tool looks like a binutils program
76 isbinutils = 0
77 if verstr.startswith('GNU '):
78 isbinutils = 1
79 verstr = verstr[4:]
80 # Extract version information and exclude program name
81 if ' ' not in verstr:
Kevin O'Connorefd70a52015-10-13 15:44:25 -040082 continue
Kevin O'Connor23421012015-10-21 20:35:50 -040083 prog, ver = verstr.split(' ', 1)
84 if not prog or not ver:
85 continue
86 # Check for any version conflicts
87 if versions[isbinutils] and versions[isbinutils] != ver:
Kevin O'Connorfa7545a2015-11-10 09:26:52 -050088 logging.debug("Mixed version %s vs %s" % (
89 repr(versions[isbinutils]), repr(ver)))
Roger Pau Monne3b8c5372015-12-28 13:50:41 +010090 versions[isbinutils] = "mixed"
Kevin O'Connor23421012015-10-21 20:35:50 -040091 continue
92 versions[isbinutils] = ver
93 success += 1
94 cleanbuild = versions[0] and versions[1] and success == len(tools)
95 return cleanbuild, "gcc: %s binutils: %s" % (versions[0], versions[1])
Kevin O'Connorefd70a52015-10-13 15:44:25 -040096
Kevin O'Connora6c87742015-10-13 15:09:40 -040097def main():
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")
Kevin O'Connorefd70a52015-10-13 15:44:25 -0400102 opts.add_option("-t", "--tools", dest="tools", default="",
Kevin O'Connor23421012015-10-21 20:35:50 -0400103 help="list of build programs to extract version from")
Kevin O'Connorfa7545a2015-11-10 09:26:52 -0500104 opts.add_option("-v", action="store_true", dest="verbose",
105 help="enable debug messages")
Kevin O'Connora6c87742015-10-13 15:09:40 -0400106
107 options, args = opts.parse_args()
108 if len(args) != 1:
109 opts.error("Incorrect arguments")
110 outfile = args[0]
Kevin O'Connorfa7545a2015-11-10 09:26:52 -0500111 if options.verbose:
112 logging.basicConfig(level=logging.DEBUG)
Kevin O'Connora6c87742015-10-13 15:09:40 -0400113
Kevin O'Connorefd70a52015-10-13 15:44:25 -0400114 cleanbuild, toolstr = tool_versions(options.tools)
115
Kevin O'Connora6c87742015-10-13 15:09:40 -0400116 ver = git_version()
Kevin O'Connor98a100c2015-10-22 11:59:47 -0400117 cleanbuild = cleanbuild and 'dirty' not in ver
Kevin O'Connora6c87742015-10-13 15:09:40 -0400118 if not ver:
119 ver = file_version()
Kevin O'Connor98a100c2015-10-22 11:59:47 -0400120 # 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 != ""
Kevin O'Connora6c87742015-10-13 15:09:40 -0400125 if not ver:
126 ver = "?"
Kevin O'Connora1b4dd02015-10-13 15:49:03 -0400127 if not cleanbuild:
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)
Kevin O'Connora6c87742015-10-13 15:09:40 -0400132
133if __name__ == '__main__':
134 main()