blob: 17278df88be911fce48f8ed76b0eae3c3592d954 [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
José Fonsecabf484472009-10-22 19:11:48 +010033import sys
José Fonseca8edc6b02010-03-05 15:07:50 +000034import distutils.version
José Fonseca836a9f02009-09-01 12:22:52 +010035
José Fonseca836a9f02009-09-01 12:22:52 +010036import SCons.Errors
37import SCons.Util
38
39
José Fonseca013ff2f2014-10-22 13:09:59 +010040required_llvm_version = '3.3'
José Fonsecac0ef9a62014-05-08 13:32:07 +010041
42
José Fonseca836a9f02009-09-01 12:22:52 +010043def generate(env):
José Fonseca601498a2010-11-01 13:30:22 +000044 env['llvm'] = False
45
José Fonseca836a9f02009-09-01 12:22:52 +010046 try:
47 llvm_dir = os.environ['LLVM']
48 except KeyError:
49 # Do nothing -- use the system headers/libs
José Fonsecabf484472009-10-22 19:11:48 +010050 llvm_dir = None
José Fonseca836a9f02009-09-01 12:22:52 +010051 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):
José Fonseca459ea002009-09-16 10:39:06 +010062 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"
José Fonseca836a9f02009-09-01 12:22:52 +010065
66 env.PrependENVPath('PATH', llvm_bin_dir)
67
José Fonseca9e3728c2009-11-25 18:06:12 +000068 if env['platform'] == 'windows':
José Fonsecabf484472009-10-22 19:11:48 +010069 # XXX: There is no llvm-config on Windows, so assume a standard layout
José Fonseca8edc6b02010-03-05 15:07:50 +000070 if llvm_dir is None:
José Fonsecaea532f02010-04-10 02:41:39 +010071 print 'scons: LLVM environment variable must be specified when building for windows'
José Fonseca601498a2010-11-01 13:30:22 +000072 return
José Fonseca8edc6b02010-03-05 15:07:50 +000073
74 # Try to determine the LLVM version from llvm/Config/config.h
Olivier Penab94a4e82015-04-27 10:23:58 +000075 llvm_config = os.path.join(llvm_dir, 'include/llvm/Config/llvm-config.h')
José Fonseca8edc6b02010-03-05 15:07:50 +000076 if not os.path.exists(llvm_config):
77 print 'scons: could not find %s' % llvm_config
José Fonseca601498a2010-11-01 13:30:22 +000078 return
Olivier Penab94a4e82015-04-27 10:23:58 +000079 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]+)')
José Fonseca8edc6b02010-03-05 15:07:50 +000081 llvm_version = None
Olivier Penab94a4e82015-04-27 10:23:58 +000082 llvm_version_major = None
83 llvm_version_minor = None
José Fonseca8edc6b02010-03-05 15:07:50 +000084 for line in open(llvm_config, 'rt'):
Olivier Penab94a4e82015-04-27 10:23:58 +000085 mo = llvm_version_major_re.match(line)
José Fonseca8edc6b02010-03-05 15:07:50 +000086 if mo:
Olivier Penab94a4e82015-04-27 10:23:58 +000087 llvm_version_major = mo.group(1)
88 mo = llvm_version_minor_re.match(line)
89 if mo:
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))
93
José Fonseca8edc6b02010-03-05 15:07:50 +000094 if llvm_version is None:
95 print 'scons: could not determine the LLVM version from %s' % llvm_config
José Fonseca601498a2010-11-01 13:30:22 +000096 return
José Fonsecac0ef9a62014-05-08 13:32:07 +010097 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)
99 return
José Fonseca8edc6b02010-03-05 15:07:50 +0000100
101 env.Prepend(CPPPATH = [os.path.join(llvm_dir, 'include')])
102 env.AppendUnique(CPPDEFINES = [
103 '__STDC_LIMIT_MACROS',
104 '__STDC_CONSTANT_MACROS',
105 'HAVE_STDINT_H',
106 ])
107 env.Prepend(LIBPATH = [os.path.join(llvm_dir, 'lib')])
Jose Fonseca5b941ce2015-02-03 10:16:50 +0000108 # LIBS should match the output of `llvm-config --libs engine mcjit bitwriter x86asmprinter`
Olivier Penab94a4e82015-04-27 10:23:58 +0000109 if llvm_version >= distutils.version.LooseVersion('3.6'):
110 env.Prepend(LIBS = [
111 'LLVMBitWriter', 'LLVMX86Disassembler', 'LLVMX86AsmParser',
112 'LLVMX86CodeGen', 'LLVMSelectionDAG', 'LLVMAsmPrinter',
113 'LLVMCodeGen', 'LLVMScalarOpts', 'LLVMProfileData',
114 'LLVMInstCombine', 'LLVMTransformUtils', 'LLVMipa',
115 'LLVMAnalysis', 'LLVMX86Desc', 'LLVMMCDisassembler',
116 'LLVMX86Info', 'LLVMX86AsmPrinter', 'LLVMX86Utils',
117 'LLVMMCJIT', 'LLVMTarget', 'LLVMExecutionEngine',
118 'LLVMRuntimeDyld', 'LLVMObject', 'LLVMMCParser',
119 'LLVMBitReader', 'LLVMMC', 'LLVMCore', 'LLVMSupport'
120 ])
121 elif llvm_version >= distutils.version.LooseVersion('3.5'):
Jose Fonseca5b941ce2015-02-03 10:16:50 +0000122 env.Prepend(LIBS = [
123 'LLVMBitWriter', 'LLVMMCJIT', 'LLVMRuntimeDyld',
124 'LLVMX86Disassembler', 'LLVMX86AsmParser', 'LLVMX86CodeGen',
125 'LLVMSelectionDAG', 'LLVMAsmPrinter', 'LLVMX86Desc',
126 'LLVMObject', 'LLVMMCParser', 'LLVMBitReader', 'LLVMX86Info',
127 'LLVMX86AsmPrinter', 'LLVMX86Utils', 'LLVMJIT',
128 'LLVMExecutionEngine', 'LLVMCodeGen', 'LLVMScalarOpts',
129 'LLVMInstCombine', 'LLVMTransformUtils', 'LLVMipa',
130 'LLVMAnalysis', 'LLVMTarget', 'LLVMMC', 'LLVMCore',
131 'LLVMSupport'
132 ])
133 else:
Keith Kriewallefd83112013-02-28 15:40:02 +0000134 env.Prepend(LIBS = [
135 'LLVMBitWriter', 'LLVMX86Disassembler', 'LLVMX86AsmParser',
136 'LLVMX86CodeGen', 'LLVMX86Desc', 'LLVMSelectionDAG',
137 'LLVMAsmPrinter', 'LLVMMCParser', 'LLVMX86AsmPrinter',
José Fonseca2c02f342014-05-14 12:20:14 +0100138 'LLVMX86Utils', 'LLVMX86Info', 'LLVMMCJIT', 'LLVMJIT',
Keith Kriewallefd83112013-02-28 15:40:02 +0000139 'LLVMExecutionEngine', 'LLVMCodeGen', 'LLVMScalarOpts',
140 'LLVMInstCombine', 'LLVMTransformUtils', 'LLVMipa',
141 'LLVMAnalysis', 'LLVMTarget', 'LLVMMC', 'LLVMCore',
142 'LLVMSupport', 'LLVMRuntimeDyld', 'LLVMObject'
143 ])
José Fonseca8edc6b02010-03-05 15:07:50 +0000144 env.Append(LIBS = [
145 'imagehlp',
146 'psapi',
Brian Paul3d1af782011-08-25 15:14:37 -0600147 'shell32',
148 'advapi32'
José Fonseca8edc6b02010-03-05 15:07:50 +0000149 ])
150 if env['msvc']:
151 # Some of the LLVM C headers use the inline keyword without
152 # defining it.
153 env.Append(CPPDEFINES = [('inline', '__inline')])
Jose Fonsecae518d972015-03-19 22:09:20 +0000154 # Match some of the warning options from llvm/cmake/modules/HandleLLVMOptions.cmake
155 env.AppendUnique(CXXFLAGS = [
156 '/wd4355', # 'this' : used in base member initializer list
157 '/wd4624', # 'derived class' : destructor could not be generated because a base class destructor is inaccessible
158 ])
José Fonsecae3a3a532010-09-29 14:24:52 +0100159 if env['build'] in ('debug', 'checked'):
José Fonseca8edc6b02010-03-05 15:07:50 +0000160 # LLVM libraries are static, build with /MT, and they
161 # automatically link agains LIBCMT. When we're doing a
162 # debug build we'll be linking against LIBCMTD, so disable
163 # that.
164 env.Append(LINKFLAGS = ['/nodefaultlib:LIBCMT'])
José Fonsecaea532f02010-04-10 02:41:39 +0100165 else:
166 if not env.Detect('llvm-config'):
José Fonseca39509532013-03-05 22:46:01 +0000167 print 'scons: llvm-config script not found'
José Fonseca601498a2010-11-01 13:30:22 +0000168 return
José Fonsecaea532f02010-04-10 02:41:39 +0100169
José Fonseca8edc6b02010-03-05 15:07:50 +0000170 llvm_version = env.backtick('llvm-config --version').rstrip()
José Fonseca19a63332010-03-06 09:18:15 +0000171 llvm_version = distutils.version.LooseVersion(llvm_version)
José Fonseca836a9f02009-09-01 12:22:52 +0100172
José Fonsecac0ef9a62014-05-08 13:32:07 +0100173 if llvm_version < distutils.version.LooseVersion(required_llvm_version):
174 print 'scons: LLVM version %s found, but %s is required' % (llvm_version, required_llvm_version)
175 return
176
Vinson Lee79f48c92009-09-07 15:16:25 +0100177 try:
José Fonsecaacf82192011-07-11 15:36:40 +0100178 # Treat --cppflags specially to prevent NDEBUG from disabling
179 # assertion failures in debug builds.
180 cppflags = env.ParseFlags('!llvm-config --cppflags')
181 try:
182 cppflags['CPPDEFINES'].remove('NDEBUG')
183 except ValueError:
184 pass
185 env.MergeFlags(cppflags)
186
Alexander von Gluck IV9aad1ba2013-10-16 20:24:13 -0500187 # Match llvm --fno-rtti flag
188 cxxflags = env.backtick('llvm-config --cxxflags').split()
189 if '-fno-rtti' in cxxflags:
190 env.Append(CXXFLAGS = ['-fno-rtti'])
191
José Fonseca2c02f342014-05-14 12:20:14 +0100192 components = ['engine', 'mcjit', 'bitwriter', 'x86asmprinter']
José Fonseca34697152012-07-13 18:09:30 +0100193
194 env.ParseConfig('llvm-config --libs ' + ' '.join(components))
Vinson Lee79f48c92009-09-07 15:16:25 +0100195 env.ParseConfig('llvm-config --ldflags')
Vinson Lee35a34142013-12-19 15:55:28 -0800196 if llvm_version >= distutils.version.LooseVersion('3.5'):
197 env.ParseConfig('llvm-config --system-libs')
Vinson Leef2d724c2014-03-01 18:31:07 -0800198 env.Append(CXXFLAGS = ['-std=c++11'])
Vinson Lee79f48c92009-09-07 15:16:25 +0100199 except OSError:
José Fonsecaea532f02010-04-10 02:41:39 +0100200 print 'scons: llvm-config version %s failed' % llvm_version
José Fonseca601498a2010-11-01 13:30:22 +0000201 return
José Fonseca8edc6b02010-03-05 15:07:50 +0000202
203 assert llvm_version is not None
José Fonseca601498a2010-11-01 13:30:22 +0000204 env['llvm'] = True
José Fonseca8edc6b02010-03-05 15:07:50 +0000205
206 print 'scons: Found LLVM version %s' % llvm_version
José Fonseca8edc6b02010-03-05 15:07:50 +0000207 env['LLVM_VERSION'] = llvm_version
208
209 # Define HAVE_LLVM macro with the major/minor version number (e.g., 0x0206 for 2.6)
210 llvm_version_major = int(llvm_version.version[0])
211 llvm_version_minor = int(llvm_version.version[1])
212 llvm_version_hex = '0x%02x%02x' % (llvm_version_major, llvm_version_minor)
213 env.Prepend(CPPDEFINES = [('HAVE_LLVM', llvm_version_hex)])
José Fonseca836a9f02009-09-01 12:22:52 +0100214
215def exists(env):
216 return True
217
218# vim:set ts=4 sw=4 et: