]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - config.py
* removed a lot of HACKs for several games by merging the enginepath_win32, enginepat...
[xonotic/netradiant.git] / config.py
1 import sys, traceback, platform, re, commands, platform
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' ]
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.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( CFLAGS = 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                         # NOTE: check the svnurl matches?
254                         cmd = 'svn update "%s"' % path
255                         print cmd
256                 else:
257                         cmd = 'svn checkout %s "%s"' % ( svnurl, path )
258                 ret = os.system( cmd )
259                 if ( ret != 0 ):
260                         raise 'checkout or update failed'
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' ):
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                 for platform in self.setup_platforms:
271                         if ( platform == 'local' ):
272                                 # special case, fetch external paks under the local install directory
273                                 self.FetchGamePaks( self.install_directory )
274
275 # parse the config statement line to produce/update an existing config list
276 # the configs expose a list of keywords and accepted values, which the engine parses out
277 class ConfigParser:
278         def __init__( self ):
279                 self.operators = {}
280
281         def _processOp( self, ops ):
282                 assert( len( ops ) == 1 )
283                 op = ops.pop()
284                 if ( op == 'clear' ):
285                         self.configs = []
286                         self.current_config = None
287                 elif ( op == 'pop' ):
288                         self.configs.pop()
289                         self.current_config = None
290                 elif ( op == 'push' ):
291                         self.configs.append( self.current_config )
292                         self.current_config = Config()
293                         self._setupParser( self.current_config )
294
295         def _setupParser( self, c ):
296                 self.operators = { 'op' : self._processOp }
297                 c.setupParser( self.operators )
298
299         def _parseStatement( self, s ):
300                 statement_re = re.compile( '(.*)=(.*)' )
301                 value_list_re = re.compile( '([^,]*),?' )
302                 if ( not statement_re.match( s ) ):
303                         print 'syntax error (statement match): %s' % repr( s )
304                         return
305                 statement_split = statement_re.split( s )
306                 if ( len( statement_split ) != 4 ):
307                         print 'syntax error (statement split): %s' % repr( s )
308                         return
309                 ( foo, name, value, bar ) = statement_split
310                 value_split = value_list_re.split( value )
311                 if ( len( value_split ) < 2 or len( value_split ) % 2 != 1 ):
312                         print 'syntax error (value split): %s' % ( repr( value_split ) )
313                         return
314                 try:
315                         value_array = []
316                         value_split.reverse()
317                         value_split.pop()
318                         while ( len( value_split ) != 0 ):
319                                 value_array.append( value_split.pop() )
320                                 value_split.pop()
321                 except:
322                         print traceback.print_exception( sys.exc_type, sys.exc_value, sys.exc_traceback )
323                         print 'syntax error (value to array): %s' % ( repr( value_split ) )
324                         return
325
326                 return ( name, value_array )
327
328         def parseStatements( self, _configs, statements ):
329                 self.current_config = None
330                 self.configs = _configs
331                 if ( self.configs is None ):
332                         self.configs = []
333                 for s in statements:
334
335                         if ( self.current_config is None ):
336                                 # use a provided config, or create a default one
337                                 if ( len( self.configs ) > 0 ):
338                                         self.current_config = self.configs.pop()
339                                 else:
340                                         self.current_config = Config()
341                                 # setup the operator table for this config
342                                 # NOTE: have that in self._processOp too
343                                 self._setupParser( self.current_config )
344
345                         ret = self._parseStatement( s )
346                         if ( ret is None ):
347                                 print 'stop statement parse at %s' % repr( s )
348                                 break
349                         ( name, value_array ) = ret
350                         try:
351                                 processor = self.operators[name]
352                         except:
353                                 print 'unknown operator %s - stop statement parse at %s' % ( repr( name ), repr( s ) )
354                                 break
355                         processor( value_array )
356
357                 if ( not self.current_config is None ):
358                         self.configs.append( self.current_config )
359                 # make sure there is at least one config
360                 if ( len( self.configs ) == 0 ):
361                         print 'pushing a default config'
362                         self.configs.append( Config() )
363                 return self.configs
364
365 import unittest
366
367 class TestConfigParse( unittest.TestCase ):
368
369         def setUp( self ):
370                 self.parser = ConfigParser()
371
372         def testBasicParse( self ):
373                 # test basic config parsing
374                 # needs to cleanly stop at the first config statement that is not recognized
375                 configs = self.parser.parseStatements( None, [ 'game=missionpack', 'config=qvm', 'foobar' ] )
376                 print repr( configs )
377
378         def testMultiParse( self ):
379                 # multiple configs seperated by commas
380                 configs = self.parser.parseStatements( None, [ 'target=server,game,cgame' ] )
381                 print repr( configs )
382
383         def testOp( self ):
384                 # test the operator for multiple configs
385                 configs = self.parser.parseStatements( None, [ 'target=core', 'config=release', 'op=push', 'target=game,cgame,ui', 'config=debug' ] )
386                 print repr( configs )
387
388 if __name__ == '__main__':
389         unittest.main()