autogen_interposer.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #!/usr/bin/env python3
  2. import re
  3. import json
  4. import os
  5. import sys
  6. project_path = "../../"
  7. flatdump_path = project_path+"build/coderef/examples-256/flatdump/"
  8. output_file_path = project_path + 'code/lib/xrn_interfaces/c/inc/xrn_lib_interposer.h'
  9. output_file_path1 = project_path + 'code/app/parser/inc/xrn_lib_interposer_dll.h'
  10. if ( len(sys.argv) == 4 ) :
  11. output_file_path = sys.argv[1]
  12. output_file_path1 = sys.argv[2]
  13. flatdump_path = sys.argv[3]
  14. defines_to_look_for = ['XRN_SUBJECT_CERTIFICATE_LENGTH','XRN_BUFF_SIZE','XBIT_TYPE','XRN_ENC_PASS_BUFF_SIZE','XRN_BUFFER_ENC_CHECK_SIZE','XRN_BUFFER_ENC_BIST_SIZE','XSUCCESS','XRN_MAX_MERGE_PARALLELISM']
  15. structs_to_look_for = ['xrn_log_t','xrn_crypto_lib_header_t','xrn_encoding_checks_t','xrn_encoding_param_t','xrn_encoding_tmp_param_t','xrn_encoding_settings_t','xrn_interface_settings_t','xrn_crypto_extra_settings_t','xblock_settings_t']
  16. enums_to_look_for = ['xrn_encoding_mode_t','xrn_check_initialization_mode_t','xrn_lib_xtype_t']
  17. # Read the input .h file
  18. with open(project_path +'scripts/backend/' + 'filelist.json', 'r') as file:
  19. jsondata = file.read()
  20. filelists_json = json.loads( jsondata )
  21. # fetch library header files which are not backend
  22. input_file_paths = []
  23. for file in filelists_json["libh"]:
  24. if not ( "_backend" in file ) :
  25. input_file_paths.append( flatdump_path + os.path.basename(file) )
  26. parser_file = ""
  27. for file in filelists_json["parserc"]:
  28. if ( "parser.c" in file ) :
  29. parser_file = project_path + file
  30. def find_words_ending_with_dll(input_file):
  31. """Finds all words in the given input file that end with "_wrapper".
  32. Args:
  33. input_file: A string containing the path to the input file.
  34. Returns:
  35. A list of all words in the input file that end with "_wrapper".
  36. """
  37. words_ending_with_wrapper = []
  38. with open(input_file, 'r') as f:
  39. for line in f:
  40. words = re.findall(r'\w+', line)
  41. words_ending_with_wrapper.extend([word for word in words if word.endswith('_dll')])
  42. return words_ending_with_wrapper
  43. input_file = parser_file
  44. words_ending_with_dll = find_words_ending_with_dll(input_file)
  45. words_processed = []
  46. # Define a regular expression pattern to match function declarations with multiline definitions
  47. function_pattern = r'(\w+\s*\**\s*)\s+(\w+)\s*\(([^)]*)\)\s*;'
  48. function_info_pattern = r'(\w+(\s*\*\s*)*)\s*(\w+(\[\w+\])*)'
  49. # Define a regular expression pattern to extract enumeration,struct,define
  50. enum_pattern = r'\s*enum\s*{([^}]*)}\s*(\w+)'
  51. struct_pattern = r'\s*struct\s+(\w+)\s*{([^}]*\s*(\w+))'
  52. define_pattern = r'#define\s+(\w+)\s+(.+)'
  53. with open(output_file_path,"w") as output_file:
  54. output_file.write('#ifndef XRN_TYPEDEF_H \n#define XRN_TYPEDEF_H\n#include <stdio.h>\n#include <stdint.h>\n#include <stdbool.h>\n\n')
  55. for de in defines_to_look_for:
  56. for input_file_path in input_file_paths:
  57. with open(input_file_path, 'r') as file:
  58. content = file.read()
  59. defines = re.findall(define_pattern,content)
  60. for define in defines:
  61. if de == define[0]:
  62. output_file.write('#define '+define[0]+" "+define[1]+"\n")
  63. output_file.write("\n")
  64. for en in enums_to_look_for:
  65. for input_file_path in input_file_paths:
  66. with open(input_file_path, 'r') as file:
  67. content = file.read()
  68. enumerations = re.findall(enum_pattern,content)
  69. for enumeration in enumerations:
  70. if enumeration[1] == en:
  71. output_file.write("typedef enum {")
  72. first = 0
  73. e_con = enumeration[0].split(',')
  74. for con in e_con:
  75. if first == 0:
  76. first = 1
  77. else:
  78. output_file.write(",")
  79. output_file.write(con)
  80. output_file.write('} '+enumeration[1]+";\n\n")
  81. for st in structs_to_look_for:
  82. for input_file_path in input_file_paths:
  83. with open(input_file_path, 'r') as file:
  84. content = file.read()
  85. structs = re.findall(struct_pattern,content)
  86. for struct in structs:
  87. if struct[0] == st:
  88. output_file.write("typedef struct "+ st + " {")
  89. first = 0
  90. s_con = struct[0].split(',')
  91. output_file.write(struct[1])
  92. output_file.write(';\n} '+struct[0]+";\n\n")
  93. output_file.write('#endif\n')
  94. with open(output_file_path1,"w") as output_file:
  95. output_file.write('#ifndef XRN_DLL_TYPEDEF_H \n#define XRN_DLL_TYPEDEF_H\n#include <stdio.h>\n#include <stdint.h>\n#include <stdbool.h>\n#include "xrn_lib_interposer.h"\n\n')
  96. for input_file_path in input_file_paths:
  97. with open(input_file_path, 'r') as file:
  98. content = file.read()
  99. content = content.replace('\n', ' ')
  100. # Find all function declarations with multiline definitions in the content
  101. function_declarations = re.findall(function_pattern, content)
  102. # Process each function declaration
  103. for declaration in function_declarations:
  104. # Extract function name, return type, and arguments
  105. d_list = (declaration[2]).split(',')
  106. first = 0
  107. typ = declaration [0]
  108. dec = declaration [1] #[:-3]
  109. for word in words_ending_with_dll:
  110. word_ep = word[:-4]
  111. if word_ep == dec:
  112. output_file.write( typ+ " (* " + dec +"_dll)( " )
  113. words_ending_with_dll.remove(word)
  114. words_processed.append(word)
  115. for d in d_list:
  116. d = d.strip()
  117. match = re.match(function_info_pattern, d)
  118. if (match != None):
  119. a ,b,c,d =match.groups()
  120. if first == 0:
  121. first = 1
  122. else:
  123. output_file.write(",")
  124. output_file.write (a + " ")
  125. if d != None :
  126. output_file.write("* ")
  127. output_file.write(");\n")
  128. break
  129. output_file.write('\n#endif\n')
  130. # mixed_array = words_ending_with_dll + words_processed
  131. # seen = set()
  132. # unique_array = []
  133. # for element in words_ending_with_dll:
  134. # if element not in seen:
  135. # seen.add(element)
  136. # unique_array.append(element)
  137. # for w in words_processed:
  138. # if w in words_ending_with_dll:
  139. # unique_array.remove(w)
  140. # print(sorted(unique_array))