angstrom: prefer the git version of tslib
[openembedded.git] / classes / gitver.bbclass
blobee8323d6f43520a87fab5dc65881176754f0f946
1 # Copyright (C) 2009 Chris Larson <clarson@kergoth.com>
2 # Released under the MIT license (see COPYING.MIT for the terms)
4 # gitver.bbclass provides a GITVER variable which is a (fairly) sane version,
5 # for use in ${PV}, extracted from the ${S} git checkout, assuming it is one.
6 # This is most useful in concert with srctree.bbclass.
8 def git_drop_tag_prefix(version):
9     import re
10     if re.match("v\d", version):
11         return version[1:]
12     else:
13         return version
15 GIT_TAGADJUST = "git_drop_tag_prefix(version)"
16 GITVER = "${@get_git_pv('${S}', d, tagadjust=lambda version:${GIT_TAGADJUST})}"
17 GITSHA = "${@get_git_hash('${S}', d)}"
19 def get_git_hash(path, d):
20     return oe_run(d, ["git", "rev-parse", "--short", "HEAD"], cwd=path).rstrip()
22 def get_git_pv(path, d, tagadjust=None):
23     import os
24     import oe.process
26     gitdir = os.path.abspath(os.path.join(d.getVar("S", True), ".git"))
27     def git(cmd):
28         try:
29             return oe_run(d, ["git"] + cmd, cwd=gitdir).rstrip()
30         except oe.process.CmdError, exc:
31             bb.fatal(str(exc))
33     try:
34         ver = oe_run(d, ["git", "describe", "--tags"], cwd=gitdir).rstrip()
35     except Exception, exc:
36         bb.fatal(str(exc))
38     if not ver:
39         try:
40             ver = get_git_hash(gitdir, d)
41         except Exception, exc:
42             bb.fatal(str(exc))
44         if ver:
45             return "0.0+%s" % ver
46         else:
47             return "0.0"
48     else:
49         if tagadjust:
50             ver = tagadjust(ver)
51         return ver
53 def mark_recipe_dependencies(path, d):
54     from bb.parse import mark_dependency
56     gitdir = os.path.join(path, ".git")
58     # Force the recipe to be reparsed so the version gets bumped
59     # if the active branch is switched, or if the branch changes.
60     mark_dependency(d, os.path.join(gitdir, "HEAD"))
62     # Force a reparse if anything in the index changes.
63     mark_dependency(d, os.path.join(gitdir, "index"))
65     try:
66         ref = oe_run(d, ["git", "symbolic-ref", "-q", "HEAD"], cwd=gitdir).rstrip()
67     except oe.process.CmdError:
68         pass
69     else:
70         if ref:
71             mark_dependency(d, os.path.join(gitdir, ref))
73     # Catch new tags.
74     tagdir = os.path.join(gitdir, "refs", "tags")
75     if os.path.exists(tagdir):
76         mark_dependency(d, tagdir)
78 python () {
79     mark_recipe_dependencies(d.getVar("S", True), d)