blob: 1045049ad542b625fc9bfe5c273b70d73829f1e5 [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'Connor8c126942015-11-09 09:23:26 -05007import sys, os, subprocess, shlex, time, socket, optparse
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):
17 try:
18 process = subprocess.Popen(shlex.split(prog), stdout=subprocess.PIPE)
19 output = process.communicate()[0]
20 retcode = process.poll()
21 except OSError:
22 return ""
23 if retcode:
24 return ""
25 try:
26 return output.decode()
27 except UnicodeError:
28 return ""
29
Kevin O'Connora6c87742015-10-13 15:09:40 -040030# Obtain version info from "git" program
31def git_version():
32 if not os.path.exists('.git'):
33 return ""
Kevin O'Connor8c126942015-11-09 09:23:26 -050034 return check_output("git describe --tags --long --dirty").strip()
Kevin O'Connora6c87742015-10-13 15:09:40 -040035
Kevin O'Connor98a100c2015-10-22 11:59:47 -040036# Look for version in a ".version" file. Official release tarballs
37# have this file (see scripts/tarball.sh).
Kevin O'Connora6c87742015-10-13 15:09:40 -040038def file_version():
39 if not os.path.isfile('.version'):
40 return ""
41 try:
42 f = open('.version', 'r')
43 ver = f.readline().strip()
44 f.close()
Kevin O'Connor8c126942015-11-09 09:23:26 -050045 except OSError:
Kevin O'Connora6c87742015-10-13 15:09:40 -040046 return ""
47 return ver
48
49# Generate an output file with the version information
Kevin O'Connorefd70a52015-10-13 15:44:25 -040050def write_version(outfile, version, toolstr):
Kevin O'Connora6c87742015-10-13 15:09:40 -040051 sys.stdout.write("Version: %s\n" % (version,))
52 f = open(outfile, 'w')
Kevin O'Connorefd70a52015-10-13 15:44:25 -040053 f.write(VERSION_FORMAT % (version, toolstr))
Kevin O'Connora6c87742015-10-13 15:09:40 -040054 f.close()
55
Kevin O'Connorefd70a52015-10-13 15:44:25 -040056# Run "tool --version" for each specified tool and extract versions
57def tool_versions(tools):
58 tools = [t.strip() for t in tools.split(';')]
Kevin O'Connor23421012015-10-21 20:35:50 -040059 versions = ['', '']
Kevin O'Connorefd70a52015-10-13 15:44:25 -040060 success = 0
61 for tool in tools:
Kevin O'Connor23421012015-10-21 20:35:50 -040062 # Extract first line from "tool --version" output
Kevin O'Connor8c126942015-11-09 09:23:26 -050063 verstr = check_output("%s --version" % (tool,)).split('\n')[0]
Kevin O'Connor23421012015-10-21 20:35:50 -040064 # Check if this tool looks like a binutils program
65 isbinutils = 0
66 if verstr.startswith('GNU '):
67 isbinutils = 1
68 verstr = verstr[4:]
69 # Extract version information and exclude program name
70 if ' ' not in verstr:
Kevin O'Connorefd70a52015-10-13 15:44:25 -040071 continue
Kevin O'Connor23421012015-10-21 20:35:50 -040072 prog, ver = verstr.split(' ', 1)
73 if not prog or not ver:
74 continue
75 # Check for any version conflicts
76 if versions[isbinutils] and versions[isbinutils] != ver:
77 vers[isbinutils] = "mixed"
78 continue
79 versions[isbinutils] = ver
80 success += 1
81 cleanbuild = versions[0] and versions[1] and success == len(tools)
82 return cleanbuild, "gcc: %s binutils: %s" % (versions[0], versions[1])
Kevin O'Connorefd70a52015-10-13 15:44:25 -040083
Kevin O'Connora6c87742015-10-13 15:09:40 -040084def main():
85 usage = "%prog [options] <outputheader.h>"
86 opts = optparse.OptionParser(usage)
87 opts.add_option("-e", "--extra", dest="extra", default="",
88 help="extra version string to append to version")
Kevin O'Connorefd70a52015-10-13 15:44:25 -040089 opts.add_option("-t", "--tools", dest="tools", default="",
Kevin O'Connor23421012015-10-21 20:35:50 -040090 help="list of build programs to extract version from")
Kevin O'Connora6c87742015-10-13 15:09:40 -040091
92 options, args = opts.parse_args()
93 if len(args) != 1:
94 opts.error("Incorrect arguments")
95 outfile = args[0]
96
Kevin O'Connorefd70a52015-10-13 15:44:25 -040097 cleanbuild, toolstr = tool_versions(options.tools)
98
Kevin O'Connora6c87742015-10-13 15:09:40 -040099 ver = git_version()
Kevin O'Connor98a100c2015-10-22 11:59:47 -0400100 cleanbuild = cleanbuild and 'dirty' not in ver
Kevin O'Connora6c87742015-10-13 15:09:40 -0400101 if not ver:
102 ver = file_version()
Kevin O'Connor98a100c2015-10-22 11:59:47 -0400103 # We expect the "extra version" to contain information on the
104 # distributor and distribution package version (if
105 # applicable). It is a "clean" build if this is a build from
106 # an official release tarball and the above info is present.
107 cleanbuild = cleanbuild and ver and options.extra != ""
Kevin O'Connora6c87742015-10-13 15:09:40 -0400108 if not ver:
109 ver = "?"
Kevin O'Connora1b4dd02015-10-13 15:49:03 -0400110 if not cleanbuild:
111 btime = time.strftime("%Y%m%d_%H%M%S")
112 hostname = socket.gethostname()
113 ver = "%s-%s-%s" % (ver, btime, hostname)
114 write_version(outfile, ver + options.extra, toolstr)
Kevin O'Connora6c87742015-10-13 15:09:40 -0400115
116if __name__ == '__main__':
117 main()