]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - config.py
* sample plugin
[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' )
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                 if ( self.platform == 'Darwin' ):
30                         self.cc = 'gcc'
31                         self.cxx = 'g++'
32                 else:
33                         self.cc = 'gcc-4.1'
34                         self.cxx = 'g++-4.1'
35                 self.install = True
36
37         def __repr__( self ):
38                 return 'config: target=%s config=%s' % ( self.target_selected, self.config_selected )
39
40         def _processTarget( self, ops ):
41                 self.target_selected = ops
42
43         def _processConfig( self, ops ):
44                 self.config_selected = ops
45
46         def _processCC( self, ops ):
47                 self.cc = ops
48
49         def _processCXX( self, ops ):
50                 self.cxx = ops
51
52         def _processInstall( self, ops ):
53                 ops = ops[0]
54                 if ( ops == 'yes' or ops == 'true' or ops == 'True' or ops == '1' or ops == True ):
55                         self.install = True
56                         return
57                 self.install = False
58
59         def setupParser( self, operators ):
60                 operators['target'] = self._processTarget
61                 operators['config'] = self._processConfig
62                 operators['cc'] = self._processCC
63                 operators['cxx'] = self._processCXX
64                 operators['install'] = self._processInstall
65
66         def InstallAs( self, target, source ):
67                 if ( self.install ):
68                         iret = InstallAs( target, source )
69                         Default( iret )
70                 else:
71                         Default( source )
72
73         def emit_radiant( self ):
74                 settings = self
75                 for config_name in self.config_selected:
76                         config = {}
77                         config['name'] = config_name
78                         config['shared'] = False
79                         Export( 'utils', 'settings', 'config' )
80                         build_dir = os.path.join( 'build', config_name, 'radiant' )
81                         BuildDir( build_dir, '.', duplicate = 0 )
82                         # left out jpeg6, splines (FIXME: I think jpeg6 is not used at all, can trash?)
83                         lib_objects = []
84                         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' ]:
85                                 Export( 'project' )
86                                 lib_objects += SConscript( os.path.join( build_dir, 'SConscript.lib' ) )
87                         Export( 'lib_objects' )
88                         radiant = SConscript( os.path.join( build_dir, 'SConscript.radiant' ) )
89                         self.InstallAs( 'install/radiant.bin', radiant )
90
91                         # PIC versions of the libs for the modules
92                         shlib_objects_extra = {}
93                         for project in [ 'libs/synapse/synapse.vcproj', 'libs/mathlib/mathlib.vcproj', 'libs/picomodel/picomodel.vcproj', 'libs/cmdlib/cmdlib.vcproj' ]:
94                                 ( libpath, libname ) = os.path.split( project )
95                                 libname = os.path.splitext( libname )[0]
96                                 config['shared'] = True
97                                 Export( 'project', 'config' )
98                                 build_dir = os.path.join( 'build', config_name, 'shobjs' )
99                                 BuildDir( build_dir, '.', duplicate = 0 )
100                                 shlib_objects_extra[libname] = SConscript( os.path.join( build_dir, 'SConscript.lib' ) )
101
102                         for project in [ 'plugins/vfspk3/vfspk3.vcproj',
103                                          'plugins/vfspak/vfspak.vcproj',
104                                          'plugins/vfswad/vfswad.vcproj',
105                                          'plugins/eclassfgd/fgd.vcproj',
106                                          'plugins/entity/entity.vcproj',
107                                          'plugins/image/image.vcproj',
108                                          'plugins/model/model.vcproj',
109                                         # FIXME: Fix linker flags - xml2, z
110                                         # 'plugins/imagepng/imagepng.vcproj',
111                                          'plugins/imagewal/imagewal.vcproj',
112                                          'plugins/imagem8/imagem8.vcproj',
113                                          'plugins/spritemodel/spritemodel.vcproj',
114                                          'plugins/textool/TexTool.vcproj',
115                                         # 'plugins/sample/sample.vcproj',
116                                          'plugins/map/map.vcproj',
117                                          'plugins/mapxml/mapxml.vcproj',
118                                          'plugins/shaders/shaders.vcproj',
119                                          'plugins/surface/surface.vcproj',
120                                          'plugins/surface_ufoai/surface_ufoai.vcproj',
121                                          'plugins/surface_quake2/surface_quake2.vcproj',
122                                          'plugins/surface_heretic2/surface_heretic2.vcproj',
123                                         # FIXME Needs splines
124                                         # 'contrib/camera/camera.vcproj',
125
126                                         # FIXME What is this? Empty dir for me - remove me?
127                                         # 'contrib/patches/patches.vcproj',
128                                         # 'plugins/archivewad/archivewad.vcproj',
129
130                                         # FIXME Doesn't compile cleanly
131                                         # 'contrib/prtview/PrtView.vcproj',
132                                          'contrib/hydratoolz/hydratoolz.vcproj',
133                                          'contrib/bobtoolz/bobToolz_gtk.vcproj',
134                                          'contrib/gtkgensurf/gtkgensurf.vcproj',
135                                          'contrib/bkgrnd2d/bkgrnd2d.vcproj'
136                                  ]:
137                                 ( libpath, libname ) = os.path.split( project )
138                                 libname = os.path.splitext( libname )[0]
139                                 shlib_objects = shlib_objects_extra['synapse']
140                                 if ( libname == 'entity' ):
141                                         shlib_objects += shlib_objects_extra['mathlib']
142                                 elif ( libname == 'model' ):
143                                         shlib_objects += shlib_objects_extra['picomodel']
144 #                               elif ( libname == 'spritemodel' ):
145 #                                       shlib_objects += shlib_objects_extra['mathlib']
146 #                               elif ( libname == 'TexTool' ):
147 #                                       shlib_objects += shlib_objects_extra['mathlib']
148                                 elif ( libname == 'map' ):
149                                         shlib_objects += shlib_objects_extra['cmdlib']
150                                 Export( 'project', 'shlib_objects' )
151                                 module = SConscript( os.path.join( build_dir, 'SConscript.module' ) )
152                                 self.InstallAs( 'install/modules/%s.so' % libname, module )
153
154         def emit_q3map2( self ):
155                 settings = self
156                 for config_name in self.config_selected:
157                         config = {}
158                         config['name'] = config_name
159                         config['shared'] = False
160                         Export( 'utils', 'settings', 'config' )
161                         build_dir = os.path.join( 'build', config_name, 'q3map2' )
162                         BuildDir( build_dir, '.', duplicate = 0 )
163                         lib_objects = []
164                         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' ]:
165                                 Export( 'project' )
166                                 lib_objects += SConscript( os.path.join( build_dir, 'SConscript.lib' ) )
167                         Export( 'lib_objects' )
168                         q3map2 = SConscript( os.path.join( build_dir, 'SConscript.q3map2' ) )
169                         self.InstallAs( 'install/q3map2', q3map2 )
170
171
172         def emit( self ):
173                 try:
174                         self.target_selected.index( 'radiant' )
175                 except:
176                         pass
177                 else:
178                         self.emit_radiant()
179                 try:
180                         self.target_selected.index( 'q3map2' )
181                 except:
182                         pass
183                 else:
184                         self.emit_q3map2()
185
186         def SetupEnvironment( self, env, config, useGtk = False, useGtkGL = False, useJPEG = False ):
187                 env['CC'] = self.cc
188                 env['CXX'] = self.cxx
189                 ( ret, xml2 ) = commands.getstatusoutput( 'xml2-config --cflags' )
190                 if ( ret != 0 ):
191                         print 'xml2-config failed'
192                         assert( False )
193                 xml2libs = commands.getoutput( 'xml2-config --libs' )
194                 env.Append( LINKFLAGS = xml2libs.split( ' ' ) )
195                 baseflags = [ '-pipe', '-Wall', '-fmessage-length=0', '-fvisibility=hidden', xml2.split( ' ' ) ]
196 #               baseflags += [ '-m32' ]
197
198                 if ( self.platform == 'Darwin' ):
199                         env.Append( CPPPATH = [ '/Developer/SDKs/MacOSX10.4u.sdk/usr/X11R6/include' ] )
200
201                 if ( useGtk ):
202                         ( ret, gtk2 ) = commands.getstatusoutput( 'pkg-config gtk+-2.0 --cflags' )
203                         if ( ret != 0 ):
204                                 print 'pkg-config gtk+-2.0 failed'
205                                 assert( False )
206                         baseflags += gtk2.split( ' ' )
207                         gtk2libs = commands.getoutput( 'pkg-config gtk+-2.0 --libs' )
208                         env.Append( LINKFLAGS = gtk2libs.split( ' ' ) )
209                 else:
210                         # always setup at least glib
211                         ( ret, glib ) = commands.getstatusoutput( 'pkg-config glib-2.0 --cflags' )
212                         if ( ret != 0 ):
213                                 print 'pkg-config glib-2.0 failed'
214                                 assert( False )
215                         baseflags += glib.split( ' ' )
216                         gliblibs = commands.getoutput( 'pkg-config glib-2.0 --libs' )
217                         env.Append( LINKFLAGS = gliblibs.split( ' ' ) )
218
219                 if ( useGtkGL ):
220                         ( ret, gtkgl ) = commands.getstatusoutput( 'pkg-config gtkglext-1.0 --cflags' )
221                         if ( ret != 0 ):
222                                 print 'pkg-config gtkglext-1.0 failed'
223                                 assert( False )
224                         baseflags += gtkgl.split( ' ' )
225                         gtkgllibs = commands.getoutput( 'pkg-config gtkglext-1.0 --libs' )
226                         env.Append( LINKFLAGS = gtkgllibs.split( ' ' ) )
227                 if ( useJPEG ):
228                         env.Append( LIBS = 'jpeg' )
229
230                 env.Append( CFLAGS = baseflags )
231                 env.Append( CXXFLAGS = baseflags + [ '-fpermissive', '-fvisibility-inlines-hidden' ] )
232                 env.Append( CPPPATH = [ 'include', 'libs' ] )
233                 env.Append( CPPDEFINES = [ 'Q_NO_STLPORT' ] )
234                 if ( config == 'debug' ):
235                         env.Append( CFLAGS = [ '-g' ] )
236                         env.Append( CXXFLAGS = [ '-g' ] )
237                         env.Append( CPPDEFINES = [ '_DEBUG' ] )
238                 else:
239                         env.Append( CFLAGS = [ '-O3', '-Winline', '-ffast-math', '-fno-unsafe-math-optimizations', '-fno-strict-aliasing' ] )
240                         env.Append( CXXFLAGS = [ '-O3', '-Winline', '-ffast-math', '-fno-unsafe-math-optimizations','-fno-strict-aliasing' ] )
241                         #env.Append( CFLAGS = [ '-march=pentium3' ] )
242
243 #               env.Append( LINKFLAGS = [ '-m32' ] )
244
245 # parse the config statement line to produce/update an existing config list
246 # the configs expose a list of keywords and accepted values, which the engine parses out
247 class ConfigParser:
248         def __init__( self ):
249                 self.operators = {}
250
251         def _processOp( self, ops ):
252                 assert( len( ops ) == 1 )
253                 op = ops.pop()
254                 if ( op == 'clear' ):
255                         self.configs = []
256                         self.current_config = None
257                 elif ( op == 'pop' ):
258                         self.configs.pop()
259                         self.current_config = None
260                 elif ( op == 'push' ):
261                         self.configs.append( self.current_config )
262                         self.current_config = Config()
263                         self._setupParser( self.current_config )
264
265         def _setupParser( self, c ):
266                 self.operators = { 'op' : self._processOp }
267                 c.setupParser( self.operators )
268
269         def _parseStatement( self, s ):
270                 statement_re = re.compile( '(.*)=(.*)' )
271                 value_list_re = re.compile( '([^,]*),?' )
272                 if ( not statement_re.match( s ) ):
273                         print 'syntax error (statement match): %s' % repr( s )
274                         return
275                 statement_split = statement_re.split( s )
276                 if ( len( statement_split ) != 4 ):
277                         print 'syntax error (statement split): %s' % repr( s )
278                         return
279                 ( foo, name, value, bar ) = statement_split
280                 value_split = value_list_re.split( value )
281                 if ( len( value_split ) < 2 or len( value_split ) % 2 != 1 ):
282                         print 'syntax error (value split): %s' % ( repr( value_split ) )
283                         return
284                 try:
285                         value_array = []
286                         value_split.reverse()
287                         value_split.pop()
288                         while ( len( value_split ) != 0 ):
289                                 value_array.append( value_split.pop() )
290                                 value_split.pop()
291                 except:
292                         print traceback.print_exception( sys.exc_type, sys.exc_value, sys.exc_traceback )
293                         print 'syntax error (value to array): %s' % ( repr( value_split ) )
294                         return
295
296                 return ( name, value_array )
297
298         def parseStatements( self, _configs, statements ):
299                 self.current_config = None
300                 self.configs = _configs
301                 if ( self.configs is None ):
302                         self.configs = []
303                 for s in statements:
304
305                         if ( self.current_config is None ):
306                                 # use a provided config, or create a default one
307                                 if ( len( self.configs ) > 0 ):
308                                         self.current_config = self.configs.pop()
309                                 else:
310                                         self.current_config = Config()
311                                 # setup the operator table for this config
312                                 # NOTE: have that in self._processOp too
313                                 self._setupParser( self.current_config )
314
315                         ret = self._parseStatement( s )
316                         if ( ret is None ):
317                                 print 'stop statement parse at %s' % repr( s )
318                                 break
319                         ( name, value_array ) = ret
320                         try:
321                                 processor = self.operators[name]
322                         except:
323                                 print 'unknown operator %s - stop statement parse at %s' % ( repr( name ), repr( s ) )
324                                 break
325                         processor( value_array )
326
327                 if ( not self.current_config is None ):
328                         self.configs.append( self.current_config )
329                 # make sure there is at least one config
330                 if ( len( self.configs ) == 0 ):
331                         print 'pushing a default config'
332                         self.configs.append( Config() )
333                 return self.configs
334
335 import unittest
336
337 class TestConfigParse( unittest.TestCase ):
338
339         def setUp( self ):
340                 self.parser = ConfigParser()
341
342         def testBasicParse( self ):
343                 # test basic config parsing
344                 # needs to cleanly stop at the first config statement that is not recognized
345                 configs = self.parser.parseStatements( None, [ 'game=missionpack', 'config=qvm', 'foobar' ] )
346                 print repr( configs )
347
348         def testMultiParse( self ):
349                 # multiple configs seperated by commas
350                 configs = self.parser.parseStatements( None, [ 'target=server,game,cgame' ] )
351                 print repr( configs )
352
353         def testOp( self ):
354                 # test the operator for multiple configs
355                 configs = self.parser.parseStatements( None, [ 'target=core', 'config=release', 'op=push', 'target=game,cgame,ui', 'config=debug' ] )
356                 print repr( configs )
357
358 if __name__ == '__main__':
359         unittest.main()