Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | ### |
| 3 | # |
| 4 | # build_glob.py : Build the global_functions.h and global_functions.c |
| 5 | # files which are required to implement the user |
| 6 | # interface to global variables now that thread specific |
| 7 | # data (TSD) is used to emulate global state. |
| 8 | # |
| 9 | # See Copyright for the status of this software. |
| 10 | # Gary.Pennington@sun.com |
| 11 | ### |
| 12 | import os, string |
| 13 | |
| 14 | class globvar: |
| 15 | def __init__(self, type, name): |
| 16 | self.type=type |
| 17 | self.name=name |
| 18 | |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame] | 19 | def striplinesep(line): |
| 20 | while line and line[-1] in ('\r','\n'): |
| 21 | line = line[:-1] |
| 22 | return line |
| 23 | |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 24 | def writeline(file, line=None): |
| 25 | if line: |
| 26 | file.write(line) |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame] | 27 | file.write("\n") |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 28 | |
| 29 | if __name__ == "__main__": |
| 30 | globals={} |
| 31 | global_data=open("global.data").readlines() |
| 32 | global_code=open("globals.c").readlines() |
| 33 | global_hdr=open("include/libxml/globals.h").readlines() |
| 34 | global_functions_hdr=open("include/libxml/globals.h", "w+") |
| 35 | global_functions_impl=open("globals.c", "w+") |
| 36 | |
| 37 | # |
| 38 | # Rebuild the beginning of the file up to the |
| 39 | # Automatically generated string |
| 40 | # |
| 41 | for line in global_hdr: |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame] | 42 | line = striplinesep(line) |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 43 | if line == " * Automatically generated by build_glob.py.": |
| 44 | break |
| 45 | writeline(global_functions_hdr, line) |
| 46 | |
| 47 | writeline(global_functions_hdr, " * Automatically generated by build_glob.py.") |
| 48 | writeline(global_functions_hdr, " * Do not modify the previous line.") |
| 49 | writeline(global_functions_hdr, " */") |
| 50 | writeline(global_functions_hdr) |
| 51 | |
| 52 | for line in global_code: |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame] | 53 | line = striplinesep(line) |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 54 | if line == " * Automatically generated by build_glob.py.": |
| 55 | break |
| 56 | writeline(global_functions_impl, line) |
| 57 | |
| 58 | writeline(global_functions_impl, " * Automatically generated by build_glob.py.") |
| 59 | writeline(global_functions_impl, " * Do not modify the previous line.") |
| 60 | writeline(global_functions_impl, " */") |
| 61 | writeline(global_functions_impl) |
| 62 | |
| 63 | # Now process the data and write it to the appropriate output file |
| 64 | for line in global_data: |
| 65 | if line[0]=='#': |
| 66 | continue |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame] | 67 | line = striplinesep(line) |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 68 | fields = string.split(line, ",") |
| 69 | # Update the header file |
| 70 | writeline(global_functions_hdr) |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 71 | global_functions_hdr.write("extern "+fields[0]+" *") |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame] | 72 | if fields[2]: |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 73 | global_functions_hdr.write("(*") |
| 74 | global_functions_hdr.write("__"+fields[1]+"(void)") |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame] | 75 | if fields[2]: |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 76 | global_functions_hdr.write(")"+fields[2]) |
| 77 | writeline(global_functions_hdr,";") |
Daniel Veillard | 0ba5923 | 2002-02-10 13:20:39 +0000 | [diff] [blame] | 78 | writeline(global_functions_hdr, "#ifdef LIBXML_THREAD_ENABLED") |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 79 | writeline(global_functions_hdr,"#define "+fields[1]+" \\") |
| 80 | writeline(global_functions_hdr,"(*(__"+fields[1]+"()))") |
| 81 | writeline(global_functions_hdr,"#else") |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame] | 82 | if fields[2]: |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 83 | writeline(global_functions_hdr,"LIBXML_DLL_IMPORT extern "+fields[0]+" "+fields[1]+fields[2]+";") |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 84 | else: |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 85 | writeline(global_functions_hdr,"LIBXML_DLL_IMPORT extern "+fields[0]+" "+fields[1]+";") |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 86 | writeline(global_functions_hdr,"#endif") |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame] | 87 | # set/get for per-thread global defaults |
| 88 | if fields[3]: |
| 89 | writeline(global_functions_hdr,fields[0]+" "+fields[1][:3]+"ThrDef"+fields[1][3:]+"("+fields[0]+" v);") |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 90 | # Update the implementation file |
| 91 | writeline(global_functions_impl) |
Daniel Veillard | 0ba5923 | 2002-02-10 13:20:39 +0000 | [diff] [blame] | 92 | # writeline(global_functions_impl, "extern "+fields[0]+" "+fields[1]+";") |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 93 | writeline(global_functions_impl, "#undef\t"+fields[1]) |
| 94 | writeline(global_functions_impl, fields[0]+" *") |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame] | 95 | if fields[2]: |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 96 | global_functions_impl.write("(*") |
| 97 | global_functions_impl.write("__"+fields[1]+"(void)") |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame] | 98 | if fields[2]: |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 99 | writeline(global_functions_impl, ")[]") |
| 100 | writeline(global_functions_impl, " {") |
| 101 | writeline(global_functions_impl, " if (IS_MAIN_THREAD)") |
| 102 | writeline(global_functions_impl, "\treturn (&"+fields[1]+");") |
| 103 | writeline(global_functions_impl, " else") |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 104 | writeline(global_functions_impl, "\treturn (&xmlGetGlobalState()->"+fields[1]+");") |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 105 | writeline(global_functions_impl, "}") |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame] | 106 | # set/get for per-thread global defaults |
| 107 | if fields[3]: |
| 108 | writeline(global_functions_impl,fields[0]+" "+fields[1][:3]+"ThrDef"+fields[1][3:]+"("+fields[0]+" v) {") |
| 109 | writeline(global_functions_impl," "+fields[0]+" ret;"); |
| 110 | writeline(global_functions_impl," xmlMutexLock(xmlThrDefMutex);") |
| 111 | writeline(global_functions_impl," ret = "+fields[1][:3]+fields[1][3:]+"ThrDef;") |
| 112 | writeline(global_functions_impl," "+fields[1][:3]+fields[1][3:]+"ThrDef = v;") |
| 113 | writeline(global_functions_impl," xmlMutexUnlock(xmlThrDefMutex);") |
| 114 | writeline(global_functions_impl," return ret;") |
| 115 | writeline(global_functions_impl,"}") |
Daniel Veillard | b847864 | 2001-10-12 17:29:10 +0000 | [diff] [blame] | 116 | # Terminate the header file with appropriate boilerplate |
| 117 | writeline(global_functions_hdr) |
| 118 | writeline(global_functions_hdr, "#ifdef __cplusplus") |
| 119 | writeline(global_functions_hdr, "}") |
| 120 | writeline(global_functions_hdr, "#endif") |
| 121 | writeline(global_functions_hdr) |
| 122 | writeline(global_functions_hdr, "#endif /* __XML_GLOBALS_H */") |