1# -*- Python -*- 2# Taken from utils/lit/tests in the LLVM tree and hacked together to support 3# our tests. 4# 5# Note: This configuration has simple commands to run Subzero's translator. 6# They have the form %X2i (i.e. %p2i, %l2i, and %lc2i) where X is defined 7# as follows: 8# 9# p : Run Subzero's translator, building ICE from PNaCl bitcode directly. 10# l : Run Subzero's translator, converting the .ll file to a PNaCl bitcode 11# file, reading in the bitcode file and generating LLVM IR, and 12# then convert LLVM IR to ICE IR. 13# lc : Run Subzero's translator, directly parsing the .ll file into LLVM IR, 14# and then convert it to ICE IR. 15# 16# These commands can be used in RUN lines by FileCheck. If the Subzero 17# build being tested lacks any required attributes (e.g., the ability 18# to parse .ll files), the command will simply return successfully, 19# generating no output. This allows translation tests to be able to 20# conditionally test the translator, based on the translator built. 21# 22# This conditional handling of translation introduces potential problems 23# when the output is piped to another command on a RUN line. Executables 24# like FileCheck expect non-empty input. 25# 26# To handle the problem that the pipe is conditional, any command that 27# doesn't accept empty input should be prefixed by a corresponding 28# %ifX (i.e. %p2i, %ifl, or %ifpc). Note: %p2i should always work, and 29# hence %ifp is not necessary (i.e. it is a nop). 30# 31# If you need to check other build attributes (other than the 32# existence of %l2i and %lc2i), you can use the %if command (which is 33# a short hand for using pydir/ifatts.py). 34 35import os 36import re 37import sys 38 39import lit.formats 40 41sys.path.insert(0, 'pydir') 42from utils import FindBaseNaCl, shellcmd 43 44# name: The name of this test suite. 45config.name = 'subzero' 46 47# testFormat: The test format to use to interpret tests. 48config.test_format = lit.formats.ShTest() 49 50# suffixes: A list of file extensions to treat as test files. 51config.suffixes = ['.ll', '.test'] 52 53# test_source_root: The root path where tests are located. 54config.test_source_root = os.path.dirname(__file__) 55config.test_exec_root = config.test_source_root 56config.target_triple = '(unused)' 57 58src_root = os.path.join(FindBaseNaCl(), 'toolchain_build/src/subzero') 59bin_root = src_root 60config.substitutions.append(('%{src_root}', src_root)) 61config.substitutions.append(('%{python}', sys.executable)) 62 63pydir = os.path.join(bin_root, 'pydir') 64 65# Finding PNaCl binary tools. Tools used in the tests must be listed in the 66# pnaclbintools list. 67pnaclbinpath = os.path.abspath(os.environ.get('PNACL_BIN_PATH')) 68 69# Define the location of the pnacl-sz tool. 70pnacl_sz_tool = os.path.join(bin_root, 'pnacl-sz') 71pnacl_sz_atts = shellcmd(' '.join([pnacl_sz_tool, '--build-atts']), 72 echo=False).split() 73 74# Add build attributes of pnacl-sz tool to the set of available features. 75config.available_features.update(pnacl_sz_atts) 76 77def if_cond_flag(Value): 78 return '--cond=true' if Value else '--cond=false' 79 80# shell conditional commands. 81if_atts = [os.path.join(pydir, 'if.py')] 82if_atts_cmd = if_atts + ['--have=' + att for att in pnacl_sz_atts] 83ifl2i_atts_cmd = if_atts + [if_cond_flag('allow_llvm_ir' in pnacl_sz_atts), 84 '--command'] 85iflc2i_atts_cmd = if_atts + [if_cond_flag('allow_llvm_ir_as_input' 86 in pnacl_sz_atts), '--command'] 87 88# Base command for running pnacl-sz 89pnacl_sz_cmd = [os.path.join(pydir, 'run-pnacl-sz.py'), 90 '--echo-cmd', 91 '--pnacl-sz', pnacl_sz_tool, 92 '--pnacl-bin-path', pnaclbinpath] 93if 'FORCEASM' in lit_config.params: 94 pnacl_sz_cmd += ['--forceasm'] 95 96# Run commands only if corresponding build attributes apply, including 97# for each compiler setup. 98config.substitutions.append(('%ifp', ' ')) 99config.substitutions.append(('%iflc', ' '.join(iflc2i_atts_cmd))) 100config.substitutions.append(('%ifl', ' '.join(ifl2i_atts_cmd))) 101config.substitutions.append(('%if', ' '.join(if_atts_cmd))) 102 103# Translate LLVM source for each compiler setup. 104config.substitutions.append(('%p2i', ' '.join(pnacl_sz_cmd))) 105config.substitutions.append(('%l2i', ' '.join(ifl2i_atts_cmd + pnacl_sz_cmd 106 + ['--llvm']))) 107config.substitutions.append(('%lc2i', ' '.join(iflc2i_atts_cmd + pnacl_sz_cmd 108 + ['--llvm-source']))) 109 110config.substitutions.append(('%pnacl_sz', pnacl_sz_tool)) 111 112pnaclbintools = [r'\b' + x + r'\b' for x in 113 ['FileCheck', 114 'llvm-as', 115 'llvm-mc', 116 'llvm-readobj', 117 'not', 118 'pnacl-bcdis', 119 'pnacl-bcfuzz', 120 'pnacl-freeze']] 121 122for tool in pnaclbintools: 123 # The re.sub() line is adapted from one of LLVM's lit.cfg files. 124 # Extract the tool name from the pattern. This relies on the tool 125 # name being surrounded by \b word match operators. If the 126 # pattern starts with "| ", include it in the string to be 127 # substituted. 128 substitution = re.sub(r"^(\\)?((\| )?)\W+b([0-9A-Za-z-_]+)\\b\W*$", 129 r"\2" + pnaclbinpath + "/" + r"\4", 130 tool) 131 config.substitutions.append((tool, substitution)) 132 133# Add a feature to detect the Python version. 134config.available_features.add("python%d.%d" % (sys.version_info[0], 135 sys.version_info[1])) 136 137# Debugging output 138def dbg(s): 139 print '[DBG] %s' % s 140 141dbg('bin_root = %s' % bin_root) 142dbg('pnaclbinpath = %s' % pnaclbinpath) 143dbg("Build attributes = %s" % pnacl_sz_atts) 144