José Fonseca | 9409043 | 2008-02-27 17:36:28 +0900 | [diff] [blame] | 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' |
José Fonseca | 35460fc | 2008-04-25 18:16:25 +0900 | [diff] [blame] | 37 | elif default_platform in ('winddk', 'windows'): |
José Fonseca | 9409043 | 2008-02-27 17:36:28 +0900 | [diff] [blame] | 38 | default_dri = 'no' |
| 39 | else: |
| 40 | default_dri = 'no' |
| 41 | |
| 42 | |
| 43 | ####################################################################### |
| 44 | # Common options |
| 45 | |
José Fonseca | 13174c1 | 2008-03-03 18:52:37 +0100 | [diff] [blame] | 46 | def AddOptions(opts): |
José Fonseca | c9acd43 | 2008-05-01 00:58:04 +0900 | [diff] [blame^] | 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 |
José Fonseca | 9409043 | 2008-02-27 17:36:28 +0900 | [diff] [blame] | 55 | opts.Add(BoolOption('debug', 'build debug version', 'no')) |
José Fonseca | 5aa1082 | 2008-03-04 14:29:27 +0100 | [diff] [blame] | 56 | #opts.Add(BoolOption('quiet', 'quiet command lines', 'no')) |
José Fonseca | 9409043 | 2008-02-27 17:36:28 +0900 | [diff] [blame] | 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, |
José Fonseca | 35460fc | 2008-04-25 18:16:25 +0900 | [diff] [blame] | 60 | allowed_values=('linux', 'cell', 'windows', 'winddk'))) |
José Fonseca | 9409043 | 2008-02-27 17:36:28 +0900 | [diff] [blame] | 61 | opts.Add(BoolOption('llvm', 'use LLVM', 'no')) |
| 62 | opts.Add(BoolOption('dri', 'build DRI drivers', default_dri)) |
José Fonseca | 9409043 | 2008-02-27 17:36:28 +0900 | [diff] [blame] | 63 | |
| 64 | |
| 65 | ####################################################################### |
José Fonseca | 5aa1082 | 2008-03-04 14:29:27 +0100 | [diff] [blame] | 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 | ####################################################################### |
José Fonseca | 9409043 | 2008-02-27 17:36:28 +0900 | [diff] [blame] | 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) |
José Fonseca | 7a67855 | 2008-02-27 20:13:16 +0900 | [diff] [blame] | 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')) |
José Fonseca | 9409043 | 2008-02-27 17:36:28 +0900 | [diff] [blame] | 132 | return build_dir |
| 133 | |
José Fonseca | 5aa1082 | 2008-03-04 14:29:27 +0100 | [diff] [blame] | 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) |
José Fonseca | 35460fc | 2008-04-25 18:16:25 +0900 | [diff] [blame] | 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') |
José Fonseca | 5aa1082 | 2008-03-04 14:29:27 +0100 | [diff] [blame] | 150 | |
José Fonseca | 35460fc | 2008-04-25 18:16:25 +0900 | [diff] [blame] | 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', |
José Fonseca | 1e71283 | 2008-04-26 01:55:32 +0900 | [diff] [blame] | 166 | '_CRT_SECURE_NO_DEPRECATE', |
José Fonseca | 35460fc | 2008-04-25 18:16:25 +0900 | [diff] [blame] | 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 | '/GS', # enable security checks |
| 249 | '/G6', # optimize for PPro, P-II, P-III |
| 250 | '/Ze', # enable extensions |
| 251 | #'/Gi-', # ??? |
| 252 | '/QIfdiv-', # disable Pentium FDIV fix |
| 253 | #'/hotpatch', # ??? |
| 254 | #'/Z7', #enable old-style debug info |
| 255 | ] |
| 256 | # Put debugging information in a separate .pdb file for each object file as |
| 257 | # descrived in the scons manpage |
| 258 | env['CCPDBFLAGS'] = '/Zi /Fd${TARGET}.pdb' |
| 259 | env.Append(CFLAGS = cflags) |
| 260 | env.Append(CXXFLAGS = cflags) |
| 261 | |
| 262 | # Linker options |
| 263 | if platform == 'winddk': |
| 264 | # See also: |
| 265 | # - http://msdn2.microsoft.com/en-us/library/y0zzbyt4.aspx |
| 266 | env.Append(LINKFLAGS = [ |
| 267 | '/merge:_PAGE=PAGE', |
| 268 | '/merge:_TEXT=.text', |
| 269 | '/section:INIT,d', |
| 270 | '/opt:ref', |
| 271 | '/opt:icf', |
| 272 | '/ignore:4198,4010,4037,4039,4065,4070,4078,4087,4089,4221', |
| 273 | '/incremental:no', |
| 274 | '/fullbuild', |
| 275 | '/release', |
| 276 | '/nodefaultlib', |
| 277 | '/wx', |
| 278 | '/debug', |
| 279 | '/debugtype:cv', |
| 280 | '/version:5.1', |
| 281 | '/osversion:5.1', |
| 282 | '/functionpadmin:5', |
| 283 | '/safeseh', |
| 284 | '/pdbcompress', |
| 285 | '/stack:0x40000,0x1000', |
| 286 | '/driver', |
| 287 | '/align:0x80', |
| 288 | '/subsystem:native,5.01', |
| 289 | '/base:0x10000', |
| 290 | |
| 291 | '/entry:DrvEnableDriver', |
| 292 | ]) |
| 293 | |
| 294 | |
| 295 | createConvenienceLibBuilder(env) |
| 296 | |
| 297 | |
José Fonseca | 5aa1082 | 2008-03-04 14:29:27 +0100 | [diff] [blame] | 298 | # for debugging |
| 299 | #print env.Dump() |
| 300 | |