I see two approaches for achieving this:dhylands wrote:The issue I see with generating qstrdefs automtically is figuring out what #ifdefs they belong in. We don't want to include unnecessary qstrs in the image.
a) collect all qstrdefs and remember set of #if conditionals that need to be met in order to emit this definition. Obviously this needs a lot of postprocessing in order to generate a proper qstrdefs.h file.
b) just process the source files, throw away everything else except #if .. #endif blocks and convert MP_QSTR_foo usage to definition of Q(foo). Throw away unnecessary blocks (not containing any Q() lines). We also leverage the fact that Q() can be called multiple times (if this produces unwanted results, we should fix them, but I haven't found any).
I created a proof-of-concepts for both approaches here:
a) https://gist.github.com/prusnak/44aec7557e0ffc9d505b
b) https://gist.github.com/prusnak/8390fe090f0f1a43f357
Output of script a) for py/objstr.c:
Code: Select all
('Q(keepends)', ['(MICROPY_PY_BUILTINS_STR_SPLITLINES)'])
('Q(_backslash_n)', ['(MICROPY_PY_BUILTINS_STR_SPLITLINES)'])
('Q(utf_hyphen_8)', ['(MICROPY_CPYTHON_COMPAT)'])
('Q(utf_hyphen_8)', ['(MICROPY_CPYTHON_COMPAT)'])
('Q(decode)', ['(MICROPY_CPYTHON_COMPAT)'])
('Q(encode)', ['(MICROPY_CPYTHON_COMPAT)', '(!MICROPY_PY_BUILTINS_STR_UNICODE)'])
('Q(find)', [])
('Q(rfind)', [])
('Q(index)', [])
('Q(rindex)', [])
('Q(join)', [])
('Q(split)', [])
('Q(splitlines)', ['(MICROPY_PY_BUILTINS_STR_SPLITLINES)'])
('Q(rsplit)', [])
('Q(startswith)', [])
('Q(endswith)', [])
('Q(strip)', [])
('Q(lstrip)', [])
('Q(rstrip)', [])
('Q(format)', [])
('Q(replace)', [])
('Q(count)', [])
('Q(partition)', [])
('Q(rpartition)', [])
('Q(lower)', [])
('Q(upper)', [])
('Q(isspace)', [])
('Q(isalpha)', [])
('Q(isdigit)', [])
('Q(isupper)', [])
('Q(islower)', [])
('Q(str)', ['(!MICROPY_PY_BUILTINS_STR_UNICODE)'])
('Q(bytes)', [])
('Q(NULL)', [])
('Q(NULL)', [])
Code: Select all
#if MICROPY_PY_BUILTINS_STR_SPLITLINES
Q(keepends)
Q(_backslash_n)
#endif
#if MICROPY_CPYTHON_COMPAT
Q(utf_hyphen_8)
Q(utf_hyphen_8)
#endif
#if MICROPY_CPYTHON_COMPAT
Q(decode)
#if !MICROPY_PY_BUILTINS_STR_UNICODE
Q(encode)
#endif
Q(find)
Q(rfind)
Q(index)
Q(rindex)
Q(join)
Q(split)
#if MICROPY_PY_BUILTINS_STR_SPLITLINES
Q(splitlines)
#endif
Q(rsplit)
Q(startswith)
Q(endswith)
Q(strip)
Q(lstrip)
Q(rstrip)
Q(format)
Q(replace)
Q(count)
Q(partition)
Q(rpartition)
Q(lower)
Q(upper)
Q(isspace)
Q(isalpha)
Q(isdigit)
Q(isupper)
Q(islower)
#if !MICROPY_PY_BUILTINS_STR_UNICODE
Q(str)
#endif
Q(bytes)
Q(NULL)
Q(NULL)