blob: 1aade4b71d92a3d02418f3797d19eaa8bee98f59 [file] [log] [blame]
José Fonseca836a9f02009-09-01 12:22:52 +01001"""llvm
2
3Tool-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
30import os
31import os.path
José Fonseca8edc6b02010-03-05 15:07:50 +000032import re
pal1000dc099562019-03-01 12:30:15 +020033import platform as host_platform
José Fonsecabf484472009-10-22 19:11:48 +010034import sys
José Fonseca8edc6b02010-03-05 15:07:50 +000035import distutils.version
José Fonseca836a9f02009-09-01 12:22:52 +010036
José Fonseca836a9f02009-09-01 12:22:52 +010037import SCons.Errors
38import SCons.Util
39
40
José Fonseca013ff2f2014-10-22 13:09:59 +010041required_llvm_version = '3.3'
José Fonsecac0ef9a62014-05-08 13:32:07 +010042
43
José Fonseca836a9f02009-09-01 12:22:52 +010044def generate(env):
José Fonseca601498a2010-11-01 13:30:22 +000045 env['llvm'] = False
46
José Fonseca836a9f02009-09-01 12:22:52 +010047 try:
48 llvm_dir = os.environ['LLVM']
49 except KeyError:
50 # Do nothing -- use the system headers/libs
José Fonsecabf484472009-10-22 19:11:48 +010051 llvm_dir = None
José Fonseca836a9f02009-09-01 12:22:52 +010052 else:
53 if not os.path.isdir(llvm_dir):
Eric Engestrombb66af92017-09-19 14:09:01 +010054 raise SCons.Errors.InternalError("Specified LLVM directory not found")
José Fonseca836a9f02009-09-01 12:22:52 +010055
56 if env['debug']:
57 llvm_subdir = 'Debug'
58 else:
59 llvm_subdir = 'Release'
60
61 llvm_bin_dir = os.path.join(llvm_dir, llvm_subdir, 'bin')
62 if not os.path.isdir(llvm_bin_dir):
José Fonseca459ea002009-09-16 10:39:06 +010063 llvm_bin_dir = os.path.join(llvm_dir, 'bin')
64 if not os.path.isdir(llvm_bin_dir):
Eric Engestrombb66af92017-09-19 14:09:01 +010065 raise SCons.Errors.InternalError("LLVM binary directory not found")
José Fonseca836a9f02009-09-01 12:22:52 +010066
67 env.PrependENVPath('PATH', llvm_bin_dir)
68
José Fonseca9e3728c2009-11-25 18:06:12 +000069 if env['platform'] == 'windows':
José Fonsecabf484472009-10-22 19:11:48 +010070 # XXX: There is no llvm-config on Windows, so assume a standard layout
José Fonseca8edc6b02010-03-05 15:07:50 +000071 if llvm_dir is None:
Eric Engestrom7d482192017-09-19 13:56:34 +010072 print('scons: LLVM environment variable must be specified when building for windows')
José Fonseca601498a2010-11-01 13:30:22 +000073 return
José Fonseca8edc6b02010-03-05 15:07:50 +000074
75 # Try to determine the LLVM version from llvm/Config/config.h
Olivier Penab94a4e82015-04-27 10:23:58 +000076 llvm_config = os.path.join(llvm_dir, 'include/llvm/Config/llvm-config.h')
José Fonseca8edc6b02010-03-05 15:07:50 +000077 if not os.path.exists(llvm_config):
Eric Engestrom7d482192017-09-19 13:56:34 +010078 print('scons: could not find %s' % llvm_config)
José Fonseca601498a2010-11-01 13:30:22 +000079 return
Olivier Penab94a4e82015-04-27 10:23:58 +000080 llvm_version_major_re = re.compile(r'^#define LLVM_VERSION_MAJOR ([0-9]+)')
81 llvm_version_minor_re = re.compile(r'^#define LLVM_VERSION_MINOR ([0-9]+)')
José Fonseca8edc6b02010-03-05 15:07:50 +000082 llvm_version = None
Olivier Penab94a4e82015-04-27 10:23:58 +000083 llvm_version_major = None
84 llvm_version_minor = None
José Fonseca8edc6b02010-03-05 15:07:50 +000085 for line in open(llvm_config, 'rt'):
Olivier Penab94a4e82015-04-27 10:23:58 +000086 mo = llvm_version_major_re.match(line)
José Fonseca8edc6b02010-03-05 15:07:50 +000087 if mo:
Olivier Penab94a4e82015-04-27 10:23:58 +000088 llvm_version_major = mo.group(1)
89 mo = llvm_version_minor_re.match(line)
90 if mo:
91 llvm_version_minor = mo.group(1)
92 if llvm_version_major is not None and llvm_version_minor is not None:
93 llvm_version = distutils.version.LooseVersion('%s.%s' % (llvm_version_major, llvm_version_minor))
94
José Fonseca8edc6b02010-03-05 15:07:50 +000095 if llvm_version is None:
Eric Engestrom7d482192017-09-19 13:56:34 +010096 print('scons: could not determine the LLVM version from %s' % llvm_config)
José Fonseca601498a2010-11-01 13:30:22 +000097 return
José Fonsecac0ef9a62014-05-08 13:32:07 +010098 if llvm_version < distutils.version.LooseVersion(required_llvm_version):
Eric Engestrom7d482192017-09-19 13:56:34 +010099 print('scons: LLVM version %s found, but %s is required' % (llvm_version, required_llvm_version))
José Fonsecac0ef9a62014-05-08 13:32:07 +0100100 return
José Fonseca8edc6b02010-03-05 15:07:50 +0000101
102 env.Prepend(CPPPATH = [os.path.join(llvm_dir, 'include')])
José Fonseca8edc6b02010-03-05 15:07:50 +0000103 env.Prepend(LIBPATH = [os.path.join(llvm_dir, 'lib')])
Ben Boeckel58f51f02017-04-27 16:31:48 -0400104 # LIBS should match the output of `llvm-config --libs engine mcjit bitwriter x86asmprinter irreader`
Alexandru-Liviu Prodeac1b01372017-09-15 07:26:33 +0000105 if llvm_version >= distutils.version.LooseVersion('5.0'):
106 env.Prepend(LIBS = [
107 'LLVMX86Disassembler', 'LLVMX86AsmParser',
108 'LLVMX86CodeGen', 'LLVMSelectionDAG', 'LLVMAsmPrinter',
109 'LLVMDebugInfoCodeView', 'LLVMCodeGen',
110 'LLVMScalarOpts', 'LLVMInstCombine',
111 'LLVMTransformUtils',
112 'LLVMBitWriter', 'LLVMX86Desc',
113 'LLVMMCDisassembler', 'LLVMX86Info',
114 'LLVMX86AsmPrinter', 'LLVMX86Utils',
115 'LLVMMCJIT', 'LLVMExecutionEngine', 'LLVMTarget',
116 'LLVMAnalysis', 'LLVMProfileData',
117 'LLVMRuntimeDyld', 'LLVMObject', 'LLVMMCParser',
118 'LLVMBitReader', 'LLVMMC', 'LLVMCore',
119 'LLVMSupport',
120 'LLVMIRReader', 'LLVMAsmParser',
121 'LLVMDemangle', 'LLVMGlobalISel', 'LLVMDebugInfoMSF',
122 'LLVMBinaryFormat',
123 ])
Jose Fonseca8841c2c2018-06-01 19:57:31 +0100124 if env['platform'] == 'windows' and env['crosscompile']:
125 # LLVM 5.0 requires MinGW w/ pthreads due to use of std::thread and friends.
126 assert env['gcc']
Michel Zou88eb2a12019-05-24 12:32:01 +0200127 env.AppendUnique(CXXFLAGS = ['-posix'])
Alexandru-Liviu Prodeac1b01372017-09-15 07:26:33 +0000128 elif llvm_version >= distutils.version.LooseVersion('4.0'):
Ben Boeckel58f51f02017-04-27 16:31:48 -0400129 env.Prepend(LIBS = [
130 'LLVMX86Disassembler', 'LLVMX86AsmParser',
131 'LLVMX86CodeGen', 'LLVMSelectionDAG', 'LLVMAsmPrinter',
132 'LLVMDebugInfoCodeView', 'LLVMCodeGen',
133 'LLVMScalarOpts', 'LLVMInstCombine',
134 'LLVMTransformUtils',
135 'LLVMBitWriter', 'LLVMX86Desc',
136 'LLVMMCDisassembler', 'LLVMX86Info',
137 'LLVMX86AsmPrinter', 'LLVMX86Utils',
138 'LLVMMCJIT', 'LLVMExecutionEngine', 'LLVMTarget',
139 'LLVMAnalysis', 'LLVMProfileData',
140 'LLVMRuntimeDyld', 'LLVMObject', 'LLVMMCParser',
141 'LLVMBitReader', 'LLVMMC', 'LLVMCore',
142 'LLVMSupport',
143 'LLVMIRReader', 'LLVMAsmParser',
144 'LLVMDemangle', 'LLVMGlobalISel', 'LLVMDebugInfoMSF',
145 ])
146 elif llvm_version >= distutils.version.LooseVersion('3.9'):
George Kyriazis30ae2cb2016-11-09 16:35:48 -0600147 env.Prepend(LIBS = [
148 'LLVMX86Disassembler', 'LLVMX86AsmParser',
149 'LLVMX86CodeGen', 'LLVMSelectionDAG', 'LLVMAsmPrinter',
150 'LLVMDebugInfoCodeView', 'LLVMCodeGen',
151 'LLVMScalarOpts', 'LLVMInstCombine',
152 'LLVMInstrumentation', 'LLVMTransformUtils',
153 'LLVMBitWriter', 'LLVMX86Desc',
154 'LLVMMCDisassembler', 'LLVMX86Info',
155 'LLVMX86AsmPrinter', 'LLVMX86Utils',
156 'LLVMMCJIT', 'LLVMExecutionEngine', 'LLVMTarget',
157 'LLVMAnalysis', 'LLVMProfileData',
158 'LLVMRuntimeDyld', 'LLVMObject', 'LLVMMCParser',
159 'LLVMBitReader', 'LLVMMC', 'LLVMCore',
160 'LLVMSupport',
161 'LLVMIRReader', 'LLVMASMParser'
162 ])
163 elif llvm_version >= distutils.version.LooseVersion('3.7'):
Olivier Penaa5256012015-12-07 17:13:18 +0100164 env.Prepend(LIBS = [
165 'LLVMBitWriter', 'LLVMX86Disassembler', 'LLVMX86AsmParser',
166 'LLVMX86CodeGen', 'LLVMSelectionDAG', 'LLVMAsmPrinter',
167 'LLVMCodeGen', 'LLVMScalarOpts', 'LLVMProfileData',
168 'LLVMInstCombine', 'LLVMInstrumentation', 'LLVMTransformUtils', 'LLVMipa',
169 'LLVMAnalysis', 'LLVMX86Desc', 'LLVMMCDisassembler',
170 'LLVMX86Info', 'LLVMX86AsmPrinter', 'LLVMX86Utils',
171 'LLVMMCJIT', 'LLVMTarget', 'LLVMExecutionEngine',
172 'LLVMRuntimeDyld', 'LLVMObject', 'LLVMMCParser',
173 'LLVMBitReader', 'LLVMMC', 'LLVMCore', 'LLVMSupport'
174 ])
175 elif llvm_version >= distutils.version.LooseVersion('3.6'):
Olivier Penab94a4e82015-04-27 10:23:58 +0000176 env.Prepend(LIBS = [
177 'LLVMBitWriter', 'LLVMX86Disassembler', 'LLVMX86AsmParser',
178 'LLVMX86CodeGen', 'LLVMSelectionDAG', 'LLVMAsmPrinter',
179 'LLVMCodeGen', 'LLVMScalarOpts', 'LLVMProfileData',
180 'LLVMInstCombine', 'LLVMTransformUtils', 'LLVMipa',
181 'LLVMAnalysis', 'LLVMX86Desc', 'LLVMMCDisassembler',
182 'LLVMX86Info', 'LLVMX86AsmPrinter', 'LLVMX86Utils',
183 'LLVMMCJIT', 'LLVMTarget', 'LLVMExecutionEngine',
184 'LLVMRuntimeDyld', 'LLVMObject', 'LLVMMCParser',
185 'LLVMBitReader', 'LLVMMC', 'LLVMCore', 'LLVMSupport'
186 ])
187 elif llvm_version >= distutils.version.LooseVersion('3.5'):
Jose Fonseca5b941ce2015-02-03 10:16:50 +0000188 env.Prepend(LIBS = [
Jose Fonseca0db4ef92015-05-28 16:55:10 +0100189 'LLVMMCDisassembler',
Jose Fonseca5b941ce2015-02-03 10:16:50 +0000190 'LLVMBitWriter', 'LLVMMCJIT', 'LLVMRuntimeDyld',
191 'LLVMX86Disassembler', 'LLVMX86AsmParser', 'LLVMX86CodeGen',
192 'LLVMSelectionDAG', 'LLVMAsmPrinter', 'LLVMX86Desc',
193 'LLVMObject', 'LLVMMCParser', 'LLVMBitReader', 'LLVMX86Info',
194 'LLVMX86AsmPrinter', 'LLVMX86Utils', 'LLVMJIT',
195 'LLVMExecutionEngine', 'LLVMCodeGen', 'LLVMScalarOpts',
196 'LLVMInstCombine', 'LLVMTransformUtils', 'LLVMipa',
197 'LLVMAnalysis', 'LLVMTarget', 'LLVMMC', 'LLVMCore',
198 'LLVMSupport'
199 ])
200 else:
Keith Kriewallefd83112013-02-28 15:40:02 +0000201 env.Prepend(LIBS = [
Jose Fonseca0db4ef92015-05-28 16:55:10 +0100202 'LLVMMCDisassembler',
Keith Kriewallefd83112013-02-28 15:40:02 +0000203 'LLVMBitWriter', 'LLVMX86Disassembler', 'LLVMX86AsmParser',
204 'LLVMX86CodeGen', 'LLVMX86Desc', 'LLVMSelectionDAG',
205 'LLVMAsmPrinter', 'LLVMMCParser', 'LLVMX86AsmPrinter',
José Fonseca2c02f342014-05-14 12:20:14 +0100206 'LLVMX86Utils', 'LLVMX86Info', 'LLVMMCJIT', 'LLVMJIT',
Keith Kriewallefd83112013-02-28 15:40:02 +0000207 'LLVMExecutionEngine', 'LLVMCodeGen', 'LLVMScalarOpts',
208 'LLVMInstCombine', 'LLVMTransformUtils', 'LLVMipa',
209 'LLVMAnalysis', 'LLVMTarget', 'LLVMMC', 'LLVMCore',
210 'LLVMSupport', 'LLVMRuntimeDyld', 'LLVMObject'
211 ])
José Fonseca8edc6b02010-03-05 15:07:50 +0000212 env.Append(LIBS = [
213 'imagehlp',
214 'psapi',
Brian Paul3d1af782011-08-25 15:14:37 -0600215 'shell32',
Jose Fonseca8841c2c2018-06-01 19:57:31 +0100216 'advapi32',
217 'ole32',
218 'uuid',
José Fonseca8edc6b02010-03-05 15:07:50 +0000219 ])
Jose Fonseca8841c2c2018-06-01 19:57:31 +0100220
pal1000dc099562019-03-01 12:30:15 +0200221 # Mingw-w64 zlib is required when building with LLVM support in MSYS2 environment
222 if host_platform.system().lower().startswith('mingw'):
223 env.Append(LIBS = [
224 'z',
225 ])
226
José Fonseca8edc6b02010-03-05 15:07:50 +0000227 if env['msvc']:
228 # Some of the LLVM C headers use the inline keyword without
229 # defining it.
230 env.Append(CPPDEFINES = [('inline', '__inline')])
Jose Fonsecae518d972015-03-19 22:09:20 +0000231 # Match some of the warning options from llvm/cmake/modules/HandleLLVMOptions.cmake
232 env.AppendUnique(CXXFLAGS = [
233 '/wd4355', # 'this' : used in base member initializer list
234 '/wd4624', # 'derived class' : destructor could not be generated because a base class destructor is inaccessible
235 ])
José Fonsecae3a3a532010-09-29 14:24:52 +0100236 if env['build'] in ('debug', 'checked'):
José Fonseca8edc6b02010-03-05 15:07:50 +0000237 # LLVM libraries are static, build with /MT, and they
238 # automatically link agains LIBCMT. When we're doing a
239 # debug build we'll be linking against LIBCMTD, so disable
240 # that.
241 env.Append(LINKFLAGS = ['/nodefaultlib:LIBCMT'])
José Fonsecaea532f02010-04-10 02:41:39 +0100242 else:
Vinson Leef07da5a2016-11-22 17:01:35 -0800243 llvm_config = os.environ.get('LLVM_CONFIG', 'llvm-config')
244 if not env.Detect(llvm_config):
Eric Engestrom7d482192017-09-19 13:56:34 +0100245 print('scons: %s script not found' % llvm_config)
José Fonseca601498a2010-11-01 13:30:22 +0000246 return
José Fonsecaea532f02010-04-10 02:41:39 +0100247
Vinson Leef07da5a2016-11-22 17:01:35 -0800248 llvm_version = env.backtick('%s --version' % llvm_config).rstrip()
José Fonseca19a63332010-03-06 09:18:15 +0000249 llvm_version = distutils.version.LooseVersion(llvm_version)
José Fonseca836a9f02009-09-01 12:22:52 +0100250
José Fonsecac0ef9a62014-05-08 13:32:07 +0100251 if llvm_version < distutils.version.LooseVersion(required_llvm_version):
Eric Engestrom7d482192017-09-19 13:56:34 +0100252 print('scons: LLVM version %s found, but %s is required' % (llvm_version, required_llvm_version))
José Fonsecac0ef9a62014-05-08 13:32:07 +0100253 return
254
Vinson Lee79f48c92009-09-07 15:16:25 +0100255 try:
José Fonsecaacf82192011-07-11 15:36:40 +0100256 # Treat --cppflags specially to prevent NDEBUG from disabling
257 # assertion failures in debug builds.
Vinson Leef07da5a2016-11-22 17:01:35 -0800258 cppflags = env.ParseFlags('!%s --cppflags' % llvm_config)
José Fonsecaacf82192011-07-11 15:36:40 +0100259 try:
260 cppflags['CPPDEFINES'].remove('NDEBUG')
261 except ValueError:
262 pass
263 env.MergeFlags(cppflags)
264
Alexander von Gluck IV9aad1ba2013-10-16 20:24:13 -0500265 # Match llvm --fno-rtti flag
Vinson Leef07da5a2016-11-22 17:01:35 -0800266 cxxflags = env.backtick('%s --cxxflags' % llvm_config).split()
Alexander von Gluck IV9aad1ba2013-10-16 20:24:13 -0500267 if '-fno-rtti' in cxxflags:
268 env.Append(CXXFLAGS = ['-fno-rtti'])
269
Roland Scheidegger84f3f1c2019-05-24 03:46:07 +0200270 if llvm_version < distutils.version.LooseVersion('9.0'):
271 components = ['engine', 'mcjit', 'bitwriter', 'x86asmprinter', 'mcdisassembler', 'irreader']
272 else:
273 components = ['engine', 'mcjit', 'bitwriter', 'mcdisassembler', 'irreader']
José Fonseca34697152012-07-13 18:09:30 +0100274
Vinson Leef07da5a2016-11-22 17:01:35 -0800275 env.ParseConfig('%s --libs ' % llvm_config + ' '.join(components))
276 env.ParseConfig('%s --ldflags' % llvm_config)
Vinson Lee35a34142013-12-19 15:55:28 -0800277 if llvm_version >= distutils.version.LooseVersion('3.5'):
Vinson Leef07da5a2016-11-22 17:01:35 -0800278 env.ParseConfig('%s --system-libs' % llvm_config)
Kai Wasserbäch1abe8732019-08-17 10:59:43 +0200279 env.Append(CXXFLAGS = ['-std=c++14'])
Vinson Lee79f48c92009-09-07 15:16:25 +0100280 except OSError:
Eric Engestrom7d482192017-09-19 13:56:34 +0100281 print('scons: llvm-config version %s failed' % llvm_version)
José Fonseca601498a2010-11-01 13:30:22 +0000282 return
José Fonseca8edc6b02010-03-05 15:07:50 +0000283
284 assert llvm_version is not None
José Fonseca601498a2010-11-01 13:30:22 +0000285 env['llvm'] = True
José Fonseca8edc6b02010-03-05 15:07:50 +0000286
Eric Engestrom7d482192017-09-19 13:56:34 +0100287 print('scons: Found LLVM version %s' % llvm_version)
José Fonseca8edc6b02010-03-05 15:07:50 +0000288 env['LLVM_VERSION'] = llvm_version
289
290 # Define HAVE_LLVM macro with the major/minor version number (e.g., 0x0206 for 2.6)
291 llvm_version_major = int(llvm_version.version[0])
292 llvm_version_minor = int(llvm_version.version[1])
293 llvm_version_hex = '0x%02x%02x' % (llvm_version_major, llvm_version_minor)
294 env.Prepend(CPPDEFINES = [('HAVE_LLVM', llvm_version_hex)])
José Fonseca836a9f02009-09-01 12:22:52 +0100295
296def exists(env):
297 return True
298
299# vim:set ts=4 sw=4 et: