scons: Fix cross-compilation.
[mesa.git] / common.py
1 #######################################################################
2 # Common SCons code
3
4 import os
5 import os.path
6 import re
7 import subprocess
8 import sys
9 import platform as _platform
10
11 import SCons.Script.SConscript
12
13
14 #######################################################################
15 # Defaults
16
17 _platform_map = {
18 'linux2': 'linux',
19 'win32': 'windows',
20 }
21
22 host_platform = sys.platform
23 host_platform = _platform_map.get(host_platform, host_platform)
24
25 # Search sys.argv[] for a "platform=foo" argument since we don't have
26 # an 'env' variable at this point.
27 if 'platform' in SCons.Script.ARGUMENTS:
28 target_platform = SCons.Script.ARGUMENTS['platform']
29 else:
30 target_platform = host_platform
31
32 cross_compiling = target_platform != host_platform
33
34 _machine_map = {
35 'x86': 'x86',
36 'i386': 'x86',
37 'i486': 'x86',
38 'i586': 'x86',
39 'i686': 'x86',
40 'ppc' : 'ppc',
41 'x86_64': 'x86_64',
42 }
43
44
45 # find host_machine value
46 if 'PROCESSOR_ARCHITECTURE' in os.environ:
47 host_machine = os.environ['PROCESSOR_ARCHITECTURE']
48 else:
49 host_machine = _platform.machine()
50 host_machine = _machine_map.get(host_machine, 'generic')
51
52 default_machine = host_machine
53 default_toolchain = 'default'
54
55 if target_platform == 'windows' and cross_compiling:
56 default_machine = 'x86'
57 default_toolchain = 'crossmingw'
58
59
60 # find default_llvm value
61 if 'LLVM' in os.environ:
62 default_llvm = 'yes'
63 else:
64 default_llvm = 'no'
65 try:
66 if target_platform != 'windows' and \
67 subprocess.call(['llvm-config', '--version'], stdout=subprocess.PIPE) == 0:
68 default_llvm = 'yes'
69 except:
70 pass
71
72
73 #######################################################################
74 # Common options
75
76 def AddOptions(opts):
77 try:
78 from SCons.Variables.BoolVariable import BoolVariable as BoolOption
79 except ImportError:
80 from SCons.Options.BoolOption import BoolOption
81 try:
82 from SCons.Variables.EnumVariable import EnumVariable as EnumOption
83 except ImportError:
84 from SCons.Options.EnumOption import EnumOption
85 opts.Add(EnumOption('build', 'build type', 'debug',
86 allowed_values=('debug', 'checked', 'profile', 'release')))
87 opts.Add(BoolOption('quiet', 'quiet command lines', 'yes'))
88 opts.Add(EnumOption('machine', 'use machine-specific assembly code', default_machine,
89 allowed_values=('generic', 'ppc', 'x86', 'x86_64')))
90 opts.Add(EnumOption('platform', 'target platform', host_platform,
91 allowed_values=('linux', 'cell', 'windows', 'winddk', 'wince', 'darwin', 'embedded', 'cygwin', 'sunos5', 'freebsd8')))
92 opts.Add('toolchain', 'compiler toolchain', default_toolchain)
93 opts.Add(BoolOption('llvm', 'use LLVM', default_llvm))
94 opts.Add(BoolOption('debug', 'DEPRECATED: debug build', 'yes'))
95 opts.Add(BoolOption('profile', 'DEPRECATED: profile build', 'no'))
96 opts.Add(EnumOption('MSVS_VERSION', 'MS Visual C++ version', None, allowed_values=('7.1', '8.0', '9.0')))