blob: 8855ec79872a266db2ba28fbc8be42ae9baedfd4 [file] [log] [blame]
Daniel Veillardb8478642001-10-12 17:29:10 +00001#! /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###
12import os, string
13
14class globvar:
15 def __init__(self, type, name):
16 self.type=type
17 self.name=name
18
Daniel Veillard781ac8b2003-05-15 22:11:36 +000019def striplinesep(line):
20 while line and line[-1] in ('\r','\n'):
21 line = line[:-1]
22 return line
23
Daniel Veillardb8478642001-10-12 17:29:10 +000024def writeline(file, line=None):
25 if line:
26 file.write(line)
Daniel Veillard781ac8b2003-05-15 22:11:36 +000027 file.write("\n")
Daniel Veillardb8478642001-10-12 17:29:10 +000028
29if __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 Veillard781ac8b2003-05-15 22:11:36 +000042 line = striplinesep(line)
Daniel Veillardb8478642001-10-12 17:29:10 +000043 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 Veillard781ac8b2003-05-15 22:11:36 +000053 line = striplinesep(line)
Daniel Veillardb8478642001-10-12 17:29:10 +000054 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 Veillard781ac8b2003-05-15 22:11:36 +000067 line = striplinesep(line)
Daniel Veillardb8478642001-10-12 17:29:10 +000068 fields = string.split(line, ",")
69 # Update the header file
70 writeline(global_functions_hdr)
Daniel Veillardb8478642001-10-12 17:29:10 +000071 global_functions_hdr.write("extern "+fields[0]+" *")
Daniel Veillard781ac8b2003-05-15 22:11:36 +000072 if fields[2]:
Daniel Veillardb8478642001-10-12 17:29:10 +000073 global_functions_hdr.write("(*")
74 global_functions_hdr.write("__"+fields[1]+"(void)")
Daniel Veillard781ac8b2003-05-15 22:11:36 +000075 if fields[2]:
Daniel Veillardb8478642001-10-12 17:29:10 +000076 global_functions_hdr.write(")"+fields[2])
77 writeline(global_functions_hdr,";")
Daniel Veillard0ba59232002-02-10 13:20:39 +000078 writeline(global_functions_hdr, "#ifdef LIBXML_THREAD_ENABLED")
Daniel Veillardb8478642001-10-12 17:29:10 +000079 writeline(global_functions_hdr,"#define "+fields[1]+" \\")
80 writeline(global_functions_hdr,"(*(__"+fields[1]+"()))")
81 writeline(global_functions_hdr,"#else")
Daniel Veillard781ac8b2003-05-15 22:11:36 +000082 if fields[2]:
Daniel Veillardd0463562001-10-13 09:15:48 +000083 writeline(global_functions_hdr,"LIBXML_DLL_IMPORT extern "+fields[0]+" "+fields[1]+fields[2]+";")
Daniel Veillardb8478642001-10-12 17:29:10 +000084 else:
Daniel Veillardd0463562001-10-13 09:15:48 +000085 writeline(global_functions_hdr,"LIBXML_DLL_IMPORT extern "+fields[0]+" "+fields[1]+";")
Daniel Veillardb8478642001-10-12 17:29:10 +000086 writeline(global_functions_hdr,"#endif")
Daniel Veillard781ac8b2003-05-15 22:11:36 +000087 # 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 Veillardb8478642001-10-12 17:29:10 +000090 # Update the implementation file
91 writeline(global_functions_impl)
Daniel Veillard0ba59232002-02-10 13:20:39 +000092# writeline(global_functions_impl, "extern "+fields[0]+" "+fields[1]+";")
Daniel Veillardb8478642001-10-12 17:29:10 +000093 writeline(global_functions_impl, "#undef\t"+fields[1])
94 writeline(global_functions_impl, fields[0]+" *")
Daniel Veillard781ac8b2003-05-15 22:11:36 +000095 if fields[2]:
Daniel Veillardb8478642001-10-12 17:29:10 +000096 global_functions_impl.write("(*")
97 global_functions_impl.write("__"+fields[1]+"(void)")
Daniel Veillard781ac8b2003-05-15 22:11:36 +000098 if fields[2]:
Daniel Veillardb8478642001-10-12 17:29:10 +000099 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 Veillardd0463562001-10-13 09:15:48 +0000104 writeline(global_functions_impl, "\treturn (&xmlGetGlobalState()->"+fields[1]+");")
Daniel Veillardb8478642001-10-12 17:29:10 +0000105 writeline(global_functions_impl, "}")
Daniel Veillard781ac8b2003-05-15 22:11:36 +0000106 # 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 Veillardb8478642001-10-12 17:29:10 +0000116 # 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 */")