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