diff options
author | stijn <stijn@ignitron.net> | 2020-10-08 16:40:17 +0200 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2020-10-29 15:27:30 +1100 |
commit | 8e94fa0d2eb94483f387b1ae2e081d1998575a7f (patch) | |
tree | 7ea3a13ffdd65378868396ffc6307d94cc9cc0e9 /py/makeqstrdefs.py | |
parent | f1666419a8aaee846f7175ccdb8799ab9deea376 (diff) | |
download | micropython-8e94fa0d2eb94483f387b1ae2e081d1998575a7f.tar.gz micropython-8e94fa0d2eb94483f387b1ae2e081d1998575a7f.zip |
py/makeqstrdefs.py: Support preprocessing C++ files for QSTR generation.
When SCR_QSTR contains C++ files they should be preprocessed with the same
compiler flags (CXXFLAGS) as they will be compiled with, to make sure code
scanned for QSTR occurrences is effectively the code used in the rest of
the build. The 'split SCR_QSTR in .c and .cpp files and process each with
different flags' logic isn't trivial to express in a Makefile and the
existing principle for deciding which files to preprocess was already
rather complicated, so the actual preprocessing is moved into
makeqstrdefs.py completely.
Diffstat (limited to 'py/makeqstrdefs.py')
-rw-r--r-- | py/makeqstrdefs.py | 59 |
1 files changed, 58 insertions, 1 deletions
diff --git a/py/makeqstrdefs.py b/py/makeqstrdefs.py index 1db00d9633..f514ae0c10 100644 --- a/py/makeqstrdefs.py +++ b/py/makeqstrdefs.py @@ -8,6 +8,7 @@ This script works with Python 2.6, 2.7, 3.3 and 3.4. from __future__ import print_function import re +import subprocess import sys import io import os @@ -20,6 +21,31 @@ _MODE_QSTR = "qstr" _MODE_COMPRESS = "compress" +def preprocess(): + if any(src in args.dependencies for src in args.changed_sources): + sources = args.sources + elif any(args.changed_sources): + sources = args.changed_sources + else: + sources = args.sources + csources = [] + cxxsources = [] + for source in sources: + if source.endswith(".cpp"): + cxxsources.append(source) + else: + csources.append(source) + try: + os.makedirs(os.path.dirname(args.output[0])) + except OSError: + pass + with open(args.output[0], "w") as out_file: + if csources: + subprocess.check_call(args.pp + args.cflags + csources, stdout=out_file) + if cxxsources: + subprocess.check_call(args.pp + args.cxxflags + cxxsources, stdout=out_file) + + def write_out(fname, output): if output: for m, r in [("/", "__"), ("\\", "__"), (":", "@"), ("..", "@@")]: @@ -105,7 +131,7 @@ def cat_together(): if __name__ == "__main__": - if len(sys.argv) != 6: + if len(sys.argv) < 6: print("usage: %s command mode input_filename output_dir output_file" % sys.argv[0]) sys.exit(2) @@ -114,6 +140,37 @@ if __name__ == "__main__": args = Args() args.command = sys.argv[1] + + if args.command == "pp": + named_args = { + s: [] + for s in [ + "pp", + "output", + "cflags", + "cxxflags", + "sources", + "changed_sources", + "dependencies", + ] + } + + for arg in sys.argv[1:]: + if arg in named_args: + current_tok = arg + else: + named_args[current_tok].append(arg) + + if not named_args["pp"] or len(named_args["output"]) != 1: + print("usage: %s %s ..." % (sys.argv[0], " ... ".join(named_args))) + sys.exit(2) + + for k, v in named_args.items(): + setattr(args, k, v) + + preprocess() + sys.exit(0) + args.mode = sys.argv[2] args.input_filename = sys.argv[3] # Unused for command=cat args.output_dir = sys.argv[4] |