]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - SConstruct
d4985c675332b6df5b81053b9d785a74c26ef52e
[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 CCFLAGS = '' + warningFlags
171 CXXFLAGS = '-pipe -DQ_NO_STLPORT ' + warningFlags + warningFlagsCXX
172 CPPPATH = []
173 if (BUILD == 'debug'):
174         CXXFLAGS += '-g -D_DEBUG '
175         CCFLAGS += '-g -D_DEBUG '
176 elif (BUILD == 'release'):
177         CXXFLAGS += '-O2 '
178         CCFLAGS += '-O2 '
179 else:
180         print 'Unknown build configuration ' + BUILD
181         sys.exit( 0 )
182
183 LINKFLAGS = ''
184 if ( OS == 'Linux' ):
185
186   # static
187   # 2112833 /opt/gtkradiant/radiant.x86
188   # 35282 /opt/gtkradiant/modules/archivezip.so
189   # 600099 /opt/gtkradiant/modules/entity.so
190   
191   # dynamic
192   # 2237060 /opt/gtkradiant/radiant.x86
193   # 110605 /opt/gtkradiant/modules/archivezip.so
194   # 730222 /opt/gtkradiant/modules/entity.so
195   
196   # EVIL HACK - force static-linking for libstdc++ - create a symbolic link to the static libstdc++ in the root
197   os.system("ln -s `g++ -print-file-name=libstdc++.a`")
198   
199   #if not os.path.exists("./install"):
200   #  os.mkdir("./install")
201   #os.system("cp `g++ -print-file-name=libstdc++.so` ./install")
202   
203   CXXFLAGS += '-fno-exceptions -fno-rtti '
204   LINKFLAGS += '-Wl,-fini,fini_stub -L. -static-libgcc '
205 if ( OS == 'Darwin' ):
206   CCFLAGS += '-force_cpusubtype_ALL -fPIC '
207   CXXFLAGS += '-force_cpusubtype_ALL -fPIC -fno-exceptions -fno-rtti '
208   CPPPATH.append('/sw/include')
209   CPPPATH.append('/usr/X11R6/include')
210   LINKFLAGS += '-L/sw/lib -L/usr/lib -L/usr/X11R6/lib '
211
212 CPPPATH.append('libs')
213
214 # extend the standard Environment a bit
215 class idEnvironment(Environment):
216
217   def __init__(self):
218     Environment.__init__(self,
219       ENV = os.environ, 
220       CC = CC,
221       CXX = CXX,
222       LINK = LINK,
223       CCFLAGS = CCFLAGS,
224       CXXFLAGS = CXXFLAGS,
225       CPPPATH = CPPPATH,
226       LINKFLAGS = LINKFLAGS)
227
228   def useGlib2(self):
229     self['CXXFLAGS'] += '`pkg-config glib-2.0 --cflags` '
230     self['CCFLAGS'] += '`pkg-config glib-2.0 --cflags` '
231     self['LINKFLAGS'] += '-lglib-2.0 '
232     
233   def useXML2(self):
234     self['CXXFLAGS'] += '`xml2-config --cflags` '      
235     self['CCFLAGS'] += '`xml2-config --cflags` '      
236     self['LINKFLAGS'] += '-lxml2 '
237
238   def useGtk2(self):
239     self['CXXFLAGS'] += '`pkg-config gtk+-2.0 --cflags` '
240     self['CCFLAGS'] += '`pkg-config gtk+-2.0 --cflags` '
241     self['LINKFLAGS'] += '-lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lpango-1.0 -lgdk_pixbuf-2.0 '
242    
243   def useGtkGLExt(self):
244     self['CXXFLAGS'] += '`pkg-config gtkglext-1.0 --cflags` '
245     self['CCFLAGS'] += '`pkg-config gtkglext-1.0 --cflags` '
246     self['LINKFLAGS'] += '-lgtkglext-x11-1.0 -lgdkglext-x11-1.0 '      
247     
248   def usePNG(self):
249     self['CXXFLAGS'] += '`libpng-config --cflags` '
250     self['CCFLAGS'] += '`libpng-config --cflags` '
251     self['LINKFLAGS'] += '`libpng-config --ldflags` '
252     
253   def useMHash(self):
254     self['LINKFLAGS'] += '-lmhash '
255   
256   def useZLib(self):
257     self['LINKFLAGS'] += '-lz '
258     
259   def usePThread(self):
260     if ( OS == 'Darwin' ):
261       self['LINKFLAGS'] += '-lpthread -Wl,-stack_size,0x400000 '
262     else:
263       self['LINKFLAGS'] += '-lpthread '
264
265   def CheckLDD(self, target, source, env):
266     file = target[0]
267     if (not os.path.isfile(file.abspath)):
268         print('ERROR: CheckLDD: target %s not found\n' % target[0])
269         Exit(1)
270     # not using os.popen3 as I want to check the return code
271     ldd = popen2.Popen3('`which ldd` -r %s' % target[0], 1)
272     stdout_lines = ldd.fromchild.readlines()
273     stderr_lines = ldd.childerr.readlines()
274     ldd_ret = ldd.wait()
275     del ldd
276     have_undef = 0
277     if ( ldd_ret != 0 ):
278         print "ERROR: ldd command returned with exit code %d" % ldd_ret
279         os.system('rm %s' % target[0])
280         Exit()
281     for i_line in stderr_lines:
282         print repr(i_line)
283         regex = re.compile('undefined symbol: (.*)\t\\((.*)\\)\n')
284         if ( regex.match(i_line) ):
285             symbol = regex.sub('\\1', i_line)
286             try:
287                 env['ALLOWED_SYMBOLS'].index(symbol)
288             except:
289                 have_undef = 1
290         else:
291             print "ERROR: failed to parse ldd stderr line: %s" % i_line
292             os.system('rm %s' % target[0])
293             Exit(1)
294     if ( have_undef ):
295         print "ERROR: undefined symbols"
296         os.system('rm %s' % target[0])
297         Exit(1)
298   
299   def SharedLibrarySafe(self, target, source, LIBS=[], LIBPATH='.'):
300     result = self.SharedLibrary(target, source, LIBS=LIBS, LIBPATH=LIBPATH)
301     if (OS != 'Darwin'):
302       AddPostAction(target + '.so', self.CheckLDD)
303     return result
304
305 g_env = idEnvironment()
306
307 # export the globals
308 GLOBALS = 'g_env INSTALL SETUP g_cpu'
309
310 radiant_makeversion('\\ngcc version: %s.%s.%s' % ( ver_cc[0], ver_cc[1], ver_cc[2] ) )
311
312 # end general configuration ----------------------
313
314 # targets ----------------------------------------
315
316 Default('.')
317
318 Export('GLOBALS ' + GLOBALS)
319 BuildDir(g_build, '.', duplicate = 0)
320 SConscript(g_build + '/SConscript')
321
322 # end targets ------------------------------------