]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - config.py
get the basics of a new scons build system together
[xonotic/netradiant.git] / config.py
1 import sys, traceback, platform, re, commands
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' )
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' ]
26                 self.config_selected = [ 'release' ]
27                 # those are global to each config
28                 self.cc = 'gcc-4.1'
29                 self.cxx = 'g++-4.1'
30
31         def __repr__( self ):
32                 return 'config: target=%s config=%s' % ( self.target_selected, self.config_selected )
33
34         def _processTarget( self, ops ):
35                 self.target_selected = ops
36
37         def _processConfig( self, ops ):
38                 self.config_selected = ops
39
40         def _processCC( self, ops ):
41                 self.cc = ops
42
43         def _processCXX( self, ops ):
44                 self.cxx = ops
45
46         def setupParser( self, operators ):
47                 operators['target'] = self._processTarget
48                 operators['config'] = self._processConfig
49                 operators['cc'] = self._processCC
50                 operators['cxx'] = self._processCXX
51
52         def emit( self ):
53                 settings = self
54                 for config in self.config_selected:
55                         Export( 'utils', 'settings', 'config' )
56                         build_dir = os.path.join( 'build', config )
57                         BuildDir( build_dir, '.', duplicate = 0 )
58                         # left out jpeg6, splines
59                         lib_objects = []
60                         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' ]:
61                                 Export( 'project' )
62                                 lib_objects += SConscript( os.path.join( build_dir, 'SConscript.lib' ) )
63                         Export( 'lib_objects' )
64                         SConscript( os.path.join( build_dir, 'SConscript.radiant' ) )
65
66         def SetupEnvironment( self, env, config, useGtk = False, useGtkGL = False ):
67                 env['CC'] = self.cc
68                 env['CXX'] = self.cxx
69                 ( ret, xml2 ) = commands.getstatusoutput( 'xml2-config --cflags' )
70                 if ( ret != 0 ):
71                         print 'xml2-config failed'
72                         assert( False )
73                 xml2libs = commands.getoutput( 'xml2-config --libs' )
74                 env.Append( LINKFLAGS = xml2libs.split( ' ' ) )
75                 baseflags = [ '-m32', '-pipe', '-Wall', '-fmessage-length=0', '-fvisibility=hidden', xml2.split( ' ' ) ]
76                 if ( useGtk ):
77                         ( ret, gtk2 ) = commands.getstatusoutput( 'pkg-config gtk+-2.0 --cflags' )
78                         if ( ret != 0 ):
79                                 print 'pkg-config gtk+-2.0 failed'
80                                 assert( False )
81                         baseflags += gtk2.split( ' ' )
82                         gtk2libs = commands.getoutput( 'pkg-config gtk+-2.0 --libs' )
83                         env.Append( LINKFLAGS = gtk2libs.split( ' ' ) )
84                 else:
85                         ( ret, glib ) = commands.getstatusoutput( 'pkg-config glib-2.0 --cflags' )
86                         if ( ret != 0 ):
87                                 print 'pkg-config glib-2.0 failed'
88                                 assert( False )
89                         baseflags += glib.split( ' ' )
90                 if ( useGtkGL ):
91                         ( ret, gtkgl ) = commands.getstatusoutput( 'pkg-config gtkglext-1.0 --cflags' )
92                         if ( ret != 0 ):
93                                 print 'pkg-config gtkglext-1.0 failed'
94                                 assert( False )
95                         baseflags += gtkgl.split( ' ' )
96                         gtkgllibs = commands.getoutput( 'pkg-config gtkglext-1.0 --libs' )
97                         env.Append( LINKFLAGS = gtkgllibs.split( ' ' ) )
98                         
99                 env.Append( CFLAGS = baseflags )
100                 env.Append( CXXFLAGS = baseflags + [ '-fpermissive', '-fvisibility-inlines-hidden' ] )
101                 env.Append( CPPPATH = [ 'include', 'libs' ] )
102                 env.Append( CPPDEFINES = [ 'Q_NO_STLPORT' ] )
103                 if ( config == 'debug' ):
104                         env.Append( CFLAGS = [ '-g' ] )
105                         env.Append( CPPDEFINES = [ '_DEBUG' ] )                         
106                 else:
107                         env.Append( CFLAGS = [ '-O3', '-march=pentium3', '-Winline', '-ffast-math', '-fno-unsafe-math-optimizations' ] )
108
109 # parse the config statement line to produce/update an existing config list
110 # the configs expose a list of keywords and accepted values, which the engine parses out 
111 class ConfigParser:
112         def __init__( self ):
113                 self.operators = {}
114
115         def _processOp( self, ops ):
116                 assert( len( ops ) == 1 )
117                 op = ops.pop()
118                 if ( op == 'clear' ):
119                         self.configs = []
120                         self.current_config = None
121                 elif ( op == 'pop' ):
122                         self.configs.pop()
123                         self.current_config = None
124                 elif ( op == 'push' ):
125                         self.configs.append( self.current_config )
126                         self.current_config = Config()
127                         self._setupParser( self.current_config )
128
129         def _setupParser( self, c ):
130                 self.operators = { 'op' : self._processOp }
131                 c.setupParser( self.operators )
132
133         def _parseStatement( self, s ):
134                 statement_re = re.compile( '(.*)=(.*)' )
135                 value_list_re = re.compile( '([^,]*),?' )
136                 if ( not statement_re.match( s ) ):
137                         print 'syntax error (statement match): %s' % repr( s )
138                         return
139                 statement_split = statement_re.split( s )
140                 if ( len( statement_split ) != 4 ):
141                         print 'syntax error (statement split): %s' % repr( s )
142                         return
143                 ( foo, name, value, bar ) = statement_split
144                 value_split = value_list_re.split( value )
145                 if ( len( value_split ) < 2 or len( value_split ) % 2 != 1 ):
146                         print 'syntax error (value split): %s' % ( repr( value_split ) )
147                         return
148                 try:
149                         value_array = []
150                         value_split.reverse()
151                         value_split.pop()
152                         while ( len( value_split ) != 0 ):
153                                 value_array.append( value_split.pop() )
154                                 value_split.pop()                                       
155                 except:
156                         print traceback.print_exception( sys.exc_type, sys.exc_value, sys.exc_traceback )
157                         print 'syntax error (value to array): %s' % ( repr( value_split ) )
158                         return
159
160                 return ( name, value_array )            
161         
162         def parseStatements( self, _configs, statements ):
163                 self.current_config = None
164                 self.configs = _configs
165                 if ( self.configs is None ):
166                         self.configs = []
167                 for s in statements:
168
169                         if ( self.current_config is None ):
170                                 # use a provided config, or create a default one
171                                 if ( len( self.configs ) > 0 ):
172                                         self.current_config = self.configs.pop()
173                                 else:
174                                         self.current_config = Config()
175                                 # setup the operator table for this config
176                                 # NOTE: have that in self._processOp too
177                                 self._setupParser( self.current_config )
178                         
179                         ret = self._parseStatement( s )
180                         if ( ret is None ):
181                                 print 'stop statement parse at %s' % repr( s )
182                                 break
183                         ( name, value_array ) = ret
184                         try:
185                                 processor = self.operators[name]
186                         except:
187                                 print 'unknown operator %s - stop statement parse at %s' % ( repr( name ), repr( s ) )
188                                 break
189                         processor( value_array )
190
191                 if ( not self.current_config is None ):
192                         self.configs.append( self.current_config )
193                 # make sure there is at least one config
194                 if ( len( self.configs ) == 0 ):
195                         print 'pushing a default config'
196                         self.configs.append( Config() )
197                 return self.configs
198
199 import unittest
200
201 class TestConfigParse( unittest.TestCase ):
202
203         def setUp( self ):
204                 self.parser = ConfigParser()
205
206         def testBasicParse( self ):
207                 # test basic config parsing
208                 # needs to cleanly stop at the first config statement that is not recognized
209                 configs = self.parser.parseStatements( None, [ 'game=missionpack', 'config=qvm', 'foobar' ] )
210                 print repr( configs )
211
212         def testMultiParse( self ):
213                 # multiple configs seperated by commas
214                 configs = self.parser.parseStatements( None, [ 'target=server,game,cgame' ] )
215                 print repr( configs )
216
217         def testOp( self ):
218                 # test the operator for multiple configs
219                 configs = self.parser.parseStatements( None, [ 'target=core', 'config=release', 'op=push', 'target=game,cgame,ui', 'config=debug' ] )
220                 print repr( configs )
221
222 if __name__ == '__main__':
223         unittest.main()