scons: Some provisions to cross-compile x86 on x86_64 machines and vice-versa.
[mesa.git] / common.py
1 #######################################################################
2 # Common SCons code
3
4 import os
5 import os.path
6 import sys
7 import platform as _platform
8
9
10 #######################################################################
11 # Defaults
12
13 _platform_map = {
14 'linux2': 'linux',
15 'win32': 'winddk',
16 }
17
18 default_platform = sys.platform
19 default_platform = _platform_map.get(default_platform, default_platform)
20
21 _machine_map = {
22 'x86': 'x86',
23 'i386': 'x86',
24 'i486': 'x86',
25 'i586': 'x86',
26 'i686': 'x86',
27 'x86_64': 'x86_64',
28 }
29 if 'PROCESSOR_ARCHITECTURE' in os.environ:
30 default_machine = os.environ['PROCESSOR_ARCHITECTURE']
31 else:
32 default_machine = _platform.machine()
33 default_machine = _machine_map.get(default_machine, 'generic')
34
35 if default_platform in ('linux', 'freebsd', 'darwin'):
36 default_dri = 'yes'
37 elif default_platform in ('winddk', 'windows', 'wince'):
38 default_dri = 'no'
39 else:
40 default_dri = 'no'
41
42
43 #######################################################################
44 # Common options
45
46 def AddOptions(opts):
47 try:
48 from SCons.Options.BoolOption import BoolOption
49 except ImportError:
50 from SCons.Variables.BoolVariable import BoolVariable as BoolOption
51 try:
52 from SCons.Options.EnumOption import EnumOption
53 except ImportError:
54 from SCons.Variables.EnumVariable import EnumVariable as EnumOption
55 opts.Add(BoolOption('debug', 'debug build', 'no'))
56 opts.Add(BoolOption('profile', 'profile build', 'no'))
57 #opts.Add(BoolOption('quiet', 'quiet command lines', 'no'))
58 opts.Add(EnumOption('machine', 'use machine-specific assembly code', default_machine,
59 allowed_values=('generic', 'x86', 'x86_64')))
60 opts.Add(EnumOption('platform', 'target platform', default_platform,
61 allowed_values=('linux', 'cell', 'windows', 'winddk', 'wince')))
62 opts.Add(BoolOption('llvm', 'use LLVM', 'no'))
63 opts.Add(BoolOption('dri', 'build DRI drivers', default_dri))
64
65
66 #######################################################################
67 # Quiet command lines
68 #
69 # See also http://www.scons.org/wiki/HidingCommandLinesInOutput
70
71 def quietCommandLines(env):
72 env['CCCOMSTR'] = "Compiling $SOURCE ..."
73 env['CXXCOMSTR'] = "Compiling $SOURCE ..."
74 env['ARCOMSTR'] = "Archiving $TARGET ..."
75 env['RANLIBCOMSTR'] = ""
76 env['LINKCOMSTR'] = "Linking $TARGET ..."
77
78
79 #######################################################################
80 # Convenience Library Builder
81 # based on the stock StaticLibrary and SharedLibrary builders
82
83 import SCons.Action
84 import SCons.Builder
85
86 def createConvenienceLibBuilder(env):
87 """This is a utility function that creates the ConvenienceLibrary
88 Builder in an Environment if it is not there already.
89
90 If it is already there, we return the existing one.
91 """
92
93 try:
94 convenience_lib = env['BUILDERS']['ConvenienceLibrary']
95 except KeyError:
96 action_list = [ SCons.Action.Action("$ARCOM", "$ARCOMSTR") ]
97 if env.Detect('ranlib'):
98 ranlib_action = SCons.Action.Action("$RANLIBCOM", "$RANLIBCOMSTR")
99 action_list.append(ranlib_action)
100
101 convenience_lib = SCons.Builder.Builder(action = action_list,
102 emitter = '$LIBEMITTER',
103 prefix = '$LIBPREFIX',
104 suffix = '$LIBSUFFIX',
105 src_suffix = '$SHOBJSUFFIX',
106 src_builder = 'SharedObject')
107 env['BUILDERS']['ConvenienceLibrary'] = convenience_lib
108 env['BUILDERS']['Library'] = convenience_lib
109
110 return convenience_lib
111
112
113 #######################################################################
114 # Build
115
116 def make_build_dir(env):
117 # Put build output in a separate dir, which depends on the current configuration
118 # See also http://www.scons.org/wiki/AdvancedBuildExample
119 build_topdir = 'build'
120 build_subdir = env['platform']
121 if env['dri']:
122 build_subdir += "-dri"
123 if env['llvm']:
124 build_subdir += "-llvm"
125 if env['machine'] != 'generic':
126 build_subdir += '-' + env['machine']
127 if env['debug']:
128 build_subdir += "-debug"
129 if env['profile']:
130 build_subdir += "-profile"
131 build_dir = os.path.join(build_topdir, build_subdir)
132 # Place the .sconsign file on the builddir too, to avoid issues with different scons
133 # versions building the same source file
134 env.SConsignFile(os.path.join(build_dir, '.sconsign'))
135 return build_dir
136
137
138 #######################################################################
139 # Common environment generation code
140
141 def generate(env):
142 # FIXME: this is already too late
143 #if env.get('quiet', False):
144 # quietCommandLines(env)
145
146 # shortcuts
147 debug = env['debug']
148 machine = env['machine']
149 platform = env['platform']
150 x86 = env['machine'] == 'x86'
151 gcc = env['platform'] in ('linux', 'freebsd', 'darwin')
152 msvc = env['platform'] in ('windows', 'winddk', 'wince')
153
154 # C preprocessor options
155 cppdefines = []
156 if debug:
157 cppdefines += ['DEBUG']
158 else:
159 cppdefines += ['NDEBUG']
160 if env['profile']:
161 cppdefines += ['PROFILE']
162 if platform == 'windows':
163 cppdefines += [
164 'WIN32',
165 '_WINDOWS',
166 '_UNICODE',
167 'UNICODE',
168 # http://msdn2.microsoft.com/en-us/library/6dwk3a1z.aspx,
169 'WIN32_LEAN_AND_MEAN',
170 'VC_EXTRALEAN',
171 '_CRT_SECURE_NO_DEPRECATE',
172 ]
173 if debug:
174 cppdefines += ['_DEBUG']
175 if platform == 'winddk':
176 # Mimic WINDDK's builtin flags. See also:
177 # - WINDDK's bin/makefile.new i386mk.inc for more info.
178 # - buildchk_wxp_x86.log files, generated by the WINDDK's build
179 # - http://alter.org.ua/docs/nt_kernel/vc8_proj/
180 cppdefines += [
181 ('_X86_', '1'),
182 ('i386', '1'),
183 'STD_CALL',
184 ('CONDITION_HANDLING', '1'),
185 ('NT_INST', '0'),
186 ('WIN32', '100'),
187 ('_NT1X_', '100'),
188 ('WINNT', '1'),
189 ('_WIN32_WINNT', '0x0501'), # minimum required OS version
190 ('WINVER', '0x0501'),
191 ('_WIN32_IE', '0x0603'),
192 ('WIN32_LEAN_AND_MEAN', '1'),
193 ('DEVL', '1'),
194 ('__BUILDMACHINE__', 'WinDDK'),
195 ('FPO', '0'),
196 ]
197 if debug:
198 cppdefines += [('DBG', 1)]
199 if platform == 'wince':
200 cppdefines += [
201 ('_WIN32_WCE', '500'),
202 'WCE_PLATFORM_STANDARDSDK_500',
203 '_i386_',
204 ('UNDER_CE', '500'),
205 'UNICODE',
206 '_UNICODE',
207 '_X86_',
208 'x86',
209 '_USRDLL',
210 'TEST_EXPORTS' ,
211 ]
212 if platform == 'windows':
213 cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_USER']
214 if platform == 'winddk':
215 cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_DISPLAY']
216 if platform == 'wince':
217 cppdefines += ['PIPE_SUBSYSTEM_WINDOWS_CE']
218 env.Append(CPPDEFINES = cppdefines)
219
220 # C compiler options
221 cflags = []
222 if gcc:
223 if debug:
224 cflags += ['-O0', '-g3']
225 else:
226 cflags += ['-O3', '-g3']
227 if env['profile']:
228 cflags += ['-pg']
229 if env['machine'] == 'x86' and default_machine == 'x86_64':
230 cflags += ['-m32']
231 if env['machine'] == 'x86_64' and default_machine == 'x86':
232 cflags += ['-m64']
233 cflags += [
234 '-Wall',
235 '-Wmissing-prototypes',
236 '-Wno-long-long',
237 '-ffast-math',
238 '-pedantic',
239 '-fmessage-length=0', # be nice to Eclipse
240 ]
241 if msvc:
242 # See also:
243 # - http://msdn2.microsoft.com/en-us/library/y0zzbyt4.aspx
244 # - cl /?
245 if debug:
246 cflags += [
247 '/Od', # disable optimizations
248 '/Oi', # enable intrinsic functions
249 '/Oy-', # disable frame pointer omission
250 ]
251 else:
252 cflags += [
253 '/Ox', # maximum optimizations
254 '/Oi', # enable intrinsic functions
255 '/Os', # favor code space
256 ]
257 if env['profile']:
258 cflags += [
259 '/Gh', # enable _penter hook function
260 '/GH', # enable _pexit hook function
261 ]
262 cflags += [
263 '/W3', # warning level
264 #'/Wp64', # enable 64 bit porting warnings
265 ]
266 if platform == 'windows':
267 cflags += [
268 # TODO
269 ]
270 if platform == 'winddk':
271 cflags += [
272 '/Zl', # omit default library name in .OBJ
273 '/Zp8', # 8bytes struct member alignment
274 '/Gy', # separate functions for linker
275 '/Gm-', # disable minimal rebuild
276 '/WX', # treat warnings as errors
277 '/Gz', # __stdcall Calling convention
278 '/GX-', # disable C++ EH
279 '/GR-', # disable C++ RTTI
280 '/GF', # enable read-only string pooling
281 '/G6', # optimize for PPro, P-II, P-III
282 '/Ze', # enable extensions
283 '/Gi-', # disable incremental compilation
284 '/QIfdiv-', # disable Pentium FDIV fix
285 '/hotpatch', # prepares an image for hotpatching.
286 #'/Z7', #enable old-style debug info
287 ]
288 if platform == 'wince':
289 cflags += [
290 '/Gs8192',
291 '/GF', # enable read-only string pooling
292 ]
293 # Put debugging information in a separate .pdb file for each object file as
294 # described in the scons manpage
295 env['CCPDBFLAGS'] = '/Zi /Fd${TARGET}.pdb'
296 env.Append(CFLAGS = cflags)
297 env.Append(CXXFLAGS = cflags)
298
299 # Linker options
300 linkflags = []
301 if gcc:
302 if env['machine'] == 'x86' and default_machine == 'x86_64':
303 linkflags += ['-m32']
304 if env['machine'] == 'x86_64' and default_machine == 'x86':
305 linkflags += ['-m64']
306 if platform == 'winddk':
307 # See also:
308 # - http://msdn2.microsoft.com/en-us/library/y0zzbyt4.aspx
309 linkflags += [
310 '/merge:_PAGE=PAGE',
311 '/merge:_TEXT=.text',
312 '/section:INIT,d',
313 '/opt:ref',
314 '/opt:icf',
315 '/ignore:4198,4010,4037,4039,4065,4070,4078,4087,4089,4221',
316 '/incremental:no',
317 '/fullbuild',
318 '/release',
319 '/nodefaultlib',
320 '/wx',
321 '/debug',
322 '/debugtype:cv',
323 '/version:5.1',
324 '/osversion:5.1',
325 '/functionpadmin:5',
326 '/safeseh',
327 '/pdbcompress',
328 '/stack:0x40000,0x1000',
329 '/driver',
330 '/align:0x80',
331 '/subsystem:native,5.01',
332 '/base:0x10000',
333
334 '/entry:DrvEnableDriver',
335 ]
336 env.Append(LINKFLAGS = linkflags)
337
338
339 createConvenienceLibBuilder(env)
340
341
342 # for debugging
343 #print env.Dump()
344