scons: Autodetect the default machine.
[profile/ivi/mesa.git] / SConstruct
1 #######################################################################
2 # Top-level SConstruct
3
4 import os
5 import os.path
6 import sys
7 import platform as _platform
8
9
10 #######################################################################
11 # Configuration options
12 #
13 # For example, invoke scons as 
14 #
15 #   scons debug=1 dri=0 machine=x86
16 #
17 # to set configuration variables. Or you can write those options to a file
18 # named config.py:
19 #
20 #   # config.py
21 #   debug=1
22 #   dri=0
23 #   machine='x86'
24
25 # Invoke
26 #
27 #   scons -h
28 #
29 # to get the full list of options. See scons manpage for more info.
30 #  
31
32 platform_map = {
33         'linux2': 'linux',
34         'win32': 'winddk',
35 }
36 default_platform = sys.platform
37 default_platform = platform_map.get(default_platform, default_platform)
38
39 machine_map = {
40         'x86': 'x86',
41         'i386': 'x86',
42         'i486': 'x86',
43         'i586': 'x86',
44         'i686': 'x86',
45         'x86_64': 'x86_64',
46 }
47 if 'PROCESSOR_ARCHITECTURE' in os.environ:
48         default_machine = os.environ['PROCESSOR_ARCHITECTURE']
49 else:
50         default_machine = _platform.machine()
51 default_machine = machine_map.get(default_machine, 'generic')
52
53 if default_platform in ('linux', 'freebsd', 'darwin'):
54         default_statetrackers = 'mesa'
55         default_drivers = 'softpipe,failover,i915simple,i965simple'
56         default_winsys = 'xlib'
57         default_dri = 'yes'
58 elif default_platform in ('winddk',):
59         default_statetrackers = 'none'
60         default_drivers = 'softpipe,i915simple'
61         default_winsys = 'none'
62         default_dri = 'no'
63 else:
64         default_drivers = 'all'
65         default_winsys = 'all'
66         default_dri = 'no'
67
68
69 # TODO: auto-detect defaults
70 opts = Options('config.py')
71 opts.Add(BoolOption('debug', 'build debug version', 'no'))
72 opts.Add(EnumOption('machine', 'use machine-specific assembly code', default_machine,
73                      allowed_values=('generic', 'x86', 'x86_64')))
74 opts.Add(EnumOption('platform', 'target platform', default_platform,
75                      allowed_values=('linux', 'cell', 'winddk')))
76 opts.Add(ListOption('statetrackers', 'state_trackers to build', default_statetrackers,
77                      [
78                         'mesa', 
79                      ],
80                      ))
81 opts.Add(ListOption('drivers', 'pipe drivers to build', default_drivers,
82                      [
83                         'softpipe', 
84                         'failover', 
85                         'i915simple', 
86                         'i965simple', 
87                         'cell',
88                      ],
89                      ))
90 opts.Add(ListOption('winsys', 'winsys drivers to build', default_winsys,
91                      [
92                         'xlib', 
93                         'intel',
94                      ],
95                      ))
96 opts.Add(BoolOption('llvm', 'use LLVM', 'no'))
97 opts.Add(BoolOption('dri', 'build DRI drivers', default_dri))
98
99 env = Environment(
100         options = opts, 
101         ENV = os.environ)
102 Help(opts.GenerateHelpText(env))
103
104 # for debugging
105 #print env.Dump()
106
107 # replicate options values in local variables
108 debug = env['debug']
109 dri = env['dri']
110 llvm = env['llvm']
111 machine = env['machine']
112 platform = env['platform']
113
114 # derived options
115 x86 = machine == 'x86'
116 gcc = platform in ('linux', 'freebsd', 'darwin')
117 msvc = platform in ('win32', 'winddk')
118
119 Export([
120         'debug', 
121         'x86', 
122         'dri', 
123         'llvm',
124         'platform',
125         'gcc',
126         'msvc',
127 ])
128
129
130 #######################################################################
131 # Environment setup
132 #
133 # TODO: put the compiler specific settings in separate files
134 # TODO: auto-detect as much as possible
135
136
137 if platform == 'winddk':
138         env.Tool('winddk', ['.'])
139         
140         env.Append(CPPPATH = [
141                 env['SDK_INC_PATH'],
142                 env['DDK_INC_PATH'],
143                 env['WDM_INC_PATH'],
144                 env['CRT_INC_PATH'],
145         ])
146
147 # Optimization flags
148 if gcc:
149         if debug:
150                 env.Append(CFLAGS = '-O0 -g3')
151                 env.Append(CXXFLAGS = '-O0 -g3')
152         else:
153                 env.Append(CFLAGS = '-O3 -g3')
154                 env.Append(CXXFLAGS = '-O3 -g3')
155
156         env.Append(CFLAGS = '-Wall -Wmissing-prototypes -Wno-long-long -ffast-math -pedantic')
157         env.Append(CXXFLAGS = '-Wall -pedantic')
158         
159         # Be nice to Eclipse
160         env.Append(CFLAGS = '-fmessage-length=0')
161         env.Append(CXXFLAGS = '-fmessage-length=0')
162
163 if msvc:
164         env.Append(CFLAGS = '/W3')
165         if debug:
166                 cflags = [
167                         '/Od', # disable optimizations
168                         '/Oy-', # disable frame pointer omission
169                 ]
170         else:
171                 cflags = [
172                         '/Ox', # maximum optimizations
173                         '/Os', # favor code space
174                 ]
175         env.Append(CFLAGS = cflags)
176         env.Append(CXXFLAGS = cflags)
177         # Put debugging information in a separate .pdb file for each object file as
178         # descrived in the scons manpage
179         env['CCPDBFLAGS'] = '/Zi /Fd${TARGET}.pdb'
180
181 # Defines
182 if debug:
183         if gcc:
184                 env.Append(CPPDEFINES = ['DEBUG'])
185         if msvc:
186                 env.Append(CPPDEFINES = [
187                         ('DBG', '1'),
188                         ('DEBUG', '1'),
189                         ('_DEBUG', '1'),
190                 ])
191 else:
192         env.Append(CPPDEFINES = ['NDEBUG'])
193
194
195 # Includes
196 env.Append(CPPPATH = [
197         '#/include',
198         '#/src/gallium/include',
199         '#/src/gallium/auxiliary',
200         '#/src/gallium/drivers',
201 ])
202
203
204 # x86 assembly
205 if x86:
206         env.Append(CPPDEFINES = [
207                 'USE_X86_ASM', 
208                 'USE_MMX_ASM',
209                 'USE_3DNOW_ASM',
210                 'USE_SSE_ASM',
211         ])
212         if gcc: 
213                 env.Append(CFLAGS = '-m32')
214                 env.Append(CXXFLAGS = '-m32')
215
216
217 # Posix
218 if platform in ('posix', 'linux', 'freebsd', 'darwin'):
219         env.Append(CPPDEFINES = [
220                 '_POSIX_SOURCE',
221                 ('_POSIX_C_SOURCE', '199309L'), 
222                 '_SVID_SOURCE',
223                 '_BSD_SOURCE', 
224                 '_GNU_SOURCE',
225                 
226                 'PTHREADS',
227                 'HAVE_POSIX_MEMALIGN',
228         ])
229         env.Append(CPPPATH = ['/usr/X11R6/include'])
230         env.Append(LIBPATH = ['/usr/X11R6/lib'])
231         env.Append(LIBS = [
232                 'm',
233                 'pthread',
234                 'expat',
235                 'dl',
236         ])
237
238
239 # DRI
240 if dri:
241         env.ParseConfig('pkg-config --cflags --libs libdrm')
242         env.Append(CPPDEFINES = [
243                 ('USE_EXTERNAL_DXTN_LIB', '1'), 
244                 'IN_DRI_DRIVER',
245                 'GLX_DIRECT_RENDERING',
246                 'GLX_INDIRECT_RENDERING',
247         ])
248
249 # LLVM
250 if llvm:
251         # See also http://www.scons.org/wiki/UsingPkgConfig
252         env.ParseConfig('llvm-config --cflags --ldflags --libs')
253         env.Append(CPPDEFINES = ['MESA_LLVM'])
254         env.Append(CXXFLAGS = ['-Wno-long-long'])
255         
256
257 # libGL
258 if platform not in ('winddk',):
259         env.Append(LIBS = [
260                 'X11',
261                 'Xext',
262                 'Xxf86vm',
263                 'Xdamage',
264                 'Xfixes',
265         ])
266
267 Export('env')
268
269
270 #######################################################################
271 # Convenience Library Builder
272 # based on the stock StaticLibrary and SharedLibrary builders
273
274 def createConvenienceLibBuilder(env):
275     """This is a utility function that creates the ConvenienceLibrary
276     Builder in an Environment if it is not there already.
277
278     If it is already there, we return the existing one.
279     """
280
281     try:
282         convenience_lib = env['BUILDERS']['ConvenienceLibrary']
283     except KeyError:
284         action_list = [ Action("$ARCOM", "$ARCOMSTR") ]
285         if env.Detect('ranlib'):
286             ranlib_action = Action("$RANLIBCOM", "$RANLIBCOMSTR")
287             action_list.append(ranlib_action)
288
289         convenience_lib = Builder(action = action_list,
290                                   emitter = '$LIBEMITTER',
291                                   prefix = '$LIBPREFIX',
292                                   suffix = '$LIBSUFFIX',
293                                   src_suffix = '$SHOBJSUFFIX',
294                                   src_builder = 'SharedObject')
295         env['BUILDERS']['ConvenienceLibrary'] = convenience_lib
296         env['BUILDERS']['Library'] = convenience_lib
297
298     return convenience_lib
299
300 createConvenienceLibBuilder(env)
301
302
303 #######################################################################
304 # Invoke SConscripts
305
306 # Put build output in a separate dir, which depends on the current configuration
307 # See also http://www.scons.org/wiki/AdvancedBuildExample
308 build_topdir = 'build'
309 build_subdir = platform
310 if dri:
311         build_subdir += "-dri"
312 if llvm:
313         build_subdir += "-llvm"
314 if env['machine'] != 'generic':
315         build_subdir += '-' + env['machine']
316 if debug:
317         build_subdir += "-debug"
318 build_dir = os.path.join(build_topdir, build_subdir)
319
320 # TODO: Build several variants at the same time?
321 # http://www.scons.org/wiki/SimultaneousVariantBuilds
322
323 SConscript(
324         'src/SConscript',
325         build_dir = build_dir,
326         duplicate = 0 # http://www.scons.org/doc/0.97/HTML/scons-user/x2261.html
327 )