1#!/usr/bin/python 2 3from __future__ import print_function 4 5import sys 6from functools import reduce 7 8def read_levels(): 9 f = open('../../../libs/toollog/include/xentoollog.h', 'r') 10 11 levels = [] 12 record = False 13 for l in f.readlines(): 14 if 'XTL_NUM_LEVELS' in l: 15 break 16 if record == True: 17 levels.append(l.split(',')[0].strip()) 18 if 'XTL_NONE' in l: 19 record = True 20 21 f.close() 22 23 olevels = [level[4:].capitalize() for level in levels] 24 25 return levels, olevels 26 27# .ml 28 29def gen_ml(olevels): 30 s = "" 31 32 s += "type level = \n" 33 for level in olevels: 34 s += '\t| %s\n' % level 35 36 s += "\nlet level_to_string level =\n" 37 s += "\tmatch level with\n" 38 for level in olevels: 39 s += '\t| %s -> "%s"\n' % (level, level) 40 41 s += "\nlet level_to_prio level =\n" 42 s += "\tmatch level with\n" 43 for index,level in enumerate(olevels): 44 s += '\t| %s -> %d\n' % (level, index) 45 46 return s 47 48# .mli 49 50def gen_mli(olevels): 51 s = "" 52 53 s += "type level = \n" 54 for level in olevels: 55 s += '\t| %s\n' % level 56 57 return s 58 59# .c 60 61def gen_c(level): 62 s = "" 63 64 s += "static value Val_level(xentoollog_level c_level)\n" 65 s += "{\n" 66 s += "\tswitch (c_level) {\n" 67 s += "\tcase XTL_NONE: /* Not a real value */\n" 68 s += '\t\tcaml_raise_sys_error(caml_copy_string("Val_level XTL_NONE"));\n' 69 s += "\t\tbreak;\n" 70 71 for index,level in enumerate(levels): 72 s += "\tcase %s:\n\t\treturn Val_int(%d);\n" % (level, index) 73 74 s += """\tcase XTL_NUM_LEVELS: /* Not a real value! */ 75 \t\tcaml_raise_sys_error( 76 \t\t\tcaml_copy_string("Val_level XTL_NUM_LEVELS")); 77 #if 0 /* Let the compiler catch this */ 78 \tdefault: 79 \t\tcaml_raise_sys_error(caml_copy_string("Val_level Unknown")); 80 \t\tbreak; 81 #endif 82 \t} 83 \tabort(); 84 } 85 """ 86 87 return s 88 89def autogen_header(open_comment, close_comment): 90 s = open_comment + " AUTO-GENERATED FILE DO NOT EDIT " + close_comment + "\n" 91 s += open_comment + " autogenerated by \n" 92 s += reduce(lambda x,y: x + " ", range(len(open_comment + " ")), "") 93 s += "%s" % " ".join(sys.argv) 94 s += "\n " + close_comment + "\n\n" 95 return s 96 97if __name__ == '__main__': 98 if len(sys.argv) < 3: 99 print("Usage: genlevels.py <mli> <ml> <c-inc>", file=sys.stderr) 100 sys.exit(1) 101 102 levels, olevels = read_levels() 103 104 _mli = sys.argv[1] 105 mli = open(_mli, 'w') 106 mli.write(autogen_header("(*", "*)")) 107 108 _ml = sys.argv[2] 109 ml = open(_ml, 'w') 110 ml.write(autogen_header("(*", "*)")) 111 112 _cinc = sys.argv[3] 113 cinc = open(_cinc, 'w') 114 cinc.write(autogen_header("/*", "*/")) 115 116 mli.write(gen_mli(olevels)) 117 mli.write("\n") 118 119 ml.write(gen_ml(olevels)) 120 ml.write("\n") 121 122 cinc.write(gen_c(levels)) 123 cinc.write("\n") 124 125 ml.write("(* END OF AUTO-GENERATED CODE *)\n") 126 ml.close() 127 mli.write("(* END OF AUTO-GENERATED CODE *)\n") 128 mli.close() 129 cinc.close() 130 131