Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

llvm.py 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. """llvm
  2. Tool-specific initialization for LLVM
  3. """
  4. #
  5. # Copyright (c) 2009 VMware, Inc.
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining
  8. # a copy of this software and associated documentation files (the
  9. # "Software"), to deal in the Software without restriction, including
  10. # without limitation the rights to use, copy, modify, merge, publish,
  11. # distribute, sublicense, and/or sell copies of the Software, and to
  12. # permit persons to whom the Software is furnished to do so, subject to
  13. # the following conditions:
  14. #
  15. # The above copyright notice and this permission notice shall be included
  16. # in all copies or substantial portions of the Software.
  17. #
  18. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  19. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  20. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. #
  26. import os
  27. import os.path
  28. import re
  29. import sys
  30. import distutils.version
  31. import SCons.Errors
  32. import SCons.Util
  33. def generate(env):
  34. try:
  35. llvm_dir = os.environ['LLVM']
  36. except KeyError:
  37. # Do nothing -- use the system headers/libs
  38. llvm_dir = None
  39. else:
  40. if not os.path.isdir(llvm_dir):
  41. raise SCons.Errors.InternalError, "Specified LLVM directory not found"
  42. if env['debug']:
  43. llvm_subdir = 'Debug'
  44. else:
  45. llvm_subdir = 'Release'
  46. llvm_bin_dir = os.path.join(llvm_dir, llvm_subdir, 'bin')
  47. if not os.path.isdir(llvm_bin_dir):
  48. llvm_bin_dir = os.path.join(llvm_dir, 'bin')
  49. if not os.path.isdir(llvm_bin_dir):
  50. raise SCons.Errors.InternalError, "LLVM binary directory not found"
  51. env.PrependENVPath('PATH', llvm_bin_dir)
  52. if env['platform'] == 'windows':
  53. # XXX: There is no llvm-config on Windows, so assume a standard layout
  54. if llvm_dir is None:
  55. print 'scons: LLVM environment variable must be specified when building for windows'
  56. env.Exit(1)
  57. # Try to determine the LLVM version from llvm/Config/config.h
  58. llvm_config = os.path.join(llvm_dir, 'include/llvm/Config/config.h')
  59. if not os.path.exists(llvm_config):
  60. print 'scons: could not find %s' % llvm_config
  61. env.Exit(1)
  62. llvm_version_re = re.compile(r'^#define PACKAGE_VERSION "([^"]*)"')
  63. llvm_version = None
  64. for line in open(llvm_config, 'rt'):
  65. mo = llvm_version_re.match(line)
  66. if mo:
  67. llvm_version = mo.group(1)
  68. llvm_version = distutils.version.LooseVersion(llvm_version)
  69. break
  70. if llvm_version is None:
  71. print 'scons: could not determine the LLVM version from %s' % llvm_config
  72. env.Exit(1)
  73. env.Prepend(CPPPATH = [os.path.join(llvm_dir, 'include')])
  74. env.AppendUnique(CPPDEFINES = [
  75. '__STDC_LIMIT_MACROS',
  76. '__STDC_CONSTANT_MACROS',
  77. 'HAVE_STDINT_H',
  78. ])
  79. env.Prepend(LIBPATH = [os.path.join(llvm_dir, 'lib')])
  80. if llvm_version >= distutils.version.LooseVersion('2.7'):
  81. # 2.7
  82. env.Prepend(LIBS = [
  83. 'LLVMLinker', 'LLVMipo', 'LLVMInterpreter',
  84. 'LLVMInstrumentation', 'LLVMJIT', 'LLVMExecutionEngine',
  85. 'LLVMBitWriter', 'LLVMX86Disassembler', 'LLVMX86AsmParser',
  86. 'LLVMMCParser', 'LLVMX86AsmPrinter', 'LLVMX86CodeGen',
  87. 'LLVMSelectionDAG', 'LLVMX86Info', 'LLVMAsmPrinter',
  88. 'LLVMCodeGen', 'LLVMScalarOpts', 'LLVMInstCombine',
  89. 'LLVMTransformUtils', 'LLVMipa', 'LLVMAsmParser',
  90. 'LLVMArchive', 'LLVMBitReader', 'LLVMAnalysis', 'LLVMTarget',
  91. 'LLVMMC', 'LLVMCore', 'LLVMSupport', 'LLVMSystem',
  92. ])
  93. else:
  94. # 2.6
  95. env.Prepend(LIBS = [
  96. 'LLVMX86AsmParser', 'LLVMX86AsmPrinter', 'LLVMX86CodeGen',
  97. 'LLVMX86Info', 'LLVMLinker', 'LLVMipo', 'LLVMInterpreter',
  98. 'LLVMInstrumentation', 'LLVMJIT', 'LLVMExecutionEngine',
  99. 'LLVMDebugger', 'LLVMBitWriter', 'LLVMAsmParser',
  100. 'LLVMArchive', 'LLVMBitReader', 'LLVMSelectionDAG',
  101. 'LLVMAsmPrinter', 'LLVMCodeGen', 'LLVMScalarOpts',
  102. 'LLVMTransformUtils', 'LLVMipa', 'LLVMAnalysis',
  103. 'LLVMTarget', 'LLVMMC', 'LLVMCore', 'LLVMSupport',
  104. 'LLVMSystem',
  105. ])
  106. env.Append(LIBS = [
  107. 'imagehlp',
  108. 'psapi',
  109. ])
  110. if env['msvc']:
  111. # Some of the LLVM C headers use the inline keyword without
  112. # defining it.
  113. env.Append(CPPDEFINES = [('inline', '__inline')])
  114. if env['debug']:
  115. # LLVM libraries are static, build with /MT, and they
  116. # automatically link agains LIBCMT. When we're doing a
  117. # debug build we'll be linking against LIBCMTD, so disable
  118. # that.
  119. env.Append(LINKFLAGS = ['/nodefaultlib:LIBCMT'])
  120. else:
  121. if not env.Detect('llvm-config'):
  122. print 'scons: llvm-config script not found' % llvm_version
  123. env.Exit(1)
  124. llvm_version = env.backtick('llvm-config --version').rstrip()
  125. llvm_version = distutils.version.LooseVersion(llvm_version)
  126. try:
  127. env.ParseConfig('llvm-config --cppflags')
  128. env.ParseConfig('llvm-config --libs jit interpreter nativecodegen bitwriter')
  129. env.ParseConfig('llvm-config --ldflags')
  130. except OSError:
  131. print 'scons: llvm-config version %s failed' % llvm_version
  132. env.Exit(1)
  133. else:
  134. env['LINK'] = env['CXX']
  135. assert llvm_version is not None
  136. print 'scons: Found LLVM version %s' % llvm_version
  137. env['LLVM_VERSION'] = llvm_version
  138. # Define HAVE_LLVM macro with the major/minor version number (e.g., 0x0206 for 2.6)
  139. llvm_version_major = int(llvm_version.version[0])
  140. llvm_version_minor = int(llvm_version.version[1])
  141. llvm_version_hex = '0x%02x%02x' % (llvm_version_major, llvm_version_minor)
  142. env.Prepend(CPPDEFINES = [('HAVE_LLVM', llvm_version_hex)])
  143. def exists(env):
  144. return True
  145. # vim:set ts=4 sw=4 et: