d3ff368fcfb735414a01013cea90524a7dc845dd
[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', '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' ]
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                         # left out jpeg6, splines (FIXME: I think jpeg6 is not used at all, can trash?)
81                         lib_objects = []
82                         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' ]:
83                                 Export( 'project' )
84                                 lib_objects += SConscript( os.path.join( build_dir, 'SConscript.lib' ) )
85                         Export( 'lib_objects' )
86                         radiant = SConscript( os.path.join( build_dir, 'SConscript.radiant' ) )
87                         Default( InstallAs( os.path.join( self.install_directory, 'radiant.bin' ), radiant ) )
88
89                         # PIC versions of the libs for the modules
90                         shlib_objects_extra = {}
91                         for project in [ 'libs/synapse/synapse.vcproj', 'libs/mathlib/mathlib.vcproj', 'libs/picomodel/picomodel.vcproj', 'libs/cmdlib/cmdlib.vcproj' ]:
92                                 ( libpath, libname ) = os.path.split( project )
93                                 libname = os.path.splitext( libname )[0]
94                                 config['shared'] = True
95                                 Export( 'project', 'config' )
96                                 build_dir = os.path.join( 'build', config_name, 'shobjs' )
97                                 BuildDir( build_dir, '.', duplicate = 0 )
98                                 shlib_objects_extra[libname] = SConscript( os.path.join( build_dir, 'SConscript.lib' ) )
99
100                         for project in [ 'plugins/vfspk3/vfspk3.vcproj',
101                                          'plugins/vfspak/vfspak.vcproj',
102                                          'plugins/vfswad/vfswad.vcproj',
103                                          'plugins/eclassfgd/fgd.vcproj',
104                                          'plugins/entity/entity.vcproj',
105                                          'plugins/image/image.vcproj',
106                                          'plugins/model/model.vcproj',
107                                          'plugins/imagepng/imagepng.vcproj',
108                                          'plugins/imagewal/imagewal.vcproj',
109                                          'plugins/imagem8/imagem8.vcproj',
110                                          'plugins/spritemodel/spritemodel.vcproj',
111                                          'plugins/textool/TexTool.vcproj',
112                                         # 'plugins/sample/sample.vcproj',
113                                          'plugins/map/map.vcproj',
114                                          'plugins/mapxml/mapxml.vcproj',
115                                          'plugins/shaders/shaders.vcproj',
116                                          'plugins/surface/surface.vcproj',
117                                          'plugins/surface_ufoai/surface_ufoai.vcproj',
118                                          'plugins/surface_quake2/surface_quake2.vcproj',
119                                          'plugins/surface_heretic2/surface_heretic2.vcproj',
120                                         # FIXME Needs splines
121                                         # 'contrib/camera/camera.vcproj',
122
123                                         # FIXME What is this? Empty dir for me - remove me?
124                                         # 'contrib/patches/patches.vcproj',
125                                         # 'plugins/archivewad/archivewad.vcproj',
126
127                                          'contrib/prtview/PrtView.vcproj',
128                                          'contrib/hydratoolz/hydratoolz.vcproj',
129                                          'contrib/bobtoolz/bobToolz_gtk.vcproj',
130                                          'contrib/gtkgensurf/gtkgensurf.vcproj',
131                                          'contrib/ufoai/ufoai.vcproj',
132                                          'contrib/bkgrnd2d/bkgrnd2d.vcproj'
133                                  ]:
134                                 ( libpath, libname ) = os.path.split( project )
135                                 libname = os.path.splitext( libname )[0]
136                                 shlib_objects = shlib_objects_extra['synapse']
137                                 if ( libname == 'entity' ):
138                                         shlib_objects += shlib_objects_extra['mathlib']
139                                 elif ( libname == 'model' ):
140                                         shlib_objects += shlib_objects_extra['picomodel']
141 #                               elif ( libname == 'spritemodel' ):
142 #                                       shlib_objects += shlib_objects_extra['mathlib']
143 #                               elif ( libname == 'TexTool' ):
144 #                                       shlib_objects += shlib_objects_extra['mathlib']
145                                 elif ( libname == 'map' ):
146                                         shlib_objects += shlib_objects_extra['cmdlib']
147                                 Export( 'project', 'shlib_objects' )
148                                 module = SConscript( os.path.join( build_dir, 'SConscript.module' ) )
149                                 Default( InstallAs( os.path.join( self.install_directory, 'modules/%s.so' % libname ), module ) )
150
151         def emit_q3map2( self ):
152                 settings = self
153                 for config_name in self.config_selected:
154                         config = {}
155                         config['name'] = config_name
156                         config['shared'] = False
157                         Export( 'utils', 'settings', 'config' )
158                         build_dir = os.path.join( 'build', config_name, 'q3map2' )
159                         BuildDir( build_dir, '.', duplicate = 0 )
160                         lib_objects = []
161                         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' ]:
162                                 Export( 'project' )
163                                 lib_objects += SConscript( os.path.join( build_dir, 'SConscript.lib' ) )
164                         Export( 'lib_objects' )
165                         q3map2 = SConscript( os.path.join( build_dir, 'SConscript.q3map2' ) )
166                         Default( InstallAs( os.path.join( self.install_directory, 'q3map2' ), q3map2 ) )
167
168
169         def emit( self ):
170                 try:
171                         self.target_selected.index( 'radiant' )
172                 except:
173                         pass
174                 else:
175                         self.emit_radiant()
176                 try:
177                         self.target_selected.index( 'q3map2' )
178                 except:
179                         pass
180                 else:
181                         self.emit_q3map2()
182
183                 try:
184                         self.target_selected.index( 'setup' )
185                 except:
186                         pass
187                 else:
188                         self.Setup()
189
190         def SetupEnvironment( self, env, config, useGtk = False, useGtkGL = False, useJPEG = False, useZ = False, usePNG = False ):
191                 env['CC'] = self.cc
192                 env['CXX'] = self.cxx
193                 ( ret, xml2 ) = commands.getstatusoutput( 'xml2-config --cflags' )
194                 if ( ret != 0 ):
195                         print 'xml2-config failed'
196                         assert( False )
197                 xml2libs = commands.getoutput( 'xml2-config --libs' )
198                 env.Append( LINKFLAGS = xml2libs.split( ' ' ) )
199                 baseflags = [ '-pipe', '-Wall', '-fmessage-length=0', '-fvisibility=hidden', xml2.split( ' ' ) ]
200 #               baseflags += [ '-m32' ]
201
202                 if ( self.platform == 'Darwin' ):
203                         env.Append( CPPPATH = [ '/Developer/SDKs/MacOSX10.4u.sdk/usr/X11R6/include' ] )
204
205                 if ( useGtk ):
206                         ( ret, gtk2 ) = commands.getstatusoutput( 'pkg-config gtk+-2.0 --cflags' )
207                         if ( ret != 0 ):
208                                 print 'pkg-config gtk+-2.0 failed'
209                                 assert( False )
210                         baseflags += gtk2.split( ' ' )
211                         gtk2libs = commands.getoutput( 'pkg-config gtk+-2.0 --libs' )
212                         env.Append( LINKFLAGS = gtk2libs.split( ' ' ) )
213                 else:
214                         # always setup at least glib
215                         ( ret, glib ) = commands.getstatusoutput( 'pkg-config glib-2.0 --cflags' )
216                         if ( ret != 0 ):
217                                 print 'pkg-config glib-2.0 failed'
218                                 assert( False )
219                         baseflags += glib.split( ' ' )
220                         gliblibs = commands.getoutput( 'pkg-config glib-2.0 --libs' )
221                         env.Append( LINKFLAGS = gliblibs.split( ' ' ) )
222
223                 if ( useGtkGL ):
224                         ( ret, gtkgl ) = commands.getstatusoutput( 'pkg-config gtkglext-1.0 --cflags' )
225                         if ( ret != 0 ):
226                                 print 'pkg-config gtkglext-1.0 failed'
227                                 assert( False )
228                         baseflags += gtkgl.split( ' ' )
229                         gtkgllibs = commands.getoutput( 'pkg-config gtkglext-1.0 --libs' )
230                         env.Append( LINKFLAGS = gtkgllibs.split( ' ' ) )
231                 if ( useJPEG ):
232                         env.Append( LIBS = 'jpeg' )
233                 if ( usePNG ):
234                         pnglibs = 'png z'
235                         env.Append( LIBS = pnglibs.split( ' ' ) )
236                 if ( useZ ):
237                         env.Append( LIBS = 'z' )
238
239                 env.Append( CCFLAGS = baseflags )
240                 env.Append( CXXFLAGS = baseflags + [ '-fpermissive', '-fvisibility-inlines-hidden' ] )
241                 env.Append( CPPPATH = [ 'include', 'libs' ] )
242                 env.Append( CPPDEFINES = [ 'Q_NO_STLPORT' ] )
243                 if ( config == 'debug' ):
244                         env.Append( CFLAGS = [ '-g' ] )
245                         env.Append( CXXFLAGS = [ '-g' ] )
246                         env.Append( CPPDEFINES = [ '_DEBUG' ] )
247                 else:
248                         env.Append( CFLAGS = [ '-O3', '-Winline', '-ffast-math', '-fno-unsafe-math-optimizations', '-fno-strict-aliasing' ] )
249                         env.Append( CXXFLAGS = [ '-O3', '-Winline', '-ffast-math', '-fno-unsafe-math-optimizations','-fno-strict-aliasing' ] )
250
251         def CheckoutOrUpdate( self, svnurl, path ):
252                 if ( os.path.exists( path ) ):
253                         # NOTE: check the svnurl matches?
254                         cmd = 'svn update "%s"' % path
255                         print cmd
256   &