blob: 88a63854bc6d1fffa470bb55049bb1b6f1e6f7e1 [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
40def generate(env):
41 try:
42 llvm_dir = os.environ['LLVM']
43 except KeyError:
44 # Do nothing -- use the system headers/libs
José Fonsecabf484472009-10-22 19:11:48 +010045 llvm_dir = None
José Fonseca836a9f02009-09-01 12:22:52 +010046 else:
47 if not os.path.isdir(llvm_dir):
48 raise SCons.Errors.InternalError, "Specified LLVM directory not found"
49
50 if env['debug']:
51 llvm_subdir = 'Debug'
52 else:
53 llvm_subdir = 'Release'
54
55 llvm_bin_dir = os.path.join(llvm_dir, llvm_subdir, 'bin')
56 if not os.path.isdir(llvm_bin_dir):
José Fonseca459ea002009-09-16 10:39:06 +010057 llvm_bin_dir = os.path.join(llvm_dir, 'bin')
58 if not os.path.isdir(llvm_bin_dir):
59 raise SCons.Errors.InternalError, "LLVM binary directory not found"
José Fonseca836a9f02009-09-01 12:22:52 +010060
61 env.PrependENVPath('PATH', llvm_bin_dir)
62
José Fonseca9e3728c2009-11-25 18:06:12 +000063 if env['platform'] == 'windows':
José Fonsecabf484472009-10-22 19:11:48 +010064 # XXX: There is no llvm-config on Windows, so assume a standard layout
José Fonseca8edc6b02010-03-05 15:07:50 +000065 if llvm_dir is None:
66 return
67
68 # Try to determine the LLVM version from llvm/Config/config.h
69 llvm_config = os.path.join(llvm_dir, 'include/llvm/Config/config.h')
70 if not os.path.exists(llvm_config):
71 print 'scons: could not find %s' % llvm_config
72 return
73 llvm_version_re = re.compile(r'^#define PACKAGE_VERSION "([^"]*)"')
74 llvm_version = None
75 for line in open(llvm_config, 'rt'):
76 mo = llvm_version_re.match(line)
77 if mo:
78 llvm_version = mo.group(1)
79 break
80 if llvm_version is None:
81 print 'scons: could not determine the LLVM version from %s' % llvm_config
82 return
83
84 env.Prepend(CPPPATH = [os.path.join(llvm_dir, 'include')])
85 env.AppendUnique(CPPDEFINES = [
86 '__STDC_LIMIT_MACROS',
87 '__STDC_CONSTANT_MACROS',
88 'HAVE_STDINT_H',
89 ])
90 env.Prepend(LIBPATH = [os.path.join(llvm_dir, 'lib')])
91 if llvm_version >= distutils.version.LooseVersion('2.7'):
92 # 2.7
José Fonsecabf484472009-10-22 19:11:48 +010093 env.Prepend(LIBS = [
José Fonseca8edc6b02010-03-05 15:07:50 +000094 'LLVMLinker', 'LLVMipo', 'LLVMInterpreter',
95 'LLVMInstrumentation', 'LLVMJIT', 'LLVMExecutionEngine',
96 'LLVMBitWriter', 'LLVMX86Disassembler', 'LLVMX86AsmParser',
97 'LLVMMCParser', 'LLVMX86AsmPrinter', 'LLVMX86CodeGen',
98 'LLVMSelectionDAG', 'LLVMX86Info', 'LLVMAsmPrinter',
99 'LLVMCodeGen', 'LLVMScalarOpts', 'LLVMInstCombine',
100 'LLVMTransformUtils', 'LLVMipa', 'LLVMAsmParser',
101 'LLVMArchive', 'LLVMBitReader', 'LLVMAnalysis', 'LLVMTarget',
102 'LLVMMC', 'LLVMCore', 'LLVMSupport', 'LLVMSystem',
José Fonsecabf484472009-10-22 19:11:48 +0100103 ])
José Fonseca8edc6b02010-03-05 15:07:50 +0000104 else:
105 # 2.6
106 env.Prepend(LIBS = [
107 'LLVMX86AsmParser', 'LLVMX86AsmPrinter', 'LLVMX86CodeGen',
108 'LLVMX86Info', 'LLVMLinker', 'LLVMipo', 'LLVMInterpreter',
109 'LLVMInstrumentation', 'LLVMJIT', 'LLVMExecutionEngine',
110 'LLVMDebugger', 'LLVMBitWriter', 'LLVMAsmParser',
111 'LLVMArchive', 'LLVMBitReader', 'LLVMSelectionDAG',
112 'LLVMAsmPrinter', 'LLVMCodeGen', 'LLVMScalarOpts',
113 'LLVMTransformUtils', 'LLVMipa', 'LLVMAnalysis',
114 'LLVMTarget', 'LLVMMC', 'LLVMCore', 'LLVMSupport',
115 'LLVMSystem',
116 ])
117 env.Append(LIBS = [
118 'imagehlp',
119 'psapi',
120 ])
121 if env['msvc']:
122 # Some of the LLVM C headers use the inline keyword without
123 # defining it.
124 env.Append(CPPDEFINES = [('inline', '__inline')])
125 if env['debug']:
126 # LLVM libraries are static, build with /MT, and they
127 # automatically link agains LIBCMT. When we're doing a
128 # debug build we'll be linking against LIBCMTD, so disable
129 # that.
130 env.Append(LINKFLAGS = ['/nodefaultlib:LIBCMT'])
José Fonsecabf484472009-10-22 19:11:48 +0100131 elif env.Detect('llvm-config'):
José Fonseca8edc6b02010-03-05 15:07:50 +0000132 llvm_version = env.backtick('llvm-config --version').rstrip()
José Fonseca836a9f02009-09-01 12:22:52 +0100133
Vinson Lee79f48c92009-09-07 15:16:25 +0100134 try:
135 env.ParseConfig('llvm-config --cppflags')
136 env.ParseConfig('llvm-config --libs jit interpreter nativecodegen bitwriter')
137 env.ParseConfig('llvm-config --ldflags')
138 except OSError:
José Fonseca8edc6b02010-03-05 15:07:50 +0000139 print 'llvm-config version %s failed' % llvm_version
Vinson Lee79f48c92009-09-07 15:16:25 +0100140 else:
141 env['LINK'] = env['CXX']
José Fonseca8edc6b02010-03-05 15:07:50 +0000142 else:
143 return
144
145 assert llvm_version is not None
146
147 print 'scons: Found LLVM version %s' % llvm_version
148 llvm_version = distutils.version.LooseVersion(llvm_version)
149 env['LLVM_VERSION'] = llvm_version
150
151 # Define HAVE_LLVM macro with the major/minor version number (e.g., 0x0206 for 2.6)
152 llvm_version_major = int(llvm_version.version[0])
153 llvm_version_minor = int(llvm_version.version[1])
154 llvm_version_hex = '0x%02x%02x' % (llvm_version_major, llvm_version_minor)
155 env.Prepend(CPPDEFINES = [('HAVE_LLVM', llvm_version_hex)])
José Fonseca836a9f02009-09-01 12:22:52 +0100156
157def exists(env):
158 return True
159
160# vim:set ts=4 sw=4 et: