108 lines
3.5 KiB
Python
108 lines
3.5 KiB
Python
#!/usr/bin/env python3
|
|
import argparse
|
|
from collections import defaultdict
|
|
import csv
|
|
from pathlib import Path
|
|
|
|
|
|
SIGNATURE_FIELDS = (
|
|
"address",
|
|
"calling_convention",
|
|
"return_type",
|
|
"parameter_count",
|
|
"variadic",
|
|
"prototype",
|
|
)
|
|
|
|
|
|
def normalize_name(name: str) -> str:
|
|
return name.lstrip("._")
|
|
|
|
|
|
def address_key(row: dict[str, str]) -> tuple[int, str]:
|
|
address = row["address"]
|
|
try:
|
|
return int(address, 16), address
|
|
except ValueError:
|
|
return 0, address
|
|
|
|
|
|
def parse_platform(value: str) -> tuple[str, Path]:
|
|
if "=" not in value:
|
|
raise argparse.ArgumentTypeError("platform inputs must use NAME=DIRECTORY")
|
|
name, directory = value.split("=", 1)
|
|
if not name or not directory:
|
|
raise argparse.ArgumentTypeError("platform inputs must use NAME=DIRECTORY")
|
|
path = Path(directory)
|
|
if not path.is_dir():
|
|
raise argparse.ArgumentTypeError(f"signature directory does not exist: {path}")
|
|
return name, path
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("output", type=Path)
|
|
parser.add_argument("platform", nargs="+", type=parse_platform)
|
|
args = parser.parse_args()
|
|
|
|
platforms = [name for name, _ in args.platform]
|
|
if len(platforms) != len(set(platforms)):
|
|
parser.error("platform names must be unique")
|
|
|
|
signatures = defaultdict(lambda: defaultdict(list))
|
|
for platform, directory in args.platform:
|
|
for path in sorted(directory.glob("*.tsv")):
|
|
with path.open(newline="", encoding="utf-8") as handle:
|
|
reader = csv.DictReader(handle, delimiter="\t")
|
|
for row in reader:
|
|
key = path.stem, normalize_name(row["name"])
|
|
signatures[key][platform].append(row)
|
|
|
|
fieldnames = ["module", "name", "occurrence", "platform_count"]
|
|
fieldnames.extend(
|
|
f"{platform}_{field}"
|
|
for platform in platforms
|
|
for field in SIGNATURE_FIELDS
|
|
)
|
|
|
|
args.output.parent.mkdir(parents=True, exist_ok=True)
|
|
output_rows = 0
|
|
with args.output.open("w", newline="", encoding="utf-8") as handle:
|
|
writer = csv.DictWriter(
|
|
handle,
|
|
fieldnames=fieldnames,
|
|
delimiter="\t",
|
|
lineterminator="\n",
|
|
)
|
|
writer.writeheader()
|
|
for (module, name), by_platform in sorted(signatures.items()):
|
|
for rows in by_platform.values():
|
|
rows.sort(key=address_key)
|
|
occurrences = max(len(rows) for rows in by_platform.values())
|
|
for occurrence in range(occurrences):
|
|
output = {
|
|
"module": module,
|
|
"name": name,
|
|
"occurrence": occurrence + 1,
|
|
"platform_count": sum(
|
|
occurrence < len(by_platform.get(platform, ()))
|
|
for platform in platforms
|
|
),
|
|
}
|
|
for platform in platforms:
|
|
rows = by_platform.get(platform, ())
|
|
if occurrence >= len(rows):
|
|
continue
|
|
row = rows[occurrence]
|
|
for field in SIGNATURE_FIELDS:
|
|
output[f"{platform}_{field}"] = row[field]
|
|
writer.writerow(output)
|
|
output_rows += 1
|
|
|
|
print(f"merged {output_rows} function occurrences across {len(platforms)} platforms")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|