Skip to content

Commit

Permalink
Update amalgamate.py
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rmisev committed Aug 12, 2024
1 parent 2d0e26b commit 8366f21
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
4 changes: 2 additions & 2 deletions tools/amalgamate.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions tools/amalgamate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
43 changes: 27 additions & 16 deletions tools/amalgamate/amalgamate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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

Expand Down Expand Up @@ -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")
Expand All @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion tools/amalgamate/config-cpp.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"include_paths": [
"include"
],
"ingnore_includes": true
"ignore_includes": true
}

0 comments on commit 8366f21

Please sign in to comment.