blob: 708d67df7f98597d88009722f25219abc90347fa [file] [log] [blame]
Damien George26b512e2015-05-30 23:11:16 +01001"""
2Generate header file with macros defining MicroPython version info.
3
4This script works with Python 2.6, 2.7, 3.3 and 3.4.
5"""
Damien George95f53462015-04-22 17:38:05 +01006
7from __future__ import print_function
8
9import sys
10import os
11import datetime
12import subprocess
13
Damien George1a97f672015-05-25 13:26:47 +010014def get_version_info_from_git():
Damien George26b512e2015-05-30 23:11:16 +010015 # Python 2.6 doesn't have check_output, so check for that
16 try:
17 subprocess.check_output
18 subprocess.check_call
19 except AttributeError:
20 return None
21
Damien George95f53462015-04-22 17:38:05 +010022 # Note: git describe doesn't work if no tag is available
23 try:
24 git_tag = subprocess.check_output(["git", "describe", "--dirty", "--always"], universal_newlines=True).strip()
25 except subprocess.CalledProcessError:
26 git_tag = ""
Damien George1a97f672015-05-25 13:26:47 +010027 except OSError:
28 return None
Damien George95f53462015-04-22 17:38:05 +010029 try:
30 git_hash = subprocess.check_output(["git", "rev-parse", "--short", "HEAD"], stderr=subprocess.STDOUT, universal_newlines=True).strip()
31 except subprocess.CalledProcessError:
32 git_hash = "unknown"
Damien George1a97f672015-05-25 13:26:47 +010033 except OSError:
34 return None
Damien George95f53462015-04-22 17:38:05 +010035
36 try:
37 # Check if there are any modified files.
38 subprocess.check_call(["git", "diff", "--no-ext-diff", "--quiet", "--exit-code"], stderr=subprocess.STDOUT)
39 # Check if there are any staged files.
40 subprocess.check_call(["git", "diff-index", "--cached", "--quiet", "HEAD", "--"], stderr=subprocess.STDOUT)
41 except subprocess.CalledProcessError:
42 git_hash += "-dirty"
Damien George1a97f672015-05-25 13:26:47 +010043 except OSError:
44 return None
Damien George95f53462015-04-22 17:38:05 +010045
46 # Try to extract MicroPython version from git tag
47 if git_tag.startswith("v"):
48 ver = git_tag[1:].split("-")[0].split(".")
49 if len(ver) == 2:
50 ver.append("0")
51 else:
52 ver = ["0", "0", "1"]
53
Damien George1a97f672015-05-25 13:26:47 +010054 return git_tag, git_hash, ver
55
56def get_version_info_from_docs_conf():
57 with open("%s/docs/conf.py" % sys.argv[0].rsplit("/", 2)[0]) as f:
58 for line in f:
59 if line.startswith("release = '"):
60 ver = line.strip()[10:].strip("'")
61 git_tag = "v" + ver
62 ver = ver.split(".")
63 if len(ver) == 2:
64 ver.append("0")
65 return git_tag, "<no hash>", ver
66 return None
67
68def make_version_header(filename):
69 # Get version info using git, with fallback to docs/conf.py
70 info = get_version_info_from_git()
71 if info is None:
72 info = get_version_info_from_docs_conf()
73
74 git_tag, git_hash, ver = info
75
Damien George95f53462015-04-22 17:38:05 +010076 # Generate the file with the git and version info
77 file_data = """\
78// This file was generated by py/makeversionhdr.py
79#define MICROPY_GIT_TAG "%s"
80#define MICROPY_GIT_HASH "%s"
81#define MICROPY_BUILD_DATE "%s"
82#define MICROPY_VERSION_MAJOR (%s)
83#define MICROPY_VERSION_MINOR (%s)
84#define MICROPY_VERSION_MICRO (%s)
85#define MICROPY_VERSION_STRING "%s.%s.%s"
86""" % (git_tag, git_hash, datetime.date.today().strftime("%Y-%m-%d"),
87 ver[0], ver[1], ver[2], ver[0], ver[1], ver[2])
88
89 # Check if the file contents changed from last time
90 write_file = True
91 if os.path.isfile(filename):
92 with open(filename, 'r') as f:
93 existing_data = f.read()
94 if existing_data == file_data:
95 write_file = False
96
97 # Only write the file if we need to
98 if write_file:
99 print("Generating %s" % filename)
100 with open(filename, 'w') as f:
101 f.write(file_data)
102
Damien George26b512e2015-05-30 23:11:16 +0100103if __name__ == "__main__":
104 make_version_header(sys.argv[1])