Kevin O'Connor | a6c8774 | 2015-10-13 15:09:40 -0400 | [diff] [blame] | 1 | #!/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'Connor | 8c12694 | 2015-11-09 09:23:26 -0500 | [diff] [blame^] | 7 | import sys, os, subprocess, shlex, time, socket, optparse |
Kevin O'Connor | a6c8774 | 2015-10-13 15:09:40 -0400 | [diff] [blame] | 8 | |
| 9 | VERSION_FORMAT = """ |
| 10 | /* DO NOT EDIT! This is an autogenerated file. See scripts/buildversion.py. */ |
| 11 | #define BUILD_VERSION "%s" |
Kevin O'Connor | efd70a5 | 2015-10-13 15:44:25 -0400 | [diff] [blame] | 12 | #define BUILD_TOOLS "%s" |
Kevin O'Connor | a6c8774 | 2015-10-13 15:09:40 -0400 | [diff] [blame] | 13 | """ |
| 14 | |
Kevin O'Connor | 8c12694 | 2015-11-09 09:23:26 -0500 | [diff] [blame^] | 15 | # Run program and return the specified output |
| 16 | def 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'Connor | a6c8774 | 2015-10-13 15:09:40 -0400 | [diff] [blame] | 30 | # Obtain version info from "git" program |
| 31 | def git_version(): |
| 32 | if not os.path.exists('.git'): |
| 33 | return "" |
Kevin O'Connor | 8c12694 | 2015-11-09 09:23:26 -0500 | [diff] [blame^] | 34 | return check_output("git describe --tags --long --dirty").strip() |
Kevin O'Connor | a6c8774 | 2015-10-13 15:09:40 -0400 | [diff] [blame] | 35 | |
Kevin O'Connor | 98a100c | 2015-10-22 11:59:47 -0400 | [diff] [blame] | 36 | # Look for version in a ".version" file. Official release tarballs |
| 37 | # have this file (see scripts/tarball.sh). |
Kevin O'Connor | a6c8774 | 2015-10-13 15:09:40 -0400 | [diff] [blame] | 38 | def 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'Connor | 8c12694 | 2015-11-09 09:23:26 -0500 | [diff] [blame^] | 45 | except OSError: |
Kevin O'Connor | a6c8774 | 2015-10-13 15:09:40 -0400 | [diff] [blame] | 46 | return "" |
| 47 | return ver |
| 48 | |
| 49 | # Generate an output file with the version information |
Kevin O'Connor | efd70a5 | 2015-10-13 15:44:25 -0400 | [diff] [blame] | 50 | def write_version(outfile, version, toolstr): |
Kevin O'Connor | a6c8774 | 2015-10-13 15:09:40 -0400 | [diff] [blame] | 51 | sys.stdout.write("Version: %s\n" % (version,)) |
| 52 | f = open(outfile, 'w') |
Kevin O'Connor | efd70a5 | 2015-10-13 15:44:25 -0400 | [diff] [blame] | 53 | f.write(VERSION_FORMAT % (version, toolstr)) |
Kevin O'Connor | a6c8774 | 2015-10-13 15:09:40 -0400 | [diff] [blame] | 54 | f.close() |
| 55 | |
Kevin O'Connor | efd70a5 | 2015-10-13 15:44:25 -0400 | [diff] [blame] | 56 | # Run "tool --version" for each specified tool and extract versions |
| 57 | def tool_versions(tools): |
| 58 | tools = [t.strip() for t in tools.split(';')] |
Kevin O'Connor | 2342101 | 2015-10-21 20:35:50 -0400 | [diff] [blame] | 59 | versions = ['', ''] |
Kevin O'Connor | efd70a5 | 2015-10-13 15:44:25 -0400 | [diff] [blame] | 60 | success = 0 |
| 61 | for tool in tools: |
Kevin O'Connor | 2342101 | 2015-10-21 20:35:50 -0400 | [diff] [blame] | 62 | # Extract first line from "tool --version" output |
Kevin O'Connor | 8c12694 | 2015-11-09 09:23:26 -0500 | [diff] [blame^] | 63 | verstr = check_output("%s --version" % (tool,)).split('\n')[0] |
Kevin O'Connor | 2342101 | 2015-10-21 20:35:50 -0400 | [diff] [blame] | 64 | # 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'Connor | efd70a5 | 2015-10-13 15:44:25 -0400 | [diff] [blame] | 71 | continue |
Kevin O'Connor | 2342101 | 2015-10-21 20:35:50 -0400 | [diff] [blame] | 72 | 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'Connor | efd70a5 | 2015-10-13 15:44:25 -0400 | [diff] [blame] | 83 | |
Kevin O'Connor | a6c8774 | 2015-10-13 15:09:40 -0400 | [diff] [blame] | 84 | def 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'Connor | efd70a5 | 2015-10-13 15:44:25 -0400 | [diff] [blame] | 89 | opts.add_option("-t", "--tools", dest="tools", default="", |
Kevin O'Connor | 2342101 | 2015-10-21 20:35:50 -0400 | [diff] [blame] | 90 | help="list of build programs to extract version from") |
Kevin O'Connor | a6c8774 | 2015-10-13 15:09:40 -0400 | [diff] [blame] | 91 | |
| 92 | options, args = opts.parse_args() |
| 93 | if len(args) != 1: |
| 94 | opts.error("Incorrect arguments") |
| 95 | outfile = args[0] |
| 96 | |
Kevin O'Connor | efd70a5 | 2015-10-13 15:44:25 -0400 | [diff] [blame] | 97 | cleanbuild, toolstr = tool_versions(options.tools) |
| 98 | |
Kevin O'Connor | a6c8774 | 2015-10-13 15:09:40 -0400 | [diff] [blame] | 99 | ver = git_version() |
Kevin O'Connor | 98a100c | 2015-10-22 11:59:47 -0400 | [diff] [blame] | 100 | cleanbuild = cleanbuild and 'dirty' not in ver |
Kevin O'Connor | a6c8774 | 2015-10-13 15:09:40 -0400 | [diff] [blame] | 101 | if not ver: |
| 102 | ver = file_version() |
Kevin O'Connor | 98a100c | 2015-10-22 11:59:47 -0400 | [diff] [blame] | 103 | # 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'Connor | a6c8774 | 2015-10-13 15:09:40 -0400 | [diff] [blame] | 108 | if not ver: |
| 109 | ver = "?" |
Kevin O'Connor | a1b4dd0 | 2015-10-13 15:49:03 -0400 | [diff] [blame] | 110 | 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'Connor | a6c8774 | 2015-10-13 15:09:40 -0400 | [diff] [blame] | 115 | |
| 116 | if __name__ == '__main__': |
| 117 | main() |