OSDN Git Service

configure: check for xlocale.h and strtof
[android-x86/external-mesa.git] / scons / llvm.py
1 """llvm
2
3 Tool-specific initialization for LLVM
4
5 """
6
7 #
8 # Copyright (c) 2009 VMware, Inc.
9 #
10 # Permission is hereby granted, free of charge, to any person obtaining
11 # a copy of this software and associated documentation files (the
12 # "Software"), to deal in the Software without restriction, including
13 # without limitation the rights to use, copy, modify, merge, publish,
14 # distribute, sublicense, and/or sell copies of the Software, and to
15 # permit persons to whom the Software is furnished to do so, subject to
16 # the following conditions:
17 #
18 # The above copyright notice and this permission notice shall be included
19 # in all copies or substantial portions of the Software.
20 #
21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
22 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
23 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #
29
30 import os
31 import os.path
32 import re
33 import sys
34 import distutils.version
35
36 import SCons.Errors
37 import SCons.Util
38
39
40 required_llvm_version = '3.3'
41
42
43 def generate(env):
44     env['llvm'] = False
45
46     try:
47         llvm_dir = os.environ['LLVM']
48     except KeyError:
49         # Do nothing -- use the system headers/libs
50         llvm_dir = None
51     else:
52         if not os.path.isdir(llvm_dir):
53             raise SCons.Errors.InternalError, "Specified LLVM directory not found"
54
55         if env['debug']:
56             llvm_subdir = 'Debug'
57         else:
58             llvm_subdir = 'Release'
59
60         llvm_bin_dir = os.path.join(llvm_dir, llvm_subdir, 'bin')
61         if not os.path.isdir(llvm_bin_dir):
62             llvm_bin_dir = os.path.join(llvm_dir, 'bin')
63             if not os.path.isdir(llvm_bin_dir):
64                 raise SCons.Errors.InternalError, "LLVM binary directory not found"
65
66         env.PrependENVPath('PATH', llvm_bin_dir)
67
68     if env['platform'] == 'windows':
69         # XXX: There is no llvm-config on Windows, so assume a standard layout
70         if llvm_dir is None:
71             print 'scons: LLVM environment variable must be specified when building for windows'
72             return
73
74         # Try to determine the LLVM version from llvm/Config/config.h
75         llvm_config = os.path.join(llvm_dir, 'include/llvm/Config/config.h')
76         if not os.path.exists(llvm_config):
77             print 'scons: could not find %s' % llvm_config
78             return
79         llvm_version_re = re.compile(r'^#define PACKAGE_VERSION "([^"]*)"')
80         llvm_version = None
81         for line in open(llvm_config, 'rt'):
82             mo = llvm_version_re.match(line)
83             if mo:
84                 llvm_version = mo.group(1)
85                 llvm_version = distutils.version.LooseVersion(llvm_version)
86                 break
87         if llvm_version is None:
88             print 'scons: could not determine the LLVM version from %s' % llvm_config
89             return
90         if llvm_version < distutils.version.LooseVersion(required_llvm_version):
91             print 'scons: LLVM version %s found, but %s is required' % (llvm_version, required_llvm_version)
92             return
93
94         env.Prepend(CPPPATH = [os.path.join(llvm_dir, 'include')])
95         env.AppendUnique(CPPDEFINES = [
96             '__STDC_LIMIT_MACROS', 
97             '__STDC_CONSTANT_MACROS',
98             'HAVE_STDINT_H',
99         ])
100         env.Prepend(LIBPATH = [os.path.join(llvm_dir, 'lib')])
101         if True:
102             # 3.2
103             env.Prepend(LIBS = [
104                 'LLVMBitWriter', 'LLVMX86Disassembler', 'LLVMX86AsmParser',
105                 'LLVMX86CodeGen', 'LLVMX86Desc', 'LLVMSelectionDAG',
106                 'LLVMAsmPrinter', 'LLVMMCParser', 'LLVMX86AsmPrinter',
107                 'LLVMX86Utils', 'LLVMX86Info', 'LLVMMCJIT', 'LLVMJIT',
108                 'LLVMExecutionEngine', 'LLVMCodeGen', 'LLVMScalarOpts',
109                 'LLVMInstCombine', 'LLVMTransformUtils', 'LLVMipa',
110                 'LLVMAnalysis', 'LLVMTarget', 'LLVMMC', 'LLVMCore',
111                 'LLVMSupport', 'LLVMRuntimeDyld', 'LLVMObject'
112             ])
113         env.Append(LIBS = [
114             'imagehlp',
115             'psapi',
116             'shell32',
117             'advapi32'
118         ])
119         if env['msvc']:
120             # Some of the LLVM C headers use the inline keyword without
121             # defining it.
122             env.Append(CPPDEFINES = [('inline', '__inline')])
123             if env['build'] in ('debug', 'checked'):
124                 # LLVM libraries are static, build with /MT, and they
125                 # automatically link agains LIBCMT. When we're doing a
126                 # debug build we'll be linking against LIBCMTD, so disable
127                 # that.
128                 env.Append(LINKFLAGS = ['/nodefaultlib:LIBCMT'])
129     else:
130         if not env.Detect('llvm-config'):
131             print 'scons: llvm-config script not found'
132             return
133
134         llvm_version = env.backtick('llvm-config --version').rstrip()
135         llvm_version = distutils.version.LooseVersion(llvm_version)
136
137         if llvm_version < distutils.version.LooseVersion(required_llvm_version):
138             print 'scons: LLVM version %s found, but %s is required' % (llvm_version, required_llvm_version)
139             return
140
141         try:
142             # Treat --cppflags specially to prevent NDEBUG from disabling
143             # assertion failures in debug builds.
144             cppflags = env.ParseFlags('!llvm-config --cppflags')
145             try:
146                 cppflags['CPPDEFINES'].remove('NDEBUG')
147             except ValueError:
148                 pass
149             env.MergeFlags(cppflags)
150
151             # Match llvm --fno-rtti flag
152             cxxflags = env.backtick('llvm-config --cxxflags').split()
153             if '-fno-rtti' in cxxflags:
154                 env.Append(CXXFLAGS = ['-fno-rtti'])
155
156             components = ['engine', 'mcjit', 'bitwriter', 'x86asmprinter']
157
158             env.ParseConfig('llvm-config --libs ' + ' '.join(components))
159             env.ParseConfig('llvm-config --ldflags')
160             if llvm_version >= distutils.version.LooseVersion('3.5'):
161                 env.ParseConfig('llvm-config --system-libs')
162                 env.Append(CXXFLAGS = ['-std=c++11'])
163         except OSError:
164             print 'scons: llvm-config version %s failed' % llvm_version
165             return
166
167     assert llvm_version is not None
168     env['llvm'] = True
169
170     print 'scons: Found LLVM version %s' % llvm_version
171     env['LLVM_VERSION'] = llvm_version
172
173     # Define HAVE_LLVM macro with the major/minor version number (e.g., 0x0206 for 2.6)
174     llvm_version_major = int(llvm_version.version[0])
175     llvm_version_minor = int(llvm_version.version[1])
176     llvm_version_hex = '0x%02x%02x' % (llvm_version_major, llvm_version_minor)
177     env.Prepend(CPPDEFINES = [('HAVE_LLVM', llvm_version_hex)])
178
179 def exists(env):
180     return True
181
182 # vim:set ts=4 sw=4 et: