Adding winding_logging.patch to disappearing_sliver regression test that
[xonotic/netradiant.git] / config.py
1 import sys, traceback, platform, re, commands, platform, subprocess
2
3 if __name__ != '__main__':
4         from SCons.Script import *
5
6 import utils
7
8 # config = debug release
9 # aliases are going to be very needed here
10 # we have dependency situations too
11 # target =
12
13 class Config:
14         # not used atm, but useful to keep a list in mind
15         # may use them eventually for the 'all' and other aliases expansions?
16         target_choices = utils.Enum( 'radiant', 'q3map2', 'setup' )
17         config_choices = utils.Enum( 'debug', 'release' )
18
19         # aliases
20         # 'all' -> for each choices
21         # 'gamecode' for the targets, 'game' 'cgame' 'ui'
22
23         def __init__( self ):
24                 # initialize defaults
25                 self.target_selected = [ 'radiant', 'q3map2' ]
26                 self.config_selected = [ 'release' ]
27                 # those are global to each config
28                 self.platform = platform.system()
29                 self.cc = 'gcc'
30                 self.cxx = 'g++'
31                 self.install_directory = 'install'
32
33                 # platforms for which to assemble a setup
34                 self.setup_platforms = [ 'local', 'x86', 'x64', 'win32' ]
35                 # paks to assemble in the setup
36                 self.setup_packs = [ 'Q3Pack', 'UrTPack', 'UFOAIPack', 'Q2WPack', 'JAPack' ]
37
38         def __repr__( self ):
39                 return 'config: target=%s config=%s' % ( self.target_selected, self.config_selected )
40
41         def _processTarget( self, ops ):
42                 self.target_selected = ops
43
44         def _processConfig( self, ops ):
45                 self.config_selected = ops
46
47         def _processCC( self, ops ):
48                 self.cc = ops
49
50         def _processCXX( self, ops ):
51                 self.cxx = ops
52
53         def _processInstallDir( self, ops ):
54                 self.install_directory = os.path.normpath( os.path.expanduser( ops[0] ) )
55
56         def _processSetupPlatforms( self, ops ):
57                 self.setup_platforms = ops
58
59         def _processSetupPacks( self, ops ):
60                 self.setup_packs = ops
61
62         def setupParser( self, operators ):
63                 operators['target'] = self._processTarget
64                 operators['config'] = self._processConfig
65                 operators['cc'] = self._processCC
66                 operators['cxx'] = self._processCXX
67                 operators['install_directory'] = self._processInstallDir
68                 operators['setup_platforms'] = self._processSetupPlatforms
69                 operators['setup_packs'] = self._processSetupPacks
70
71         def emit_radiant( self ):
72                 settings = self
73                 for config_name in self.config_selected:
74                         config = {}
75                         config['name'] = config_name
76                         config['shared'] = False
77                         Export( 'utils', 'settings', 'config' )
78                         build_dir = os.path.join( 'build', config_name, 'radiant' )
79                         BuildDir( build_dir, '.', duplicate = 0 )
80                         # left out jpeg6, splines (FIXME: I think jpeg6 is not used at all, can trash?)
81                         lib_objects = []
82                         for project in [ 'libs/synapse/synapse.vcproj', 'libs/cmdlib/cmdlib.vcproj', 'libs/mathlib/mathlib.vcproj', 'libs/l_net/l_net.vcproj', 'libs/ddslib/ddslib.vcproj', 'libs/picomodel/picomodel.vcproj', 'libs/md5lib/md5lib.vcproj' ]:
83                                 Export( 'project' )
84                                 lib_objects += SConscript( os.path.join( build_dir, 'SConscript.lib' ) )
85                         Export( 'lib_objects' )
86                         radiant = SConscript( os.path.join( build_dir, 'SConscript.radiant' ) )
87                         Default( InstallAs( os.path.join( self.install_directory, 'radiant.bin' ), radiant ) )
88
89                         # PIC versions of the libs for the modules
90                         shlib_objects_extra = {}
91                         for project in [ 'libs/synapse/synapse.vcproj', 'libs/mathlib/mathlib.vcproj', 'libs/picomodel/picomodel.vcproj', 'libs/cmdlib/cmdlib.vcproj' ]:
92                                 ( libpath, libname ) = os.path.split( project )
93                                 libname = os.path.splitext( libname )[0]
94                                 config['shared'] = True
95                                 Export( 'project', 'config' )
96                                 build_dir = os.path.join( 'build', config_name, 'shobjs' )
97                                 BuildDir( build_dir, '.', duplicate = 0 )
98                                 shlib_objects_extra[libname] = SConscript( os.path.join( build_dir, 'SConscript.lib' ) )
99
100                         for project in [ 'plugins/vfspk3/vfspk3.vcproj',
101                                          'plugins/vfspak/vfspak.vcproj',
102                                          'plugins/vfswad/vfswad.vcproj',
103                                          'plugins/eclassfgd/fgd.vcproj',
104                                          'plugins/entity/entity.vcproj',
105                                          'plugins/image/image.vcproj',
106                                          'plugins/model/model.vcproj',
107                                          'plugins/imagepng/imagepng.vcproj',
108                                          'plugins/imagewal/imagewal.vcproj',
109                                          'plugins/imagem8/imagem8.vcproj',
110                                          'plugins/spritemodel/spritemodel.vcproj',
111                                          'plugins/textool/TexTool.vcproj',
112                                         # 'plugins/sample/sample.vcproj',
113                                          'plugins/map/map.vcproj',
114                                          'plugins/mapxml/mapxml.vcproj',
115                                          'plugins/shaders/shaders.vcproj',
116                                          'plugins/surface/surface.vcproj',
117                                          'plugins/surface_ufoai/surface_ufoai.vcproj',
118                                          'plugins/surface_quake2/surface_quake2.vcproj',
119                                          'plugins/surface_heretic2/surface_heretic2.vcproj',
120                                         # FIXME Needs splines
121                                         # 'contrib/camera/camera.vcproj',
122
123                                         # FIXME What is this? Empty dir for me - remove me?
124                                         # 'contrib/patches/patches.vcproj',
125                                         # 'plugins/archivewad/archivewad.vcproj',
126
127                                          'contrib/prtview/PrtView.vcproj',
128                                          'contrib/hydratoolz/hydratoolz.vcproj',
129                                          'contrib/bobtoolz/bobToolz_gtk.vcproj',
130                                          'contrib/gtkgensurf/gtkgensurf.vcproj',
131                                          'contrib/ufoai/ufoai.vcproj',
132                                          'contrib/bkgrnd2d/bkgrnd2d.vcproj'
133                                  ]:
134                                 ( libpath, libname ) = os.path.split( project )
135                                 libname = os.path.splitext( libname )[0]
136                                 shlib_objects = shlib_objects_extra['synapse']
137                                 if ( libname == 'entity' ):
138                                         shlib_objects += shlib_objects_extra['mathlib']
139                                 elif ( libname == 'model' ):
140                                         shlib_objects += shlib_objects_extra['picomodel']
141 #                               elif ( libname == 'spritemodel' ):
142 #                                       shlib_objects += shlib_objects_extra['mathlib']
143 #                               elif ( libname == 'TexTool' ):
144 #                                       shlib_objects += shlib_objects_extra['mathlib']
145                                 elif ( libname == 'map' ):
146                                         shlib_objects += shlib_objects_extra['cmdlib']
147                                 Export( 'project', 'shlib_objects' )
148                                 module = SConscript( os.path.join( build_dir, 'SConscript.module' ) )
149                                 Default( InstallAs( os.path.join( self.install_directory, 'modules/%s.so' % libname ), module ) )
150
151         def emit_q3map2( self ):
152                 settings = self
153                 for config_name in self.config_selected:
154                         config = {}
155                         config['name'] = config_name
156                         config['shared'] = False
157                         Export( 'utils', 'settings', 'config' )
158                         build_dir = os.path.join( 'build', config_name, 'q3map2' )
159                         BuildDir( build_dir, '.', duplicate = 0 )
160                         lib_objects = []
161                         for project in [ 'libs/cmdlib/cmdlib.vcproj', 'libs/mathlib/mathlib.vcproj', 'libs/l_net/l_net.vcproj', 'libs/ddslib/ddslib.vcproj', 'libs/picomodel/picomodel.vcproj', 'libs/md5lib/md5lib_VC9.vcproj' ]:
162                                 Export( 'project' )
163                                 lib_objects += SConscript( os.path.join( build_dir, 'SConscript.lib' ) )
164                         Export( 'lib_objects' )
165                         q3map2 = SConscript( os.path.join( build_dir, 'SConscript.q3map2' ) )
166                         Default( InstallAs( os.path.join( self.install_directory, 'q3map2' ), q3map2 ) )
167
168
169         def emit( self ):
170                 try:
171                         self.target_selected.index( 'radiant' )
172                 except:
173                         pass
174                 else:
175                         self.emit_radiant()
176                 try:
177                         self.target_selected.index( 'q3map2' )
178                 except:
179                         pass
180                 else:
181                         self.emit_q3map2()
182
183                 try:
184                         self.target_selected.index( 'setup' )
185                 except:
186                         pass
187                 else:
188                         self.Setup()
189
190         def SetupEnvironment( self, env, config, useGtk = False, useGtkGL = False, useJPEG = False, useZ = False, usePNG = False ):
191                 env['CC'] = self.cc
192                 env['CXX'] = self.cxx
193                 ( ret, xml2 ) = commands.getstatusoutput( 'xml2-config --cflags' )
194                 if ( ret != 0 ):
195                         print 'xml2-config failed'
196                         assert( False )
197                 xml2libs = commands.getoutput( 'xml2-config --libs' )
198                 env.Append( LINKFLAGS = xml2libs.split( ' ' ) )
199                 baseflags = [ '-pipe', '-Wall', '-fmessage-length=0', '-fvisibility=hidden', xml2.split( ' ' ) ]
200 #               baseflags += [ '-m32' ]
201
202                 if ( self.platform == 'Darwin' ):
203                         env.Append( CPPPATH = [ '/Developer/SDKs/MacOSX10.4u.sdk/usr/X11R6/include' ] )
204
205                 if ( useGtk ):
206                         ( ret, gtk2 ) = commands.getstatusoutput( 'pkg-config gtk+-2.0 --cflags' )
207                         if ( ret != 0 ):
208                                 print 'pkg-config gtk+-2.0 failed'
209                                 assert( False )
210                         baseflags += gtk2.split( ' ' )
211                         gtk2libs = commands.getoutput( 'pkg-config gtk+-2.0 --libs' )
212                         env.Append( LINKFLAGS = gtk2libs.split( ' ' ) )
213                 else:
214                         # always setup at least glib
215                         ( ret, glib ) = commands.getstatusoutput( 'pkg-config glib-2.0 --cflags' )
216                         if ( ret != 0 ):
217                                 print 'pkg-config glib-2.0 failed'
218                                 assert( False )
219                         baseflags += glib.split( ' ' )
220                         gliblibs = commands.getoutput( 'pkg-config glib-2.0 --libs' )
221                         env.Append( LINKFLAGS = gliblibs.split( ' ' ) )
222
223                 if ( useGtkGL ):
224                         ( ret, gtkgl ) = commands.getstatusoutput( 'pkg-config gtkglext-1.0 --cflags' )
225                         if ( ret != 0 ):
226                                 print 'pkg-config gtkglext-1.0 failed'
227                                 assert( False )
228                         baseflags += gtkgl.split( ' ' )
229                         gtkgllibs = commands.getoutput( 'pkg-config gtkglext-1.0 --libs' )
230                         env.Append( LINKFLAGS = gtkgllibs.split( ' ' ) )
231                 if ( useJPEG ):
232                         env.Append( LIBS = 'jpeg' )
233                 if ( usePNG ):
234                         pnglibs = 'png z'
235                         env.Append( LIBS = pnglibs.split( ' ' ) )
236                 if ( useZ ):
237                         env.Append( LIBS = 'z' )
238
239                 env.Append( CCFLAGS = baseflags )
240                 env.Append( CXXFLAGS = baseflags + [ '-fpermissive', '-fvisibility-inlines-hidden' ] )
241                 env.Append( CPPPATH = [ 'include', 'libs' ] )
242                 env.Append( CPPDEFINES = [ 'Q_NO_STLPORT' ] )
243                 if ( config == 'debug' ):
244                         env.Append( CFLAGS = [ '-g' ] )
245                         env.Append( CXXFLAGS = [ '-g' ] )
246                         env.Append( CPPDEFINES = [ '_DEBUG' ] )
247                 else:
248                         env.Append( CFLAGS = [ '-O3', '-Winline', '-ffast-math', '-fno-unsafe-math-optimizations', '-fno-strict-aliasing' ] )
249                         env.Append( CXXFLAGS = [ '-O3', '-Winline', '-ffast-math', '-fno-unsafe-math-optimizations','-fno-strict-aliasing' ] )
250
251         def CheckoutOrUpdate( self, svnurl, path ):
252                 if ( os.path.exists( path ) ):
253                         cmd = [ 'svn', 'update', path ]
254                 else:
255                         cmd = [ 'svn', 'checkout', svnurl, path ]
256                 print( repr( cmd ) )
257                 subprocess.check_call( cmd )
258
259
260         def FetchGamePaks( self, path ):
261                 for pak in self.setup_packs:
262                         if ( pak == 'Q3Pack' or pak == 'UrTPack' or pak == 'UFOAIPack' or pak == 'Q2WPack' or pak == 'JAPack' ):
263                                 svnurl = 'https://zerowing.idsoftware.com/svn/radiant.gamepacks/%s/trunk' % pak
264                                 self.CheckoutOrUpdate( svnurl, os.path.join( path, 'installs', pak ) )
265
266         def Setup( self ):
267                 try:
268                         self.setup_platforms.index( 'local' )
269                 except:
270                         pass
271                 else:
272                         # special case, fetch external paks under the local install directory
273                         self.FetchGamePaks( self.install_directory )
274                 # NOTE: unrelated to self.setup_platforms - grab support files and binaries and install them
275                 if ( self.platform == 'Windows' ):
276                         depsfile = 'GtkR-deps-1.6-3.zip'
277                         if ( not os.path.exists( depsfile ) ):
278                                 cmd = [ 'wget', '-N', 'http://zerowing.idsoftware.com/files/radiant/developer/1.6.1/%s' % depsfile ]
279                                 print( repr( cmd ) )
280                                 subprocess.check_call( cmd )
281
282                                 # extract one directoy above
283                                 f = os.path.abspath( depsfile )
284                                 backup_cwd = os.getcwd()
285                                 os.chdir( os.path.dirname( backup_cwd ) )
286                                 cmd = [ 'unzip', '-o', f ]
287                                 print( repr( cmd ) )
288                                 subprocess.check_call( cmd )
289                                 os.chdir( backup_cwd )
290
291                                 # copy all the dependent runtime data to the install directory
292                                 srcdir = os.path.dirname( backup_cwd )
293                                 for f in [
294                                         'libxml2/bin/libxml2.dll',
295                                         'gtk2/bin/libglib-2.0-0.dll',
296                                         'gtk2/bin/libgobject-2.0-0.dll',
297                                         'gtk2/bin/libgdk-win32-2.0-0.dll',
298                                         'gtk2/bin/libgtk-win32-2.0-0.dll',
299                                         'gtk2/bin/intl.dll',
300                                         'gtk2/bin/libatk-1.0-0.dll',
301                                         'gtk2/bin/libcairo-2.dll',
302                                         'gtk2/bin/libgdk_pixbuf-2.0-0.dll',
303                                         'gtk2/bin/libgmodule-2.0-0.dll',
304                                         'gtk2/bin/libpng13.dll',
305                                         'gtk2/bin/libpango-1.0-0.dll',
306                                         'gtk2/bin/libpangoft2-1.0-0.dll',
307                                         'gtk2/bin/libpangocairo-1.0-0.dll',
308                                         'gtk2/bin/libpangowin32-1.0-0.dll',
309                                         'gtk2/lib/libgtkglext-win32-1.0-0.dll',
310                                         'gtk2/lib/libgdkglext-win32-1.0-0.dll',
311                                         'gtk2/lib/iconv.dll',
312                                         'gtk2/zlib1.dll',
313 #                                       'freetype-dev_2.4.2-1_win32/bin/freetype6.dll',
314 #                                       'fontconfig-dev_2.8.0-2_win32/bin/libfontconfig-1.dll',
315 #                                       'expat_2.0.1-1_win32/bin/libexpat-1.dll',
316                                         ]:
317                                         cmd = [ 'cp', '-v', os.path.join( srcdir, f ), 'install' ]
318                                         print( repr( cmd ) )
319                                         subprocess.check_call( cmd )
320                                 for d in [
321                                         'gtk2/etc',
322                                         'gtk2/share',
323 #                                       'fontconfig-dev_2.8.0-2_win32/etc',
324 #                                       'fontconfig-dev_2.8.0-2_win32/share',
325 #                                       'freetype-dev_2.4.2-1_win32/share',
326                                         ]:
327                                         cmd = [ 'cp', '-r', '-v', os.path.join( srcdir, d ), 'install' ]
328                                         print( repr( cmd ) )
329                                         subprocess.check_call( cmd )
330
331 # parse the config statement line to produce/update an existing config list
332 # the configs expose a list of keywords and accepted values, which the engine parses out
333 class ConfigParser:
334         def __init__( self ):
335                 self.operators = {}
336
337         def _processOp( self, ops ):
338                 assert( len( ops ) == 1 )
339                 op = ops.pop()
340                 if ( op == 'clear' ):
341                         self.configs = []
342                         self.current_config = None
343                 elif ( op == 'pop' ):
344                         self.configs.pop()
345                         self.current_config = None
346                 elif ( op == 'push' ):
347                         self.configs.append( self.current_config )
348                         self.current_config = Config()
349                         self._setupParser( self.current_config )
350
351         def _setupParser( self, c ):
352                 self.operators = { 'op' : self._processOp }
353                 c.setupParser( self.operators )
354
355         def _parseStatement( self, s ):
356                 statement_re = re.compile( '(.*)=(.*)' )
357                 value_list_re = re.compile( '([^,]*),?' )
358                 if ( not statement_re.match( s ) ):
359                         print 'syntax error (statement match): %s' % repr( s )
360                         return
361                 statement_split = statement_re.split( s )
362                 if ( len( statement_split ) != 4 ):
363                         print 'syntax error (statement split): %s' % repr( s )
364                         return
365                 ( foo, name, value, bar ) = statement_split
366                 value_split = value_list_re.split( value )
367                 if ( len( value_split ) < 2 or len( value_split ) % 2 != 1 ):
368                         print 'syntax error (value split): %s' % ( repr( value_split ) )
369                         return
370                 try:
371                         value_array = []
372                         value_split.reverse()
373                         value_split.pop()
374                         while ( len( value_split ) != 0 ):
375                                 value_array.append( value_split.pop() )
376                                 value_split.pop()
377                 except:
378                         print traceback.print_exception( sys.exc_type, sys.exc_value, sys.exc_traceback )
379                         print 'syntax error (value to array): %s' % ( repr( value_split ) )
380                         return
381
382                 return ( name, value_array )
383
384         def parseStatements( self, _configs, statements ):
385                 self.current_config = None
386                 self.configs = _configs
387                 if ( self.configs is None ):
388                         self.configs = []
389                 for s in statements:
390
391                         if ( self.current_config is None ):
392                                 # use a provided config, or create a default one
393                                 if ( len( self.configs ) > 0 ):
394                                         self.current_config = self.configs.pop()
395                                 else:
396                                         self.current_config = Config()
397                                 # setup the operator table for this config
398                                 # NOTE: have that in self._processOp too
399                                 self._setupParser( self.current_config )
400
401                         ret = self._parseStatement( s )
402                         if ( ret is None ):
403                                 print 'stop statement parse at %s' % repr( s )
404                                 break
405                         ( name, value_array ) = ret
406                         try:
407                                 processor = self.operators[name]
408                         except:
409                                 print 'unknown operator %s - stop statement parse at %s' % ( repr( name ), repr( s ) )
410                                 break
411                         processor( value_array )
412
413                 if ( not self.current_config is None ):
414                         self.configs.append( self.current_config )
415                 # make sure there is at least one config
416                 if ( len( self.configs ) == 0 ):
417                         print 'pushing a default config'
418                         self.configs.append( Config() )
419                 return self.configs
420
421 import unittest
422
423 class TestConfigParse( unittest.TestCase ):
424
425         def setUp( self ):
426                 self.parser = ConfigParser()
427
428         def testBasicParse( self ):
429                 # test basic config parsing
430                 # needs to cleanly stop at the first config statement that is not recognized
431                 configs = self.parser.parseStatements( None, [ 'game=missionpack', 'config=qvm', 'foobar' ] )
432                 print repr( configs )
433
434         def testMultiParse( self ):
435                 # multiple configs seperated by commas
436                 configs = self.parser.parseStatements( None, [ 'target=server,game,cgame' ] )
437                 print repr( configs )
438
439         def testOp( self ):
440                 # test the operator for multiple configs
441                 configs = self.parser.parseStatements( None, [ 'target=core', 'config=release', 'op=push', 'target=game,cgame,ui', 'config=debug' ] )
442                 print repr( configs )
443
444 if __name__ == '__main__':
445         unittest.main()