José Fonseca | 836a9f0 | 2009-09-01 12:22:52 +0100 | [diff] [blame] | 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 |
José Fonseca | 8edc6b0 | 2010-03-05 15:07:50 +0000 | [diff] [blame] | 32 | import re |
José Fonseca | bf48447 | 2009-10-22 19:11:48 +0100 | [diff] [blame] | 33 | import sys |
José Fonseca | 8edc6b0 | 2010-03-05 15:07:50 +0000 | [diff] [blame] | 34 | import distutils.version |
José Fonseca | 836a9f0 | 2009-09-01 12:22:52 +0100 | [diff] [blame] | 35 | |
José Fonseca | 836a9f0 | 2009-09-01 12:22:52 +0100 | [diff] [blame] | 36 | import SCons.Errors |
| 37 | import SCons.Util |
| 38 | |
| 39 | |
| 40 | def generate(env): |
| 41 | try: |
| 42 | llvm_dir = os.environ['LLVM'] |
| 43 | except KeyError: |
| 44 | # Do nothing -- use the system headers/libs |
José Fonseca | bf48447 | 2009-10-22 19:11:48 +0100 | [diff] [blame] | 45 | llvm_dir = None |
José Fonseca | 836a9f0 | 2009-09-01 12:22:52 +0100 | [diff] [blame] | 46 | 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é Fonseca | 459ea00 | 2009-09-16 10:39:06 +0100 | [diff] [blame] | 57 | 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é Fonseca | 836a9f0 | 2009-09-01 12:22:52 +0100 | [diff] [blame] | 60 | |
| 61 | env.PrependENVPath('PATH', llvm_bin_dir) |
| 62 | |
José Fonseca | 9e3728c | 2009-11-25 18:06:12 +0000 | [diff] [blame] | 63 | if env['platform'] == 'windows': |
José Fonseca | bf48447 | 2009-10-22 19:11:48 +0100 | [diff] [blame] | 64 | # XXX: There is no llvm-config on Windows, so assume a standard layout |
José Fonseca | 8edc6b0 | 2010-03-05 15:07:50 +0000 | [diff] [blame] | 65 | if llvm_dir is None: |
José Fonseca | ea532f0 | 2010-04-10 02:41:39 +0100 | [diff] [blame] | 66 | print 'scons: LLVM environment variable must be specified when building for windows' |
| 67 | env.Exit(1) |
José Fonseca | 8edc6b0 | 2010-03-05 15:07:50 +0000 | [diff] [blame] | 68 | |
| 69 | # Try to determine the LLVM version from llvm/Config/config.h |
| 70 | llvm_config = os.path.join(llvm_dir, 'include/llvm/Config/config.h') |
| 71 | if not os.path.exists(llvm_config): |
| 72 | print 'scons: could not find %s' % llvm_config |
José Fonseca | ea532f0 | 2010-04-10 02:41:39 +0100 | [diff] [blame] | 73 | env.Exit(1) |
José Fonseca | 8edc6b0 | 2010-03-05 15:07:50 +0000 | [diff] [blame] | 74 | llvm_version_re = re.compile(r'^#define PACKAGE_VERSION "([^"]*)"') |
| 75 | llvm_version = None |
| 76 | for line in open(llvm_config, 'rt'): |
| 77 | mo = llvm_version_re.match(line) |
| 78 | if mo: |
| 79 | llvm_version = mo.group(1) |
José Fonseca | 19a6333 | 2010-03-06 09:18:15 +0000 | [diff] [blame] | 80 | llvm_version = distutils.version.LooseVersion(llvm_version) |
José Fonseca | 8edc6b0 | 2010-03-05 15:07:50 +0000 | [diff] [blame] | 81 | break |
| 82 | if llvm_version is None: |
| 83 | print 'scons: could not determine the LLVM version from %s' % llvm_config |
José Fonseca | ea532f0 | 2010-04-10 02:41:39 +0100 | [diff] [blame] | 84 | env.Exit(1) |
José Fonseca | 8edc6b0 | 2010-03-05 15:07:50 +0000 | [diff] [blame] | 85 | |
| 86 | env.Prepend(CPPPATH = [os.path.join(llvm_dir, 'include')]) |
| 87 | env.AppendUnique(CPPDEFINES = [ |
| 88 | '__STDC_LIMIT_MACROS', |
| 89 | '__STDC_CONSTANT_MACROS', |
| 90 | 'HAVE_STDINT_H', |
| 91 | ]) |
| 92 | env.Prepend(LIBPATH = [os.path.join(llvm_dir, 'lib')]) |
| 93 | if llvm_version >= distutils.version.LooseVersion('2.7'): |
| 94 | # 2.7 |
José Fonseca | bf48447 | 2009-10-22 19:11:48 +0100 | [diff] [blame] | 95 | env.Prepend(LIBS = [ |
José Fonseca | 8edc6b0 | 2010-03-05 15:07:50 +0000 | [diff] [blame] | 96 | 'LLVMLinker', 'LLVMipo', 'LLVMInterpreter', |
| 97 | 'LLVMInstrumentation', 'LLVMJIT', 'LLVMExecutionEngine', |
| 98 | 'LLVMBitWriter', 'LLVMX86Disassembler', 'LLVMX86AsmParser', |
| 99 | 'LLVMMCParser', 'LLVMX86AsmPrinter', 'LLVMX86CodeGen', |
| 100 | 'LLVMSelectionDAG', 'LLVMX86Info', 'LLVMAsmPrinter', |
| 101 | 'LLVMCodeGen', 'LLVMScalarOpts', 'LLVMInstCombine', |
| 102 | 'LLVMTransformUtils', 'LLVMipa', 'LLVMAsmParser', |
| 103 | 'LLVMArchive', 'LLVMBitReader', 'LLVMAnalysis', 'LLVMTarget', |
| 104 | 'LLVMMC', 'LLVMCore', 'LLVMSupport', 'LLVMSystem', |
José Fonseca | bf48447 | 2009-10-22 19:11:48 +0100 | [diff] [blame] | 105 | ]) |
José Fonseca | 8edc6b0 | 2010-03-05 15:07:50 +0000 | [diff] [blame] | 106 | else: |
| 107 | # 2.6 |
| 108 | env.Prepend(LIBS = [ |
| 109 | 'LLVMX86AsmParser', 'LLVMX86AsmPrinter', 'LLVMX86CodeGen', |
| 110 | 'LLVMX86Info', 'LLVMLinker', 'LLVMipo', 'LLVMInterpreter', |
| 111 | 'LLVMInstrumentation', 'LLVMJIT', 'LLVMExecutionEngine', |
| 112 | 'LLVMDebugger', 'LLVMBitWriter', 'LLVMAsmParser', |
| 113 | 'LLVMArchive', 'LLVMBitReader', 'LLVMSelectionDAG', |
| 114 | 'LLVMAsmPrinter', 'LLVMCodeGen', 'LLVMScalarOpts', |
| 115 | 'LLVMTransformUtils', 'LLVMipa', 'LLVMAnalysis', |
| 116 | 'LLVMTarget', 'LLVMMC', 'LLVMCore', 'LLVMSupport', |
| 117 | 'LLVMSystem', |
| 118 | ]) |
| 119 | env.Append(LIBS = [ |
| 120 | 'imagehlp', |
| 121 | 'psapi', |
| 122 | ]) |
| 123 | if env['msvc']: |
| 124 | # Some of the LLVM C headers use the inline keyword without |
| 125 | # defining it. |
| 126 | env.Append(CPPDEFINES = [('inline', '__inline')]) |
| 127 | if env['debug']: |
| 128 | # LLVM libraries are static, build with /MT, and they |
| 129 | # automatically link agains LIBCMT. When we're doing a |
| 130 | # debug build we'll be linking against LIBCMTD, so disable |
| 131 | # that. |
| 132 | env.Append(LINKFLAGS = ['/nodefaultlib:LIBCMT']) |
José Fonseca | ea532f0 | 2010-04-10 02:41:39 +0100 | [diff] [blame] | 133 | else: |
| 134 | if not env.Detect('llvm-config'): |
| 135 | print 'scons: llvm-config script not found' % llvm_version |
| 136 | env.Exit(1) |
| 137 | |
José Fonseca | 8edc6b0 | 2010-03-05 15:07:50 +0000 | [diff] [blame] | 138 | llvm_version = env.backtick('llvm-config --version').rstrip() |
José Fonseca | 19a6333 | 2010-03-06 09:18:15 +0000 | [diff] [blame] | 139 | llvm_version = distutils.version.LooseVersion(llvm_version) |
José Fonseca | 836a9f0 | 2009-09-01 12:22:52 +0100 | [diff] [blame] | 140 | |
Vinson Lee | 79f48c9 | 2009-09-07 15:16:25 +0100 | [diff] [blame] | 141 | try: |
| 142 | env.ParseConfig('llvm-config --cppflags') |
| 143 | env.ParseConfig('llvm-config --libs jit interpreter nativecodegen bitwriter') |
| 144 | env.ParseConfig('llvm-config --ldflags') |
| 145 | except OSError: |
José Fonseca | ea532f0 | 2010-04-10 02:41:39 +0100 | [diff] [blame] | 146 | print 'scons: llvm-config version %s failed' % llvm_version |
| 147 | env.Exit(1) |
Vinson Lee | 79f48c9 | 2009-09-07 15:16:25 +0100 | [diff] [blame] | 148 | else: |
| 149 | env['LINK'] = env['CXX'] |
José Fonseca | 8edc6b0 | 2010-03-05 15:07:50 +0000 | [diff] [blame] | 150 | |
| 151 | assert llvm_version is not None |
| 152 | |
| 153 | print 'scons: Found LLVM version %s' % llvm_version |
José Fonseca | 8edc6b0 | 2010-03-05 15:07:50 +0000 | [diff] [blame] | 154 | env['LLVM_VERSION'] = llvm_version |
| 155 | |
| 156 | # Define HAVE_LLVM macro with the major/minor version number (e.g., 0x0206 for 2.6) |
| 157 | llvm_version_major = int(llvm_version.version[0]) |
| 158 | llvm_version_minor = int(llvm_version.version[1]) |
| 159 | llvm_version_hex = '0x%02x%02x' % (llvm_version_major, llvm_version_minor) |
| 160 | env.Prepend(CPPDEFINES = [('HAVE_LLVM', llvm_version_hex)]) |
José Fonseca | 836a9f0 | 2009-09-01 12:22:52 +0100 | [diff] [blame] | 161 | |
| 162 | def exists(env): |
| 163 | return True |
| 164 | |
| 165 | # vim:set ts=4 sw=4 et: |