|
| 1 | +#!/usr/bin/env python |
| 2 | +######################################################################## |
| 3 | +## |
| 4 | +## Copyright 2014 PMC-Sierra, Inc. |
| 5 | +## |
| 6 | +## Licensed under the Apache License, Version 2.0 (the "License"); you |
| 7 | +## may not use this file except in compliance with the License. You may |
| 8 | +## obtain a copy of the License at |
| 9 | +## http://www.apache.org/licenses/LICENSE-2.0 Unless required by |
| 10 | +## applicable law or agreed to in writing, software distributed under the |
| 11 | +## License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | +## CONDITIONS OF ANY KIND, either express or implied. See the License for |
| 13 | +## the specific language governing permissions and limitations under the |
| 14 | +## License. |
| 15 | +## |
| 16 | +######################################################################## |
| 17 | + |
| 18 | +######################################################################## |
| 19 | +## |
| 20 | +## Author: Logan Gunthorpe |
| 21 | +## |
| 22 | +## Date: Oct 23, 2014 |
| 23 | +## |
| 24 | +## Description: |
| 25 | +## Obtain a version string from git information |
| 26 | +## |
| 27 | +######################################################################## |
| 28 | + |
| 29 | +from __future__ import print_function |
| 30 | + |
| 31 | +import os |
| 32 | +import subprocess as sp |
| 33 | + |
| 34 | +try: |
| 35 | + from waflib.Task import Task |
| 36 | +except: |
| 37 | + Task = object |
| 38 | + |
| 39 | +template = """ |
| 40 | +#ifndef VERSION_H |
| 41 | +#define VERSION_H |
| 42 | +
|
| 43 | +#define VERSION "%s" |
| 44 | +
|
| 45 | +#endif |
| 46 | +""" |
| 47 | + |
| 48 | +def options(opt): |
| 49 | + pass |
| 50 | + |
| 51 | +def configure(conf): |
| 52 | + conf.find_program(["git"], var='GIT', mandatory=False) |
| 53 | + conf.env.append_unique("INCLUDES", ".") |
| 54 | + |
| 55 | +def get_git_version(git="git"): |
| 56 | + if hasattr(get_git_version, "cached"): |
| 57 | + return get_git_version.cached |
| 58 | + |
| 59 | + p = sp.Popen([git, "describe", "--always"], stdout=sp.PIPE, |
| 60 | + stderr=open(os.devnull, "w")) |
| 61 | + version = p.communicate()[0].strip() |
| 62 | + |
| 63 | + if p.wait(): |
| 64 | + return "exported" |
| 65 | + |
| 66 | + status = sp.Popen([git, "status", "--porcelain", "-uno"], |
| 67 | + stdout=sp.PIPE, stderr=open(os.devnull, "w")).communicate()[0] |
| 68 | + |
| 69 | + max_length = get_git_version.max_length |
| 70 | + if max_length: |
| 71 | + version=version[:max_length] |
| 72 | + |
| 73 | + if status.strip(): |
| 74 | + if max_length: |
| 75 | + version=version[:max_length-1] |
| 76 | + version += "M" |
| 77 | + |
| 78 | + get_git_version.cached = version |
| 79 | + |
| 80 | + return version |
| 81 | + |
| 82 | +get_git_version.max_length = 24 |
| 83 | + |
| 84 | +class VersionHeader(Task): |
| 85 | + color = "PINK" |
| 86 | + |
| 87 | + def __init__(self, *k, **kw): |
| 88 | + Task.__init__(self, *k, **kw) |
| 89 | + |
| 90 | + if "target" in kw: |
| 91 | + self.set_outputs(kw["target"]) |
| 92 | + |
| 93 | + def run(self): |
| 94 | + rev = self.signature() |
| 95 | + rev = rev.strip() |
| 96 | + |
| 97 | + for o in self.outputs: |
| 98 | + f = open(o.abspath(), "w") |
| 99 | + print(template % (rev), file=f) |
| 100 | + f.close() |
| 101 | + |
| 102 | + def signature(self): |
| 103 | + try: return self.cache_sig |
| 104 | + except AttributeError: pass |
| 105 | + |
| 106 | + if self.env.GIT: |
| 107 | + self.cache_sig = get_git_version(self.env.GIT[0]) |
| 108 | + else: |
| 109 | + self.cache_sig = "unknown" |
| 110 | + |
| 111 | + return self.cache_sig |
| 112 | + |
| 113 | +def build(ctx): |
| 114 | + tsk = VersionHeader(target=ctx.path.find_or_declare("version.h"), |
| 115 | + env=ctx.env) |
| 116 | + ctx.add_to_group(tsk) |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + try: |
| 120 | + print(get_git_version()) |
| 121 | + except sp.CalledProcessError: |
| 122 | + print("unknown") |
0 commit comments