xenocara/lib/mesa/scons/llvm.py

324 lines
14 KiB
Python
Raw Normal View History

2015-11-21 19:36:47 -07:00
"""llvm
Tool-specific initialization for LLVM
"""
#
# Copyright (c) 2009 VMware, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
import os
import os.path
import re
2020-01-21 19:49:17 -07:00
import platform as host_platform
2015-11-21 19:36:47 -07:00
import sys
import distutils.version
import SCons.Errors
import SCons.Util
2020-08-26 00:02:31 -06:00
required_llvm_version = '3.9'
2015-11-21 19:36:47 -07:00
def generate(env):
env['llvm'] = False
try:
llvm_dir = os.environ['LLVM']
except KeyError:
# Do nothing -- use the system headers/libs
llvm_dir = None
else:
if not os.path.isdir(llvm_dir):
raise SCons.Errors.InternalError("Specified LLVM directory not found")
2015-11-21 19:36:47 -07:00
if env['debug']:
llvm_subdir = 'Debug'
else:
llvm_subdir = 'Release'
llvm_bin_dir = os.path.join(llvm_dir, llvm_subdir, 'bin')
if not os.path.isdir(llvm_bin_dir):
llvm_bin_dir = os.path.join(llvm_dir, 'bin')
if not os.path.isdir(llvm_bin_dir):
raise SCons.Errors.InternalError("LLVM binary directory not found")
2015-11-21 19:36:47 -07:00
env.PrependENVPath('PATH', llvm_bin_dir)
if env['platform'] == 'windows':
# XXX: There is no llvm-config on Windows, so assume a standard layout
if llvm_dir is None:
print('scons: LLVM environment variable must be specified when building for windows')
2015-11-21 19:36:47 -07:00
return
# Try to determine the LLVM version from llvm/Config/config.h
llvm_config = os.path.join(llvm_dir, 'include/llvm/Config/llvm-config.h')
if not os.path.exists(llvm_config):
print('scons: could not find %s' % llvm_config)
2015-11-21 19:36:47 -07:00
return
llvm_version_major_re = re.compile(r'^#define LLVM_VERSION_MAJOR ([0-9]+)')
llvm_version_minor_re = re.compile(r'^#define LLVM_VERSION_MINOR ([0-9]+)')
llvm_version = None
llvm_version_major = None
llvm_version_minor = None
for line in open(llvm_config, 'rt'):
mo = llvm_version_major_re.match(line)
if mo:
llvm_version_major = mo.group(1)
mo = llvm_version_minor_re.match(line)
if mo:
llvm_version_minor = mo.group(1)
if llvm_version_major is not None and llvm_version_minor is not None:
llvm_version = distutils.version.LooseVersion('%s.%s' % (llvm_version_major, llvm_version_minor))
if llvm_version is None:
print('scons: could not determine the LLVM version from %s' % llvm_config)
2015-11-21 19:36:47 -07:00
return
if llvm_version < distutils.version.LooseVersion(required_llvm_version):
print('scons: LLVM version %s found, but %s is required' % (llvm_version, required_llvm_version))
2015-11-21 19:36:47 -07:00
return
env.Prepend(CPPPATH = [os.path.join(llvm_dir, 'include')])
env.Prepend(LIBPATH = [os.path.join(llvm_dir, 'lib')])
2020-01-21 19:49:17 -07:00
# LLVM 5.0 and newer requires MinGW w/ pthreads due to use of std::thread and friends.
if llvm_version >= distutils.version.LooseVersion('5.0') and env['crosscompile']:
assert env['gcc']
env.AppendUnique(CXXFLAGS = ['-posix'])
# LIBS should match the output of `llvm-config --libs engine mcjit bitwriter x86asmprinter irreader` for LLVM<=7.0
2020-08-26 00:02:31 -06:00
# and `llvm-config --libs engine coroutines` for LLVM>=8.0
# LLVMAggressiveInstCombine library part of engine component since LLVM 6 is only needed by Mesa3D for LLVM>=8.
# While not directly needed by Mesa3D, this library is needed by LLVMipo which is part of coroutines component.
if llvm_version >= distutils.version.LooseVersion('10.0'):
2020-01-21 19:49:17 -07:00
env.Prepend(LIBS = [
'LLVMX86Disassembler', 'LLVMX86AsmParser',
'LLVMX86CodeGen', 'LLVMSelectionDAG', 'LLVMAsmPrinter',
'LLVMDebugInfoCodeView', 'LLVMCodeGen',
'LLVMScalarOpts', 'LLVMInstCombine',
'LLVMTransformUtils',
'LLVMBitWriter', 'LLVMX86Desc',
'LLVMMCDisassembler', 'LLVMX86Info',
'LLVMX86Utils',
'LLVMMCJIT', 'LLVMExecutionEngine', 'LLVMTarget',
'LLVMAnalysis', 'LLVMProfileData',
'LLVMRuntimeDyld', 'LLVMObject', 'LLVMMCParser',
'LLVMBitReader', 'LLVMMC', 'LLVMCore',
'LLVMSupport',
'LLVMIRReader', 'LLVMAsmParser',
'LLVMDemangle', 'LLVMGlobalISel', 'LLVMDebugInfoMSF',
'LLVMBinaryFormat',
'LLVMRemarks', 'LLVMBitstreamReader', 'LLVMDebugInfoDWARF',
2020-08-26 00:02:31 -06:00
'LLVMAggressiveInstCombine','LLVMLinker', 'LLVMVectorize',
'LLVMInstrumentation', 'LLVMipo', 'LLVMCoroutines',
'LLVMCFGuard', 'LLVMTextAPI',
2020-01-21 19:49:17 -07:00
])
2020-08-26 00:02:31 -06:00
elif llvm_version >= distutils.version.LooseVersion('9.0'):
env.Prepend(LIBS = [
'LLVMX86Disassembler', 'LLVMX86AsmParser',
'LLVMX86CodeGen', 'LLVMSelectionDAG', 'LLVMAsmPrinter',
'LLVMDebugInfoCodeView', 'LLVMCodeGen',
'LLVMScalarOpts', 'LLVMInstCombine',
'LLVMTransformUtils',
'LLVMBitWriter', 'LLVMX86Desc',
'LLVMMCDisassembler', 'LLVMX86Info',
2020-08-26 00:02:31 -06:00
'LLVMX86Utils',
'LLVMMCJIT', 'LLVMExecutionEngine', 'LLVMTarget',
'LLVMAnalysis', 'LLVMProfileData',
'LLVMRuntimeDyld', 'LLVMObject', 'LLVMMCParser',
'LLVMBitReader', 'LLVMMC', 'LLVMCore',
'LLVMSupport',
'LLVMIRReader', 'LLVMAsmParser',
'LLVMDemangle', 'LLVMGlobalISel', 'LLVMDebugInfoMSF',
'LLVMBinaryFormat',
2020-08-26 00:02:31 -06:00
'LLVMRemarks', 'LLVMBitstreamReader', 'LLVMDebugInfoDWARF',
# Add these libraries to enable ompute shaders support.
'LLVMAggressiveInstCombine','LLVMLinker', 'LLVMVectorize',
'LLVMInstrumentation', 'LLVMipo', 'LLVMCoroutines',
])
2020-08-26 00:02:31 -06:00
elif llvm_version >= distutils.version.LooseVersion('8.0'):
env.Prepend(LIBS = [
'LLVMX86Disassembler', 'LLVMX86AsmParser',
'LLVMX86CodeGen', 'LLVMSelectionDAG', 'LLVMAsmPrinter',
'LLVMDebugInfoCodeView', 'LLVMCodeGen',
'LLVMScalarOpts', 'LLVMInstCombine',
'LLVMTransformUtils',
'LLVMBitWriter', 'LLVMX86Desc',
'LLVMMCDisassembler', 'LLVMX86Info',
'LLVMX86AsmPrinter', 'LLVMX86Utils',
'LLVMMCJIT', 'LLVMExecutionEngine', 'LLVMTarget',
'LLVMAnalysis', 'LLVMProfileData',
'LLVMRuntimeDyld', 'LLVMObject', 'LLVMMCParser',
'LLVMBitReader', 'LLVMMC', 'LLVMCore',
'LLVMSupport',
'LLVMIRReader', 'LLVMAsmParser',
'LLVMDemangle', 'LLVMGlobalISel', 'LLVMDebugInfoMSF',
2020-08-26 00:02:31 -06:00
'LLVMBinaryFormat',
# Add these libraries to enable ompute shaders support.
'LLVMAggressiveInstCombine', 'LLVMLinker', 'LLVMVectorize',
'LLVMInstrumentation', 'LLVMipo', 'LLVMCoroutines',
])
2020-08-26 00:02:31 -06:00
elif llvm_version >= distutils.version.LooseVersion('5.0'):
env.Prepend(LIBS = [
'LLVMX86Disassembler', 'LLVMX86AsmParser',
'LLVMX86CodeGen', 'LLVMSelectionDAG', 'LLVMAsmPrinter',
'LLVMDebugInfoCodeView', 'LLVMCodeGen',
'LLVMScalarOpts', 'LLVMInstCombine',
2020-08-26 00:02:31 -06:00
'LLVMTransformUtils',
'LLVMBitWriter', 'LLVMX86Desc',
'LLVMMCDisassembler', 'LLVMX86Info',
'LLVMX86AsmPrinter', 'LLVMX86Utils',
'LLVMMCJIT', 'LLVMExecutionEngine', 'LLVMTarget',
'LLVMAnalysis', 'LLVMProfileData',
'LLVMRuntimeDyld', 'LLVMObject', 'LLVMMCParser',
'LLVMBitReader', 'LLVMMC', 'LLVMCore',
'LLVMSupport',
2020-08-26 00:02:31 -06:00
'LLVMIRReader', 'LLVMAsmParser',
'LLVMDemangle', 'LLVMGlobalISel', 'LLVMDebugInfoMSF',
'LLVMBinaryFormat',
])
2020-08-26 00:02:31 -06:00
elif llvm_version >= distutils.version.LooseVersion('4.0'):
2016-05-29 04:11:54 -06:00
env.Prepend(LIBS = [
2020-08-26 00:02:31 -06:00
'LLVMX86Disassembler', 'LLVMX86AsmParser',
2016-05-29 04:11:54 -06:00
'LLVMX86CodeGen', 'LLVMSelectionDAG', 'LLVMAsmPrinter',
2020-08-26 00:02:31 -06:00
'LLVMDebugInfoCodeView', 'LLVMCodeGen',
'LLVMScalarOpts', 'LLVMInstCombine',
'LLVMTransformUtils',
'LLVMBitWriter', 'LLVMX86Desc',
'LLVMMCDisassembler', 'LLVMX86Info',
'LLVMX86AsmPrinter', 'LLVMX86Utils',
'LLVMMCJIT', 'LLVMExecutionEngine', 'LLVMTarget',
'LLVMAnalysis', 'LLVMProfileData',
2016-05-29 04:11:54 -06:00
'LLVMRuntimeDyld', 'LLVMObject', 'LLVMMCParser',
2020-08-26 00:02:31 -06:00
'LLVMBitReader', 'LLVMMC', 'LLVMCore',
'LLVMSupport',
'LLVMIRReader', 'LLVMAsmParser',
'LLVMDemangle', 'LLVMGlobalISel', 'LLVMDebugInfoMSF',
2016-05-29 04:11:54 -06:00
])
2020-08-26 00:02:31 -06:00
else:
2015-11-21 19:36:47 -07:00
env.Prepend(LIBS = [
2020-08-26 00:02:31 -06:00
'LLVMX86Disassembler', 'LLVMX86AsmParser',
2015-11-21 19:36:47 -07:00
'LLVMX86CodeGen', 'LLVMSelectionDAG', 'LLVMAsmPrinter',
2020-08-26 00:02:31 -06:00
'LLVMDebugInfoCodeView', 'LLVMCodeGen',
'LLVMScalarOpts', 'LLVMInstCombine',
'LLVMInstrumentation', 'LLVMTransformUtils',
'LLVMBitWriter', 'LLVMX86Desc',
'LLVMMCDisassembler', 'LLVMX86Info',
'LLVMX86AsmPrinter', 'LLVMX86Utils',
'LLVMMCJIT', 'LLVMExecutionEngine', 'LLVMTarget',
'LLVMAnalysis', 'LLVMProfileData',
2015-11-21 19:36:47 -07:00
'LLVMRuntimeDyld', 'LLVMObject', 'LLVMMCParser',
2020-08-26 00:02:31 -06:00
'LLVMBitReader', 'LLVMMC', 'LLVMCore',
'LLVMSupport',
'LLVMIRReader', 'LLVMASMParser'
2015-11-21 19:36:47 -07:00
])
env.Append(LIBS = [
'imagehlp',
'psapi',
'shell32',
2019-01-29 04:52:04 -07:00
'advapi32',
'ole32',
'uuid',
2015-11-21 19:36:47 -07:00
])
2019-01-29 04:52:04 -07:00
2020-01-21 19:49:17 -07:00
# Mingw-w64 zlib is required when building with LLVM support in MSYS2 environment
if host_platform.system().lower().startswith('mingw'):
env.Append(LIBS = [
'z',
])
2015-11-21 19:36:47 -07:00
if env['msvc']:
# Some of the LLVM C headers use the inline keyword without
# defining it.
env.Append(CPPDEFINES = [('inline', '__inline')])
# Match some of the warning options from llvm/cmake/modules/HandleLLVMOptions.cmake
env.AppendUnique(CXXFLAGS = [
'/wd4355', # 'this' : used in base member initializer list
'/wd4624', # 'derived class' : destructor could not be generated because a base class destructor is inaccessible
])
if env['build'] in ('debug', 'checked'):
# LLVM libraries are static, build with /MT, and they
# automatically link agains LIBCMT. When we're doing a
# debug build we'll be linking against LIBCMTD, so disable
# that.
env.Append(LINKFLAGS = ['/nodefaultlib:LIBCMT'])
else:
llvm_config = os.environ.get('LLVM_CONFIG', 'llvm-config')
if not env.Detect(llvm_config):
print('scons: %s script not found' % llvm_config)
2015-11-21 19:36:47 -07:00
return
llvm_version = env.backtick('%s --version' % llvm_config).rstrip()
2015-11-21 19:36:47 -07:00
llvm_version = distutils.version.LooseVersion(llvm_version)
if llvm_version < distutils.version.LooseVersion(required_llvm_version):
print('scons: LLVM version %s found, but %s is required' % (llvm_version, required_llvm_version))
2015-11-21 19:36:47 -07:00
return
try:
# Treat --cppflags specially to prevent NDEBUG from disabling
# assertion failures in debug builds.
cppflags = env.ParseFlags('!%s --cppflags' % llvm_config)
2015-11-21 19:36:47 -07:00
try:
cppflags['CPPDEFINES'].remove('NDEBUG')
except ValueError:
pass
env.MergeFlags(cppflags)
# Match llvm --fno-rtti flag
cxxflags = env.backtick('%s --cxxflags' % llvm_config).split()
2015-11-21 19:36:47 -07:00
if '-fno-rtti' in cxxflags:
env.Append(CXXFLAGS = ['-fno-rtti'])
2020-01-21 19:49:17 -07:00
if llvm_version < distutils.version.LooseVersion('9.0'):
components = ['engine', 'mcjit', 'bitwriter', 'x86asmprinter', 'mcdisassembler', 'irreader']
else:
components = ['engine', 'mcjit', 'bitwriter', 'mcdisassembler', 'irreader']
2015-11-21 19:36:47 -07:00
2020-08-26 00:02:31 -06:00
if llvm_version >= distutils.version.LooseVersion('8.0'):
components.append('coroutines')
env.ParseConfig('%s --libs ' % llvm_config + ' '.join(components))
env.ParseConfig('%s --ldflags' % llvm_config)
2020-08-26 00:02:31 -06:00
env.ParseConfig('%s --system-libs' % llvm_config)
env.Append(CXXFLAGS = ['-std=c++14'])
2015-11-21 19:36:47 -07:00
except OSError:
print('scons: llvm-config version %s failed' % llvm_version)
2015-11-21 19:36:47 -07:00
return
assert llvm_version is not None
env['llvm'] = True
print('scons: Found LLVM version %s' % llvm_version)
2015-11-21 19:36:47 -07:00
env['LLVM_VERSION'] = llvm_version
2020-08-26 00:02:31 -06:00
# Define LLVM_AVAILABLE macro to guard code blocks, and MESA_LLVM_VERSION_STRING
env.Prepend(CPPDEFINES = [('LLVM_AVAILABLE', 1)])
env.Prepend(CPPDEFINES = [('MESA_LLVM_VERSION_STRING=\\"%s\\"' % llvm_version)])
2015-11-21 19:36:47 -07:00
def exists(env):
return True
# vim:set ts=4 sw=4 et: