Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a new tool, CompressCompilation.py, for making it easy to package up a single translation unit. #566

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions bin/Examples/CompressCompilation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright 2024, Peter Goodman. All rights reserved.

import argparse
import os.path
import pathlib
import tarfile
import json

import multiplier as mx


def make_compile_commands(comp: mx.frontend.Compilation, root_path: pathlib.PurePath):
with open(str(root_path / "compile_commands.json"), "w") as cc:
cc.write(json.dumps([ {
"directory": str(comp.working_directory),
"file": str(comp.main_source_file_path),
"arguments": [arg for arg in comp.arguments]
}], indent=" "))


def harness(comp: mx.frontend.Compilation, root_path: pathlib.PurePath):
for file in comp.files:
for path in file.paths:
nested = root_path / pathlib.PurePath("." + str(path))
parent_dir = nested.parent
os.makedirs(parent_dir, exist_ok=True)
with open(str(nested), "w") as file_copy:
file_copy.write(file.data)

make_compile_commands(comp, root_path)

with open(str(root_path / "builtin-macros.h"), "w") as macros:
index = mx.Index.containing(comp)
for macro in mx.frontend.DefineMacroDirective.IN(index):
file = mx.frontend.File.containing(macro)
if not file:
macros.write(" ".join(t.data for t in macro.generate_use_tokens))
macros.write('\n')

with tarfile.open(str(root_path) + ".tar.gz", "w:gz") as tar:
tar.add(str(root_path), arcname=root_path.name)


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Extract and compress a compilation")
parser.add_argument('--db', type=str,
required=True,
help="Path to mx-index-produced database")
parser.add_argument('--entity_id', type=int, required=True,
help="Id of the entity to harness")
parser.add_argument('--working_dir', type=str, required=True,
help="The working directory into which to place files that will be compresed")

args = parser.parse_args()
index = mx.Index.in_memory_cache(mx.Index.from_database(args.db))
if entity := index.entity(args.entity_id):
if comp := mx.frontend.Compilation.containing(entity):
if not os.path.isdir(args.working_dir):
os.makedirs(args.working_dir, exist_ok=True)

harness(comp, pathlib.PosixPath(args.working_dir))
else:
print(f"Could not find compilation containing entity {entity.id}")
else:
print(f"Invalid entity ID {args.entity_id}")
Loading