]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - config.py
Removing optimization GCC flags because q3map2 crashes on Ubuntu 10.10 32 bit,
[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                         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                         # '-O' causes q3map2 errors on Ubuntu 10.10 32 bit.
251                         env.Append( CFLAGS = [ ] )
252                         env.Append( CXXFLAGS = [ ] )
253
254         def CheckoutOrUpdate( self, svnurl, path ):
255                 if ( os.path.exists( path ) ):
256                         cmd = [ 'svn', 'update', path ]
257                 else:
258                         cmd = [ 'svn', 'checkout', svnurl, path ]
259                 print( repr( cmd ) )
260                 subprocess.check_call( cmd )
261
262
263         def FetchGamePaks( self, path ):
264                 for pak in self.setup_packs:
265                         if ( pak == 'Q3Pack' or pak == 'UrTPack' or pak == 'UFOAIPack' or pak == 'Q2WPack' or pak == 'ReactionPack' ):
266                                 svnurl = 'https://zerowing.idsoftware.com/svn/radiant.gamepacks/%s/trunk' % pak
267                                 self.CheckoutOrUpdate( svnurl, os.path.join( path, 'installs', pak ) )
268
269         def Setup( self ):
270                 try:
271                         self.setup_platforms.index( 'local' )
272                 except:
273                         pass
274                 else:
275                         # special case, fetch external paks under the local install directory
276                         self.FetchGamePaks( self.install_directory )
277                 # NOTE: unrelated to self.setup_platforms - grab support files and binaries and install them
278                 if ( self.platform == 'Windows' ):
279                         backup_cwd = os.getcwd()
280                         for lib_archive in [
281                                 'gtk+-bundle-2.16.6-20100912-3-win32.zip',
282                                 'gtkglext-1.2.0-3-win32.zip',
283                                 'libxml2-2.7.3-2-win32.zip',
284                                 'jpeg-8c-4-win32.zip',
285                                 'STLport-5.2.1-4.zip'
286                                 ]:
287                                 if ( not os.path.exists( lib_archive ) ):
288                                         cmd = [ 'wget', '-N', 'http://zerowing.idsoftware.com/files/radiant/developer/1.6.2/%s' % lib_archive ]
289                                         print( repr( cmd ) )
290                                         subprocess.check_call( cmd )
291                                         lib_archive_path = os.path.abspath( lib_archive )
292                                         os.chdir( os.path.dirname( backup_cwd ) )
293                                         cmd = [ 'unzip', '-o', lib_archive_path ]
294                                         print( repr( cmd ) )
295                                         subprocess.check_call( cmd )
296                                         os.chdir( backup_cwd )
297
298                         # copy all the dependent runtime data to the install directory
299                         srcdir = os.path.dirname( backup_cwd )
300                         for dll in [
301                                 'gtk-2.16.6/bin/freetype6.dll',
302                                 'gtk-2.16.6/bin/intl.dll',
303                                 'gtk-2.16.6/bin/libasprintf-0.dll',
304                                 'gtk-2.16.6/bin/libatk-1.0-0.dll',
305                                 'gtk-2.16.6/bin/libcairo-2.dll',
306                                 'gtk-2.16.6/bin/libexpat-1.dll',
307                                 'gtk-2.16.6/bin/libfontconfig-1.dll',
308                                 'gtk-2.16.6/bin/libgailutil-18.dll',
309                                 'gtk-2.16.6/bin/libgcc_s_dw2-1.dll',
310                                 'gtk-2.16.6/bin/libgdk-win32-2.0-0.dll',
311                                 'gtk-2.16.6/bin/libgdk_pixbuf-2.0-0.dll',
312                                 'gtk-2.16.6/bin/libgio-2.0-0.dll',
313                                 'gtk-2.16.6/bin/libglib-2.0-0.dll',
314                                 'gtk-2.16.6/bin/libgmodule-2.0-0.dll',
315                                 'gtk-2.16.6/bin/libgobject-2.0-0.dll',
316                                 'gtk-2.16.6/bin/libgthread-2.0-0.dll',
317                                 'gtk-2.16.6/bin/libgtk-win32-2.0-0.dll',
318                                 'gtk-2.16.6/bin/libpango-1.0-0.dll',
319                                 'gtk-2.16.6/bin/libpangocairo-1.0-0.dll',
320                                 'gtk-2.16.6/bin/libpangoft2-1.0-0.dll',
321                                 'gtk-2.16.6/bin/libpangowin32-1.0-0.dll',
322                                 'gtk-2.16.6/bin/libpng14-14.dll',
323                                 'gtk-2.16.6/bin/zlib1.dll',
324                                 'gtk-2.16.6/lib/GNU.Gettext.dll',
325                                 'gtk-2.16.6/lib/gtk-2.0/2.10.0/engines/libpixmap.dll',
326                                 'gtk-2.16.6/lib/gtk-2.0/2.10.0/engines/libwimp.dll',
327                                 'gtk-2.16.6/lib/gtk-2.0/modules/libgail.dll',
328                                 'gtkglext-1.2.0/bin/libgdkglext-win32-1.0-0.dll',
329                                 'gtkglext-1.2.0/bin/libgtkglext-win32-1.0-0.dll',
330                                 'libxml2-2.7.3/bin/libxml2-2.dll'
331                                 ]:
332                                 cmd = [ 'cp', '-v', os.path.join( srcdir, dll ), 'install' ]
333                                 print( repr( cmd ) )
334                                 subprocess.check_call( cmd )
335                         for extra in [
336                                 'gtk-2.16.6/etc',
337                                 'gtk-2.16.6/share',
338                                 'gtkglext-1.2.0/share',
339                                 'libxml2-2.7.3/share'
340                                 ]:
341                                 cmd = [ 'cp', '-r', '-v', os.path.join( srcdir, extra ), 'install' ]
342                                 print( repr( cmd ) )
343                                 subprocess.check_call( cmd )
344
345 # parse the config statement line to produce/update an existing config list
346 # the configs expose a list of keywords and accepted values, which the engine parses out
347 class ConfigParser:
348         def __init__( self ):
349                 self.operators = {}
350
351         def _processOp( self, ops ):
352                 assert( len( ops ) == 1 )
353                 op = ops.pop()
354                 if ( op == 'clear' ):
355                         self.configs = []
356                         self.current_config = None
357                 elif ( op == 'pop' ):
358                         self.configs.pop()
359                         self.current_config = None
360                 elif ( op == 'push' ):
361                         self.configs.append( self.current_config )
362                         self.current_config = Config()
363                         self._setupParser( self.current_config )
364
365         def _setupParser( self, c ):
366                 self.operators = { 'op' : self._processOp }
367                 c.setupParser( self.operators )
368
369         def _parseStatement( self, s ):
370                 statement_re = re.compile( '(.*)=(.*)' )
371                 value_list_re = re.compile( '([^,]*),?' )
372                 if ( not statement_re.match( s ) ):
373                         print 'syntax error (statement match): %s' % repr( s )
374                         return
375                 statement_split = statement_re.split( s )
376                 if ( len( statement_split ) != 4 ):
377                         print 'syntax error (statement split): %s' % repr( s )
378                         return
379                 ( foo, name, value, bar ) = statement_split
380                 value_split = value_list_re.split( value )
381                 if ( len( value_split ) < 2 or len( value_split ) % 2 != 1 ):
382                         print 'syntax error (value split): %s' % ( repr( value_split ) )
383                         return
384                 try:
385                         value_array = []
386                         value_split.reverse()
387                         value_split.pop()
388                         while ( len( value_split ) != 0 ):
389                                 value_array.append( value_split.pop() )
390                                 value_split.pop()
391                 except:
392                         print traceback.print_exception( sys.exc_type, sys.exc_value, sys.exc_traceback )
393                         print 'syntax error (value to array): %s' % ( repr( value_split ) )
394                         return
395
396                 return ( name, value_array )
397
398         def parseStatements( self, _configs, statements ):
399                 self.current_config = None
400                 self.configs = _configs
401                 if ( self.configs is None ):
402                         self.configs = []
403                 for s in statements:
404
405                         if ( self.current_config is None ):
406                                 # use a provided config, or create a default one
407                                 if ( len( self.configs ) > 0 ):
408                                         self.current_config = self.configs.pop()
409                                 else:
410                                         self.current_config = Config()
411                                 # setup the operator table for this config
412                                 # NOTE: have that in self._processOp too
413                                 self._setupParser( self.current_config )
414
415                         ret = self._parseStatement( s )
416                         if ( ret is None ):
417                                 print 'stop statement parse at %s' % repr( s )
418                                 break
419                         ( name, value_array ) = ret
420                         try:
421                                 processor = self.operators[name]
422                         except:
423                                 print 'unknown operator %s - stop statement parse at %s' % ( repr( name ), repr( s ) )
424                                 break
425                         processor( value_array )
426
427                 if ( not self.current_config is None ):
428                         self.configs.append( self.current_config )
429                 # make sure there is at least one config
430                 if ( len( self.configs ) == 0 ):
431                         print 'pushing a default config'
432                         self.configs.append( Config() )
433                 return self.configs
434
435 import unittest
436
437 class TestConfigParse( unittest.TestCase ):
438
439         def setUp( self ):
440                 self.parser = ConfigParser()
441
442         def testBasicParse( self ):
443                 # test basic config parsing
444                 # needs to cleanly stop at the first config statement that is not recognized
445                 configs = self.parser.parseStatements( None, [ 'game=missionpack', 'config=qvm', 'foobar' ] )
446                 print repr( configs )
447
448         def testMultiParse( self ):
449                 # multiple configs seperated by commas
450                 configs = self.parser.parseStatements( None, [ 'target=server,game,cgame' ] )
451                 print repr( configs )
452
453         def testOp( self ):
454                 # test the operator for multiple configs
455                 configs = self.parser.parseStatements( None, [ 'target=core', 'config=release', 'op=push', 'target=game,cgame,ui', 'config=debug' ] )
456                 print repr( configs )
457
458 if __name__ == '__main__':
459         unittest.main()