1#!/usr/bin/env python3 2# 3# Copyright 2019 The Hafnium Authors. 4# 5# Use of this source code is governed by a BSD-style 6# license that can be found in the LICENSE file or at 7# https://opensource.org/licenses/BSD-3-Clause. 8 9"""Generate a header file with definitions of constants parsed from a binary.""" 10 11import argparse 12import os 13import re 14import subprocess 15import sys 16 17STRINGS = "llvm-strings" 18 19PROLOGUE = """ 20/** 21 * This file was auto-generated by {}. 22 * Changes will be overwritten. 23 */ 24 25#pragma once 26 27""".format(__file__) 28 29def main(): 30 parser = argparse.ArgumentParser() 31 parser.add_argument("bin_file", 32 help="binary file to be parsed for definitions of constants") 33 parser.add_argument("out_file", help="output file"); 34 args = parser.parse_args() 35 36 # Regex for finding definitions: <HAFNIUM_DEFINE name value /> 37 regex = re.compile(r'<HAFNIUM_DEFINE\s([A-Za-z0-9_]+)\s([0-9]+) />') 38 39 # Extract strings from the input binary file. 40 stdout = subprocess.check_output([ STRINGS, args.bin_file ]) 41 stdout = stdout.decode('utf-8').split(os.linesep) 42 43 with open(args.out_file, "w") as f: 44 f.write(PROLOGUE) 45 for line in stdout: 46 for match in regex.findall(line): 47 f.write("#define {} ({})\n".format( 48 match[0], match[1])) 49 50if __name__ == "__main__": 51 sys.exit(main()) 52