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 | 2342101 | 2015-10-21 20:35:50 -0400 | [diff] [blame^] | 7 | import sys, os, subprocess, 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 | |
| 15 | # Obtain version info from "git" program |
| 16 | def git_version(): |
| 17 | if not os.path.exists('.git'): |
| 18 | return "" |
| 19 | params = "git describe --tags --long --dirty".split() |
| 20 | try: |
| 21 | ver = subprocess.check_output(params).decode().strip() |
| 22 | except: |
| 23 | return "" |
| 24 | return ver |
| 25 | |
| 26 | # Look for version in a ".version" file |
| 27 | def file_version(): |
| 28 | if not os.path.isfile('.version'): |
| 29 | return "" |
| 30 | try: |
| 31 | f = open('.version', 'r') |
| 32 | ver = f.readline().strip() |
| 33 | f.close() |
| 34 | except: |
| 35 | return "" |
| 36 | return ver |
| 37 | |
| 38 | # Generate an output file with the version information |
Kevin O'Connor | efd70a5 | 2015-10-13 15:44:25 -0400 | [diff] [blame] | 39 | def write_version(outfile, version, toolstr): |
Kevin O'Connor | a6c8774 | 2015-10-13 15:09:40 -0400 | [diff] [blame] | 40 | sys.stdout.write("Version: %s\n" % (version,)) |
| 41 | f = open(outfile, 'w') |
Kevin O'Connor | efd70a5 | 2015-10-13 15:44:25 -0400 | [diff] [blame] | 42 | f.write(VERSION_FORMAT % (version, toolstr)) |
Kevin O'Connor | a6c8774 | 2015-10-13 15:09:40 -0400 | [diff] [blame] | 43 | f.close() |
| 44 | |
Kevin O'Connor | efd70a5 | 2015-10-13 15:44:25 -0400 | [diff] [blame] | 45 | # Run "tool --version" for each specified tool and extract versions |
| 46 | def tool_versions(tools): |
| 47 | tools = [t.strip() for t in tools.split(';')] |
Kevin O'Connor | 2342101 | 2015-10-21 20:35:50 -0400 | [diff] [blame^] | 48 | versions = ['', ''] |
Kevin O'Connor | efd70a5 | 2015-10-13 15:44:25 -0400 | [diff] [blame] | 49 | success = 0 |
| 50 | for tool in tools: |
Kevin O'Connor | 2342101 | 2015-10-21 20:35:50 -0400 | [diff] [blame^] | 51 | # Extract first line from "tool --version" output |
Kevin O'Connor | efd70a5 | 2015-10-13 15:44:25 -0400 | [diff] [blame] | 52 | try: |
| 53 | ver = subprocess.check_output([tool, '--version']).decode() |
| 54 | except: |
| 55 | continue |
Kevin O'Connor | 2342101 | 2015-10-21 20:35:50 -0400 | [diff] [blame^] | 56 | verstr = ver.split('\n')[0] |
| 57 | # Check if this tool looks like a binutils program |
| 58 | isbinutils = 0 |
| 59 | if verstr.startswith('GNU '): |
| 60 | isbinutils = 1 |
| 61 | verstr = verstr[4:] |
| 62 | # Extract version information and exclude program name |
| 63 | if ' ' not in verstr: |
Kevin O'Connor | efd70a5 | 2015-10-13 15:44:25 -0400 | [diff] [blame] | 64 | continue |
Kevin O'Connor | 2342101 | 2015-10-21 20:35:50 -0400 | [diff] [blame^] | 65 | prog, ver = verstr.split(' ', 1) |
| 66 | if not prog or not ver: |
| 67 | continue |
| 68 | # Check for any version conflicts |
| 69 | if versions[isbinutils] and versions[isbinutils] != ver: |
| 70 | vers[isbinutils] = "mixed" |
| 71 | continue |
| 72 | versions[isbinutils] = ver |
| 73 | success += 1 |
| 74 | cleanbuild = versions[0] and versions[1] and success == len(tools) |
| 75 | return cleanbuild, "gcc: %s binutils: %s" % (versions[0], versions[1]) |
Kevin O'Connor | efd70a5 | 2015-10-13 15:44:25 -0400 | [diff] [blame] | 76 | |
Kevin O'Connor | a6c8774 | 2015-10-13 15:09:40 -0400 | [diff] [blame] | 77 | def main(): |
| 78 | usage = "%prog [options] <outputheader.h>" |
| 79 | opts = optparse.OptionParser(usage) |
| 80 | opts.add_option("-e", "--extra", dest="extra", default="", |
| 81 | help="extra version string to append to version") |
Kevin O'Connor | efd70a5 | 2015-10-13 15:44:25 -0400 | [diff] [blame] | 82 | opts.add_option("-t", "--tools", dest="tools", default="", |
Kevin O'Connor | 2342101 | 2015-10-21 20:35:50 -0400 | [diff] [blame^] | 83 | help="list of build programs to extract version from") |
Kevin O'Connor | a6c8774 | 2015-10-13 15:09:40 -0400 | [diff] [blame] | 84 | |
| 85 | options, args = opts.parse_args() |
| 86 | if len(args) != 1: |
| 87 | opts.error("Incorrect arguments") |
| 88 | outfile = args[0] |
| 89 | |
Kevin O'Connor | efd70a5 | 2015-10-13 15:44:25 -0400 | [diff] [blame] | 90 | cleanbuild, toolstr = tool_versions(options.tools) |
| 91 | |
Kevin O'Connor | a6c8774 | 2015-10-13 15:09:40 -0400 | [diff] [blame] | 92 | ver = git_version() |
Kevin O'Connor | a1b4dd0 | 2015-10-13 15:49:03 -0400 | [diff] [blame] | 93 | cleanbuild = cleanbuild and ver and 'dirty' not in ver |
Kevin O'Connor | a6c8774 | 2015-10-13 15:09:40 -0400 | [diff] [blame] | 94 | if not ver: |
| 95 | ver = file_version() |
| 96 | if not ver: |
| 97 | ver = "?" |
Kevin O'Connor | a1b4dd0 | 2015-10-13 15:49:03 -0400 | [diff] [blame] | 98 | if not cleanbuild: |
| 99 | btime = time.strftime("%Y%m%d_%H%M%S") |
| 100 | hostname = socket.gethostname() |
| 101 | ver = "%s-%s-%s" % (ver, btime, hostname) |
| 102 | write_version(outfile, ver + options.extra, toolstr) |
Kevin O'Connor | a6c8774 | 2015-10-13 15:09:40 -0400 | [diff] [blame] | 103 | |
| 104 | if __name__ == '__main__': |
| 105 | main() |