]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - config.py
add q3map2 build to scons
[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/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/image/image.vcproj',
104                                          'plugins/entity/entity.vcproj',
105                                          'plugins/map/map.vcproj',
106                                          'plugins/mapxml/mapxml.vcproj',
107                                          'plugins/shaders/shaders.vcproj',
108                                          'plugins/surface/surface.vcproj'
109                                          ]:
110                                 ( libpath, libname ) = os.path.split( project )
111                                 libname = os.path.splitext( libname )[0]
112                                 shlib_objects = shlib_objects_extra['synapse']
113                                 if ( libname == 'entity' ):
114                                         shlib_objects += shlib_objects_extra['mathlib']
115                                 if ( libname == 'map' ):
116                                         shlib_objects += shlib_objects_extra['cmdlib']
117                                 Export( 'project', 'shlib_objects' )
118                                 module = SConscript( os.path.join( build_dir, 'SConscript.module' ) )
119                                 self.InstallAs( 'install/modules/%s.so' % libname, module )
120
121         def emit_q3map2( self ):
122                 settings = self
123                 for config_name in self.config_selected:
124                         config = {}
125                         config['name'] = config_name
126                         config['shared'] = False
127                         Export( 'utils', 'settings', 'config' )
128                         build_dir = os.path.join( 'build', config_name, 'radiant' )
129                         BuildDir( build_dir, '.', duplicate = 0 )
130                         lib_objects = []
131                         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' ]:
132                                 Export( 'project' )
133                                 lib_objects += SConscript( os.path.join( build_dir, 'SConscript.lib' ) )
134                         Export( 'lib_objects' )
135                         q3map2 = SConscript( os.path.join( build_dir, 'SConscript.q3map2' ) )
136                         self.InstallAs( 'install/q3map2.bin', q3map2 )
137                                         
138
139         def emit( self ):
140                 try:
141                         self.target_selected.index( 'radiant' )
142                 except:
143                         pass
144                 else:
145                         self.emit_radiant()
146                 try:
147                         self.target_selected.index( 'q3map2' )
148                 except:
149                         pass
150                 else:
151                         self.emit_q3map2()
152
153         def SetupEnvironment( self, env, config, useGtk = False, useGtkGL = False, useJPEG = False ):
154                 env['CC'] = self.cc
155                 env['CXX'] = self.cxx
156                 ( ret, xml2 ) = commands.getstatusoutput( 'xml2-config --cflags' )
157                 if ( ret != 0 ):
158                         print 'xml2-config failed'
159                         assert( False )
160                 xml2libs = commands.getoutput( 'xml2-config --libs' )
161                 env.Append( LINKFLAGS = xml2libs.split( ' ' ) )
162                 baseflags = [ '-pipe', '-Wall', '-fmessage-length=0', '-fvisibility=hidden', xml2.split( ' ' ) ]
163 #               baseflags += [ '-m32' ]
164
165                 if ( self.platform == 'Darwin' ):
166                         env.Append( CPPPATH = [ '/Developer/SDKs/MacOSX10.4u.sdk/usr/X11R6/include' ] )
167
168                 if ( useGtk ):
169                         ( ret, gtk2 ) = commands.getstatusoutput( 'pkg-config gtk+-2.0 --cflags' )
170                         if ( ret != 0 ):
171                                 print 'pkg-config gtk+-2.0 failed'
172                                 assert( False )
173                         baseflags += gtk2.split( ' ' )
174                         gtk2libs = commands.getoutput( 'pkg-config gtk+-2.0 --libs' )
175                         env.Append( LINKFLAGS = gtk2libs.split( ' ' ) )
176                 else:
177                         # always setup at least glib
178                         ( ret, glib ) = commands.getstatusoutput( 'pkg-config glib-2.0 --cflags' )
179                         if ( ret != 0 ):
180                                 print 'pkg-config glib-2.0 failed'
181                                 assert( False )
182                         baseflags += glib.split( ' ' )
183                         gliblibs = commands.getoutput( 'pkg-config glib-2.0 --libs' )
184                         env.Append( LINKFLAGS = gliblibs.split( ' ' ) )
185
186                 if ( useGtkGL ):
187                         ( ret, gtkgl ) = commands.getstatusoutput( 'pkg-config gtkglext-1.0 --cflags' )
188                         if ( ret != 0 ):
189                                 print 'pkg-config gtkglext-1.0 failed'
190                                 assert( False )
191                         baseflags += gtkgl.split( ' ' )
192                         gtkgllibs = commands.getoutput( 'pkg-config gtkglext-1.0 --libs' )
193                         env.Append( LINKFLAGS = gtkgllibs.split( ' ' ) )
194                 if ( useJPEG ):
195                         env.Append( LIBS = 'jpeg' )
196                         
197                 env.Append( CFLAGS = baseflags )
198                 env.Append( CXXFLAGS = baseflags + [ '-fpermissive', '-fvisibility-inlines-hidden' ] )
199                 env.Append( CPPPATH = [ 'include', 'libs' ] )
200                 env.Append( CPPDEFINES = [ 'Q_NO_STLPORT' ] )
201                 if ( config == 'debug' ):
202                         env.Append( CFLAGS = [ '-g' ] )
203                         env.Append( CXXFLAGS = [ '-g' ] )
204                         env.Append( CPPDEFINES = [ '_DEBUG' ] )                         
205                 else:
206                         env.Append( CFLAGS = [ '-O3', '-Winline', '-ffast-math', '-fno-unsafe-math-optimizations' ] )
207                         env.Append( CXXFLAGS = [ '-O3', '-Winline', '-ffast-math', '-fno-unsafe-math-optimizations' ] )
208                         #env.Append( CFLAGS = [ '-march=pentium3' ] )
209
210 #               env.Append( LINKFLAGS = [ '-m32' ] )
211
212 # parse the config statement line to produce/update an existing config list
213 # the configs expose a list of keywords and accepted values, which the engine parses out 
214 class ConfigParser:
215         def __init__( self ):
216                 self.operators = {}
217
218         def _processOp( self, ops ):
219                 assert( len( ops ) == 1 )
220                 op = ops.pop()
221                 if ( op == 'clear' ):
222                         self.configs = []
223                         self.current_config = None
224                 elif ( op == 'pop' ):
225                         self.configs.pop()
226                         self.current_config = None
227                 elif ( op == 'push' ):
228                         self.configs.append( self.current_config )
229                         self.current_config = Config()
230                         self._setupParser( self.current_config )
231
232         def _setupParser( self, c ):
233                 self.operators = { 'op' : self._processOp }
234                 c.setupParser( self.operators )
235
236         def _parseStatement( self, s ):
237                 statement_re = re.compile( '(.*)=(.*)' )
238                 value_list_re = re.compile( '([^,]*),?' )
239                 if ( not statement_re.match( s ) ):
240                         print 'syntax error (statement match): %s' % repr( s )
241                         return
242                 statement_split = statement_re.split( s )
243                 if ( len( statement_split ) != 4 ):
244                         print 'syntax error (statement split): %s' % repr( s )
245                         return
246                 ( foo, name, value, bar ) = statement_split
247                 value_split = value_list_re.split( value )
248                 if ( len( value_split ) < 2 or len( value_split ) % 2 != 1 ):
249                         print 'syntax error (value split): %s' % ( repr( value_split ) )
250                         return
251                 try:
252                         value_array = []
253                         value_split.reverse()
254                         value_split.pop()
255                         while ( len( value_split ) != 0 ):
256                                 value_array.append( value_split.pop() )
257                                 value_split.pop()                                       
258                 except:
259                         print traceback.print_exception( sys.exc_type, sys.exc_value, sys.exc_traceback )
260                         print 'syntax error (value to array): %s' % ( repr( value_split ) )
261                         return
262
263                 return ( name, value_array )            
264         
265         def parseStatements( self, _configs, statements ):
266                 self.current_config = None
267                 self.configs = _configs
268                 if ( self.configs is None ):
269                         self.configs = []
270                 for s in statements:
271
272                         if ( self.current_config is None ):
273                                 # use a provided config, or create a default one
274                                 if ( len( self.configs ) > 0 ):
275                                         self.current_config = self.configs.pop()
276                                 else:
277                                         self.current_config = Config()
278                                 # setup the operator table for this config
279                                 # NOTE: have that in self._processOp too
280                                 self._setupParser( self.current_config )
281                         
282                         ret = self._parseStatement( s )
283                         if ( ret is None ):
284                                 print 'stop statement parse at %s' % repr( s )
285                                 break
286                         ( name, value_array ) = ret
287                         try:
288                                 processor = self.operators[name]
289                         except:
290                                 print 'unknown operator %s - stop statement parse at %s' % ( repr( name ), repr( s ) )
291                                 break
292                         processor( value_array )
293
294                 if ( not self.current_config is None ):
295                         self.configs.append( self.current_config )
296                 # make sure there is at least one config
297                 if ( len( self.configs ) == 0 ):
298                         print 'pushing a default config'
299                         self.configs.append( Config() )
300                 return self.configs
301
302 import unittest
303
304 class TestConfigParse( unittest.TestCase ):
305
306         def setUp( self ):
307                 self.parser = ConfigParser()
308
309         def testBasicParse( self ):
310                 # test basic config parsing
311                 # needs to cleanly stop at the first config statement that is not recognized
312                 configs = self.parser.parseStatements( None, [ 'game=missionpack', 'config=qvm', 'foobar' ] )
313                 print repr( configs )
314
315         def testMultiParse( self ):
316                 # multiple configs seperated by commas
317                 configs = self.parser.parseStatements( None, [ 'target=server,game,cgame' ] )
318                 print repr( configs )
319
320         def testOp( self ):
321                 # test the operator for multiple configs
322                 configs = self.parser.parseStatements( None, [ 'target=core', 'config=release', 'op=push', 'target=game,cgame,ui', 'config=debug' ] )
323                 print repr( configs )
324
325 if __name__ == '__main__':
326         unittest.main()