blob: e95e0c6076ee1c7c897d8bf27b45df0263de2d16 [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:
Damien George0d5d1602015-08-11 12:27:38 +010024 git_tag = subprocess.check_output(["git", "describe", "--dirty", "--always"], stderr=subprocess.STDOUT, universal_newlines=True).strip()
25 except subprocess.CalledProcessError as er:
Damien George94ef8872015-08-12 23:28:16 +010026 if er.returncode == 128:
Damien George0d5d1602015-08-11 12:27:38 +010027 # git exit code of 128 means no repository found
28 return None
Damien George95f53462015-04-22 17:38:05 +010029 git_tag = ""
Damien George1a97f672015-05-25 13:26:47 +010030 except OSError:
31 return None
Damien George95f53462015-04-22 17:38:05 +010032 try:
33 git_hash = subprocess.check_output(["git", "rev-parse", "--short", "HEAD"], stderr=subprocess.STDOUT, universal_newlines=True).strip()
34 except subprocess.CalledProcessError:
35 git_hash = "unknown"
Damien George1a97f672015-05-25 13:26:47 +010036 except OSError:
37 return None
Damien George95f53462015-04-22 17:38:05 +010038
39 try:
40 # Check if there are any modified files.
41 subprocess.check_call(["git", "diff", "--no-ext-diff", "--quiet", "--exit-code"], stderr=subprocess.STDOUT)
42 # Check if there are any staged files.
43 subprocess.check_call(["git", "diff-index", "--cached", "--quiet", "HEAD", "--"], stderr=subprocess.STDOUT)
44 except subprocess.CalledProcessError:
45 git_hash += "-dirty"
Damien George1a97f672015-05-25 13:26:47 +010046 except OSError:
47 return None
Damien George95f53462015-04-22 17:38:05 +010048
49 # Try to extract MicroPython version from git tag
50 if git_tag.startswith("v"):
51 ver = git_tag[1:].split("-")[0].split(".")
52 if len(ver) == 2:
53 ver.append("0")
54 else:
55 ver = ["0", "0", "1"]
56
Damien George1a97f672015-05-25 13:26:47 +010057 return git_tag, git_hash, ver
58
59def get_version_info_from_docs_conf():
60 with open("%s/docs/conf.py" % sys.argv[0].rsplit("/", 2)[0]) as f:
61 for line in f:
62 if line.startswith("release = '"):
63 ver = line.strip()[10:].strip("'")
64 git_tag = "v" + ver
65 ver = ver.split(".")
66 if len(ver) == 2:
67 ver.append("0")
68 return git_tag, "<no hash>", ver
69 return None
70
71def make_version_header(filename):
72 # Get version info using git, with fallback to docs/conf.py
73 info = get_version_info_from_git()
74 if info is None:
75 info = get_version_info_from_docs_conf()
76
77 git_tag, git_hash, ver = info
78
Damien George95f53462015-04-22 17:38:05 +010079 # Generate the file with the git and version info
80 file_data = """\
81// This file was generated by py/makeversionhdr.py
82#define MICROPY_GIT_TAG "%s"
83#define MICROPY_GIT_HASH "%s"
84#define MICROPY_BUILD_DATE "%s"
85#define MICROPY_VERSION_MAJOR (%s)
86#define MICROPY_VERSION_MINOR (%s)
87#define MICROPY_VERSION_MICRO (%s)
88#define MICROPY_VERSION_STRING "%s.%s.%s"
89""" % (git_tag, git_hash, datetime.date.today().strftime("%Y-%m-%d"),
90 ver[0], ver[1], ver[2], ver[0], ver[1], ver[2])
91
92 # Check if the file contents changed from last time
93 write_file = True
94 if os.path.isfile(filename):
95 with open(filename, 'r') as f:
96 existing_data = f.read()
97 if existing_data == file_data:
98 write_file = False
99
100 # Only write the file if we need to
101 if write_file:
102 print("Generating %s" % filename)
103 with open(filename, 'w') as f:
104 f.write(file_data)
105
Damien George26b512e2015-05-30 23:11:16 +0100106if __name__ == "__main__":
107 make_version_header(sys.argv[1])