Skip to content

Commit 95f5346

Browse files
committed
py: Replace py-version.sh with makeversionhdr.py, written in Python.
Also rename py-version.h to mpversion.h for consistency with mpconfig.h.
1 parent d11317b commit 95f5346

13 files changed

Lines changed: 82 additions & 95 deletions

File tree

cc3200/bootmgr/bootloader.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,6 @@ $(BUILD)/bootloader.bin: $(BUILD)/bootmgr.bin
128128
$(HEADER_BUILD)/qstrdefs.generated.h: | $(HEADER_BUILD)
129129
touch $@
130130

131-
# Create an empty "py-version.h" needed by py/mkrules.mk
132-
$(HEADER_BUILD)/py-version.h: | $(HEADER_BUILD)
131+
# Create an empty "mpversion.h" needed by py/mkrules.mk
132+
$(HEADER_BUILD)/mpversion.h: | $(HEADER_BUILD)
133133
touch $@

cc3200/mods/modpyb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
#include "utils.h"
6666
#include "gccollect.h"
6767
#include "mperror.h"
68-
#include "genhdr/py-version.h"
68+
#include "genhdr/mpversion.h"
6969

7070

7171
#ifdef DEBUG

cc3200/mods/moduos.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include "py/obj.h"
3434
#include "py/objtuple.h"
3535
#include "py/objstr.h"
36-
#include "genhdr/py-version.h"
36+
#include "genhdr/mpversion.h"
3737
#include "ff.h"
3838
#include "diskio.h"
3939
#include "sflash_diskio.h"

cc3200/telnet/telnet.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#include "debug.h"
3636
#include "mpexception.h"
3737
#include "serverstask.h"
38-
#include "genhdr/py-version.h"
38+
#include "genhdr/mpversion.h"
3939

4040
/******************************************************************************
4141
DEFINE PRIVATE CONSTANTS

py/makeversionhdr.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# This script works with Python 2 and 3
2+
3+
from __future__ import print_function
4+
5+
import sys
6+
import os
7+
import datetime
8+
import subprocess
9+
10+
def make_version_header(filename):
11+
# Note: git describe doesn't work if no tag is available
12+
try:
13+
git_tag = subprocess.check_output(["git", "describe", "--dirty", "--always"], universal_newlines=True).strip()
14+
except subprocess.CalledProcessError:
15+
git_tag = ""
16+
try:
17+
git_hash = subprocess.check_output(["git", "rev-parse", "--short", "HEAD"], stderr=subprocess.STDOUT, universal_newlines=True).strip()
18+
except subprocess.CalledProcessError:
19+
git_hash = "unknown"
20+
21+
try:
22+
# Check if there are any modified files.
23+
subprocess.check_call(["git", "diff", "--no-ext-diff", "--quiet", "--exit-code"], stderr=subprocess.STDOUT)
24+
# Check if there are any staged files.
25+
subprocess.check_call(["git", "diff-index", "--cached", "--quiet", "HEAD", "--"], stderr=subprocess.STDOUT)
26+
except subprocess.CalledProcessError:
27+
git_hash += "-dirty"
28+
29+
# Try to extract MicroPython version from git tag
30+
if git_tag.startswith("v"):
31+
ver = git_tag[1:].split("-")[0].split(".")
32+
if len(ver) == 2:
33+
ver.append("0")
34+
else:
35+
ver = ["0", "0", "1"]
36+
37+
# Generate the file with the git and version info
38+
file_data = """\
39+
// This file was generated by py/makeversionhdr.py
40+
#define MICROPY_GIT_TAG "%s"
41+
#define MICROPY_GIT_HASH "%s"
42+
#define MICROPY_BUILD_DATE "%s"
43+
#define MICROPY_VERSION_MAJOR (%s)
44+
#define MICROPY_VERSION_MINOR (%s)
45+
#define MICROPY_VERSION_MICRO (%s)
46+
#define MICROPY_VERSION_STRING "%s.%s.%s"
47+
""" % (git_tag, git_hash, datetime.date.today().strftime("%Y-%m-%d"),
48+
ver[0], ver[1], ver[2], ver[0], ver[1], ver[2])
49+
50+
# Check if the file contents changed from last time
51+
write_file = True
52+
if os.path.isfile(filename):
53+
with open(filename, 'r') as f:
54+
existing_data = f.read()
55+
if existing_data == file_data:
56+
write_file = False
57+
58+
# Only write the file if we need to
59+
if write_file:
60+
print("Generating %s" % filename)
61+
with open(filename, 'w') as f:
62+
f.write(file_data)
63+
64+
make_version_header(sys.argv[1])

py/mkrules.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ $(BUILD)/%.pp: %.c
5858
# the right .o's to get recompiled if the generated.h file changes. Adding
5959
# an order-only dependendency to all of the .o's will cause the generated .h
6060
# to get built before we try to compile any of them.
61-
$(OBJ): | $(HEADER_BUILD)/qstrdefs.generated.h $(HEADER_BUILD)/py-version.h
61+
$(OBJ): | $(HEADER_BUILD)/qstrdefs.generated.h $(HEADER_BUILD)/mpversion.h
6262

6363
# $(sort $(var)) removes duplicates
6464
#

py/modsys.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
#if MICROPY_PY_SYS
3737

38-
#include "genhdr/py-version.h"
38+
#include "genhdr/mpversion.h"
3939

4040
/// \module sys - system specific functions
4141

py/py-version.sh

Lines changed: 0 additions & 39 deletions
This file was deleted.

py/py.mk

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,8 @@ PY_O = $(addprefix $(PY_BUILD)/, $(PY_O_BASENAME))
126126
FORCE:
127127
.PHONY: FORCE
128128

129-
$(HEADER_BUILD)/py-version.h: FORCE | $(HEADER_BUILD)
130-
$(Q)$(PY_SRC)/py-version.sh > $@.tmp
131-
$(Q)if [ -f "$@" ] && cmp -s $@ $@.tmp; then rm $@.tmp; else echo "Generating $@"; mv $@.tmp $@; fi
129+
$(HEADER_BUILD)/mpversion.h: FORCE | $(HEADER_BUILD)
130+
$(Q)$(PYTHON) $(PY_SRC)/makeversionhdr.py $@
132131

133132
# qstr data
134133

stmhal/moduos.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "py/obj.h"
3232
#include "py/objtuple.h"
3333
#include "py/objstr.h"
34-
#include "genhdr/py-version.h"
34+
#include "genhdr/mpversion.h"
3535
#include "lib/fatfs/ff.h"
3636
#include "lib/fatfs/diskio.h"
3737
#include "rng.h"

0 commit comments

Comments
 (0)