blob: d3c0261d71ea049e000a24ce536d3c7f44feb9a3 [file] [log] [blame]
José Fonseca94090432008-02-27 17:36:28 +09001#######################################################################
2# Common SCons code
3
4import os
5import os.path
6import sys
7import platform as _platform
8
9
10#######################################################################
11# Defaults
12
13_platform_map = {
14 'linux2': 'linux',
15 'win32': 'winddk',
16}
17
18default_platform = sys.platform
19default_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}
29if 'PROCESSOR_ARCHITECTURE' in os.environ:
30 default_machine = os.environ['PROCESSOR_ARCHITECTURE']
31else:
32 default_machine = _platform.machine()
33default_machine = _machine_map.get(default_machine, 'generic')
34
35if default_platform in ('linux', 'freebsd', 'darwin'):
36 default_dri = 'yes'
José Fonseca35460fc2008-04-25 18:16:25 +090037elif default_platform in ('winddk', 'windows'):
José Fonseca94090432008-02-27 17:36:28 +090038 default_dri = 'no'
39else:
40 default_dri = 'no'
41
42
43#######################################################################
44# Common options
45
José Fonseca13174c12008-03-03 18:52:37 +010046def AddOptions(opts):
José Fonsecac9acd432008-05-01 00:58:04 +090047 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
José Fonseca059a6522008-05-24 19:25:02 +090055 opts.Add(BoolOption('debug', 'debug build', 'no'))
56 opts.Add(BoolOption('profile', 'profile build', 'no'))
José Fonseca5aa10822008-03-04 14:29:27 +010057 #opts.Add(BoolOption('quiet', 'quiet command lines', 'no'))
José Fonseca94090432008-02-27 17:36:28 +090058 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,
José Fonseca35460fc2008-04-25 18:16:25 +090061 allowed_values=('linux', 'cell', 'windows', 'winddk')))
José Fonseca94090432008-02-27 17:36:28 +090062 opts.Add(BoolOption('llvm', 'use LLVM', 'no'))
63 opts.Add(BoolOption('dri', 'build DRI drivers', default_dri))
José Fonseca94090432008-02-27 17:36:28 +090064
65
66#######################################################################
José Fonseca5aa10822008-03-04 14:29:27 +010067# Quiet command lines
68#
69# See also http://www.scons.org/wiki/HidingCommandLinesInOutput
70
71def 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#######################################################################
José Fonseca94090432008-02-27 17:36:28 +090080# Convenience Library Builder
81# based on the stock StaticLibrary and SharedLibrary builders
82
83import SCons.Action
84import SCons.Builder
85
86def 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
116def 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"
José Fonseca059a6522008-05-24 19:25:02 +0900129 if env['profile']:
130 build_subdir += "-profile"
José Fonseca94090432008-02-27 17:36:28 +0900131 build_dir = os.path.join(build_topdir, build_subdir)
José Fonseca7a678552008-02-27 20:13:16 +0900132 # 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'))
José Fonseca94090432008-02-27 17:36:28 +0900135 return build_dir
136
José Fonseca5aa10822008-03-04 14:29:27 +0100137
138#######################################################################
139# Common environment generation code
140
141def generate(env):
142 # FIXME: this is already too late
143 #if env.get('quiet', False):
144 # quietCommandLines(env)
José Fonseca35460fc2008-04-25 18:16:25 +0900145
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')
José Fonseca5aa10822008-03-04 14:29:27 +0100153
José Fonseca35460fc2008-04-25 18:16:25 +0900154 # C preprocessor options
155 cppdefines = []
156 if debug:
157 cppdefines += ['DEBUG']
158 else:
159 cppdefines += ['NDEBUG']
José Fonseca059a6522008-05-24 19:25:02 +0900160 if env['profile']:
161 cppdefines += ['PROFILE']
José Fonseca35460fc2008-04-25 18:16:25 +0900162 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',
José Fonseca1e712832008-04-26 01:55:32 +0900171 '_CRT_SECURE_NO_DEPRECATE',
José Fonseca35460fc2008-04-25 18:16:25 +0900172 ]
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 == 'windows':
200 cppdefines += ['PIPE_SUBSYSTEM_USER']
201 if platform == 'winddk':
202 cppdefines += ['PIPE_SUBSYSTEM_KERNEL']
203 env.Append(CPPDEFINES = cppdefines)
204
205 # C compiler options
206 cflags = []
207 if gcc:
208 if debug:
209 cflags += ['-O0', '-g3']
210 else:
211 cflags += ['-O3', '-g3']
José Fonseca059a6522008-05-24 19:25:02 +0900212 if env['profile']:
213 cflags += ['-pg']
José Fonseca35460fc2008-04-25 18:16:25 +0900214 cflags += [
215 '-Wall',
216 '-Wmissing-prototypes',
217 '-Wno-long-long',
218 '-ffast-math',
219 '-pedantic',
220 '-fmessage-length=0', # be nice to Eclipse
221 ]
222 if msvc:
223 # See also:
224 # - http://msdn2.microsoft.com/en-us/library/y0zzbyt4.aspx
225 # - cl /?
226 if debug:
227 cflags += [
228 '/Od', # disable optimizations
229 '/Oi', # enable intrinsic functions
230 '/Oy-', # disable frame pointer omission
231 ]
232 else:
233 cflags += [
234 '/Ox', # maximum optimizations
235 '/Oi', # enable intrinsic functions
236 '/Os', # favor code space
237 ]
José Fonseca059a6522008-05-24 19:25:02 +0900238 if env['profile']:
239 cflags += [
240 '/Gh', # enable _penter hook function
241 '/GH', # enable _pexit hook function
242 ]
José Fonseca35460fc2008-04-25 18:16:25 +0900243 if platform == 'windows':
244 cflags += [
245 # TODO
246 #'/Wp64', # enable 64 bit porting warnings
247 ]
248 if platform == 'winddk':
249 cflags += [
250 '/Zl', # omit default library name in .OBJ
251 '/Zp8', # 8bytes struct member alignment
252 '/Gy', # separate functions for linker
253 '/Gm-', # disable minimal rebuild
254 '/W3', # warning level
255 '/WX', # treat warnings as errors
256 '/Gz', # __stdcall Calling convention
257 '/GX-', # disable C++ EH
258 '/GR-', # disable C++ RTTI
259 '/GF', # enable read-only string pooling
José Fonseca35460fc2008-04-25 18:16:25 +0900260 '/G6', # optimize for PPro, P-II, P-III
261 '/Ze', # enable extensions
José Fonsecaa3195e92008-05-05 23:57:51 +0900262 '/Gi-', # disable incremental compilation
José Fonseca35460fc2008-04-25 18:16:25 +0900263 '/QIfdiv-', # disable Pentium FDIV fix
José Fonsecaa3195e92008-05-05 23:57:51 +0900264 '/hotpatch', # prepares an image for hotpatching.
José Fonseca35460fc2008-04-25 18:16:25 +0900265 #'/Z7', #enable old-style debug info
266 ]
267 # Put debugging information in a separate .pdb file for each object file as
268 # descrived in the scons manpage
269 env['CCPDBFLAGS'] = '/Zi /Fd${TARGET}.pdb'
270 env.Append(CFLAGS = cflags)
271 env.Append(CXXFLAGS = cflags)
272
273 # Linker options
274 if platform == 'winddk':
275 # See also:
276 # - http://msdn2.microsoft.com/en-us/library/y0zzbyt4.aspx
277 env.Append(LINKFLAGS = [
278 '/merge:_PAGE=PAGE',
279 '/merge:_TEXT=.text',
280 '/section:INIT,d',
281 '/opt:ref',
282 '/opt:icf',
283 '/ignore:4198,4010,4037,4039,4065,4070,4078,4087,4089,4221',
284 '/incremental:no',
285 '/fullbuild',
286 '/release',
287 '/nodefaultlib',
288 '/wx',
289 '/debug',
290 '/debugtype:cv',
291 '/version:5.1',
292 '/osversion:5.1',
293 '/functionpadmin:5',
294 '/safeseh',
295 '/pdbcompress',
296 '/stack:0x40000,0x1000',
297 '/driver',
298 '/align:0x80',
299 '/subsystem:native,5.01',
300 '/base:0x10000',
301
302 '/entry:DrvEnableDriver',
303 ])
304
305
306 createConvenienceLibBuilder(env)
307
308
José Fonseca5aa10822008-03-04 14:29:27 +0100309 # for debugging
310 #print env.Dump()
311