#!/usr/bin/env python3
#
# Copyright 2019 The Hafnium Authors.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/BSD-3-Clause.
"""Generate a header file with definitions of constants parsed from a binary."""
import argparse
import os
import re
import subprocess
import sys
STRINGS = "llvm-strings"
PROLOGUE = """
/**
* This file was auto-generated by {}.
* Changes will be overwritten.
*/
#pragma once
""".format(__file__)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("bin_file",
help="binary file to be parsed for definitions of constants")
parser.add_argument("out_file", help="output file");
args = parser.parse_args()
# Regex for finding definitions:
regex = re.compile(r'')
# Extract strings from the input binary file.
stdout = subprocess.check_output([ STRINGS, args.bin_file ])
stdout = stdout.decode('utf-8').split(os.linesep)
with open(args.out_file, "w") as f:
f.write(PROLOGUE)
for line in stdout:
for match in regex.findall(line):
f.write("#define {} ({})\n".format(
match[0], match[1]))
if __name__ == "__main__":
sys.exit(main())