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