Adding patch_seam q3map2 regression test. Probably not fixable, but good to
[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', 'ReactionPack' ]
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                         lib_objects = []
81                         for project in [ 'libs/synapse/synapse.vcproj', 'libs/cmdlib/cmdlib.vcproj', 'libs/mathlib/mathlib.vcproj', 'libs/l_net/l_net.vcproj' ]:
82                                 Export( 'project' )
83                                 lib_objects += SConscript( os.path.join( build_dir, 'SConscript.lib' ) )
84                         Export( 'lib_objects' )
85                         radiant = SConscript( os.path.join( build_dir, 'SConscript.radiant' ) )
86                         Default( InstallAs( os.path.join( self.install_directory, 'radiant.bin' ), radiant ) )
87
88                         # PIC versions of the libs for the modules
89                         shlib_objects_extra = {}
90                         for project in [ 'libs/synapse/synapse.vcproj', 'libs/mathlib/mathlib.vcproj', 'libs/picomodel/picomodel.vcproj', 'libs/cmdlib/cmdlib.vcproj', 'libs/splines/splines.vcproj' ]:
91                                 ( libpath, libname ) = os.path.split( project )
92                                 libname = os.path.splitext( libname )[0]
93                                 config['shared'] = True
94                                 Export( 'project', 'config' )
95                                 build_dir = os.path.join( 'build', config_name, 'shobjs' )
96                                 BuildDir( build_dir, '.', duplicate = 0 )
97                                 shlib_objects_extra[libname] = SConscript( os.path.join( build_dir, 'SConscript.lib' ) )
98
99                         for project in [ 'plugins/vfspk3/vfspk3.vcproj',
100                                          'plugins/vfspak/vfspak.vcproj',
101                                          'plugins/vfswad/vfswad.vcproj',
102                                          'plugins/eclassfgd/fgd.vcproj',
103                                          'plugins/entity/entity.vcproj',
104                                          'plugins/image/image.vcproj',
105                                          'plugins/model/model.vcproj',
106                                          'plugins/imagepng/imagepng.vcproj',
107                                          'plugins/imagewal/imagewal.vcproj',
108                                          'plugins/imagem8/imagem8.vcproj',
109                                          'plugins/spritemodel/spritemodel.vcproj',
110                                          'plugins/textool/textool.vcproj',
111                                          'plugins/map/map.vcproj',
112                                          'plugins/mapxml/mapxml.vcproj',
113                                          'plugins/shaders/shaders.vcproj',
114                                          'plugins/surface/surface.vcproj',
115                                          'plugins/surface_ufoai/surface_ufoai.vcproj',
116                                          'plugins/surface_quake2/surface_quake2.vcproj',
117                                          'plugins/surface_heretic2/surface_heretic2.vcproj',
118                                          'contrib/camera/camera.vcproj',
119                                          'contrib/prtview/prtview.vcproj',
120                                          'contrib/hydratoolz/hydratoolz.vcproj',
121                                          'contrib/bobtoolz/bobtoolz.vcproj',
122                                          'contrib/gtkgensurf/gtkgensurf.vcproj',
123                                          'contrib/ufoai/ufoai.vcproj',
124                                          'contrib/bkgrnd2d/bkgrnd2d.vcproj'
125                                  ]:
126                                 ( libpath, libname ) = os.path.split( project )
127                                 libname = os.path.splitext( libname )[0]
128                                 # The old code assigned shlib_objects to shlib_objects_extra['synapse'],
129                                 # and this resulted in a non-copy.  Stuff is added to shlib_objects below.
130                                 # So we need the explicit copy so we don't modify shlib_objects_extra['synapse'].
131                                 shlib_objects = shlib_objects_extra['synapse'][:]
132                                 if ( libname == 'camera' ):
133                                         shlib_objects += shlib_objects_extra['splines']
134                                 elif ( libname == 'entity' ):
135                                         shlib_objects += shlib_objects_extra['mathlib']
136                                 elif ( libname == 'map' ):
137                                         shlib_objects += shlib_objects_extra['cmdlib']
138                                 elif ( libname == 'model' ):
139                                         shlib_objects += shlib_objects_extra['picomodel']
140                                         shlib_objects += shlib_objects_extra['mathlib']
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 == 'bobtoolz' ):
146                                         shlib_objects += shlib_objects_extra['mathlib']
147                                         shlib_objects += shlib_objects_extra['cmdlib']
148                                 Export( 'project', 'shlib_objects' )
149                                 module = SConscript( os.path.join( build_dir, 'SConscript.module' ) )
150                                 Default( InstallAs( os.path.join( self.install_directory, 'modules/%s.so' % libname ), module ) )
151
152         def emit_q3map2( self ):
153                 settings = self
154                 for config_name in self.config_selected:
155                         config = {}
156                         config['name'] = config_name
157                         config['shared'] = False
158                         Export( 'utils', 'settings', 'config' )
159                         build_dir = os.path.join( 'build', config_name, 'q3map2' )
160                         BuildDir( build_dir, '.', duplicate = 0 )
161                         lib_objects = []
162                         for project in [ 'tools/quake3/common/quake3-common.vcproj', 'libs/mathlib/mathlib.vcproj', 'libs/l_net/l_net.vcproj', 'libs/ddslib/ddslib.vcproj', 'libs/picomodel/picomodel.vcproj', 'libs/md5lib/md5lib.vcproj' ]:
163                                 Export( 'project' )
164                                 lib_objects += SConscript( os.path.join( build_dir, 'SConscript.lib' ) )
165                         Export( 'lib_objects' )
166                         q3map2 = SConscript( os.path.join( build_dir, 'SConscript.q3map2' ) )
167                         Default( InstallAs( os.path.join( self.install_directory, 'q3map2' ), q3map2 ) )
168
169
170         def emit( self ):
171                 try:
172                         self.target_selected.index( 'radiant' )
173                 except:
174                         pass
175                 else:
176                         self.emit_radiant()
177                 try:
178                         self.target_selected.index( 'q3map2' )
179                 except:
180                         pass
181                 else:
182                         self.emit_q3map2()
183
184                 try:
185                         self.target_selected.index( 'setup' )
186                 except:
187                         pass
188                 else:
189                         self.Setup()
190
191         def SetupEnvironment( self, env, config, useGtk = False, useGtkGL = False, useJPEG = False, useZ = False, usePNG = False ):
192                 env['CC'] = self.cc
193                 env['CXX'] = self.cxx
194                 ( ret, xml2 ) = commands.getstatusoutput( 'xml2-config --cflags' )
195                 if ( ret != 0 ):
196                         print 'xml2-config failed'
197                         assert( False )
198                 xml2libs = commands.getoutput( 'xml2-config --libs' )
199                 env.Append( LINKFLAGS = xml2libs.split( ' ' ) )
200                 baseflags = [ '-pipe', '-Wall', '-fmessage-length=0', '-fvisibility=hidden', xml2.split( ' ' ) ]
201 #               baseflags += [ '-m32' ]
202
203                 if ( self.platform == 'Darwin' ):
204                         env.Append( CPPPATH = [ '/Developer/SDKs/MacOSX10.4u.sdk/usr/X11R6/include' ] )
205
206                 if ( useGtk ):
207                         ( ret, gtk2 ) = commands.getstatusoutput( 'pkg-config gtk+-2.0 --cflags' )
208                         if ( ret != 0 ):
209                                 print 'pkg-config gtk+-2.0 failed'
210                                 assert( False )
211                         baseflags += gtk2.split( ' ' )
212                         gtk2libs = commands.getoutput( 'pkg-config gtk+-2.0 --libs' )
213                         env.Append( LINKFLAGS = gtk2libs.split( ' ' ) )
214                 else:
215                         # always setup at least glib
216                         ( ret, glib ) = commands.getstatusoutput( 'pkg-config glib-2.0 --cflags' )
217                         if ( ret != 0 ):
218                                 print 'pkg-config glib-2.0 failed'
219                                 assert( False )
220                         baseflags += glib.split( ' ' )
221                         gliblibs = commands.getoutput( 'pkg-config glib-2.0 --libs' )
222                         env.Append( LINKFLAGS = gliblibs.split( ' ' ) )
223
224                 if ( useGtkGL ):
225                         ( ret, gtkgl ) = commands.getstatusoutput( 'pkg-config gtkglext-1.0 --cflags' )
226                         if ( ret != 0 ):
227                                 print 'pkg-config gtkglext-1.0 failed'
228                                 assert( False )
229                         baseflags += gtkgl.split( ' ' )
230                         gtkgllibs = commands.getoutput( 'pkg-config gtkglext-1.0 --libs' )
231                         env.Append( LINKFLAGS = gtkgllibs.split( ' ' ) )
232                 if ( useJPEG ):
233                         env.Append( LIBS = 'jpeg' )
234                 if ( usePNG ):
235                         pnglibs = 'png z'
236                         env.Append( LIBS = pnglibs.split( ' ' ) )
237                 if ( useZ ):
238                         env.Append( LIBS = 'z' )
239
240                 env.Append( CCFLAGS = baseflags )
241                 env.Append( CXXFLAGS = baseflags + [ '-fpermissive', '-fvisibility-inlines-hidden' ] )
242                 env.Append( CPPPATH = [ 'include', 'libs' ] )
243                 env.Append( CPPDEFINES = [ 'Q_NO_STLPORT' ] )
244                 if ( config == 'debug' ):
245                         env.Append( CFLAGS = [ '-g' ] )
246                         env.Append( CXXFLAGS = [ '-g' ] )
247                         env.Append( CPPDEFINES = [ '_DEBUG' ] )
248                 else:
249                         env.Append( CFLAGS = [ '-O2', '-fno-strict-aliasing' ] )
250                         env.Append( CXXFLAGS = [ '-O2', '-fno-strict-aliasing' ] )
251
252         def CheckoutOrUpdate( self, svnurl, path ):
253                 if ( os.path.exists( path ) ):
254                         cmd = [ 'svn', 'update', path ]
255                 else:
256                         cmd = [ 'svn', 'checkout', svnurl, path ]
257                 print( repr( cmd ) )
258                 subprocess.check_call( cmd )
259
260
261         def FetchGamePaks( self, path ):
262                 for pak in self.setup_packs:
263                         if ( pak == 'Q3Pack' or pak == 'UrTPack' or pak == 'UFOAIPack' or pak == 'Q2WPack' or pak == 'ReactionPack' ):
264                                 svnurl = 'https://zerowing.idsoftware.com/svn/radiant.gamepacks/%s/trunk' % pak
265                                 self.CheckoutOrUpdate( svnurl, os.path.join( path, 'installs', pak ) )
266
267         def Setup( self ):
268                 try:
269                         self.setup_platforms.index( 'local' )
270                 except:
271                         pass
272                 else:
273                         # special case, fetch external paks under the local install directory
274                         self.FetchGamePaks( self.install_directory )
275                 # NOTE: unrelated to self.setup_platforms - grab support files and binaries and install them
276                 if ( self.platform == 'Windows' ):
277                         backup_cwd = os.getcwd()
278                         for lib_archive in [
279                                 'gtk+-bundle-2.16.6-20100912-3-win32.zip',
280                                 'gtkglext-1.2.0-3-win32.zip',
281                                 'libxml2-2.7.3-2-win32.zip',
282                                 'jpeg-8c-4-win32.zip',
283                                 'STLport-5.2.1-4.zip'
284                                 ]:
285                                 if ( not os.path.exists( lib_archive ) ):
286                                         cmd = [ 'wget', '-N', 'http://porky.nerius.com/radiant-libs-win32/%s' % lib_archive ]
287                                         print( repr( cmd ) )
288                                         subprocess.check_call( cmd )
289                                         lib_archive_path = os.path.abspath( lib_archive )
290                                         os.chdir( os.path.dirname( backup_cwd ) )
291                                         cmd = [ 'unzip', '-o', lib_archive_path ]
292                                         print( repr( cmd ) )
293                                         subprocess.check_call( cmd )
294                                         os.chdir( backup_cwd )
295
296                         # copy all the dependent runtime data to the install directory
297                         srcdir = os.path.dirname( backup_cwd )
298                         for dll in [
299                                 'gtk-2.16.6/bin/freetype6.dll',
300                                 'gtk-2.16.6/bin/intl.dll',
301                                 'gtk-2.16.6/bin/libasprintf-0.dll',
302                                 'gtk-2.16.6/bin/libatk-1.0-0.dll',
303                                 'gtk-2.16.6/bin/libcairo-2.dll',
304                                 'gtk-2.16.6/bin/libexpat-1.dll',
305                                 'gtk-2.16.6/bin/libfontconfig-1.dll',
306                                 'gtk-2.16.6/bin/libgailutil-18.dll',
307                                 'gtk-2.16.6/bin/libgcc_s_dw2-1.dll',
308                                 'gtk-2.16.6/bin/libgdk-win32-2.0-0.dll',
309                                 'gtk-2.16.6/bin/libgdk_pixbuf-2.0-0.dll',
310                                 'gtk-2.16.6/bin/libgio-2.0-0.dll',
311                                 'gtk-2.16.6/bin/libglib-2.0-0.dll',
312                                 'gtk-2.16.6/bin/libgmodule-2.0-0.dll',
313                                 'gtk-2.16.6/bin/libgobject-2.0-0.dll',
314                                 'gtk-2.16.6/bin/libgthread-2.0-0.dll',
315                                 'gtk-2.16.6/bin/libgtk-win32-2.0-0.dll',
316                                 'gtk-2.16.6/bin/libpango-1.0-0.dll',
317                                 'gtk-2.16.6/bin/libpangocairo-1.0-0.dll',
318                                 'gtk-2.16.6/bin/libpangoft2-1.0-0.dll',
319                                 'gtk-2.16.6/bin/libpangowin32-1.0-0.dll',
320                                 'gtk-2.16.6/bin/libpng14-14.dll',
321                                 'gtk-2.16.6/bin/zlib1.dll',
322                                 'gtk-2.16.6/lib/GNU.Gettext.dll',
323                                 'gtk-2.16.6/lib/gtk-2.0/2.10.0/engines/libpixmap.dll',
324                                 'gtk-2.16.6/lib/gtk-2.0/2.10.0/engines/libwimp.dll',
325                                 'gtk-2.16.6/lib/gtk-2.0/modules/libgail.dll',
326                                 'gtkglext-1.2.0/bin/libgdkglext-win32-1.0-0.dll',
327                                 'gtkglext-1.2.0/bin/libgtkglext-win32-1.0-0.dll',
328                                 'libxml2-2.7.3/bin/libxml2-2.dll'
329                                 ]:
330                                 cmd = [ 'cp', '-v', os.path.join( srcdir, dll ), 'install' ]
331                                 print( repr( cmd ) )
332                                 subprocess.check_call( cmd )
333                         for extra in [
334                                 'gtk-2.16.6/etc',
335                                 'gtk-2.16.6/share',
336                                 'gtkglext-1.2.0/share',
337                                 'libxml2-2.7.3/share'
338                                 ]:
339                                 cmd = [ 'cp', '-r', '-v', os.path.join( srcdir, extra ), 'install' ]
340                                 print( repr( cmd ) )
341                                 subprocess.check_call( cmd )
342
343 # parse the config statement line to produce/update an existing config list
344 # the configs expose a list of keywords and accepted values, which the engine parses out
345 class ConfigParser:
346         def __init__( self ):
347                 self.operators = {}
348
349         def _processOp( self, ops ):
350                 assert( len( ops ) == 1 )
351                 op = ops.pop()
352                 if ( op == 'clear' ):
353                         self.configs = []
354                         self.current_config = None
355                 elif ( op == 'pop' ):
356                         self.configs.pop()
357                         self.current_config = None
358                 elif ( op == 'push' ):
359                         self.configs.append( self.current_config )
360                         self.current_config = Config()
361                         self._setupParser( self.current_config )
362
363         def _setupParser( self, c ):
364                 self.operators = { 'op' : self._processOp }
365                 c.setupParser( self.operators )
366
367         def _parseStatement( self, s ):
368                 statement_re = re.compile( '(.*)=(.*)' )
369                 value_list_re = re.compile( '([^,]*),?' )
370                 if ( not statement_re.match( s ) ):
371                         print 'syntax error (statement match): %s' % repr( s )
372                         return
373                 statement_split = statement_re.split( s )
374                 if ( len( statement_split ) != 4 ):
375                         print 'syntax error (statement split): %s' % repr( s )
376                         return
377                 ( foo, name, value, bar ) = statement_split
378                 value_split = value_list_re.split( value )
379                 if ( len( value_split ) < 2 or len( value_split ) % 2 != 1 ):
380                         print 'syntax error (value split): %s' % ( repr( value_split ) )
381                         return
382                 try:
383                         value_array = []
384                         value_split.reverse()
385                         value_split.pop()
386                         while ( len( value_split ) != 0 ):
387                                 value_array.append( value_split.pop() )
388                                 value_split.pop()
389                 except:
390                         print traceback.print_exception( sys.exc_type, sys.exc_value, sys.exc_traceback )
391                         print 'syntax error (value to array): %s' % ( repr( value_split ) )
392                         return
393
394                 return ( name, value_array )
395
396         def parseStatements( self, _configs, statements ):
397                 self.current_config = None
398                 self.configs = _configs
399                 if ( self.configs is None ):
400                         self.configs = []
401                 for s in statements:
402
403                         if ( self.current_config is None ):
404                                 # use a provided config, or create a default one
405                                 if ( len( self.configs ) > 0 ):
406                                         self.current_config = self.configs.pop()
407                                 else:
408                                         self.current_config = Config()
409                                 # setup the operator table for this config
410                                 # NOTE: have that in self._processOp too
411                                 self._setupParser( self.current_config )
412
413                         ret = self._parseStatement( s )
414                         if ( ret is None ):
415                                 print 'stop statement parse at %s' % repr( s )
416                                 break
417                         ( name, value_array ) = ret
418                         try:
419                                 processor = self.operators[name]
420                         except:
421                                 print 'unknown operator %s - stop statement parse at %s' % ( repr( name ), repr( s ) )
422                                 break
423                         processor( value_array )
424
425                 if ( not self.current_config is None ):
426                         self.configs.append( self.current_config )
427                 # make sure there is at least one config
428                 if ( len( self.configs ) == 0 ):
429                         print 'pushing a default config'
430                         self.configs.append( Config() )
431                 return self.configs
432
433 import unittest
434
435 class TestConfigParse( unittest.TestCase ):
436
437         def setUp( self ):
438                 self.parser = ConfigParser()
439
440         def testBasicParse( self ):
441                 # test basic config parsing
442                 # needs to cleanly stop at the first config statement that is not recognized
443                 configs = self.parser.parseStatements( None, [ 'game=missionpack', 'config=qvm', 'foobar' ] )
444                 print repr( configs )
445
446         def testMultiParse( self ):
447                 # multiple configs seperated by commas
448                 configs = self.parser.parseStatements( None, [ 'target=server,game,cgame' ] )
449                 print repr( configs )
450
451         def testOp( self ):
452                 # test the operator for multiple configs
453                 configs = self.parser.parseStatements( None, [ 'target=core', 'config=release', 'op=push', 'target=game,cgame,ui', 'config=debug' ] )
454                 print repr( configs )
455
456 if __name__ == '__main__':
457         unittest.main()