compiles on OSX (10.4.10 Intel)
[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' )
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.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
36         def __repr__( self ):
37                 return 'config: target=%s config=%s' % ( self.target_selected, self.config_selected )
38
39         def _processTarget( self, ops ):
40                 self.target_selected = ops
41
42         def _processConfig( self, ops ):
43                 self.config_selected = ops
44
45         def _processCC( self, ops ):
46                 self.cc = ops
47
48         def _processCXX( self, ops ):
49                 self.cxx = ops
50
51         def setupParser( self, operators ):
52                 operators['target'] = self._processTarget
53                 operators['config'] = self._processConfig
54                 operators['cc'] = self._processCC
55                 operators['cxx'] = self._processCXX
56
57         def emit( self ):
58                 settings = self
59                 for config_name in self.config_selected:
60                         config = {}
61                         config['name'] = config_name
62                         config['shared'] = False
63                         Export( 'utils', 'settings', 'config' )
64                         build_dir = os.path.join( 'build', config_name )
65                         BuildDir( build_dir, '.', duplicate = 0 )
66                         # left out jpeg6, splines (FIXME: I think jpeg6 is not used at all, can trash?)
67                         lib_objects = []
68                         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' ]:
69                                 Export( 'project' )
70                                 lib_objects += SConscript( os.path.join( build_dir, 'SConscript.lib' ) )
71                         Export( 'lib_objects' )
72                         radiant = SConscript( os.path.join( build_dir, 'SConscript.radiant' ) )
73                         InstallAs( 'install/radiant.bin', radiant )
74
75                         # PIC versions of the libs for the modules
76                         shlib_objects_extra = {}
77                         for project in [ 'libs/synapse/synapse.vcproj', 'libs/mathlib/mathlib.vcproj', 'libs/cmdlib/cmdlib.vcproj' ]:
78                                 ( libpath, libname ) = os.path.split( project )
79                                 libname = os.path.splitext( libname )[0]
80                                 config['shared'] = True
81                                 Export( 'project', 'config' )
82                                 build_dir = os.path.join( 'build', config_name, 'shobjs' )
83                                 BuildDir( build_dir, '.', duplicate = 0 )
84                                 shlib_objects_extra[libname] = SConscript( os.path.join( build_dir, 'SConscript.lib' ) )
85
86                         for project in [ 'plugins/vfspk3/vfspk3.vcproj',
87                                          'plugins/image/image.vcproj',
88                                          'plugins/entity/entity.vcproj',
89                                          'plugins/map/map.vcproj',
90                                          'plugins/mapxml/mapxml.vcproj',
91                                          'plugins/shaders/shaders.vcproj',
92                                          'plugins/surface/surface.vcproj'
93                                          ]:
94                                 ( libpath, libname ) = os.path.split( project )
95                                 libname = os.path.splitext( libname )[0]
96                                 shlib_objects = shlib_objects_extra['synapse']
97                                 if ( libname == 'entity' ):
98                                         shlib_objects += shlib_objects_extra['mathlib']
99                                 if ( libname == 'map' ):
100                                         shlib_objects += shlib_objects_extra['cmdlib']
101                                 Export( 'project', 'shlib_objects' )
102                                 module = SConscript( os.path.join( build_dir, 'SConscript.module' ) )
103                                 InstallAs( 'install/modules/%s.so' % libname, module )
104
105         def SetupEnvironment( self, env, config, useGtk = False, useGtkGL = False, useJPEG = False ):
106                 env['CC'] = self.cc
107                 env['CXX'] = self.cxx
108                 ( ret, xml2 ) = commands.getstatusoutput( 'xml2-config --cflags' )
109                 if ( ret != 0 ):
110                         print 'xml2-config failed'
111                         assert( False )
112                 xml2libs = commands.getoutput( 'xml2-config --libs' )
113                 env.Append( LINKFLAGS = xml2libs.split( ' ' ) )
114                 baseflags = [ '-pipe', '-Wall', '-fmessage-length=0', '-fvisibility=hidden', xml2.split( ' ' ) ]
115 #               baseflags += [ '-m32' ]
116
117                 if ( self.platform == 'Darwin' ):
118                         env.Append( CPPPATH = [ '/Developer/SDKs/MacOSX10.4u.sdk/usr/X11R6/include' ] )
119
120                 if ( useGtk ):
121                         ( ret, gtk2 ) = commands.getstatusoutput( 'pkg-config gtk+-2.0 --cflags' )
122                         if ( ret != 0 ):
123                                 print 'pkg-config gtk+-2.0 failed'
124                                 assert( False )
125                         baseflags += gtk2.split( ' ' )
126                         gtk2libs = commands.getoutput( 'pkg-config gtk+-2.0 --libs' )
127                         env.Append( LINKFLAGS = gtk2libs.split( ' ' ) )
128                 else:
129                         # always setup at least glib
130                         ( ret, glib ) = commands.getstatusoutput( 'pkg-config glib-2.0 --cflags' )
131                         if ( ret != 0 ):
132                                 print 'pkg-config glib-2.0 failed'
133                                 assert( False )
134                         baseflags += glib.split( ' ' )
135                         gliblibs = commands.getoutput( 'pkg-config glib-2.0 --libs' )
136                         env.Append( LINKFLAGS = gliblibs.split( ' ' ) )
137
138                 if ( useGtkGL ):
139                         ( ret, gtkgl ) = commands.getstatusoutput( 'pkg-config gtkglext-1.0 --cflags' )
140                         if ( ret != 0 ):
141                                 print 'pkg-config gtkglext-1.0 failed'
142                                 assert( False )
143                         baseflags += gtkgl.split( ' ' )
144                         gtkgllibs = commands.getoutput( 'pkg-config gtkglext-1.0 --libs' )
145                         env.Append( LINKFLAGS = gtkgllibs.split( ' ' ) )
146                 if ( useJPEG ):
147                         env.Append( LIBS = 'jpeg' )
148                         
149                 env.Append( CFLAGS = baseflags )
150                 env.Append( CXXFLAGS = baseflags + [ '-fpermissive', '-fvisibility-inlines-hidden' ] )
151                 env.Append( CPPPATH = [ 'include', 'libs' ] )
152                 env.Append( CPPDEFINES = [ 'Q_NO_STLPORT' ] )
153                 if ( config == 'debug' ):
154                         env.Append( CFLAGS = [ '-g' ] )
155                         env.Append( CPPDEFINES = [ '_DEBUG' ] )                         
156                 else:
157                         env.Append( CFLAGS = [ '-O3', '-Winline', '-ffast-math', '-fno-unsafe-math-optimizations' ] )
158                         #env.Append( CFLAGS = [ '-march=pentium3' ] )
159
160 #               env.Append( LINKFLAGS = [ '-m32' ] )
161
162 # parse the config statement line to produce/update an existing config list
163 # the configs expose a list of keywords and accepted values, which the engine parses out 
164 class ConfigParser:
165         def __init__( self ):
166                 self.operators = {}
167
168         def _processOp( self, ops ):
169                 assert( len( ops ) == 1 )
170                 op = ops.pop()
171                 if ( op == 'clear' ):
172                         self.configs = []
173                         self.current_config = None
174                 elif ( op == 'pop' ):
175                         self.configs.pop()
176                         self.current_config = None
177                 elif ( op == 'push' ):
178                         self.configs.append( self.current_config )
179                         self.current_config = Config()
180                         self._setupParser( self.current_config )
181
182         def _setupParser( self, c ):
183                 self.operators = { 'op' : self._processOp }
184                 c.setupParser( self.operators )
185
186         def _parseStatement( self, s ):
187                 statement_re = re.compile( '(.*)=(.*)' )
188                 value_list_re = re.compile( '([^,]*),?' )
189                 if ( not statement_re.match( s ) ):
190                         print 'syntax error (statement match): %s' % repr( s )
191                         return
192                 statement_split = statement_re.split( s )
193                 if ( len( statement_split ) != 4 ):
194                         print 'syntax error (statement split): %s' % repr( s )
195                         return
196                 ( foo, name, value, bar ) = statement_split
197                 value_split = value_list_re.split( value )
198                 if ( len( value_split ) < 2 or len( value_split ) % 2 != 1 ):
199                         print 'syntax error (value split): %s' % ( repr( value_split ) )
200                         return
201                 try:
202                         value_array = []
203                         value_split.reverse()
204                         value_split.pop()
205                         while ( len( value_split ) != 0 ):
206                                 value_array.append( value_split.pop() )
207                                 value_split.pop()                                       
208                 except:
209                         print traceback.print_exception( sys.exc_type, sys.exc_value, sys.exc_traceback )
210                         print 'syntax error (value to array): %s' % ( repr( value_split ) )
211                         return
212
213                 return ( name, value_array )            
214         
215         def parseStatements( self, _configs, statements ):
216                 self.current_config = None
217                 self.configs = _configs
218                 if ( self.configs is None ):
219                         self.configs = []
220                 for s in statements:
221
222                         if ( self.current_config is None ):
223                                 # use a provided config, or create a default one
224                                 if ( len( self.configs ) > 0 ):
225                                         self.current_config = self.configs.pop()
226                                 else:
227                                         self.current_config = Config()
228                                 # setup the operator table for this config
229                                 # NOTE: have that in self._processOp too
230                                 self._setupParser( self.current_config )
231                         
232                         ret = self._parseStatement( s )
233                         if ( ret is None ):
234                                 print 'stop statement parse at %s' % repr( s )
235                                 break
236                         ( name, value_array ) = ret
237                         try:
238                                 processor = self.operators[name]
239                         except:
240                                 print 'unknown operator %s - stop statement parse at %s' % ( repr( name ), repr( s ) )
241                                 break
242                         processor( value_array )
243
244                 if ( not self.current_config is None ):
245                         self.configs.append( self.current_config )
246                 # make sure there is at least one config
247                 if ( len( self.configs ) == 0 ):
248                         print 'pushing a default config'
249                         self.configs.append( Config() )
250                 return self.configs
251
252 import unittest
253
254 class TestConfigParse( unittest.TestCase ):
255
256         def setUp( self ):
257                 self.parser = ConfigParser()
258
259         def testBasicParse( self ):
260                 # test basic config parsing
261                 # needs to cleanly stop at the first config statement that is not recognized
262                 configs = self.parser.parseStatements( None, [ 'game=missionpack', 'config=qvm', 'foobar' ] )
263                 print repr( configs )
264
265         def testMultiParse( self ):
266                 # multiple configs seperated by commas
267                 configs = self.parser.parseStatements( None, [ 'target=server,game,cgame' ] )
268                 print repr( configs )
269
270         def testOp( self ):
271                 # test the operator for multiple configs
272                 configs = self.parser.parseStatements( None, [ 'target=core', 'config=release', 'op=push', 'target=game,cgame,ui', 'config=debug' ] )
273                 print repr( configs )
274
275 if __name__ == '__main__':
276         unittest.main()