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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. env['llvm'] = False
  35. try:
  36. llvm_dir = os.environ['LLVM']
  37. except KeyError:
  38. # Do nothing -- use the system headers/libs
  39. llvm_dir = None
  40. else:
  41. if not os.path.isdir(llvm_dir):
  42. raise SCons.Errors.InternalError, "Specified LLVM directory not found"
  43. if env['debug']:
  44. llvm_subdir = 'Debug'
  45. else:
  46. llvm_subdir = 'Release'
  47. llvm_bin_dir = os.path.join(llvm_dir, llvm_subdir, 'bin')
  48. if not os.path.isdir(llvm_bin_dir):
  49. llvm_bin_dir = os.path.join(llvm_dir, 'bin')
  50. if not os.path.isdir(llvm_bin_dir):
  51. raise SCons.Errors.InternalError, "LLVM binary directory not found"
  52. env.PrependENVPath('PATH', llvm_bin_dir)
  53. if env['platform'] == 'windows':
  54. # XXX: There is no llvm-config on Windows, so assume a standard layout
  55. if llvm_dir is None:
  56. print 'scons: LLVM environment variable must be specified when building for windows'
  57. return
  58. # Try to determine the LLVM version from llvm/Config/config.h
  59. llvm_config = os.path.join(llvm_dir, 'include/llvm/Config/config.h')
  60. if not os.path.exists(llvm_config):
  61. print 'scons: could not find %s' % llvm_config
  62. return
  63. llvm_version_re = re.compile(r'^#define PACKAGE_VERSION "([^"]*)"')
  64. llvm_version = None
  65. for line in open(llvm_config, 'rt'):
  66. mo = llvm_version_re.match(line)
  67. if mo:
  68. llvm_version = mo.group(1)
  69. llvm_version = distutils.version.LooseVersion(llvm_version)
  70. break
  71. if llvm_version is None:
  72. print 'scons: could not determine the LLVM version from %s' % llvm_config
  73. return
  74. env.Prepend(CPPPATH = [os.path.join(llvm_dir, 'include')])
  75. env.AppendUnique(CPPDEFINES = [
  76. '__STDC_LIMIT_MACROS',
  77. '__STDC_CONSTANT_MACROS',
  78. 'HAVE_STDINT_H',
  79. ])
  80. env.Prepend(LIBPATH = [os.path.join(llvm_dir, 'lib')])
  81. if llvm_version >= distutils.version.LooseVersion('2.7'):
  82. # 2.7
  83. env.Prepend(LIBS = [
  84. 'LLVMLinker', 'LLVMipo', 'LLVMInterpreter',
  85. 'LLVMInstrumentation', 'LLVMJIT', 'LLVMExecutionEngine',
  86. 'LLVMBitWriter', 'LLVMX86Disassembler', 'LLVMX86AsmParser',
  87. 'LLVMMCParser', 'LLVMX86AsmPrinter', 'LLVMX86CodeGen',
  88. 'LLVMSelectionDAG', 'LLVMX86Info', 'LLVMAsmPrinter',
  89. 'LLVMCodeGen', 'LLVMScalarOpts', 'LLVMInstCombine',
  90. 'LLVMTransformUtils', 'LLVMipa', 'LLVMAsmParser',
  91. 'LLVMArchive', 'LLVMBitReader', 'LLVMAnalysis', 'LLVMTarget',
  92. 'LLVMMC', 'LLVMCore', 'LLVMSupport', 'LLVMSystem',
  93. ])
  94. else:
  95. # 2.6
  96. env.Prepend(LIBS = [
  97. 'LLVMX86AsmParser', 'LLVMX86AsmPrinter', 'LLVMX86CodeGen',
  98. 'LLVMX86Info', 'LLVMLinker', 'LLVMipo', 'LLVMInterpreter',
  99. 'LLVMInstrumentation', 'LLVMJIT', 'LLVMExecutionEngine',
  100. 'LLVMDebugger', 'LLVMBitWriter', 'LLVMAsmParser',
  101. 'LLVMArchive', 'LLVMBitReader', 'LLVMSelectionDAG',
  102. 'LLVMAsmPrinter', 'LLVMCodeGen', 'LLVMScalarOpts',
  103. 'LLVMTransformUtils', 'LLVMipa', 'LLVMAnalysis',
  104. 'LLVMTarget', 'LLVMMC', 'LLVMCore', 'LLVMSupport',
  105. 'LLVMSystem',
  106. ])
  107. env.Append(LIBS = [
  108. 'imagehlp',
  109. 'psapi',
  110. ])
  111. if env['msvc']:
  112. # Some of the LLVM C headers use the inline keyword without
  113. # defining it.
  114. env.Append(CPPDEFINES = [('inline', '__inline')])
  115. if env['build'] in ('debug', 'checked'):
  116. # LLVM libraries are static, build with /MT, and they
  117. # automatically link agains LIBCMT. When we're doing a
  118. # debug build we'll be linking against LIBCMTD, so disable
  119. # that.
  120. env.Append(LINKFLAGS = ['/nodefaultlib:LIBCMT'])
  121. else:
  122. if not env.Detect('llvm-config'):
  123. print 'scons: llvm-config script not found' % llvm_version
  124. return
  125. llvm_version = env.backtick('llvm-config --version').rstrip()
  126. llvm_version = distutils.version.LooseVersion(llvm_version)
  127. try:
  128. env.ParseConfig('llvm-config --cppflags')
  129. env.ParseConfig('llvm-config --libs')
  130. env.ParseConfig('llvm-config --ldflags')
  131. except OSError:
  132. print 'scons: llvm-config version %s failed' % llvm_version
  133. return
  134. assert llvm_version is not None
  135. env['llvm'] = True
  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: