f6f22b06035eefbd817f8af1b9782efb050613d7
[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/map/map.vcproj',
116                                          'plugins/mapxml/mapxml.vcproj',
117                                          'plugins/shaders/shaders.vcproj',
118                                          'plugins/surface/surface.vcproj',
119                                          'plugins/surface_ufoai/surface_ufoai.vcproj',
120                                          'plugins/surface_quake2/surface_quake2.vcproj',
121                                          'plugins/surface_heretic2/surface_heretic2.vcproj',
122                                         # FIXME Needs splines
123                                         # 'contrib/camera/camera.vcproj',
124
125                                         # FIXME What is this? Empty dir for me - remove me?
126                                         # 'contrib/patches/patches.vcproj',
127                                         # 'plugins/archivewad/archivewad.vcproj',
128
129                                         # FIXME Doesn't compile cleanly
130                                         # 'contrib/prtview/PrtView.vcproj',
131                                          'contrib/hydratoolz/hydratoolz.vcproj',
132                                          'contrib/bobtoolz/bobToolz_gtk.vcproj',
133                                          'contrib/gtkgensurf/gtkgensurf.vcproj',
134                                          'contrib/bkgrnd2d/bkgrnd2d.vcproj'
135                                  ]:
136                                 ( libpath, libname ) = os.path.split( project )
137                                 libname = os.path.splitext( libname )[0]
138                                 shlib_objects = shlib_objects_extra['synapse']
139                                 if ( libname == 'entity' ):
140                                         shlib_objects += shlib_objects_extra['mathlib']
141                                 elif ( libname == 'model' ):
142                                         shlib_objects += shlib_objects_extra['picomodel']
143 #                               elif ( libname == 'spritemodel' ):
144 #                                       shlib_objects += shlib_objects_extra['mathlib']
145 #                               elif ( libname == 'TexTool' ):
146 #                                       shlib_objects += shlib_objects_extra['mathlib']
147                                 elif ( libname == 'map' ):
148                                         shlib_objects += shlib_objects_extra['cmdlib']
149                                 Export( 'project', 'shlib_objects' )
150                                 module = SConscript( os.path.join( build_dir, 'SConscript.module' ) )
151                                 self.InstallAs( 'install/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                         BuildDir( build_dir, '.', duplicate = 0 )
162                         lib_objects = []
163                         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' ]:
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                         self.InstallAs( 'install/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         def SetupEnvironment( self, env, config, useGtk = False, useGtkGL = False, useJPEG = False ):
186                 env['CC'] = self.cc
187                 env['CXX'] = self.cxx
188                 ( ret, xml2 ) = commands.getstatusoutput( 'xml2-config --cflags' )
189                 if ( ret != 0 ):
190                         print 'xml2-config failed'
191                         assert( False )
192                 xml2libs = commands.getoutput( 'xml2-config --libs' )
193                 env.Append( LINKFLAGS = xml2libs.split( ' ' ) )
194                 baseflags = [ '-pipe', '-Wall', '-fmessage-length=0', '-fvisibility=hidden', xml2.split( ' ' ) ]
195 #               baseflags += [ '-m32' ]
196
197                 if ( self.platform == 'Darwin' ):
198                         env.Append( CPPPATH = [ '/Developer/SDKs/MacOSX10.4u.sdk/usr/X11R6/include' ] )
199
200                 if ( useGtk ):
201                         ( ret, gtk2 ) = commands.getstatusoutput( 'pkg-config gtk+-2.0 --cflags' )
202                         if ( ret != 0 ):
203                                 print 'pkg-config gtk+-2.0 failed'
204                                 assert( False )
205                         baseflags += gtk2.split( ' ' )
206                         gtk2libs = commands.getoutput( 'pkg-config gtk+-2.0 --libs' )
207                         env.Append( LINKFLAGS = gtk2libs.split( ' ' ) )
208                 else:
209                         # always setup at least glib
210                         ( ret, glib ) = commands.getstatusoutput( 'pkg-config glib-2.0 --cflags' )
211                         if ( ret != 0 ):
212                                 print 'pkg-config glib-2.0 failed'
213                                 assert( False )
214                         baseflags += glib.split( ' ' )
215                         gliblibs = commands.getoutput( 'pkg-config glib-2.0 --libs' )
216                         env.Append( LINKFLAGS = gliblibs.split( ' ' ) )
217
218                 if ( useGtkGL ):
219                         ( ret, gtkgl ) = commands.getstatusoutput( 'pkg-config gtkglext-1.0 --cflags' )
220                         if ( ret != 0 ):
221                                 print 'pkg-config gtkglext-1.0 failed'
222                                 assert( False )
223                         baseflags += gtkgl.split( ' ' )
224                         gtkgllibs = commands.getoutput( 'pkg-config gtkglext-1.0 --libs' )
225                         env.Append( LINKFLAGS = gtkgllibs.split( ' ' ) )
226                 if ( useJPEG ):
227                         env.Append( LIBS = 'jpeg' )
228
229                 env.Append( CFLAGS = baseflags )
230                 env.Append( CXXFLAGS = baseflags + [ '-fpermissive', '-fvisibility-inlines-hidden' ] )
231                 env.Append( CPPPATH = [ 'include', 'libs' ] )
232                 env.Append( CPPDEFINES = [ 'Q_NO_STLPORT' ] )
233                 if ( config == 'debug' ):
234                         env.Append( CFLAGS = [ '-g' ] )
235                         env.Append( CXXFLAGS = [ '-g' ] )
236                         env.Append( CPPDEFINES = [ '_DEBUG' ] )
237                 else:
238                         env.Append( CFLAGS = [ '-O3', '-Winline', '-ffast-math', '-fno-unsafe-math-optimizations', '-fno-strict-aliasing' ] )
239                         env.Append( CXXFLAGS = [ '-O3', '-Winline', '-ffast-math', '-fno-unsafe-math-optimizations','-fno-strict-aliasing' ] )
240                         #env.Append( CFLAGS = [ '-march=pentium3' ] )
241
242 #               env.Append( LINKFLAGS = [ '-m32' ] )
243
244 # parse the config statement line to produce/update an existing config list
245 # the configs expose a list of keywords and accepted values, which the engine parses out
246 class ConfigParser:
247         def __init__( self ):
248                 self.operators = {}
249
250         def _processOp( self, ops ):
251                 assert( len( ops ) == 1 )
252                 op = ops.pop()
253                 if ( op == 'clear' ):
254                         self.configs = []
255                         self.current_config = None
256                 elif ( op == 'pop' ):
257                         self.configs.pop()
258                         self.current_config = None
259                 elif ( op == 'push' ):
260                         self.configs.append( self.current_config )
261                         self.current_config = Config()
262                         self._setupParser( self.current_config )
263
264         def _setupParser( self, c ):
265                 self.operators = { 'op' : self._processOp }
266                 c.setupParser( self.operators )
267
268         def _parseStatement( self, s ):
269                 statement_re = re.compile( '(.*)=(.*)' )
270                 value_list_re = re.compile( '([^,]*),?' )
271                 if ( not statement_re.match( s ) ):
272                         print 'syntax error (statement match): %s' % repr( s )
273                         return
274                 statement_split = statement_re.split( s )
275                 if ( len( statement_split ) != 4 ):
276                         print 'syntax error (statement split): %s' % repr( s )
277                         return
278                 ( foo, name, value, bar ) = statement_split
279                 value_split = value_list_re.split( value )
280                 if ( len( value_split ) < 2 or len( value_split ) % 2 != 1 ):
281                         print 'syntax error (value split): %s' % ( repr( value_split ) )
282                         return
283                 try:
284                         value_array = []
285                         value_split.reverse()
286                         value_split.pop()
287                         while ( len( value_split ) != 0 ):
288                                 value_array.append( value_split.pop() )
289                                 value_split.pop()
290                 except:
291                         print traceback.print_exception( sys.exc_type, sys.exc_value, sys.exc_traceback )
292                         print 'syntax error (value to array): %s' % ( repr( value_split ) )
293                         return
294
295                 return ( name, value_array )
296
297         def parseStatements( self, _configs, statements ):
298                 self.current_config = None
299                 self.configs = _configs
300                 if ( self.configs is None ):
301                         self.configs = []
302                 for s in statements:
303
304                         if ( self.current_config is None ):
305                                 # use a provided config, or create a default one
306                                 if ( len( self.configs ) > 0 ):
307                                         self.current_config = self.configs.pop()
308                                 else:
309                                         self.current_config = Config()
310                                 # setup the operator table for this config
311                                 # NOTE: have that in self._processOp too
312                                 self._setupParser( self.current_config )
313
314                         ret = self._parseStatement( s )
315                         if ( ret is None ):
316                                 print 'stop statement parse at %s' % repr( s )
317                                 break
318                         ( name, value_array ) = ret
319                         try:
320                                 processor = self.operators[name]
321                         except:
322                                 print 'unknown operator %s - stop statement parse at %s' % ( repr( name ), repr( s ) )
323                                 break
324                         processor( value_array )
325
326                 if ( not self.current_config is None ):
327                         self.configs.append( self.current_config )
328                 # make sure there is at least one config
329                 if ( len( self.configs ) == 0 ):
330                         print 'pushing a default config'
331                         self.configs.append( Config() )
332                 return self.configs
333
334 import unittest
335
336 class TestConfigParse( unittest.TestCase ):
337
338         def setUp( self ):
339                 self.parser = ConfigParser()
340
341         def testBasicParse( self ):
342                 # test basic config parsing
343                 # needs to cleanly stop at the first config statement that is not recognized
344                 configs = self.parser.parseStatements( None, [ 'game=missionpack', 'config=qvm', 'foobar' ] )
345                 print repr( configs )
346
347         def testMultiParse( self ):
348                 # multiple configs seperated by commas
349                 configs = self.parser.parseStatements( None, [ 'target=server,game,cgame' ] )
350                 print repr( configs )
351
352         def testOp( self ):
353                 # test the operator for multiple configs
354                 configs = self.parser.parseStatements( None, [ 'target=core', 'config=release', 'op=push', 'target=game,cgame,ui', 'config=debug' ] )
355                 print repr( configs )
356
357 if __name__ == '__main__':
358         unittest.main()