- Updated Windows build doc, very slight revision. Changed 37 targets to 38.
[xonotic/netradiant.git] / config.py
1 import sys, traceback, platform, re, commands, platform, subprocess
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', 'setup' )
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                 # platforms for which to assemble a setup
34                 self.setup_platforms = [ 'local', 'x86', 'x64', 'win32' ]
35                 # paks to assemble in the setup
36                 self.setup_packs = [ 'Q3Pack', 'UrTPack', 'UFOAIPack', 'Q2WPack', 'ReactionPack' ]
37
38         def __repr__( self ):
39                 return 'config: target=%s config=%s' % ( self.target_selected, self.config_selected )
40
41         def _processTarget( self, ops ):
42                 self.target_selected = ops
43
44         def _processConfig( self, ops ):
45                 self.config_selected = ops
46
47         def _processCC( self, ops ):
48                 self.cc = ops
49
50         def _processCXX( self, ops ):
51                 self.cxx = ops
52
53         def _processInstallDir( self, ops ):
54                 self.install_directory = os.path.normpath( os.path.expanduser( ops[0] ) )
55
56         def _processSetupPlatforms( self, ops ):
57                 self.setup_platforms = ops
58
59         def _processSetupPacks( self, ops ):
60                 self.setup_packs = ops
61
62         def setupParser( self, operators ):
63                 operators['target'] = self._processTarget
64                 operators['config'] = self._processConfig
65                 operators['cc'] = self._processCC
66                 operators['cxx'] = self._processCXX
67                 operators['install_directory'] = self._processInstallDir
68                 operators['setup_platforms'] = self._processSetupPlatforms
69                 operators['setup_packs'] = self._processSetupPacks
70
71         def emit_radiant( self ):
72                 settings = self
73                 for config_name in self.config_selected:
74                         config = {}
75                         config['name'] = config_name
76                         config['shared'] = False
77                         Export( 'utils', 'settings', 'config' )
78                         build_dir = os.path.join( 'build', config_name, 'radiant' )
79                         BuildDir( build_dir, '.', duplicate = 0 )
80                         lib_objects = []
81                         for project in [ 'libs/synapse/synapse.vcproj', 'libs/cmdlib/cmdlib.vcproj', 'libs/mathlib/mathlib.vcproj', 'libs/l_net/l_net.vcproj' ]:
82                                 Export( 'project' )
83                                 lib_objects += SConscript( os.path.join( build_dir, 'SConscript.lib' ) )
84                         Export( 'lib_objects' )
85                         radiant = SConscript( os.path.join( build_dir, 'SConscript.radiant' ) )
86                         Default( InstallAs( os.path.join( self.install_directory, 'radiant.bin' ), radiant ) )
87
88                         # PIC versions of the libs for the modules
89                         shlib_objects_extra = {}
90                         for project in [ 'libs/synapse/synapse.vcproj', 'libs/mathlib/mathlib.vcproj', 'libs/picomodel/picomodel.vcproj', 'libs/cmdlib/cmdlib.vcproj', 'libs/splines/splines.vcproj' ]:
91                                 ( libpath, libname ) = os.path.split( project )
92                                 libname = os.path.splitext( libname )[0]
93                                 config['shared'] = True
94                                 Export( 'project', 'config' )
95                                 build_dir = os.path.join( 'build', config_name, 'shobjs' )
96                                 BuildDir( build_dir, '.', duplicate = 0 )
97                                 shlib_objects_extra[libname] = SConscript( os.path.join( build_dir, 'SConscript.lib' ) )
98
99                         for project in [ 'plugins/vfspk3/vfspk3.vcproj',
100                                          'plugins/vfspak/vfspak.vcproj',
101                                          'plugins/vfswad/vfswad.vcproj',
102                                          'plugins/eclassfgd/fgd.vcproj',
103                                          'plugins/entity/entity.vcproj',
104                                          'plugins/image/image.vcproj',
105                                          'plugins/model/model.vcproj',
106                                          'plugins/imagepng/imagepng.vcproj',
107                                          'plugins/imagewal/imagewal.vcproj',
108                                          'plugins/imagehl/imagehl.vcproj',
109                                          'plugins/imagem8/imagem8.vcproj',
110                                          'plugins/spritemodel/spritemodel.vcproj',
111                                          'plugins/textool/textool.vcproj',
112                                          'plugins/map/map.vcproj',
113                                          'plugins/mapxml/mapxml.vcproj',
114                                          'plugins/shaders/shaders.vcproj',
115                                          'plugins/surface/surface.vcproj',
116                                          'plugins/surface_ufoai/surface_ufoai.vcproj',
117                                          'plugins/surface_quake2/surface_quake2.vcproj',
118                                          'plugins/surface_heretic2/surface_heretic2.vcproj',
119                                          'contrib/camera/camera.vcproj',
120                                          'contrib/prtview/prtview.vcproj',
121                                          'contrib/hydratoolz/hydratoolz.vcproj',
122                                          'contrib/bobtoolz/bobtoolz.vcproj',
123                                          'contrib/gtkgensurf/gtkgensurf.vcproj',
124                                          'contrib/ufoai/ufoai.vcproj',
125                                          'contrib/bkgrnd2d/bkgrnd2d.vcproj'
126                                  ]:
127                                 ( libpath, libname ) = os.path.split( project )
128                                 libname = os.path.splitext( libname )[0]
129                                 # The old code assigned shlib_objects to shlib_objects_extra['synapse'],
130                                 # and this resulted in a non-copy.  Stuff is added to shlib_objects below.
131                                 # So we need the explicit copy so we don't modify shlib_objects_extra['synapse'].
132                                 shlib_objects = shlib_objects_extra['synapse'][:]
133                                 if ( libname == 'camera' ):
134                                         shlib_objects += shlib_objects_extra['splines']
135                                 elif ( libname == 'entity' ):
136                                         shlib_objects += shlib_objects_extra['mathlib']
137                                 elif ( libname == 'map' ):
138                                         shlib_objects += shlib_objects_extra['cmdlib']
139                                 elif ( libname == 'model' ):
140                                         shlib_objects += shlib_objects_extra['picomodel']
141                                         shlib_objects += shlib_objects_extra['mathlib']
142                                 elif ( libname == 'spritemodel' ):
143                                         shlib_objects += shlib_objects_extra['mathlib']
144                                 elif ( libname == 'textool' ):
145                                         shlib_objects += shlib_objects_extra['mathlib']
146                                 elif ( libname == 'bobtoolz' ):
147                                         shlib_objects += shlib_objects_extra['mathlib']
148                                         shlib_objects += shlib_objects_extra['cmdlib']
149                                 Export( 'project', 'shlib_objects' )
150                                 module = SConscript( os.path.join( build_dir, 'SConscript.module' ) )
151                                 Default( InstallAs( os.path.join( self.install_directory, '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 [ 'tools/quake3/common/quake3-common.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                         Default( InstallAs( os.path.join( self.install_directory, '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                 try:
186                         self.target_selected.index( 'setup' )
187                 except:
188                         pass
189                 else:
190                         self.Setup()
191
192         def SetupEnvironment( self, env, config, useGtk = False, useGtkGL = False, useJPEG = False, useZ = False, usePNG = False ):
193                 env['CC'] = self.cc
194                 env['CXX'] = self.cxx
195                 ( ret, xml2 ) = commands.getstatusoutput( 'xml2-config --cflags' )
196                 if ( ret != 0 ):
197                         print 'xml2-config failed'
198                         assert( False )
199                 xml2libs = commands.getoutput( 'xml2-config --libs' )
200                 env.Append( LINKFLAGS = xml2libs.split( ' ' ) )
201                 baseflags = [ '-pipe', '-Wall', '-fmessage-length=0', '-fvisibility=hidden', xml2.split( ' ' ) ]
202 #               baseflags += [ '-m32' ]
203
204                 if ( self.platform == 'Darwin' ):
205                         env.Append( CPPPATH = [ '/Developer/SDKs/MacOSX10.4u.sdk/usr/X11R6/include' ] )
206
207                 if ( useGtk ):
208                         ( ret, gtk2 ) = commands.getstatusoutput( 'pkg-config gtk+-2.0 --cflags' )
209                         if ( ret != 0 ):
210                                 print 'pkg-config gtk+-2.0 failed'
211                                 assert( False )
212                         baseflags += gtk2.split( ' ' )
213                         gtk2libs = commands.getoutput( 'pkg-config gtk+-2.0 --libs' )
214                         env.Append( LINKFLAGS = gtk2libs.split( ' ' ) )
215                 else:
216                         # always setup at least glib
217                         ( ret, glib ) = commands.getstatusoutput( 'pkg-config glib-2.0 --cflags' )
218                         if ( ret != 0 ):
219                                 print 'pkg-config glib-2.0 failed'
220                                 assert( False )
221                         baseflags += glib.split( ' ' )
222                         gliblibs = commands.getoutput( 'pkg-config glib-2.0 --libs' )
223                         env.Append( LINKFLAGS = gliblibs.split( ' ' ) )
224
225                 if ( useGtkGL ):
226                         ( ret, gtkgl ) = commands.getstatusoutput( 'pkg-config gtkglext-1.0 --cflags' )
227                         if ( ret != 0 ):
228                                 print 'pkg-config gtkglext-1.0 failed'
229                                 assert( False )
230                         baseflags += gtkgl.split( ' ' )
231                         gtkgllibs = commands.getoutput( 'pkg-config gtkglext-1.0 --libs' )
232                         env.Append( LINKFLAGS = gtkgllibs.split( ' ' ) )
233                 if ( useJPEG ):
234                         env.Append( LIBS = 'jpeg' )
235                 if ( usePNG ):
236                         pnglibs = 'png z'
237                         env.Append( LIBS = pnglibs.split( ' ' ) )
238                 if ( useZ ):
239                         env.Append( LIBS = 'z' )
240
241                 env.Append( CCFLAGS = baseflags )
242                 env.Append( CXXFLAGS = baseflags + [ '-fpermissive', '-fvisibility-inlines-hidden' ] )
243                 env.Append( CPPPATH = [ 'include', 'libs' ] )
244                 env.Append( CPPDEFINES = [ 'Q_NO_STLPORT' ] )
245                 if ( config == 'debug' ):
246                         env.Append( CFLAGS = [ '-g' ] )
247                         env.Append( CXXFLAGS = [ '-g' ] )
248                         env.Append( CPPDEFINES = [ '_DEBUG' ] )
249                 else:
250                         env.Append( CFLAGS = [ '-O2', '-fno-strict-aliasing' ] )
251                         env.Append( CXXFLAGS = [ '-O2', '-fno-strict-aliasing' ] )
252
253         def CheckoutOrUpdate( self, svnurl, path ):
254                 if ( os.path.exists( path ) ):
255                         cmd = [ 'svn', 'update', path ]
256                 else:
257                         cmd = [ 'svn', 'checkout', svnurl, path ]
258                 print( repr( cmd ) )
259                 subprocess.check_call( cmd )
260
261
262         def FetchGamePaks( self, path ):
263                 for pak in self.setup_packs:
264                         if ( pak == 'Q3Pack' or pak == 'UrTPack' or pak == 'UFOAIPack' or pak == 'Q2WPack' or pak == 'ReactionPack' ):
265                                 svnurl = 'https://zerowing.idsoftware.com/svn/radiant.gamepacks/%s/trunk' % pak
266                                 self.CheckoutOrUpdate( svnurl, os.path.join( path, 'installs', pak ) )
267
268         def Setup( self ):
269                 try:
270                         self.setup_platforms.index( 'local' )
271                 except:
272                         pass
273                 else:
274                         # special case, fetch external paks under the local install directory
275                         self.FetchGamePaks( self.install_directory )
276                 # NOTE: unrelated to self.setup_platforms - grab support files and binaries and install them
277                 if ( self.platform == 'Windows' ):
278                         backup_cwd = os.getcwd()
279                         for lib_archive in [
280                                 'gtk+-bundle-2.16.6-20100912-3-win32.zip',
281                                 'gtkglext-1.2.0-3-win32.zip',
282                                 'libxml2-2.7.3-2-win32.zip',
283                                 'jpeg-8c-4-win32.zip',
284                                 'STLport-5.2.1-4.zip'
285                                 ]:
286                                 if ( not os.path.exists( lib_archive ) ):
287                                         cmd = [ 'wget', '-N', 'http://porky.nerius.com/radiant-libs-win32/%s' % lib_archive ]
288                                         print( repr( cmd ) )
289                                         subprocess.check_call( cmd )
290                                         lib_archive_path = os.path.abspath( lib_archive )
291                                         os.chdir( os.path.dirname( backup_cwd ) )
292                                         cmd = [ 'unzip', '-o', lib_archive_path ]
293                                         print( repr( cmd ) )
294                                         subprocess.check_call( cmd )
295                                         os.chdir( backup_cwd )
296
297                         # copy all the dependent runtime data to the install directory
298                         srcdir = os.path.dirname( backup_cwd )
299                         for dll in [
300                                 'gtk-2.16.6/bin/freetype6.dll',
301                                 'gtk-2.16.6/bin/intl.dll',
302                                 'gtk-2.16.6/bin/libasprintf-0.dll',
303                                 'gtk-2.16.6/bin/libatk-1.0-0.dll',
304                                 'gtk-2.16.6/bin/libcairo-2.dll',
305                                 'gtk-2.16.6/bin/libexpat-1.dll',
306                                 'gtk-2.16.6/bin/libfontconfig-1.dll',
307                                 'gtk-2.16.6/bin/libgailutil-18.dll',
308                                 'gtk-2.16.6/bin/libgcc_s_dw2-1.dll',
309                                 'gtk-2.16.6/bin/libgdk-win32-2.0-0.dll',
310                                 'gtk-2.16.6/bin/libgdk_pixbuf-2.0-0.dll',
311                                 'gtk-2.16.6/bin/libgio-2.0-0.dll',
312                                 'gtk-2.16.6/bin/libglib-2.0-0.dll',
313                                 'gtk-2.16.6/bin/libgmodule-2.0-0.dll',
314                                 'gtk-2.16.6/bin/libgobject-2.0-0.dll',
315                                 'gtk-2.16.6/bin/libgthread-2.0-0.dll',
316                                 'gtk-2.16.6/bin/libgtk-win32-2.0-0.dll',
317                                 'gtk-2.16.6/bin/libpango-1.0-0.dll',
318                                 'gtk-2.16.6/bin/libpangocairo-1.0-0.dll',
319                                 'gtk-2.16.6/bin/libpangoft2-1.0-0.dll',
320                                 'gtk-2.16.6/bin/libpangowin32-1.0-0.dll',
321                                 'gtk-2.16.6/bin/libpng14-14.dll',
322                                 'gtk-2.16.6/bin/zlib1.dll',
323                                 'gtk-2.16.6/lib/GNU.Gettext.dll',
324                                 'gtk-2.16.6/lib/gtk-2.0/2.10.0/engines/libpixmap.dll',
325                                 'gtk-2.16.6/lib/gtk-2.0/2.10.0/engines/libwimp.dll',
326                                 'gtk-2.16.6/lib/gtk-2.0/modules/libgail.dll',
327                                 'gtkglext-1.2.0/bin/libgdkglext-win32-1.0-0.dll',
328                                 'gtkglext-1.2.0/bin/libgtkglext-win32-1.0-0.dll',
329                                 'libxml2-2.7.3/bin/libxml2-2.dll'
330                                 ]:
331                                 cmd = [ 'cp', '-v', os.path.join( srcdir, dll ), 'install' ]
332                                 print( repr( cmd ) )
333                                 subprocess.check_call( cmd )
334                         for extra in [
335                                 'gtk-2.16.6/etc',
336                                 'gtk-2.16.6/share',
337                                 'gtkglext-1.2.0/share',
338                                 'libxml2-2.7.3/share'
339                                 ]:
340                                 cmd = [ 'cp', '-r', '-v', os.path.join( srcdir, extra ), 'install' ]
341                                 print( repr( cmd ) )
342                                 subprocess.check_call( cmd )
343
344 # parse the config statement line to produce/update an existing config list
345 # the configs expose a list of keywords and accepted values, which the engine parses out
346 class ConfigParser:
347         def __init__( self ):
348                 self.operators = {}
349
350         def _processOp( self, ops ):
351                 assert( len( ops ) == 1 )
352                 op = ops.pop()
353                 if ( op == 'clear' ):
354                         self.configs = []
355                         self.current_config = None
356                 elif ( op == 'pop' ):
357                         self.configs.pop()
358                         self.current_config = None
359                 elif ( op == 'push' ):
360                         self.configs.append( self.current_config )
361                         self.current_config = Config()
362                         self._setupParser( self.current_config )
363
364         def _setupParser( self, c ):
365                 self.operators = { 'op' : self._processOp }
366                 c.setupParser( self.operators )
367
368         def _parseStatement( self, s ):
369                 statement_re = re.compile( '(.*)=(.*)' )
370                 value_list_re = re.compile( '([^,]*),?' )
371                 if ( not statement_re.match( s ) ):
372                         print 'syntax error (statement match): %s' % repr( s )
373                         return
374                 statement_split = statement_re.split( s )
375                 if ( len( statement_split ) != 4 ):
376                         print 'syntax error (statement split): %s' % repr( s )
377                         return
378                 ( foo, name, value, bar ) = statement_split
379                 value_split = value_list_re.split( value )
380                 if ( len( value_split ) < 2 or len( value_split ) % 2 != 1 ):
381                         print 'syntax error (value split): %s' % ( repr( value_split ) )
382                         return
383                 try:
384                         value_array = []
385                         value_split.reverse()
386                         value_split.pop()
387                         while ( len( value_split ) != 0 ):
388                                 value_array.append( value_split.pop() )
389                                 value_split.pop()
390                 except:
391                         print traceback.print_exception( sys.exc_type, sys.exc_value, sys.exc_traceback )
392                         print 'syntax error (value to array): %s' % ( repr( value_split ) )
393                         return
394
395                 return ( name, value_array )
396
397         def parseStatements( self, _configs, statements ):
398                 self.current_config = None
399                 self.configs = _configs
400                 if ( self.configs is None ):
401                         self.configs = []
402                 for s in statements:
403
404                         if ( self.current_config is None ):
405                                 # use a provided config, or create a default one
406                                 if ( len( self.configs ) > 0 ):
407                                         self.current_config = self.configs.pop()
408                                 else:
409                                         self.current_config = Config()
410                                 # setup the operator table for this config
411                                 # NOTE: have that in self._processOp too
412                                 self._setupParser( self.current_config )
413
414                         ret = self._parseStatement( s )
415                         if ( ret is None ):
416                                 print 'stop statement parse at %s' % repr( s )
417                                 break
418                         ( name, value_array ) = ret
419                         try:
420                                 processor = self.operators[name]
421                         except:
422                                 print 'unknown operator %s - stop statement parse at %s' % ( repr( name ), repr( s ) )
423                                 break
424                         processor( value_array )
425
426                 if ( not self.current_config is None ):
427                         self.configs.append( self.current_config )
428                 # make sure there is at least one config
429                 if ( len( self.configs ) == 0 ):
430                         print 'pushing a default config'
431                         self.configs.append( Config() )
432                 return self.configs
433
434 import unittest
435
436 class TestConfigParse( unittest.TestCase ):
437
438         def setUp( self ):
439                 self.parser = ConfigParser()
440
441         def testBasicParse( self ):
442                 # test basic config parsing
443                 # needs to cleanly stop at the first config statement that is not recognized
444                 configs = self.parser.parseStatements( None, [ 'game=missionpack', 'config=qvm', 'foobar' ] )
445                 print repr( configs )
446
447         def testMultiParse( self ):
448                 # multiple configs seperated by commas
449                 configs = self.parser.parseStatements( None, [ 'target=server,game,cgame' ] )
450                 print repr( configs )
451
452         def testOp( self ):
453                 # test the operator for multiple configs
454                 configs = self.parser.parseStatements( None, [ 'target=core', 'config=release', 'op=push', 'target=game,cgame,ui', 'config=debug' ] )
455                 print repr( configs )
456
457 if __name__ == '__main__':
458         unittest.main()