From a925cb54f1b3490a9e082816eb785750c4b25324 Mon Sep 17 00:00:00 2001
From: Paul Sokolovsky <pfalcon@users.sourceforge.net>
Date: Fri, 11 Apr 2014 20:50:15 +0300
Subject: [PATCH] py: Preprocess qstrdefs.h before feeding to makeqstrdata.py.

This is alternative implementation of supporting conditionals in qstrdefs.h,
hard to say if it's much cleaner than munging #ifdef's in Python code...
---
 py/makeqstrdata.py | 9 +++++++++
 py/py.mk           | 5 +++--
 py/qstrdefs.h      | 2 +-
 3 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/py/makeqstrdata.py b/py/makeqstrdata.py
index 741336571..81b003545 100644
--- a/py/makeqstrdata.py
+++ b/py/makeqstrdata.py
@@ -26,6 +26,7 @@ def compute_hash(qstr):
 def do_work(infiles):
     # read the qstrs in from the input files
     qstrs = {}
+    cpp_header_blocks = 3
     for infile in infiles:
         with open(infile, 'rt') as f:
             line_number = 0
@@ -37,6 +38,14 @@ def do_work(infiles):
                 if len(line) == 0 or line.startswith('//'):
                     continue
 
+                # We'll have 3 line-number lines for py/qstrdefs.h - initial, leaving it to
+                # go into other headers, and returning to it.
+                if line.startswith('# ') and 'py/qstrdefs.h' in line:
+                    cpp_header_blocks -= 1
+                    continue
+                if cpp_header_blocks != 0:
+                    continue
+
                 # verify line is of the correct form
                 match = re.match(r'Q\((.+)\)$', line)
                 if not match:
diff --git a/py/py.mk b/py/py.mk
index ecc4a6a0d..23ba9ebe7 100644
--- a/py/py.mk
+++ b/py/py.mk
@@ -104,9 +104,10 @@ $(PY_BUILD)/py-version.h: FORCE
 # Adding an order only dependency on $(PY_BUILD) causes $(PY_BUILD) to get
 # created before we run the script to generate the .h
 $(PY_BUILD)/qstrdefs.generated.h: | $(PY_BUILD)/
-$(PY_BUILD)/qstrdefs.generated.h: $(PY_QSTR_DEFS) $(QSTR_DEFS) $(PY_SRC)/makeqstrdata.py
+$(PY_BUILD)/qstrdefs.generated.h: $(PY_QSTR_DEFS) $(QSTR_DEFS) $(PY_SRC)/makeqstrdata.py mpconfigport.h $(PY_SRC)/mpconfig.h
 	$(ECHO) "makeqstrdata $(PY_QSTR_DEFS) $(QSTR_DEFS)"
-	$(Q)$(PYTHON) $(PY_SRC)/makeqstrdata.py $(PY_QSTR_DEFS) $(QSTR_DEFS) > $@
+	$(CPP) $(CFLAGS) $(PY_QSTR_DEFS) -o $(PY_BUILD)/qstrdefs.preprocessed.h
+	$(Q)$(PYTHON) $(PY_SRC)/makeqstrdata.py $(PY_BUILD)/qstrdefs.preprocessed.h $(QSTR_DEFS) > $@
 
 # We don't know which source files actually need the generated.h (since
 # it is #included from str.h). The compiler generated dependencies will cause
diff --git a/py/qstrdefs.h b/py/qstrdefs.h
index 17948434b..eb72fd265 100644
--- a/py/qstrdefs.h
+++ b/py/qstrdefs.h
@@ -1,6 +1,6 @@
+#include "mpconfig.h"
 // All the qstr definitions in this file are available as constants.
 // That is, they are in ROM and you can reference them simply as MP_QSTR_xxxx.
-// TODO make it so we can use #defines here to select only those words that will be used
 
 Q(__build_class__)
 Q(__class__)
-- 
GitLab