From 8366f2165199158facea23047ad5753054c7fdf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rimas=20Misevi=C4=8Dius?= Date: Mon, 12 Aug 2024 15:25:50 +0300 Subject: [PATCH] Update amalgamate.py * Fix spelling in amalgamate.py: ingnore_... -> ignore_ * Remove support for "head" config file parameter * Add `-d`/`--no-duplicates` option to amalgamate.py and use it. * Simplify the use of the `-v`/`--verbose` option of amalgamate.py --- tools/amalgamate.bat | 4 +-- tools/amalgamate.sh | 4 +-- tools/amalgamate/amalgamate.py | 43 ++++++++++++++++++++------------ tools/amalgamate/config-cpp.json | 2 +- 4 files changed, 32 insertions(+), 21 deletions(-) diff --git a/tools/amalgamate.bat b/tools/amalgamate.bat index ad02f26..92794ab 100644 --- a/tools/amalgamate.bat +++ b/tools/amalgamate.bat @@ -8,5 +8,5 @@ REM CD to repository root folder cd %p%.. REM Amalgamate -python tools/amalgamate/amalgamate.py -c tools/amalgamate/config-cpp.json -s . -p tools/amalgamate/config-cpp.prologue -python tools/amalgamate/amalgamate.py -c tools/amalgamate/config-h.json -s . +python tools/amalgamate/amalgamate.py -c tools/amalgamate/config-cpp.json -s . -p tools/amalgamate/config-cpp.prologue --no-duplicates +python tools/amalgamate/amalgamate.py -c tools/amalgamate/config-h.json -s . --no-duplicates diff --git a/tools/amalgamate.sh b/tools/amalgamate.sh index b0a1b59..443db2f 100755 --- a/tools/amalgamate.sh +++ b/tools/amalgamate.sh @@ -8,5 +8,5 @@ p="$(dirname "$0")" cd $p/.. # Amalgamate -python3 tools/amalgamate/amalgamate.py -c tools/amalgamate/config-cpp.json -s . -p tools/amalgamate/config-cpp.prologue -python3 tools/amalgamate/amalgamate.py -c tools/amalgamate/config-h.json -s . +python3 tools/amalgamate/amalgamate.py -c tools/amalgamate/config-cpp.json -s . -p tools/amalgamate/config-cpp.prologue --no-duplicates +python3 tools/amalgamate/amalgamate.py -c tools/amalgamate/config-h.json -s . --no-duplicates diff --git a/tools/amalgamate/amalgamate.py b/tools/amalgamate/amalgamate.py index 7a7a3ed..bed669b 100644 --- a/tools/amalgamate/amalgamate.py +++ b/tools/amalgamate/amalgamate.py @@ -62,16 +62,17 @@ def find_included_file(self, file_path, source_dir): def __init__(self, args): with open(args.config, 'r') as f: - self.head = [] # default - self.ingnore_includes = False + self.ignore_includes = False config = json.loads(f.read()) for key in config: setattr(self, key, config[key]) - self.verbose = args.verbose == "yes" + self.verbose = args.verbose self.prologue = args.prologue self.source_path = args.source_path + self.no_duplicates = args.no_duplicates self.included_files = [] + self.includes_set = set() # Generate the amalgamation and write it to the target file. def generate(self): @@ -87,10 +88,6 @@ def generate(self): print(" working_dir = {0}".format(os.getcwd())) print(" include_paths = {0}".format(self.include_paths)) print("Creating amalgamation:") - for file_path in self.head: - actual_path = self.actual_path(file_path) - with io.open(actual_path, mode="r", encoding="utf-8") as f: - amalgamation += f.read() for file_path in self.sources: # Do not check the include paths while processing the source # list, all given source paths must be correct. @@ -226,7 +223,7 @@ def _process_includes(self): search_same_dir = include_match.group(1) == '"' found_included_path = self.amalgamation.find_included_file( include_path, self.file_dir if search_same_dir else None) - if found_included_path: + if found_included_path or self.amalgamation.no_duplicates: includes.append((include_match, found_included_path)) include_match = self.include_pattern.search(self.content, @@ -237,12 +234,22 @@ def _process_includes(self): tmp_content = '' for include in includes: include_match, found_included_path = include - tmp_content += self.content[prev_end:include_match.start()] - tmp_content += "// {0}\n".format(include_match.group(0)) - if not self.amalgamation.ingnore_includes and not found_included_path in self.amalgamation.included_files: - t = TranslationUnit(found_included_path, self.amalgamation, False) - tmp_content += t.content - prev_end = include_match.end() + if found_included_path: + tmp_content += self.content[prev_end:include_match.start()] + tmp_content += "// {0}\n".format(include_match.group(0)) + if not self.amalgamation.ignore_includes and not found_included_path in self.amalgamation.included_files: + t = TranslationUnit(found_included_path, self.amalgamation, False) + tmp_content += t.content + prev_end = include_match.end() + else: + include_path = include_match.group("path") + if include_path in self.amalgamation.includes_set: + # Comment out the duplicate include + tmp_content += self.content[prev_end:include_match.start()] + tmp_content += "// {0}".format(include_match.group(0)) + prev_end = include_match.end() + else: + self.amalgamation.includes_set.add(include_path) tmp_content += self.content[prev_end:] self.content = tmp_content @@ -276,13 +283,14 @@ def main(): "[-v]", "-c path/to/config.json", "-s path/to/source/dir", - "[-p path/to/prologue.(c|h)]" + "[-p path/to/prologue.(c|h)]", + "[-d]" ]) argsparser = argparse.ArgumentParser( description=description, usage=usage) argsparser.add_argument("-v", "--verbose", dest="verbose", - choices=["yes", "no"], metavar="", help="be verbose") + action='store_true', help="be verbose") argsparser.add_argument("-c", "--config", dest="config", required=True, metavar="", help="path to a JSON config file") @@ -293,6 +301,9 @@ def main(): argsparser.add_argument("-p", "--prologue", dest="prologue", required=False, metavar="", help="path to a C prologue file") + argsparser.add_argument("-d", "--no-duplicates", dest="no_duplicates", + action='store_true', help="comment out the duplicate includes") + amalgamation = Amalgamation(argsparser.parse_args()) amalgamation.generate() diff --git a/tools/amalgamate/config-cpp.json b/tools/amalgamate/config-cpp.json index 00bccf5..4f95d87 100644 --- a/tools/amalgamate/config-cpp.json +++ b/tools/amalgamate/config-cpp.json @@ -12,5 +12,5 @@ "include_paths": [ "include" ], - "ingnore_includes": true + "ignore_includes": true }