Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4358 | Serge | 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 | def generate(env): |
||
41 | env['llvm'] = False |
||
42 | |||
43 | try: |
||
44 | llvm_dir = os.environ['LLVM'] |
||
45 | except KeyError: |
||
46 | # Do nothing -- use the system headers/libs |
||
47 | llvm_dir = None |
||
48 | else: |
||
49 | if not os.path.isdir(llvm_dir): |
||
50 | raise SCons.Errors.InternalError, "Specified LLVM directory not found" |
||
51 | |||
52 | if env['debug']: |
||
53 | llvm_subdir = 'Debug' |
||
54 | else: |
||
55 | llvm_subdir = 'Release' |
||
56 | |||
57 | llvm_bin_dir = os.path.join(llvm_dir, llvm_subdir, 'bin') |
||
58 | if not os.path.isdir(llvm_bin_dir): |
||
59 | llvm_bin_dir = os.path.join(llvm_dir, 'bin') |
||
60 | if not os.path.isdir(llvm_bin_dir): |
||
61 | raise SCons.Errors.InternalError, "LLVM binary directory not found" |
||
62 | |||
63 | env.PrependENVPath('PATH', llvm_bin_dir) |
||
64 | |||
65 | if env['platform'] == 'windows': |
||
66 | # XXX: There is no llvm-config on Windows, so assume a standard layout |
||
67 | if llvm_dir is None: |
||
68 | print 'scons: LLVM environment variable must be specified when building for windows' |
||
69 | return |
||
70 | |||
71 | # Try to determine the LLVM version from llvm/Config/config.h |
||
72 | llvm_config = os.path.join(llvm_dir, 'include/llvm/Config/config.h') |
||
73 | if not os.path.exists(llvm_config): |
||
74 | print 'scons: could not find %s' % llvm_config |
||
75 | return |
||
76 | llvm_version_re = re.compile(r'^#define PACKAGE_VERSION "([^"]*)"') |
||
77 | llvm_version = None |
||
78 | for line in open(llvm_config, 'rt'): |
||
79 | mo = llvm_version_re.match(line) |
||
80 | if mo: |
||
81 | llvm_version = mo.group(1) |
||
82 | llvm_version = distutils.version.LooseVersion(llvm_version) |
||
83 | break |
||
84 | if llvm_version is None: |
||
85 | print 'scons: could not determine the LLVM version from %s' % llvm_config |
||
86 | return |
||
87 | |||
88 | env.Prepend(CPPPATH = [os.path.join(llvm_dir, 'include')]) |
||
89 | env.AppendUnique(CPPDEFINES = [ |
||
90 | '__STDC_LIMIT_MACROS', |
||
91 | '__STDC_CONSTANT_MACROS', |
||
92 | 'HAVE_STDINT_H', |
||
93 | ]) |
||
94 | env.Prepend(LIBPATH = [os.path.join(llvm_dir, 'lib')]) |
||
95 | if llvm_version >= distutils.version.LooseVersion('3.2'): |
||
96 | # 3.2 |
||
97 | env.Prepend(LIBS = [ |
||
98 | 'LLVMBitWriter', 'LLVMX86Disassembler', 'LLVMX86AsmParser', |
||
99 | 'LLVMX86CodeGen', 'LLVMX86Desc', 'LLVMSelectionDAG', |
||
100 | 'LLVMAsmPrinter', 'LLVMMCParser', 'LLVMX86AsmPrinter', |
||
101 | 'LLVMX86Utils', 'LLVMX86Info', 'LLVMJIT', |
||
102 | 'LLVMExecutionEngine', 'LLVMCodeGen', 'LLVMScalarOpts', |
||
103 | 'LLVMInstCombine', 'LLVMTransformUtils', 'LLVMipa', |
||
104 | 'LLVMAnalysis', 'LLVMTarget', 'LLVMMC', 'LLVMCore', |
||
105 | 'LLVMSupport', 'LLVMRuntimeDyld', 'LLVMObject' |
||
106 | ]) |
||
107 | elif llvm_version >= distutils.version.LooseVersion('3.0'): |
||
108 | # 3.0 |
||
109 | env.Prepend(LIBS = [ |
||
110 | 'LLVMBitWriter', 'LLVMX86Disassembler', 'LLVMX86AsmParser', |
||
111 | 'LLVMX86CodeGen', 'LLVMX86Desc', 'LLVMSelectionDAG', |
||
112 | 'LLVMAsmPrinter', 'LLVMMCParser', 'LLVMX86AsmPrinter', |
||
113 | 'LLVMX86Utils', 'LLVMX86Info', 'LLVMJIT', |
||
114 | 'LLVMExecutionEngine', 'LLVMCodeGen', 'LLVMScalarOpts', |
||
115 | 'LLVMInstCombine', 'LLVMTransformUtils', 'LLVMipa', |
||
116 | 'LLVMAnalysis', 'LLVMTarget', 'LLVMMC', 'LLVMCore', |
||
117 | 'LLVMSupport' |
||
118 | ]) |
||
119 | elif llvm_version >= distutils.version.LooseVersion('2.9'): |
||
120 | # 2.9 |
||
121 | env.Prepend(LIBS = [ |
||
122 | 'LLVMObject', 'LLVMMCJIT', 'LLVMMCDisassembler', |
||
123 | 'LLVMLinker', 'LLVMipo', 'LLVMInterpreter', |
||
124 | 'LLVMInstrumentation', 'LLVMJIT', 'LLVMExecutionEngine', |
||
125 | 'LLVMBitWriter', 'LLVMX86Disassembler', 'LLVMX86AsmParser', |
||
126 | 'LLVMMCParser', 'LLVMX86AsmPrinter', 'LLVMX86CodeGen', |
||
127 | 'LLVMSelectionDAG', 'LLVMX86Utils', 'LLVMX86Info', 'LLVMAsmPrinter', |
||
128 | 'LLVMCodeGen', 'LLVMScalarOpts', 'LLVMInstCombine', |
||
129 | 'LLVMTransformUtils', 'LLVMipa', 'LLVMAsmParser', |
||
130 | 'LLVMArchive', 'LLVMBitReader', 'LLVMAnalysis', 'LLVMTarget', |
||
131 | 'LLVMCore', 'LLVMMC', 'LLVMSupport', |
||
132 | ]) |
||
133 | elif llvm_version >= distutils.version.LooseVersion('2.7'): |
||
134 | # 2.7 |
||
135 | env.Prepend(LIBS = [ |
||
136 | 'LLVMLinker', 'LLVMipo', 'LLVMInterpreter', |
||
137 | 'LLVMInstrumentation', 'LLVMJIT', 'LLVMExecutionEngine', |
||
138 | 'LLVMBitWriter', 'LLVMX86Disassembler', 'LLVMX86AsmParser', |
||
139 | 'LLVMMCParser', 'LLVMX86AsmPrinter', 'LLVMX86CodeGen', |
||
140 | 'LLVMSelectionDAG', 'LLVMX86Info', 'LLVMAsmPrinter', |
||
141 | 'LLVMCodeGen', 'LLVMScalarOpts', 'LLVMInstCombine', |
||
142 | 'LLVMTransformUtils', 'LLVMipa', 'LLVMAsmParser', |
||
143 | 'LLVMArchive', 'LLVMBitReader', 'LLVMAnalysis', 'LLVMTarget', |
||
144 | 'LLVMMC', 'LLVMCore', 'LLVMSupport', 'LLVMSystem', |
||
145 | ]) |
||
146 | else: |
||
147 | # 2.6 |
||
148 | env.Prepend(LIBS = [ |
||
149 | 'LLVMX86AsmParser', 'LLVMX86AsmPrinter', 'LLVMX86CodeGen', |
||
150 | 'LLVMX86Info', 'LLVMLinker', 'LLVMipo', 'LLVMInterpreter', |
||
151 | 'LLVMInstrumentation', 'LLVMJIT', 'LLVMExecutionEngine', |
||
152 | 'LLVMDebugger', 'LLVMBitWriter', 'LLVMAsmParser', |
||
153 | 'LLVMArchive', 'LLVMBitReader', 'LLVMSelectionDAG', |
||
154 | 'LLVMAsmPrinter', 'LLVMCodeGen', 'LLVMScalarOpts', |
||
155 | 'LLVMTransformUtils', 'LLVMipa', 'LLVMAnalysis', |
||
156 | 'LLVMTarget', 'LLVMMC', 'LLVMCore', 'LLVMSupport', |
||
157 | 'LLVMSystem', |
||
158 | ]) |
||
159 | env.Append(LIBS = [ |
||
160 | 'imagehlp', |
||
161 | 'psapi', |
||
162 | 'shell32', |
||
163 | 'advapi32' |
||
164 | ]) |
||
165 | if env['msvc']: |
||
166 | # Some of the LLVM C headers use the inline keyword without |
||
167 | # defining it. |
||
168 | env.Append(CPPDEFINES = [('inline', '__inline')]) |
||
169 | if env['build'] in ('debug', 'checked'): |
||
170 | # LLVM libraries are static, build with /MT, and they |
||
171 | # automatically link agains LIBCMT. When we're doing a |
||
172 | # debug build we'll be linking against LIBCMTD, so disable |
||
173 | # that. |
||
174 | env.Append(LINKFLAGS = ['/nodefaultlib:LIBCMT']) |
||
175 | else: |
||
176 | if not env.Detect('llvm-config'): |
||
177 | print 'scons: llvm-config script not found' |
||
178 | return |
||
179 | |||
180 | llvm_version = env.backtick('llvm-config --version').rstrip() |
||
181 | llvm_version = distutils.version.LooseVersion(llvm_version) |
||
182 | |||
183 | try: |
||
184 | # Treat --cppflags specially to prevent NDEBUG from disabling |
||
185 | # assertion failures in debug builds. |
||
186 | cppflags = env.ParseFlags('!llvm-config --cppflags') |
||
187 | try: |
||
188 | cppflags['CPPDEFINES'].remove('NDEBUG') |
||
189 | except ValueError: |
||
190 | pass |
||
191 | env.MergeFlags(cppflags) |
||
192 | |||
193 | components = ['engine', 'bitwriter', 'x86asmprinter'] |
||
194 | |||
195 | if llvm_version >= distutils.version.LooseVersion('3.1'): |
||
196 | components.append('mcjit') |
||
197 | |||
198 | if llvm_version >= distutils.version.LooseVersion('3.2'): |
||
199 | env.Append(CXXFLAGS = ('-fno-rtti',)) |
||
200 | |||
201 | env.ParseConfig('llvm-config --libs ' + ' '.join(components)) |
||
202 | env.ParseConfig('llvm-config --ldflags') |
||
203 | except OSError: |
||
204 | print 'scons: llvm-config version %s failed' % llvm_version |
||
205 | return |
||
206 | |||
207 | assert llvm_version is not None |
||
208 | env['llvm'] = True |
||
209 | |||
210 | print 'scons: Found LLVM version %s' % llvm_version |
||
211 | env['LLVM_VERSION'] = llvm_version |
||
212 | |||
213 | # Define HAVE_LLVM macro with the major/minor version number (e.g., 0x0206 for 2.6) |
||
214 | llvm_version_major = int(llvm_version.version[0]) |
||
215 | llvm_version_minor = int(llvm_version.version[1]) |
||
216 | llvm_version_hex = '0x%02x%02x' % (llvm_version_major, llvm_version_minor) |
||
217 | env.Prepend(CPPDEFINES = [('HAVE_LLVM', llvm_version_hex)]) |
||
218 | |||
219 | def exists(env): |
||
220 | return True |
||
221 | |||
222 | # vim:set ts=4 sw=4 et: |