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): |
José Fonseca | 601498a | 2010-11-01 13:30:22 +0000 | [diff] [blame] | 41 | env['llvm'] = False |
| 42 | |
José Fonseca | 836a9f0 | 2009-09-01 12:22:52 +0100 | [diff] [blame] | 43 | try: |
| 44 | llvm_dir = os.environ['LLVM'] |
| 45 | except KeyError: |
| 46 | # Do nothing -- use the system headers/libs |
José Fonseca | bf48447 | 2009-10-22 19:11:48 +0100 | [diff] [blame] | 47 | llvm_dir = None |
José Fonseca | 836a9f0 | 2009-09-01 12:22:52 +0100 | [diff] [blame] | 48 | else: |
| 49 | if not os.path.isdir(llvm_dir): |
| 50 | raise SCons.Errors.InternalError, "Specified LLVM directory not found" |
| 51 | |
| 52 | if env['debug']: |
| 53 | llvm_subdir = 'Debug' |
| 54 | else: |
| 55 | llvm_subdir = 'Release' |
| 56 | |
| 57 | llvm_bin_dir = os.path.join(llvm_dir, llvm_subdir, 'bin') |
| 58 | if not os.path.isdir(llvm_bin_dir): |
José Fonseca | 459ea00 | 2009-09-16 10:39:06 +0100 | [diff] [blame] | 59 | llvm_bin_dir = os.path.join(llvm_dir, 'bin') |
| 60 | if not os.path.isdir(llvm_bin_dir): |
| 61 | raise SCons.Errors.InternalError, "LLVM binary directory not found" |
José Fonseca | 836a9f0 | 2009-09-01 12:22:52 +0100 | [diff] [blame] | 62 | |
| 63 | env.PrependENVPath('PATH', llvm_bin_dir) |
| 64 | |
José Fonseca | 9e3728c | 2009-11-25 18:06:12 +0000 | [diff] [blame] | 65 | if env['platform'] == 'windows': |
José Fonseca | bf48447 | 2009-10-22 19:11:48 +0100 | [diff] [blame] | 66 | # 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] | 67 | if llvm_dir is None: |
José Fonseca | ea532f0 | 2010-04-10 02:41:39 +0100 | [diff] [blame] | 68 | print 'scons: LLVM environment variable must be specified when building for windows' |
José Fonseca | 601498a | 2010-11-01 13:30:22 +0000 | [diff] [blame] | 69 | return |
José Fonseca | 8edc6b0 | 2010-03-05 15:07:50 +0000 | [diff] [blame] | 70 | |
| 71 | # Try to determine the LLVM version from llvm/Config/config.h |
| 72 | llvm_config = os.path.join(llvm_dir, 'include/llvm/Config/config.h') |
| 73 | if not os.path.exists(llvm_config): |
| 74 | print 'scons: could not find %s' % llvm_config |
José Fonseca | 601498a | 2010-11-01 13:30:22 +0000 | [diff] [blame] | 75 | return |
José Fonseca | 8edc6b0 | 2010-03-05 15:07:50 +0000 | [diff] [blame] | 76 | llvm_version_re = re.compile(r'^#define PACKAGE_VERSION "([^"]*)"') |
| 77 | llvm_version = None |
| 78 | for line in open(llvm_config, 'rt'): |
| 79 | mo = llvm_version_re.match(line) |
| 80 | if mo: |
| 81 | llvm_version = mo.group(1) |
José Fonseca | 19a6333 | 2010-03-06 09:18:15 +0000 | [diff] [blame] | 82 | llvm_version = distutils.version.LooseVersion(llvm_version) |
José Fonseca | 8edc6b0 | 2010-03-05 15:07:50 +0000 | [diff] [blame] | 83 | break |
| 84 | if llvm_version is None: |
| 85 | print 'scons: could not determine the LLVM version from %s' % llvm_config |
José Fonseca | 601498a | 2010-11-01 13:30:22 +0000 | [diff] [blame] | 86 | return |
José Fonseca | 8edc6b0 | 2010-03-05 15:07:50 +0000 | [diff] [blame] | 87 | |
| 88 | env.Prepend(CPPPATH = [os.path.join(llvm_dir, 'include')]) |
| 89 | env.AppendUnique(CPPDEFINES = [ |
| 90 | '__STDC_LIMIT_MACROS', |
| 91 | '__STDC_CONSTANT_MACROS', |
| 92 | 'HAVE_STDINT_H', |
| 93 | ]) |
| 94 | env.Prepend(LIBPATH = [os.path.join(llvm_dir, 'lib')]) |
José Fonseca | 7be4cf9 | 2011-08-18 16:00:03 +0100 | [diff] [blame] | 95 | if llvm_version >= distutils.version.LooseVersion('2.9'): |
| 96 | # 2.9 |
| 97 | env.Prepend(LIBS = [ |
| 98 | 'LLVMObject', 'LLVMMCJIT', 'LLVMMCDisassembler', |
| 99 | 'LLVMLinker', 'LLVMipo', 'LLVMInterpreter', |
| 100 | 'LLVMInstrumentation', 'LLVMJIT', 'LLVMExecutionEngine', |
| 101 | 'LLVMBitWriter', 'LLVMX86Disassembler', 'LLVMX86AsmParser', |
| 102 | 'LLVMMCParser', 'LLVMX86AsmPrinter', 'LLVMX86CodeGen', |
| 103 | 'LLVMSelectionDAG', 'LLVMX86Utils', 'LLVMX86Info', 'LLVMAsmPrinter', |
| 104 | 'LLVMCodeGen', 'LLVMScalarOpts', 'LLVMInstCombine', |
| 105 | 'LLVMTransformUtils', 'LLVMipa', 'LLVMAsmParser', |
| 106 | 'LLVMArchive', 'LLVMBitReader', 'LLVMAnalysis', 'LLVMTarget', |
| 107 | 'LLVMCore', 'LLVMMC', 'LLVMSupport', |
| 108 | ]) |
| 109 | elif llvm_version >= distutils.version.LooseVersion('2.7'): |
José Fonseca | 8edc6b0 | 2010-03-05 15:07:50 +0000 | [diff] [blame] | 110 | # 2.7 |
José Fonseca | bf48447 | 2009-10-22 19:11:48 +0100 | [diff] [blame] | 111 | env.Prepend(LIBS = [ |
José Fonseca | 8edc6b0 | 2010-03-05 15:07:50 +0000 | [diff] [blame] | 112 | 'LLVMLinker', 'LLVMipo', 'LLVMInterpreter', |
| 113 | 'LLVMInstrumentation', 'LLVMJIT', 'LLVMExecutionEngine', |
| 114 | 'LLVMBitWriter', 'LLVMX86Disassembler', 'LLVMX86AsmParser', |
| 115 | 'LLVMMCParser', 'LLVMX86AsmPrinter', 'LLVMX86CodeGen', |
| 116 | 'LLVMSelectionDAG', 'LLVMX86Info', 'LLVMAsmPrinter', |
| 117 | 'LLVMCodeGen', 'LLVMScalarOpts', 'LLVMInstCombine', |
| 118 | 'LLVMTransformUtils', 'LLVMipa', 'LLVMAsmParser', |
| 119 | 'LLVMArchive', 'LLVMBitReader', 'LLVMAnalysis', 'LLVMTarget', |
| 120 | 'LLVMMC', 'LLVMCore', 'LLVMSupport', 'LLVMSystem', |
José Fonseca | bf48447 | 2009-10-22 19:11:48 +0100 | [diff] [blame] | 121 | ]) |
José Fonseca | 8edc6b0 | 2010-03-05 15:07:50 +0000 | [diff] [blame] | 122 | else: |
| 123 | # 2.6 |
| 124 | env.Prepend(LIBS = [ |
| 125 | 'LLVMX86AsmParser', 'LLVMX86AsmPrinter', 'LLVMX86CodeGen', |
| 126 | 'LLVMX86Info', 'LLVMLinker', 'LLVMipo', 'LLVMInterpreter', |
| 127 | 'LLVMInstrumentation', 'LLVMJIT', 'LLVMExecutionEngine', |
| 128 | 'LLVMDebugger', 'LLVMBitWriter', 'LLVMAsmParser', |
| 129 | 'LLVMArchive', 'LLVMBitReader', 'LLVMSelectionDAG', |
| 130 | 'LLVMAsmPrinter', 'LLVMCodeGen', 'LLVMScalarOpts', |
| 131 | 'LLVMTransformUtils', 'LLVMipa', 'LLVMAnalysis', |
| 132 | 'LLVMTarget', 'LLVMMC', 'LLVMCore', 'LLVMSupport', |
| 133 | 'LLVMSystem', |
| 134 | ]) |
| 135 | env.Append(LIBS = [ |
| 136 | 'imagehlp', |
| 137 | 'psapi', |
Brian Paul | 3d1af78 | 2011-08-25 15:14:37 -0600 | [diff] [blame] | 138 | 'shell32', |
| 139 | 'advapi32' |
José Fonseca | 8edc6b0 | 2010-03-05 15:07:50 +0000 | [diff] [blame] | 140 | ]) |
| 141 | if env['msvc']: |
| 142 | # Some of the LLVM C headers use the inline keyword without |
| 143 | # defining it. |
| 144 | env.Append(CPPDEFINES = [('inline', '__inline')]) |
José Fonseca | e3a3a53 | 2010-09-29 14:24:52 +0100 | [diff] [blame] | 145 | if env['build'] in ('debug', 'checked'): |
José Fonseca | 8edc6b0 | 2010-03-05 15:07:50 +0000 | [diff] [blame] | 146 | # LLVM libraries are static, build with /MT, and they |
| 147 | # automatically link agains LIBCMT. When we're doing a |
| 148 | # debug build we'll be linking against LIBCMTD, so disable |
| 149 | # that. |
| 150 | env.Append(LINKFLAGS = ['/nodefaultlib:LIBCMT']) |
José Fonseca | ea532f0 | 2010-04-10 02:41:39 +0100 | [diff] [blame] | 151 | else: |
| 152 | if not env.Detect('llvm-config'): |
| 153 | print 'scons: llvm-config script not found' % llvm_version |
José Fonseca | 601498a | 2010-11-01 13:30:22 +0000 | [diff] [blame] | 154 | return |
José Fonseca | ea532f0 | 2010-04-10 02:41:39 +0100 | [diff] [blame] | 155 | |
José Fonseca | 8edc6b0 | 2010-03-05 15:07:50 +0000 | [diff] [blame] | 156 | llvm_version = env.backtick('llvm-config --version').rstrip() |
José Fonseca | 19a6333 | 2010-03-06 09:18:15 +0000 | [diff] [blame] | 157 | llvm_version = distutils.version.LooseVersion(llvm_version) |
José Fonseca | 836a9f0 | 2009-09-01 12:22:52 +0100 | [diff] [blame] | 158 | |
Vinson Lee | 79f48c9 | 2009-09-07 15:16:25 +0100 | [diff] [blame] | 159 | try: |
José Fonseca | acf8219 | 2011-07-11 15:36:40 +0100 | [diff] [blame] | 160 | # Treat --cppflags specially to prevent NDEBUG from disabling |
| 161 | # assertion failures in debug builds. |
| 162 | cppflags = env.ParseFlags('!llvm-config --cppflags') |
| 163 | try: |
| 164 | cppflags['CPPDEFINES'].remove('NDEBUG') |
| 165 | except ValueError: |
| 166 | pass |
| 167 | env.MergeFlags(cppflags) |
| 168 | |
ojab | 25ee5a2 | 2012-01-30 12:34:46 +0400 | [diff] [blame] | 169 | env.ParseConfig('llvm-config --libs engine bitwriter') |
Vinson Lee | 79f48c9 | 2009-09-07 15:16:25 +0100 | [diff] [blame] | 170 | env.ParseConfig('llvm-config --ldflags') |
| 171 | except OSError: |
José Fonseca | ea532f0 | 2010-04-10 02:41:39 +0100 | [diff] [blame] | 172 | print 'scons: llvm-config version %s failed' % llvm_version |
José Fonseca | 601498a | 2010-11-01 13:30:22 +0000 | [diff] [blame] | 173 | return |
José Fonseca | 8edc6b0 | 2010-03-05 15:07:50 +0000 | [diff] [blame] | 174 | |
| 175 | assert llvm_version is not None |
José Fonseca | 601498a | 2010-11-01 13:30:22 +0000 | [diff] [blame] | 176 | env['llvm'] = True |
José Fonseca | 8edc6b0 | 2010-03-05 15:07:50 +0000 | [diff] [blame] | 177 | |
| 178 | print 'scons: Found LLVM version %s' % llvm_version |
José Fonseca | 8edc6b0 | 2010-03-05 15:07:50 +0000 | [diff] [blame] | 179 | env['LLVM_VERSION'] = llvm_version |
| 180 | |
| 181 | # Define HAVE_LLVM macro with the major/minor version number (e.g., 0x0206 for 2.6) |
| 182 | llvm_version_major = int(llvm_version.version[0]) |
| 183 | llvm_version_minor = int(llvm_version.version[1]) |
| 184 | llvm_version_hex = '0x%02x%02x' % (llvm_version_major, llvm_version_minor) |
| 185 | env.Prepend(CPPDEFINES = [('HAVE_LLVM', llvm_version_hex)]) |
José Fonseca | 836a9f0 | 2009-09-01 12:22:52 +0100 | [diff] [blame] | 186 | |
| 187 | def exists(env): |
| 188 | return True |
| 189 | |
| 190 | # vim:set ts=4 sw=4 et: |