import os
import re
import lit.util
expr = re.compile(r"^(\\)?((\| )?)\W+b(\S+)\\b\W*$")
wordifier = re.compile(r"(\W*)(\b[^\b]+\b)")
class FindTool(object):
def __init__(self, name):
self.name = name
def resolve(self, config, dirs):
command = config.lit_config.params.get(self.name)
if command is None:
command = lit.util.which(self.name, dirs)
if not command:
return None
if self.name == 'llc' and os.environ.get('LLVM_ENABLE_MACHINE_VERIFIER') == '1':
command += ' -verify-machineinstrs'
elif self.name == 'llvm-go':
exe = getattr(config.config, 'go_executable', None)
if exe:
command += ' go=' + exe
return command
class ToolSubst(object):
def __init__(self, key, command=None, pre=r'.-^/\<', post='-.', verbatim=False,
unresolved='warn', extra_args=None):
self.unresolved = unresolved
self.extra_args = extra_args
self.key = key
self.command = command if command is not None else FindTool(key)
self.was_resolved = False
if verbatim:
self.regex = key
return
def not_in(chars, where=''):
if not chars:
return ''
pattern_str = '|'.join(re.escape(x) for x in chars)
return r'(?{}!({}))'.format(where, pattern_str)
def wordify(word):
match = wordifier.match(word)
introducer = match.group(1)
word = match.group(2)
return introducer + r'\b' + word + r'\b'
self.regex = not_in(pre, '<') + wordify(key) + not_in(post)
def resolve(self, config, search_dirs):
tool_match = expr.match(self.regex)
if not tool_match:
return None
tool_pipe = tool_match.group(2)
tool_name = tool_match.group(4)
if isinstance(self.command, FindTool):
command_str = self.command.resolve(config, search_dirs)
else:
command_str = str(self.command)
if command_str:
if self.extra_args:
command_str = ' '.join([command_str] + self.extra_args)
else:
if self.unresolved == 'warn':
config.lit_config.note(
'Did not find ' + tool_name + ' in %s' % search_dirs)
command_str = os.path.join(
config.config.llvm_tools_dir, tool_name)
elif self.unresolved == 'fatal':
config.lit_config.fatal(
'Did not find ' + tool_name + ' in %s' % search_dirs)
elif self.unresolved == 'break':
pass
elif self.unresolved == 'ignore':
return None
else:
raise 'Unexpected value for ToolSubst.unresolved'
if command_str:
self.was_resolved = True
return (self.regex, tool_pipe, command_str)