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