improve bloom filter on send
[platform/upstream/glib.git] / build / win32 / setup.py
1 #!/usr/bin/python
2 # vim: encoding=utf-8
3 #expand *.in files
4 #this script is only intended for building from git, not for building from the released tarball, which already includes all necessary files
5 import os
6 import sys
7 import re
8 import string
9 import subprocess
10 import optparse
11
12 from process_in_win32 import get_version, process_in, get_srcroot
13
14 def process_include(src, dest, includes):
15     RE_INCLUDE = re.compile(r'^\s*#include\s+"(.*)"')
16     with open(src, 'r') as s:
17         with open(dest, 'w') as d:
18             for i in s:
19                 mo = RE_INCLUDE.search(i)
20                 if mo:
21                     target = ''
22                     for j in includes:
23                         #print "searching in ", j
24                         if mo.group(1) in os.listdir(j):
25                             target = os.path.join(j, mo.group(1))
26                             break
27                     if not target:
28                         raise Exception("Couldn't fine include file %s" % mo.group(1))
29                     else:
30                         with open(target, 'r') as t:
31                             for inc in t.readlines():
32                                 d.write(inc)
33                 else:
34                     d.write(i)
35
36 def generate_libgio_sourcefiles(srcroot, dest, stype):
37     vars = read_vars_from_AM(os.path.join(srcroot, 'gio', 'Makefile.am'),
38                              vars = {'top_srcdir': srcroot},
39                              conds = {'OS_WIN32': True},
40                              filters = ['libgio_2_0_la_SOURCES', 'win32_more_sources_for_vcproj'])
41
42     files = vars['libgio_2_0_la_SOURCES'].split() + \
43             vars['win32_more_sources_for_vcproj'].split()
44
45     sources = [i for i in files \
46                     if i != 'gdesktopappinfo.c' and \
47                        not (i.startswith('gunix') and i.endswith('.c')) \
48                        and i.endswith('.c') ]
49     if stype == '9':
50        with open(dest, 'w') as d:
51            for i in sources:
52                d.write('\t\t\t<File RelativePath="..\\..\\..\\gio\\' + i.replace('/', '\\') + '"/>\n')
53     elif stype == '10':
54        with open(dest, 'w') as d:
55            for i in sources:
56                d.write('\t\t\t<ClCompile Include="..\\..\\..\\gio\\' + i.replace('/', '\\') + '"/>\n')
57     elif stype == '10f':
58        with open(dest, 'w') as d:
59            for i in sources:
60                d.write('\t\t\t<ClCompile Include="..\\..\\..\\gio\\' + i.replace('/', '\\') + '"><Filter>Source Files</Filter></ClCompile>\n')
61     else:
62        raise Exception("Must specify project type (9, 10 or 10f)")
63
64 def generate_libgio_enumtypes(srcroot, perl):
65     vars = read_vars_from_AM(os.path.join(srcroot, 'gio', 'Makefile.am'),
66                              vars = {'top_srcdir': srcroot},
67                              conds = {'OS_WIN32': True},
68                              filters = ['gio_headers'])
69     cwd = os.getcwd()
70     os.chdir(os.path.join(srcroot, 'gio'))
71     for suffix in ['.c', '.h']:
72         cmd = [perl, os.path.join(srcroot, 'gobject', 'glib-mkenums'),
73                '--template', 'gioenumtypes' + suffix + '.template'] + vars['gio_headers'].split()
74         with open('gioenumtypes' + suffix, 'w') as d:
75             subprocess.Popen(cmd, stdout = d).communicate()
76     os.chdir(cwd)
77 def generate_libglib_sourcefiles(srcroot, dest, stype):
78     vars = read_vars_from_AM(os.path.join(srcroot, 'glib', 'Makefile.am'),
79                              vars = {'top_srcdir': srcroot},
80                              conds = {'OS_WIN32': True,
81                                       'ENABLE_REGEX': True},
82                              filters = ['libglib_2_0_la_SOURCES'])
83
84     files = vars['libglib_2_0_la_SOURCES'].split()
85
86     sources = [i for i in files \
87                     if not (i.endswith('-gcc.c') or i.endswith('-unix.c')) \
88                        and i.endswith('.c') ]
89     if stype == '9':
90         with open(dest, 'w') as d:
91             for i in sources:
92                 d.write('\t\t\t<File RelativePath="..\\..\\..\\glib\\' + i.replace('/', '\\') + '"/>\n')
93     elif stype == '10':
94         with open(dest, 'w') as d:
95             for i in sources:
96                 d.write('\t\t\t<ClCompile Include="..\\..\\..\\glib\\' + i.replace('/', '\\') + '"/>\n')
97     elif stype == '10f':
98         with open(dest, 'w') as d:
99             for i in sources:
100                 d.write('\t\t\t<ClCompile Include="..\\..\\..\\glib\\' + i.replace('/', '\\') + '"><Filter>Source Files</Filter></ClCompile>\n')
101     else:
102         raise Exception("Must specify project type (9, 10 or 10f)")
103
104 def generate_libgobject_sourcefiles(srcroot, dest, stype):
105     vars = read_vars_from_AM(os.path.join(srcroot, 'gobject', 'Makefile.am'),
106                              vars = {'top_srcdir': srcroot},
107                              conds = {'OS_WIN32': True},
108                              filters = ['libgobject_2_0_la_SOURCES'])
109
110     files = vars['libgobject_2_0_la_SOURCES'].split()
111
112     sources = [i for i in files if i.endswith('.c') ]
113     if stype == '9':
114         with open(dest, 'w') as d:
115             for i in sources:
116                 d.write('\t\t\t<File RelativePath="..\\..\\..\\gobject\\' + i.replace('/', '\\') + '"/>\n')
117     elif stype == '10':
118         with open(dest, 'w') as d:
119             for i in sources:
120                 d.write('\t\t\t<ClCompile Include="..\\..\\..\\gobject\\' + i.replace('/', '\\') + '"/>\n')
121     elif stype == '10f':
122         with open(dest, 'w') as d:
123             for i in sources:
124                 d.write('\t\t\t<ClCompile Include="..\\..\\..\\gobject\\' + i.replace('/', '\\') + '"><Filter>Source Files</Filter></ClCompile>\n')
125     else:
126         raise Exception("Must specify project type (9, 10 or 10f)")
127
128 def read_vars_from_AM(path, vars = {}, conds = {}, filters = None):
129     '''
130     path: path to the Makefile.am
131     vars: predefined variables
132     conds: condition variables for Makefile
133     filters: if None, all variables defined are returned,
134              otherwise, it is a list contains that variables should be returned
135     '''
136     cur_vars = vars.copy()
137     RE_AM_VAR_REF = re.compile(r'\$\((\w+?)\)')
138     RE_AM_VAR = re.compile(r'^\s*(\w+)\s*=(.*)$')
139     RE_AM_INCLUDE = re.compile(r'^\s*include\s+(\w+)')
140     RE_AM_CONTINUING = re.compile(r'\\\s*$')
141     RE_AM_IF = re.compile(r'^\s*if\s+(\w+)')
142     RE_AM_ELSE = re.compile(r'^\s*else')
143     RE_AM_ENDIF = re.compile(r'^\s*endif')
144     def am_eval(cont):
145         return RE_AM_VAR_REF.sub(lambda x: cur_vars.get(x.group(1), ''), cont)
146     with open(path, 'r') as f:
147         contents = f.readlines()
148     #combine continuing lines
149     i = 0
150     ncont = []
151     while i < len(contents):
152         line = contents[i]
153         if RE_AM_CONTINUING.search(line):
154             line = RE_AM_CONTINUING.sub('', line)
155             j = i + 1
156             while j < len(contents) and RE_AM_CONTINUING.search(contents[j]):
157                 line += RE_AM_CONTINUING.sub('', contents[j])
158                 j += 1
159             else:
160                 if j < len(contents):
161                     line += contents[j]
162             i = j
163         else:
164             i += 1
165         ncont.append(line)
166
167     #include, var define, var evaluation
168     i = -1
169     skip = False
170     oldskip = []
171     while i < len(ncont) - 1:
172         i += 1
173         line = ncont[i]
174         mo = RE_AM_IF.search(line)
175         if mo:
176             oldskip.append(skip)
177             skip = False if mo.group(1) in conds and conds[mo.group(1)] \
178                          else True
179             continue
180         mo = RE_AM_ELSE.search(line)
181         if mo:
182             skip = not skip
183             continue
184         mo = RE_AM_ENDIF.search(line)
185         if mo:
186             skip = oldskip.pop()
187             continue
188         if not skip:
189             mo = RE_AM_INCLUDE.search(line)
190             if mo:
191                 cur_vars.update(read_vars_from_AM(am_eval(mo.group(1)), cur_vars, conds, None))
192                 continue
193             mo = RE_AM_VAR.search(line)
194             if mo:
195                 cur_vars[mo.group(1)] = am_eval(mo.group(2).strip())
196                 continue
197
198     #filter:
199     if filters != None:
200         ret = {}
201         for i in filters:
202             ret[i] = cur_vars.get(i, '')
203         return ret
204     else:
205         return cur_vars
206
207 def main(argv):
208     parser = optparse.OptionParser()
209     parser.add_option('-p', '--perl', dest='perl', metavar='PATH', default='C:\\Perl\\bin\\perl.exe', action='store', help='path to the perl interpretor (default: C:\\Perl\\bin\\perl.exe)')
210     opt, args = parser.parse_args(argv)
211     srcroot = get_srcroot()
212     #print 'srcroot', srcroot
213     ver = get_version(srcroot)
214     #print 'ver', ver
215     config_vars = ver.copy()
216     config_vars['GETTEXT_PACKAGE'] = 'Glib'
217     process_in(os.path.join(srcroot, 'config.h.win32.in'),
218                os.path.join(srcroot, 'config.h'),
219                config_vars)
220     glibconfig_vars = ver.copy()
221     glibconfig_vars['GLIB_WIN32_STATIC_COMPILATION_DEFINE'] = ''
222     process_in(os.path.join(srcroot, 'glib', 'glibconfig.h.win32.in'),
223                os.path.join(srcroot, 'glib', 'glibconfig.h'),
224                glibconfig_vars)
225
226     for submodule in ['glib', 'gobject', 'gthread', 'gmodule', 'gio']:
227         process_in(os.path.join(srcroot, submodule, submodule + '.rc.in'),
228                    os.path.join(srcroot, submodule, submodule + '.rc'),
229                    ver)
230
231     #------------ submodule gobject -------------------
232     generate_libglib_sourcefiles(srcroot,
233                                 os.path.join(srcroot, 'build', 'win32', 'libglib.sourcefiles'), '9')
234     generate_libglib_sourcefiles(srcroot,
235                                 os.path.join(srcroot, 'build', 'win32', 'libglib.vs10.sourcefiles'), '10')
236     generate_libglib_sourcefiles(srcroot,
237                                 os.path.join(srcroot, 'build', 'win32', 'libglib.vs10.sourcefiles.filters'), '10f')
238     process_include(os.path.join(srcroot, 'build', 'win32', 'vs9', 'glib.vcprojin'),
239                     os.path.join(srcroot, 'build', 'win32', 'vs9', 'glib.vcproj'),
240                     includes = [os.path.join(srcroot, 'build', 'win32')])
241     process_include(os.path.join(srcroot, 'build', 'win32', 'vs10', 'glib.vcxprojin'),
242                     os.path.join(srcroot, 'build', 'win32', 'vs10', 'glib.vcxproj'),
243                     includes = [os.path.join(srcroot, 'build', 'win32')])
244     process_include(os.path.join(srcroot, 'build', 'win32', 'vs10', 'glib.vcxproj.filtersin'),
245                     os.path.join(srcroot, 'build', 'win32', 'vs10', 'glib.vcxproj.filters'),
246                     includes = [os.path.join(srcroot, 'build', 'win32')])
247     os.unlink(os.path.join(srcroot, 'build', 'win32', 'libglib.sourcefiles'))
248     os.unlink(os.path.join(srcroot, 'build', 'win32', 'libglib.vs10.sourcefiles'))
249     os.unlink(os.path.join(srcroot, 'build', 'win32', 'libglib.vs10.sourcefiles.filters'))
250     with open(os.path.join(srcroot, 'glib', 'gspawn-win32-helper-console.c'), 'w') as c:
251         c.write('#define HELPER_CONSOLE\n')
252         c.write('#include "gspawn-win32-helper.c"\n')
253     with open(os.path.join(srcroot, 'glib', 'gspawn-win64-helper-console.c'), 'w') as c:
254         c.write('#define HELPER_CONSOLE\n')
255         c.write('#include "gspawn-win32-helper.c"\n')
256     with open(os.path.join(srcroot, 'glib', 'gspawn-win64-helper.c'), 'w') as c:
257         c.write('#include "gspawn-win32-helper.c"\n')
258     #------------ end of submodule glib -------------------
259
260     #------------ submodule gobject -------------------
261     mkenums_vars = ver.copy()
262     mkenums_vars.update({'PERL_PATH': opt.perl})
263     process_in(os.path.join(srcroot, 'gobject', 'glib-mkenums.in'),
264                os.path.join(srcroot, 'gobject', 'glib-mkenums'),
265                mkenums_vars)
266
267     #gmarshal.strings
268     cwd = os.getcwd()
269     os.chdir(os.path.join(srcroot, 'gobject'))
270     with open(os.path.join(srcroot, 'gobject', 'gmarshal.strings'), 'w') as d:
271         with open(os.path.join(srcroot, 'gobject', 'gmarshal.list'), 'r') as s:
272             for i in s:
273                 if i[0] not in string.ascii_uppercase: #^[A-Z]
274                     continue
275                 line = '"g_cclosure_marshal_' # s/^/"g_cclosure_marshal_/
276                 for c in i:
277                     if c == ':':
278                         line += '__'          # s/:/__
279                     elif c == ',':
280                         line += '_'           # s/,/_
281                     elif c not in '\r\n':
282                         line += c
283                 d.write(line + '",\n')
284         #subprocess.Popen([opt.perl, 'marshal-genstrings.pl'], stdout=d).communicate()
285     os.chdir(cwd)
286
287     generate_libgobject_sourcefiles(srcroot,
288                                 os.path.join(srcroot, 'build', 'win32', 'libgobject.sourcefiles'), '9')
289     generate_libgobject_sourcefiles(srcroot,
290                                 os.path.join(srcroot, 'build', 'win32', 'libgobject.vs10.sourcefiles'), '10')
291     generate_libgobject_sourcefiles(srcroot,
292                                 os.path.join(srcroot, 'build', 'win32', 'libgobject.vs10.sourcefiles.filters'), '10f')
293     process_include(os.path.join(srcroot, 'build', 'win32', 'vs9', 'gobject.vcprojin'),
294                     os.path.join(srcroot, 'build', 'win32', 'vs9', 'gobject.vcproj'),
295                     includes = [os.path.join(srcroot, 'build', 'win32')])
296     process_include(os.path.join(srcroot, 'build', 'win32', 'vs10', 'gobject.vcxprojin'),
297                     os.path.join(srcroot, 'build', 'win32', 'vs10', 'gobject.vcxproj'),
298                     includes = [os.path.join(srcroot, 'build', 'win32')])
299     process_include(os.path.join(srcroot, 'build', 'win32', 'vs10', 'gobject.vcxproj.filtersin'),
300                     os.path.join(srcroot, 'build', 'win32', 'vs10', 'gobject.vcxproj.filters'),
301                     includes = [os.path.join(srcroot, 'build', 'win32')])
302     os.unlink(os.path.join(srcroot, 'build', 'win32', 'libgobject.sourcefiles'))
303     os.unlink(os.path.join(srcroot, 'build', 'win32', 'libgobject.vs10.sourcefiles'))
304     os.unlink(os.path.join(srcroot, 'build', 'win32', 'libgobject.vs10.sourcefiles.filters'))
305     #------------ end of submodule gobject -------------------
306
307     #------------ submodule gio -------------------
308     #depends on glib-mkenums
309     generate_libgio_sourcefiles(srcroot,
310                                 os.path.join(srcroot, 'build', 'win32', 'libgio.sourcefiles'), '9')
311     generate_libgio_sourcefiles(srcroot,
312                                 os.path.join(srcroot, 'build', 'win32', 'libgio.vs10.sourcefiles'), '10')
313     generate_libgio_sourcefiles(srcroot,
314                                 os.path.join(srcroot, 'build', 'win32', 'libgio.vs10.sourcefiles.filters'), '10f')
315     process_include(os.path.join(srcroot, 'build', 'win32', 'vs9', 'gio.vcprojin'),
316                     os.path.join(srcroot, 'build', 'win32', 'vs9', 'gio.vcproj'),
317                     includes = [os.path.join(srcroot, 'build', 'win32')])
318     process_include(os.path.join(srcroot, 'build', 'win32', 'vs10', 'gio.vcxprojin'),
319                     os.path.join(srcroot, 'build', 'win32', 'vs10', 'gio.vcxproj'),
320                     includes = [os.path.join(srcroot, 'build', 'win32')])
321     process_include(os.path.join(srcroot, 'build', 'win32', 'vs10', 'gio.vcxproj.filtersin'),
322                     os.path.join(srcroot, 'build', 'win32', 'vs10', 'gio.vcxproj.filters'),
323                     includes = [os.path.join(srcroot, 'build', 'win32')])
324     os.unlink(os.path.join(srcroot, 'build', 'win32', 'libgio.sourcefiles'))
325     os.unlink(os.path.join(srcroot, 'build', 'win32', 'libgio.vs10.sourcefiles'))
326     os.unlink(os.path.join(srcroot, 'build', 'win32', 'libgio.vs10.sourcefiles.filters'))
327     generate_libgio_enumtypes(srcroot, opt.perl)
328     #------------ end of submodule gio -------------------
329
330     #------------ submodule gmodule -------------------
331     #------------ end of submodule gmodule -------------------
332     return 0
333
334 if __name__ == '__main__':
335     sys.exit(main(sys.argv))