blob: 51cbd90f1dfdc9e293159c31e776100eeb63c006 [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é Fonseca94090432008-02-27 17:36:28 +090047 from SCons.Options.BoolOption import BoolOption
48 from SCons.Options.EnumOption import EnumOption
José Fonseca94090432008-02-27 17:36:28 +090049 opts.Add(BoolOption('debug', 'build debug version', 'no'))
José Fonseca5aa10822008-03-04 14:29:27 +010050 #opts.Add(BoolOption('quiet', 'quiet command lines', 'no'))
José Fonseca94090432008-02-27 17:36:28 +090051 opts.Add(EnumOption('machine', 'use machine-specific assembly code', default_machine,
52 allowed_values=('generic', 'x86', 'x86_64')))
53 opts.Add(EnumOption('platform', 'target platform', default_platform,
José Fonseca35460fc2008-04-25 18:16:25 +090054 allowed_values=('linux', 'cell', 'windows', 'winddk')))
José Fonseca94090432008-02-27 17:36:28 +090055 opts.Add(BoolOption('llvm', 'use LLVM', 'no'))
56 opts.Add(BoolOption('dri', 'build DRI drivers', default_dri))
José Fonseca94090432008-02-27 17:36:28 +090057
58
59#######################################################################
José Fonseca5aa10822008-03-04 14:29:27 +010060# Quiet command lines
61#
62# See also http://www.scons.org/wiki/HidingCommandLinesInOutput
63
64def quietCommandLines(env):
65 env['CCCOMSTR'] = "Compiling $SOURCE ..."
66 env['CXXCOMSTR'] = "Compiling $SOURCE ..."
67 env['ARCOMSTR'] = "Archiving $TARGET ..."
68 env['RANLIBCOMSTR'] = ""
69 env['LINKCOMSTR'] = "Linking $TARGET ..."
70
71
72#######################################################################
José Fonseca94090432008-02-27 17:36:28 +090073# Convenience Library Builder
74# based on the stock StaticLibrary and SharedLibrary builders
75
76import SCons.Action
77import SCons.Builder
78
79def createConvenienceLibBuilder(env):
80 """This is a utility function that creates the ConvenienceLibrary
81 Builder in an Environment if it is not there already.
82
83 If it is already there, we return the existing one.
84 """
85
86 try:
87 convenience_lib = env['BUILDERS']['ConvenienceLibrary']
88 except KeyError:
89 action_list = [ SCons.Action.Action("$ARCOM", "$ARCOMSTR") ]
90 if env.Detect('ranlib'):
91 ranlib_action = SCons.Action.Action("$RANLIBCOM", "$RANLIBCOMSTR")
92 action_list.append(ranlib_action)
93
94 convenience_lib = SCons.Builder.Builder(action = action_list,
95 emitter = '$LIBEMITTER',
96 prefix = '$LIBPREFIX',
97 suffix = '$LIBSUFFIX',
98 src_suffix = '$SHOBJSUFFIX',
99 src_builder = 'SharedObject')
100 env['BUILDERS']['ConvenienceLibrary'] = convenience_lib
101 env['BUILDERS']['Library'] = convenience_lib
102
103 return convenience_lib
104
105
106#######################################################################
107# Build
108
109def make_build_dir(env):
110 # Put build output in a separate dir, which depends on the current configuration
111 # See also http://www.scons.org/wiki/AdvancedBuildExample
112 build_topdir = 'build'
113 build_subdir = env['platform']
114 if env['dri']:
115 build_subdir += "-dri"
116 if env['llvm']:
117 build_subdir += "-llvm"
118 if env['machine'] != 'generic':
119 build_subdir += '-' + env['machine']
120 if env['debug']:
121 build_subdir += "-debug"
122 build_dir = os.path.join(build_topdir, build_subdir)
José Fonseca7a678552008-02-27 20:13:16 +0900123 # Place the .sconsign file on the builddir too, to avoid issues with different scons
124 # versions building the same source file
125 env.SConsignFile(os.path.join(build_dir, '.sconsign'))
José Fonseca94090432008-02-27 17:36:28 +0900126 return build_dir
127
José Fonseca5aa10822008-03-04 14:29:27 +0100128
129#######################################################################
130# Common environment generation code
131
132def generate(env):
133 # FIXME: this is already too late
134 #if env.get('quiet', False):
135 # quietCommandLines(env)
José Fonseca35460fc2008-04-25 18:16:25 +0900136
137 # shortcuts
138 debug = env['debug']
139 machine = env['machine']
140 platform = env['platform']
141 x86 = env['machine'] == 'x86'
142 gcc = env['platform'] in ('linux', 'freebsd', 'darwin')
143 msvc = env['platform'] in ('windows', 'winddk')
José Fonseca5aa10822008-03-04 14:29:27 +0100144
José Fonseca35460fc2008-04-25 18:16:25 +0900145 # C preprocessor options
146 cppdefines = []
147 if debug:
148 cppdefines += ['DEBUG']
149 else:
150 cppdefines += ['NDEBUG']
151 if platform == 'windows':
152 cppdefines += [
153 'WIN32',
154 '_WINDOWS',
155 '_UNICODE',
156 'UNICODE',
157 # http://msdn2.microsoft.com/en-us/library/6dwk3a1z.aspx,
158 'WIN32_LEAN_AND_MEAN',
159 'VC_EXTRALEAN',
160 ]
161 if debug:
162 cppdefines += ['_DEBUG']
163 if platform == 'winddk':
164 # Mimic WINDDK's builtin flags. See also:
165 # - WINDDK's bin/makefile.new i386mk.inc for more info.
166 # - buildchk_wxp_x86.log files, generated by the WINDDK's build
167 # - http://alter.org.ua/docs/nt_kernel/vc8_proj/
168 cppdefines += [
169 ('_X86_', '1'),
170 ('i386', '1'),
171 'STD_CALL',
172 ('CONDITION_HANDLING', '1'),
173 ('NT_INST', '0'),
174 ('WIN32', '100'),
175 ('_NT1X_', '100'),
176 ('WINNT', '1'),
177 ('_WIN32_WINNT', '0x0501'), # minimum required OS version
178 ('WINVER', '0x0501'),
179 ('_WIN32_IE', '0x0603'),
180 ('WIN32_LEAN_AND_MEAN', '1'),
181 ('DEVL', '1'),
182 ('__BUILDMACHINE__', 'WinDDK'),
183 ('FPO', '0'),
184 ]
185 if debug:
186 cppdefines += [('DBG', 1)]
187 if platform == 'windows':
188 cppdefines += ['PIPE_SUBSYSTEM_USER']
189 if platform == 'winddk':
190 cppdefines += ['PIPE_SUBSYSTEM_KERNEL']
191 env.Append(CPPDEFINES = cppdefines)
192
193 # C compiler options
194 cflags = []
195 if gcc:
196 if debug:
197 cflags += ['-O0', '-g3']
198 else:
199 cflags += ['-O3', '-g3']
200 cflags += [
201 '-Wall',
202 '-Wmissing-prototypes',
203 '-Wno-long-long',
204 '-ffast-math',
205 '-pedantic',
206 '-fmessage-length=0', # be nice to Eclipse
207 ]
208 if msvc:
209 # See also:
210 # - http://msdn2.microsoft.com/en-us/library/y0zzbyt4.aspx
211 # - cl /?
212 if debug:
213 cflags += [
214 '/Od', # disable optimizations
215 '/Oi', # enable intrinsic functions
216 '/Oy-', # disable frame pointer omission
217 ]
218 else:
219 cflags += [
220 '/Ox', # maximum optimizations
221 '/Oi', # enable intrinsic functions
222 '/Os', # favor code space
223 ]
224 if platform == 'windows':
225 cflags += [
226 # TODO
227 #'/Wp64', # enable 64 bit porting warnings
228 ]
229 if platform == 'winddk':
230 cflags += [
231 '/Zl', # omit default library name in .OBJ
232 '/Zp8', # 8bytes struct member alignment
233 '/Gy', # separate functions for linker
234 '/Gm-', # disable minimal rebuild
235 '/W3', # warning level
236 '/WX', # treat warnings as errors
237 '/Gz', # __stdcall Calling convention
238 '/GX-', # disable C++ EH
239 '/GR-', # disable C++ RTTI
240 '/GF', # enable read-only string pooling
241 '/GS', # enable security checks
242 '/G6', # optimize for PPro, P-II, P-III
243 '/Ze', # enable extensions
244 #'/Gi-', # ???
245 '/QIfdiv-', # disable Pentium FDIV fix
246 #'/hotpatch', # ???
247 #'/Z7', #enable old-style debug info
248 ]
249 # Put debugging information in a separate .pdb file for each object file as
250 # descrived in the scons manpage
251 env['CCPDBFLAGS'] = '/Zi /Fd${TARGET}.pdb'
252 env.Append(CFLAGS = cflags)
253 env.Append(CXXFLAGS = cflags)
254
255 # Linker options
256 if platform == 'winddk':
257 # See also:
258 # - http://msdn2.microsoft.com/en-us/library/y0zzbyt4.aspx
259 env.Append(LINKFLAGS = [
260 '/merge:_PAGE=PAGE',
261 '/merge:_TEXT=.text',
262 '/section:INIT,d',
263 '/opt:ref',
264 '/opt:icf',
265 '/ignore:4198,4010,4037,4039,4065,4070,4078,4087,4089,4221',
266 '/incremental:no',
267 '/fullbuild',
268 '/release',
269 '/nodefaultlib',
270 '/wx',
271 '/debug',
272 '/debugtype:cv',
273 '/version:5.1',
274 '/osversion:5.1',
275 '/functionpadmin:5',
276 '/safeseh',
277 '/pdbcompress',
278 '/stack:0x40000,0x1000',
279 '/driver',
280 '/align:0x80',
281 '/subsystem:native,5.01',
282 '/base:0x10000',
283
284 '/entry:DrvEnableDriver',
285 ])
286
287
288 createConvenienceLibBuilder(env)
289
290
José Fonseca5aa10822008-03-04 14:29:27 +0100291 # for debugging
292 #print env.Dump()
293