1848b8605Smrg#######################################################################
2848b8605Smrg# Common SCons code
3848b8605Smrg
4848b8605Smrgimport os
5848b8605Smrgimport os.path
6848b8605Smrgimport re
7848b8605Smrgimport subprocess
8848b8605Smrgimport sys
9848b8605Smrgimport platform as _platform
10848b8605Smrg
11848b8605Smrgimport SCons.Script.SConscript
12848b8605Smrg
13848b8605Smrg
14848b8605Smrg#######################################################################
15848b8605Smrg# Defaults
16848b8605Smrg
17848b8605Smrghost_platform = _platform.system().lower()
18848b8605Smrgif host_platform.startswith('cygwin'):
19848b8605Smrg    host_platform = 'cygwin'
20848b8605Smrg
21848b8605Smrg# Search sys.argv[] for a "platform=foo" argument since we don't have
22848b8605Smrg# an 'env' variable at this point.
23848b8605Smrgif 'platform' in SCons.Script.ARGUMENTS:
24848b8605Smrg    target_platform = SCons.Script.ARGUMENTS['platform']
25848b8605Smrgelse:
26848b8605Smrg    target_platform = host_platform
27848b8605Smrg
28848b8605Smrg_machine_map = {
29b8e80941Smrg    'x86': 'x86',
30b8e80941Smrg    'i386': 'x86',
31b8e80941Smrg    'i486': 'x86',
32b8e80941Smrg    'i586': 'x86',
33b8e80941Smrg    'i686': 'x86',
34b8e80941Smrg    'BePC': 'x86',
35b8e80941Smrg    'Intel': 'x86',
36b8e80941Smrg    'ppc': 'ppc',
37b8e80941Smrg    'BeBox': 'ppc',
38b8e80941Smrg    'BeMac': 'ppc',
39b8e80941Smrg    'AMD64': 'x86_64',
40b8e80941Smrg    'x86_64': 'x86_64',
41b8e80941Smrg    'sparc': 'sparc',
42b8e80941Smrg    'sun4u': 'sparc',
43848b8605Smrg}
44848b8605Smrg
45848b8605Smrg
46848b8605Smrg# find host_machine value
47848b8605Smrgif 'PROCESSOR_ARCHITECTURE' in os.environ:
48b8e80941Smrg    host_machine = os.environ['PROCESSOR_ARCHITECTURE']
49848b8605Smrgelse:
50b8e80941Smrg    host_machine = _platform.machine()
51848b8605Smrghost_machine = _machine_map.get(host_machine, 'generic')
52848b8605Smrg
53848b8605Smrgdefault_machine = host_machine
54848b8605Smrgdefault_toolchain = 'default'
55848b8605Smrg
56848b8605Smrgif target_platform == 'windows' and host_platform != 'windows':
57848b8605Smrg    default_machine = 'x86'
58848b8605Smrg    default_toolchain = 'crossmingw'
59848b8605Smrg
60848b8605Smrg
61848b8605Smrg# find default_llvm value
62b8e80941Smrgif 'LLVM' in os.environ or 'LLVM_CONFIG' in os.environ:
63848b8605Smrg    default_llvm = 'yes'
64848b8605Smrgelse:
65848b8605Smrg    default_llvm = 'no'
66848b8605Smrg    try:
67848b8605Smrg        if target_platform != 'windows' and \
68b8e80941Smrg           subprocess.call(['llvm-config', '--version'],
69b8e80941Smrg                           stdout=subprocess.PIPE) == 0:
70848b8605Smrg            default_llvm = 'yes'
71848b8605Smrg    except:
72848b8605Smrg        pass
73848b8605Smrg
74848b8605Smrg
75848b8605Smrg#######################################################################
76848b8605Smrg# Common options
77848b8605Smrg
78848b8605Smrgdef AddOptions(opts):
79b8e80941Smrg    try:
80b8e80941Smrg        from SCons.Variables.BoolVariable import BoolVariable as BoolOption
81b8e80941Smrg    except ImportError:
82b8e80941Smrg        from SCons.Options.BoolOption import BoolOption
83b8e80941Smrg    try:
84b8e80941Smrg        from SCons.Variables.EnumVariable import EnumVariable as EnumOption
85b8e80941Smrg    except ImportError:
86b8e80941Smrg        from SCons.Options.EnumOption import EnumOption
87b8e80941Smrg    opts.Add(EnumOption('build', 'build type', 'debug',
88b8e80941Smrg                        allowed_values=('debug', 'checked', 'profile',
89b8e80941Smrg                                        'release')))
90b8e80941Smrg    opts.Add(BoolOption('verbose', 'verbose output', 'no'))
91b8e80941Smrg    opts.Add(EnumOption('machine', 'use machine-specific assembly code',
92b8e80941Smrg                        default_machine,
93b8e80941Smrg                        allowed_values=('generic', 'ppc', 'x86', 'x86_64')))
94b8e80941Smrg    opts.Add(EnumOption('platform', 'target platform', host_platform,
95b8e80941Smrg                        allowed_values=('cygwin', 'darwin', 'freebsd', 'haiku',
96b8e80941Smrg                                        'linux', 'sunos', 'windows')))
97b8e80941Smrg    opts.Add(BoolOption('embedded', 'embedded build', 'no'))
98b8e80941Smrg    opts.Add(BoolOption('analyze',
99b8e80941Smrg                        'enable static code analysis where available', 'no'))
100b8e80941Smrg    opts.Add(BoolOption('asan', 'enable Address Sanitizer', 'no'))
101b8e80941Smrg    opts.Add('toolchain', 'compiler toolchain', default_toolchain)
102b8e80941Smrg    opts.Add(BoolOption('llvm', 'use LLVM', default_llvm))
103b8e80941Smrg    opts.Add(BoolOption('openmp', 'EXPERIMENTAL: compile with openmp (swrast)',
104b8e80941Smrg                        'no'))
105b8e80941Smrg    opts.Add(BoolOption('debug', 'DEPRECATED: debug build', 'yes'))
106b8e80941Smrg    opts.Add(BoolOption('profile', 'DEPRECATED: profile build', 'no'))
107b8e80941Smrg    opts.Add(BoolOption('quiet', 'DEPRECATED: profile build', 'yes'))
108b8e80941Smrg    opts.Add(BoolOption('swr', 'Build OpenSWR', 'no'))
109b8e80941Smrg    if host_platform == 'windows':
110b8e80941Smrg        opts.Add('MSVC_VERSION', 'Microsoft Visual C/C++ version')
111b8e80941Smrg        opts.Add('MSVC_USE_SCRIPT', 'Microsoft Visual C/C++ vcvarsall script', True)
112