]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - SConstruct
a8cbf68e0021f70fe10566ed5f3b1e9ce77ceae3
[xonotic/netradiant.git] / SConstruct
1 # scons build script
2 # http://scons.sourceforge.net
3
4 import commands, re, sys, os, pickle, string, popen2
5 from makeversion import radiant_makeversion, get_version
6
7 # to access some internal stuff
8 import SCons
9
10 conf_filename='site.conf'
11 # there is a default hardcoded value, you can override on command line, those are saved between runs
12 # we only handle strings
13 serialized=['CC', 'CXX', 'JOBS', 'BUILD', 'SETUP']
14
15 # help -------------------------------------------
16
17 Help("""
18 Usage: scons [OPTIONS] [TARGET] [CONFIG]
19
20 [OPTIONS] and [TARGET] are covered in command line options, use scons -H
21
22 [CONFIG]: KEY="VALUE" [...]
23 a number of configuration options saved between runs in the """ + conf_filename + """ file
24 erase """ + conf_filename + """ to start with default settings again
25
26 CC
27 CXX
28         Specify C and C++ compilers (defaults gcc and g++)
29         ex: CC="gcc-3.2"
30         You can use ccache and distcc, for instance:
31         CC="ccache distcc gcc" CXX="ccache distcc g++"
32
33 JOBS
34         Parallel build
35         ex: JOBS="4" is a good setting on SMP machines
36
37 BUILD
38         Use debug/release to select build settings
39         ex: BUILD="release" - default is debug
40 """
41 )
42
43 # end help ---------------------------------------
44   
45 # sanity -----------------------------------------
46
47 # get a recent python release
48 # that is broken in current version:
49 # http://sourceforge.net/tracker/index.php?func=detail&aid=794145&group_id=30337&atid=398971
50 #EnsurePythonVersion(2,1)
51 # above 0.90
52 EnsureSConsVersion( 0, 96 )
53 print 'SCons ' + SCons.__version__
54
55 # end sanity -------------------------------------
56
57 # system detection -------------------------------
58
59 # TODO: detect Darwin / OSX
60
61 # CPU type
62 g_cpu = commands.getoutput('uname -m')
63 exp = re.compile('.*i?86.*')
64 if (g_cpu == 'Power Macintosh' or g_cpu == 'ppc'):
65   g_cpu = 'ppc'
66 elif exp.match(g_cpu):
67   g_cpu = 'x86'
68 else:
69   g_cpu = 'cpu'
70
71 # OS
72 OS = commands.getoutput('uname')
73 print "OS=\"" + OS + "\""
74
75 if (OS == 'Linux'):
76   # libc .. do the little magic!
77   libc = commands.getoutput('/lib/libc.so.6 |grep "GNU C "|grep version|awk -F "version " \'{ print $2 }\'|cut -b -3')
78
79 # end system detection ---------------------------
80
81 # default settings -------------------------------
82
83 CC='gcc'
84 CXX='g++'
85 JOBS='1'
86 BUILD='debug'
87 INSTALL='#install'
88 SETUP='0'
89 g_build_root = 'build'
90
91 # end default settings ---------------------------
92
93 # site settings ----------------------------------
94
95 site_dict = {}
96 if (os.path.exists(conf_filename)):
97         site_file = open(conf_filename, 'r')
98         p = pickle.Unpickler(site_file)
99         site_dict = p.load()
100         print 'Loading build configuration from ' + conf_filename
101         for k, v in site_dict.items():
102                 exec_cmd = k + '=\"' + v + '\"'
103                 print exec_cmd
104                 exec(exec_cmd)
105
106 # end site settings ------------------------------
107
108 # command line settings --------------------------
109
110 for k in serialized:
111         if (ARGUMENTS.has_key(k)):
112                 exec_cmd = k + '=\"' + ARGUMENTS[k] + '\"'
113                 print 'Command line: ' + exec_cmd
114                 exec(exec_cmd)
115
116 # end command line settings ----------------------
117
118 # sanity check -----------------------------------
119
120 if (SETUP == '1' and BUILD != 'release' and BUILD != 'info'):
121   print 'Forcing release build for setup'
122   BUILD = 'release'
123
124 def GetGCCVersion(name):
125   ret = commands.getstatusoutput('%s -dumpversion' % name)
126   if ( ret[0] != 0 ):
127     return None
128   vers = string.split(ret[1], '.')
129   if ( len(vers) == 2 ):
130     return [ vers[0], vers[1], 0 ]
131   elif ( len(vers) == 3 ):
132     return vers
133   return None
134
135 ver_cc = GetGCCVersion(CC)
136 ver_cxx = GetGCCVersion(CXX)
137
138 if ( ver_cc is None or ver_cxx is None or ver_cc[0] < '3' or ver_cxx[0] < '3' or ver_cc != ver_cxx ):
139   print 'Compiler version check failed - need gcc 3.x or later:'
140   print 'CC: %s %s\nCXX: %s %s' % ( CC, repr(ver_cc), CXX, repr(ver_cxx) )
141   Exit(1)
142
143 # end sanity check -------------------------------
144
145 # save site configuration ----------------------
146
147 for k in serialized:
148         exec_cmd = 'site_dict[\'' + k + '\'] = ' + k
149         exec(exec_cmd)
150
151 site_file = open(conf_filename, 'w')
152 p = pickle.Pickler(site_file)
153 p.dump(site_dict)
154 site_file.close()
155
156 # end save site configuration ------------------
157
158 # general configuration, target selection --------
159
160 SConsignFile( "scons.signatures" )
161
162 g_build = g_build_root + '/' + BUILD
163
164 SetOption('num_jobs', JOBS)
165
166 LINK = CXX
167 # common flags
168 warningFlags = '-W -Wall -Wcast-align -Wcast-qual -Wno-unused-parameter '
169 warningFlagsCXX = '-Wno-non-virtual-dtor -Wreorder ' # -Wold-style-cast
170 # POSIX macro: platform supports posix IEEE Std 1003.1:2001
171 # XWINDOWS macro: platform supports X-Windows API
172 CCFLAGS = '-DPOSIX -DXWINDOWS ' + warningFlags
173 CXXFLAGS = '-pipe -DPOSIX -DXWINDOWS ' + warningFlags + warningFlagsCXX
174 CPPPATH = []
175 if (BUILD == 'debug'):
176         CXXFLAGS += '-g -D_DEBUG '
177         CCFLAGS += '-g -D_DEBUG '
178 elif (BUILD == 'release'):
179         CXXFLAGS += '-O2 '
180         CCFLAGS += '-O2 '
181 else:
182         print 'Unknown build configuration ' + BUILD
183         sys.exit( 0 )
184
185 LINKFLAGS = ''
186 if ( OS == 'Linux' ):
187
188   # static
189   # 2112833 /opt/gtkradiant/radiant.x86
190   # 35282 /opt/gtkradiant/modules/archivezip.so
191   # 600099 /opt/gtkradiant/modules/entity.so
192   
193   # dynamic
194   # 2237060 /opt/gtkradiant/radiant.x86
195   # 110605 /opt/gtkradiant/modules/archivezip.so
196   # 730222 /opt/gtkradiant/modules/entity.so
197   
198   # EVIL HACK - force static-linking for libstdc++ - create a symbolic link to the static libstdc++ in the root
199   os.system("ln -s `g++ -print-file-name=libstdc++.a`")
200   
201   #if not os.path.exists("./install"):
202   #  os.mkdir("./install")
203   #os.system("cp `g++ -print-file-name=libstdc++.so` ./install")
204   
205   CXXFLAGS += '-fno-exceptions -fno-rtti '
206   LINKFLAGS += '-Wl,-fini,fini_stub -L. -static-libgcc '
207 if ( OS == 'Darwin' ):
208   CCFLAGS += '-force_cpusubtype_ALL -fPIC '
209   CXXFLAGS += '-force_cpusubtype_ALL -fPIC -fno-exceptions -fno-rtti '
210   CPPPATH.append('/sw/include')
211   CPPPATH.append('/usr/X11R6/include')
212   LINKFLAGS += '-L/sw/lib -L/usr/lib -L/usr/X11R6/lib '
213
214 CPPPATH.append('libs')
215
216 # extend the standard Environment a bit
217 class idEnvironment(Environment):
218
219   def __init__(self):
220     Environment.__init__(self,
221       ENV = os.environ, 
222       CC = CC,
223       CXX = CXX,
224       LINK = LINK,
225       CCFLAGS = CCFLAGS,
226       CXXFLAGS = CXXFLAGS,
227       CPPPATH = CPPPATH,
228       LINKFLAGS = LINKFLAGS)
229
230   def useGlib2(self):
231     self['CXXFLAGS'] += '`pkg-config glib-2.0 --cflags` '
232     self['CCFLAGS'] += '`pkg-config glib-2.0 --cflags` '
233     self['LINKFLAGS'] += '-lglib-2.0 '
234     
235   def useXML2(self):
236     self['CXXFLAGS'] += '`xml2-config --cflags` '      
237     self['CCFLAGS'] += '`xml2-config --cflags` '      
238     self['LINKFLAGS'] += '-lxml2 '
239
240   def useGtk2(self):
241     self['CXXFLAGS'] += '`pkg-config gtk+-2.0 --cflags` '
242     self['CCFLAGS'] += '`pkg-config gtk+-2.0 --cflags` '
243     self['LINKFLAGS'] += '-lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lpango-1.0 -lgdk_pixbuf-2.0 '
244    
245   def useGtkGLExt(self):
246     self['CXXFLAGS'] += '`pkg-config gtkglext-1.0 --cflags` '
247     self['CCFLAGS'] += '`pkg-config gtkglext-1.0 --cflags` '
248     self['LINKFLAGS'] += '-lgtkglext-x11-1.0 -lgdkglext-x11-1.0 '      
249     
250   def usePNG(self):
251     self['CXXFLAGS'] += '`libpng-config --cflags` '
252     self['CCFLAGS'] += '`libpng-config --cflags` '
253     self['LINKFLAGS'] += '`libpng-config --ldflags` '
254     
255   def useMHash(self):
256     self['LINKFLAGS'] += '-lmhash '
257   
258   def useZLib(self):
259     self['LINKFLAGS'] += '-lz '
260     
261   def usePThread(self):
262     if ( OS == 'Darwin' ):
263       self['LINKFLAGS'] += '-lpthread -Wl,-stack_size,0x400000 '
264     else:
265       self['LINKFLAGS'] += '-lpthread '
266
267   def CheckLDD(self, target, source, env):
268     file = target[0]
269     if (not os.path.isfile(file.abspath)):
270         print('ERROR: CheckLDD: target %s not found\n' % target[0])
271         Exit(1)
272     # not using os.popen3 as I want to check the return code
273     ldd = popen2.Popen3('`which ldd` -r %s' % target[0], 1)
274     stdout_lines = ldd.fromchild.readlines()
275     stderr_lines = ldd.childerr.readlines()
276     ldd_ret = ldd.wait()
277     del ldd
278     have_undef = 0
279     if ( ldd_ret != 0 ):
280         print "ERROR: ldd command returned with exit code %d" % ldd_ret
281         os.system('rm %s' % target[0])
282         Exit()
283     for i_line in stderr_lines:
284         print repr(i_line)
285         regex = re.compile('undefined symbol: (.*)\t\\((.*)\\)\n')
286         if ( regex.match(i_line) ):
287             symbol = regex.sub('\\1', i_line)
288             try:
289                 env['ALLOWED_SYMBOLS'].index(symbol)
290             except:
291                 have_undef = 1
292         else:
293             print "ERROR: failed to parse ldd stderr line: %s" % i_line
294             os.system('rm %s' % target[0])
295             Exit(1)
296     if ( have_undef ):
297         print "ERROR: undefined symbols"
298         os.system('rm %s' % target[0])
299         Exit(1)
300   
301   def SharedLibrarySafe(self, target, source, LIBS=[], LIBPATH='.'):
302     result = self.SharedLibrary(target, source, LIBS=LIBS, LIBPATH=LIBPATH)
303     if (OS != 'Darwin'):
304       AddPostAction(target + '.so', self.CheckLDD)
305     return result
306
307 g_env = idEnvironment()
308
309 # export the globals
310 GLOBALS = 'g_env INSTALL SETUP g_cpu'
311
312 radiant_makeversion('\\ngcc version: %s.%s.%s' % ( ver_cc[0], ver_cc[1], ver_cc[2] ) )
313
314 # end general configuration ----------------------
315
316 # targets ----------------------------------------
317
318 Default('.')
319
320 Export('GLOBALS ' + GLOBALS)
321 BuildDir(g_build, '.', duplicate = 0)
322 SConscript(g_build + '/SConscript')
323
324 # end targets ------------------------------------