From 267e20dd4c89cb9a813424990d54e1602dff5872 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Sun, 20 Feb 2022 20:01:06 +0100 Subject: [PATCH] tools: teach msgenc to avoid comments (#1151) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently the C preprocessor is used to remove comments from the text files before encoding them. However the option used (`-fpreprocessed`) is only supported by gcc – but not by clang, which breaks the macOS build. By using Python to remove comments, we can remove the use of this gcc-specific option, and fix the macOS build. --- Makefile | 3 +-- tools/msgenc.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 57f6126c71..7d72161c4e 100644 --- a/Makefile +++ b/Makefile @@ -297,8 +297,7 @@ build/data/%.o: data/%.s $(AS) $(ASFLAGS) $< -o $@ build/assets/text/%.enc.h: assets/text/%.h assets/text/charmap.txt - $(CPP) -P -dD -fpreprocessed $< > $(@:.enc.h=.h) - python3 tools/msgenc.py assets/text/charmap.txt $(@:.enc.h=.h) $@ + python3 tools/msgenc.py assets/text/charmap.txt $< $@ build/assets/text/fra_message_data_static.o: build/assets/text/message_data.enc.h build/assets/text/ger_message_data_static.o: build/assets/text/message_data.enc.h diff --git a/tools/msgenc.py b/tools/msgenc.py index b2b9cab168..b62b7383cc 100644 --- a/tools/msgenc.py +++ b/tools/msgenc.py @@ -14,6 +14,21 @@ def read_charmap(path): return charmap +# From https://stackoverflow.com/questions/241327/remove-c-and-c-comments-using-python +def remove_comments(text): + def replacer(match): + s = match.group(0) + if s.startswith('/'): + return " " # note: a space and not an empty string + else: + return s + + pattern = re.compile( + r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"', + re.DOTALL | re.MULTILINE + ) + return re.sub(pattern, replacer, text) + def convert_text(text, charmap): def cvt_str(m): string = m.group(0) @@ -46,6 +61,7 @@ def main(): with open(args.input, "r") as infile: text = infile.read() + text = remove_comments(text) text = convert_text(text, charmap) with open(args.output, "w", encoding="raw_unicode_escape") as outfile: