3 Tool-specific initialization for LLVM
8 # Copyright (c) 2009 VMware, Inc.
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:
18 # The above copyright notice and this permission notice shall be included
19 # in all copies or substantial portions of the Software.
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.
34 import distutils
.version
40 required_llvm_version
= '3.3'
47 llvm_dir
= os
.environ
['LLVM']
49 # Do nothing -- use the system headers/libs
52 if not os
.path
.isdir(llvm_dir
):
53 raise SCons
.Errors
.InternalError
, "Specified LLVM directory not found"
58 llvm_subdir
= 'Release'
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"
66 env
.PrependENVPath('PATH', llvm_bin_dir
)
68 if env
['platform'] == 'windows':
69 # XXX: There is no llvm-config on Windows, so assume a standard layout
71 print 'scons: LLVM environment variable must be specified when building for windows'
74 # Try to determine the LLVM version from llvm/Config/config.h
75 llvm_config
= os
.path
.join(llvm_dir
, 'include/llvm/Config/llvm-config.h')
76 if not os
.path
.exists(llvm_config
):
77 print 'scons: could not find %s' % llvm_config
79 llvm_version_major_re
= re
.compile(r
'^#define LLVM_VERSION_MAJOR ([0-9]+)')
80 llvm_version_minor_re
= re
.compile(r
'^#define LLVM_VERSION_MINOR ([0-9]+)')
82 llvm_version_major
= None
83 llvm_version_minor
= None
84 for line
in open(llvm_config
, 'rt'):
85 mo
= llvm_version_major_re
.match(line
)
87 llvm_version_major
= mo
.group(1)
88 mo
= llvm_version_minor_re
.match(line
)
90 llvm_version_minor
= mo
.group(1)
91 if llvm_version_major
is not None and llvm_version_minor
is not None:
92 llvm_version
= distutils
.version
.LooseVersion('%s.%s' % (llvm_version_major
, llvm_version_minor
))
94 if llvm_version
is None:
95 print 'scons: could not determine the LLVM version from %s' % llvm_config
97 if llvm_version
< distutils
.version
.LooseVersion(required_llvm_version
):
98 print 'scons: LLVM version %s found, but %s is required' % (llvm_version
, required_llvm_version
)
101 env
.Prepend(CPPPATH
= [os
.path
.join(llvm_dir
, 'include')])
102 env
.AppendUnique(CPPDEFINES
= [
103 '__STDC_LIMIT_MACROS',
104 '__STDC_CONSTANT_MACROS',
107 env
.Prepend(LIBPATH
= [os
.path
.join(llvm_dir
, 'lib')])
108 # LIBS should match the output of `llvm-config --libs engine mcjit bitwriter x86asmprinter`
109 if llvm_version
>= distutils
.version
.LooseVersion('3.7'):
111 'LLVMBitWriter', 'LLVMX86Disassembler', 'LLVMX86AsmParser',
112 'LLVMX86CodeGen', 'LLVMSelectionDAG', 'LLVMAsmPrinter',
113 'LLVMCodeGen', 'LLVMScalarOpts', 'LLVMProfileData',
114 'LLVMInstCombine', 'LLVMInstrumentation', 'LLVMTransformUtils', 'LLVMipa',
115 'LLVMAnalysis', 'LLVMX86Desc', 'LLVMMCDisassembler',
116 'LLVMX86Info', 'LLVMX86AsmPrinter', 'LLVMX86Utils',
117 'LLVMMCJIT', 'LLVMTarget', 'LLVMExecutionEngine',
118 'LLVMRuntimeDyld', 'LLVMObject', 'LLVMMCParser',
119 'LLVMBitReader', 'LLVMMC', 'LLVMCore', 'LLVMSupport'
121 elif llvm_version
>= distutils
.version
.LooseVersion('3.6'):
123 'LLVMBitWriter', 'LLVMX86Disassembler', 'LLVMX86AsmParser',
124 'LLVMX86CodeGen', 'LLVMSelectionDAG', 'LLVMAsmPrinter',
125 'LLVMCodeGen', 'LLVMScalarOpts', 'LLVMProfileData',
126 'LLVMInstCombine', 'LLVMTransformUtils', 'LLVMipa',
127 'LLVMAnalysis', 'LLVMX86Desc', 'LLVMMCDisassembler',
128 'LLVMX86Info', 'LLVMX86AsmPrinter', 'LLVMX86Utils',
129 'LLVMMCJIT', 'LLVMTarget', 'LLVMExecutionEngine',
130 'LLVMRuntimeDyld', 'LLVMObject', 'LLVMMCParser',
131 'LLVMBitReader', 'LLVMMC', 'LLVMCore', 'LLVMSupport'
133 elif llvm_version
>= distutils
.version
.LooseVersion('3.5'):
135 'LLVMMCDisassembler',
136 'LLVMBitWriter', 'LLVMMCJIT', 'LLVMRuntimeDyld',
137 'LLVMX86Disassembler', 'LLVMX86AsmParser', 'LLVMX86CodeGen',
138 'LLVMSelectionDAG', 'LLVMAsmPrinter', 'LLVMX86Desc',
139 'LLVMObject', 'LLVMMCParser', 'LLVMBitReader', 'LLVMX86Info',
140 'LLVMX86AsmPrinter', 'LLVMX86Utils', 'LLVMJIT',
141 'LLVMExecutionEngine', 'LLVMCodeGen', 'LLVMScalarOpts',
142 'LLVMInstCombine', 'LLVMTransformUtils', 'LLVMipa',
143 'LLVMAnalysis', 'LLVMTarget', 'LLVMMC', 'LLVMCore',
148 'LLVMMCDisassembler',
149 'LLVMBitWriter', 'LLVMX86Disassembler', 'LLVMX86AsmParser',
150 'LLVMX86CodeGen', 'LLVMX86Desc', 'LLVMSelectionDAG',
151 'LLVMAsmPrinter', 'LLVMMCParser', 'LLVMX86AsmPrinter',
152 'LLVMX86Utils', 'LLVMX86Info', 'LLVMMCJIT', 'LLVMJIT',
153 'LLVMExecutionEngine', 'LLVMCodeGen', 'LLVMScalarOpts',
154 'LLVMInstCombine', 'LLVMTransformUtils', 'LLVMipa',
155 'LLVMAnalysis', 'LLVMTarget', 'LLVMMC', 'LLVMCore',
156 'LLVMSupport', 'LLVMRuntimeDyld', 'LLVMObject'
165 # Some of the LLVM C headers use the inline keyword without
167 env
.Append(CPPDEFINES
= [('inline', '__inline')])
168 # Match some of the warning options from llvm/cmake/modules/HandleLLVMOptions.cmake
169 env
.AppendUnique(CXXFLAGS
= [
170 '/wd4355', # 'this' : used in base member initializer list
171 '/wd4624', # 'derived class' : destructor could not be generated because a base class destructor is inaccessible
173 if env
['build'] in ('debug', 'checked'):
174 # LLVM libraries are static, build with /MT, and they
175 # automatically link agains LIBCMT. When we're doing a
176 # debug build we'll be linking against LIBCMTD, so disable
178 env
.Append(LINKFLAGS
= ['/nodefaultlib:LIBCMT'])
180 if not env
.Detect('llvm-config'):
181 print 'scons: llvm-config script not found'
184 llvm_version
= env
.backtick('llvm-config --version').rstrip()
185 llvm_version
= distutils
.version
.LooseVersion(llvm_version
)
187 if llvm_version
< distutils
.version
.LooseVersion(required_llvm_version
):
188 print 'scons: LLVM version %s found, but %s is required' % (llvm_version
, required_llvm_version
)
192 # Treat --cppflags specially to prevent NDEBUG from disabling
193 # assertion failures in debug builds.
194 cppflags
= env
.ParseFlags('!llvm-config --cppflags')
196 cppflags
['CPPDEFINES'].remove('NDEBUG')
199 env
.MergeFlags(cppflags
)
201 # Match llvm --fno-rtti flag
202 cxxflags
= env
.backtick('llvm-config --cxxflags').split()
203 if '-fno-rtti' in cxxflags
:
204 env
.Append(CXXFLAGS
= ['-fno-rtti'])
206 components
= ['engine', 'mcjit', 'bitwriter', 'x86asmprinter', 'mcdisassembler']
208 env
.ParseConfig('llvm-config --libs ' + ' '.join(components
))
209 env
.ParseConfig('llvm-config --ldflags')
210 if llvm_version
>= distutils
.version
.LooseVersion('3.5'):
211 env
.ParseConfig('llvm-config --system-libs')
212 env
.Append(CXXFLAGS
= ['-std=c++11'])
214 print 'scons: llvm-config version %s failed' % llvm_version
217 assert llvm_version
is not None
220 print 'scons: Found LLVM version %s' % llvm_version
221 env
['LLVM_VERSION'] = llvm_version
223 # Define HAVE_LLVM macro with the major/minor version number (e.g., 0x0206 for 2.6)
224 llvm_version_major
= int(llvm_version
.version
[0])
225 llvm_version_minor
= int(llvm_version
.version
[1])
226 llvm_version_hex
= '0x%02x%02x' % (llvm_version_major
, llvm_version_minor
)
227 env
.Prepend(CPPDEFINES
= [('HAVE_LLVM', llvm_version_hex
)])
232 # vim:set ts=4 sw=4 et: