Karl Schimpf | b262c5e | 2014-10-27 14:41:57 -0700 | [diff] [blame] | 1 | # -*- Python -*- |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 2 | # Taken from utils/lit/tests in the LLVM tree and hacked together to support |
| 3 | # our tests. |
Karl Schimpf | b262c5e | 2014-10-27 14:41:57 -0700 | [diff] [blame] | 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). |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 34 | |
| 35 | import os |
Jim Stichnoth | cc27a53 | 2014-06-26 13:32:27 -0700 | [diff] [blame] | 36 | import re |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 37 | import sys |
| 38 | |
| 39 | import lit.formats |
| 40 | |
Jim Stichnoth | 7076c88 | 2014-09-08 12:57:52 -0700 | [diff] [blame] | 41 | sys.path.insert(0, 'pydir') |
Karl Schimpf | 6af6336 | 2014-10-29 14:55:00 -0700 | [diff] [blame] | 42 | from utils import FindBaseNaCl, shellcmd |
Jim Stichnoth | 7076c88 | 2014-09-08 12:57:52 -0700 | [diff] [blame] | 43 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 44 | # name: The name of this test suite. |
| 45 | config.name = 'subzero' |
| 46 | |
| 47 | # testFormat: The test format to use to interpret tests. |
| 48 | config.test_format = lit.formats.ShTest() |
| 49 | |
| 50 | # suffixes: A list of file extensions to treat as test files. |
| 51 | config.suffixes = ['.ll'] |
| 52 | |
| 53 | # test_source_root: The root path where tests are located. |
| 54 | config.test_source_root = os.path.dirname(__file__) |
| 55 | config.test_exec_root = config.test_source_root |
| 56 | config.target_triple = '(unused)' |
| 57 | |
Jim Stichnoth | 7076c88 | 2014-09-08 12:57:52 -0700 | [diff] [blame] | 58 | src_root = os.path.join(FindBaseNaCl(), 'toolchain_build/src/subzero') |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 59 | bin_root = src_root |
| 60 | config.substitutions.append(('%{src_root}', src_root)) |
| 61 | config.substitutions.append(('%{python}', sys.executable)) |
| 62 | |
Jim Stichnoth | 7076c88 | 2014-09-08 12:57:52 -0700 | [diff] [blame] | 63 | pydir = os.path.join(bin_root, 'pydir') |
| 64 | |
Jim Stichnoth | 0a9e126 | 2015-04-21 09:59:21 -0700 | [diff] [blame] | 65 | # Finding PNaCl binary tools. Tools used in the tests must be listed in the |
| 66 | # pnaclbintools list. |
| 67 | pnaclbinpath = os.path.abspath(os.environ.get('PNACL_BIN_PATH')) |
Jan Voung | a2703ae | 2015-02-19 11:27:44 -0800 | [diff] [blame] | 68 | |
Jim Stichnoth | fa0cfa5 | 2015-02-26 09:42:36 -0800 | [diff] [blame] | 69 | # Define the location of the pnacl-sz tool. |
| 70 | pnacl_sz_tool = os.path.join(bin_root, 'pnacl-sz') |
| 71 | pnacl_sz_atts = shellcmd(' '.join([pnacl_sz_tool, '--build-atts']), |
Karl Schimpf | 6af6336 | 2014-10-29 14:55:00 -0700 | [diff] [blame] | 72 | echo=False).split() |
Karl Schimpf | 2a5324a | 2014-09-25 09:37:49 -0700 | [diff] [blame] | 73 | |
Jim Stichnoth | fa0cfa5 | 2015-02-26 09:42:36 -0800 | [diff] [blame] | 74 | # Add build attributes of pnacl-sz tool to the set of available features. |
| 75 | config.available_features.update(pnacl_sz_atts) |
Karl Schimpf | 2a5324a | 2014-09-25 09:37:49 -0700 | [diff] [blame] | 76 | |
Karl Schimpf | 6af6336 | 2014-10-29 14:55:00 -0700 | [diff] [blame] | 77 | def if_cond_flag(Value): |
| 78 | return '--cond=true' if Value else '--cond=false' |
| 79 | |
| 80 | # shell conditional commands. |
| 81 | if_atts = [os.path.join(pydir, 'if.py')] |
Jim Stichnoth | fa0cfa5 | 2015-02-26 09:42:36 -0800 | [diff] [blame] | 82 | if_atts_cmd = if_atts + ['--have=' + att for att in pnacl_sz_atts] |
| 83 | ifl2i_atts_cmd = if_atts + [if_cond_flag('allow_llvm_ir' in pnacl_sz_atts), |
Karl Schimpf | 6af6336 | 2014-10-29 14:55:00 -0700 | [diff] [blame] | 84 | '--command'] |
| 85 | iflc2i_atts_cmd = if_atts + [if_cond_flag('allow_llvm_ir_as_input' |
Jim Stichnoth | fa0cfa5 | 2015-02-26 09:42:36 -0800 | [diff] [blame] | 86 | in pnacl_sz_atts), '--command'] |
Karl Schimpf | 2a5324a | 2014-09-25 09:37:49 -0700 | [diff] [blame] | 87 | |
Jim Stichnoth | fa0cfa5 | 2015-02-26 09:42:36 -0800 | [diff] [blame] | 88 | # Base command for running pnacl-sz |
| 89 | pnacl_sz_cmd = [os.path.join(pydir, 'run-pnacl-sz.py'), |
| 90 | '--pnacl-sz', pnacl_sz_tool, |
Jim Stichnoth | 0a9e126 | 2015-04-21 09:59:21 -0700 | [diff] [blame] | 91 | '--pnacl-bin-path', pnaclbinpath] |
Karl Schimpf | b262c5e | 2014-10-27 14:41:57 -0700 | [diff] [blame] | 92 | |
| 93 | # Run commands only if corresponding build attributes apply, including |
| 94 | # for each compiler setup. |
| 95 | config.substitutions.append(('%ifp', ' ')) |
| 96 | config.substitutions.append(('%iflc', ' '.join(iflc2i_atts_cmd))) |
| 97 | config.substitutions.append(('%ifl', ' '.join(ifl2i_atts_cmd))) |
| 98 | config.substitutions.append(('%if', ' '.join(if_atts_cmd))) |
| 99 | |
| 100 | # Translate LLVM source for each compiler setup. |
Jim Stichnoth | fa0cfa5 | 2015-02-26 09:42:36 -0800 | [diff] [blame] | 101 | config.substitutions.append(('%p2i', ' '.join(pnacl_sz_cmd))) |
| 102 | config.substitutions.append(('%l2i', ' '.join(ifl2i_atts_cmd + pnacl_sz_cmd |
Karl Schimpf | b262c5e | 2014-10-27 14:41:57 -0700 | [diff] [blame] | 103 | + ['--llvm']))) |
Jim Stichnoth | fa0cfa5 | 2015-02-26 09:42:36 -0800 | [diff] [blame] | 104 | config.substitutions.append(('%lc2i', ' '.join(iflc2i_atts_cmd + pnacl_sz_cmd |
Karl Schimpf | b262c5e | 2014-10-27 14:41:57 -0700 | [diff] [blame] | 105 | + ['--llvm-source']))) |
Karl Schimpf | 2a5324a | 2014-09-25 09:37:49 -0700 | [diff] [blame] | 106 | |
Jim Stichnoth | fa0cfa5 | 2015-02-26 09:42:36 -0800 | [diff] [blame] | 107 | config.substitutions.append(('%pnacl_sz', pnacl_sz_tool)) |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 108 | |
Jim Stichnoth | 0a9e126 | 2015-04-21 09:59:21 -0700 | [diff] [blame] | 109 | pnaclbintools = [r"\bFileCheck\b", |
| 110 | r"\ble32-nacl-objdump\b", |
| 111 | r"\bllvm-as\b", |
| 112 | r"\bllvm-mc\b", |
| 113 | r"\bllvm-readobj\b", |
| 114 | r"\bnot\b", |
| 115 | r"\bpnacl-freeze\b", |
| 116 | r"\bpnacl-bcdis\b"] |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 117 | |
Jim Stichnoth | 0a9e126 | 2015-04-21 09:59:21 -0700 | [diff] [blame] | 118 | for tool in pnaclbintools: |
Jim Stichnoth | cc27a53 | 2014-06-26 13:32:27 -0700 | [diff] [blame] | 119 | # The re.sub() line is adapted from one of LLVM's lit.cfg files. |
| 120 | # Extract the tool name from the pattern. This relies on the tool |
| 121 | # name being surrounded by \b word match operators. If the |
| 122 | # pattern starts with "| ", include it in the string to be |
| 123 | # substituted. |
| 124 | substitution = re.sub(r"^(\\)?((\| )?)\W+b([0-9A-Za-z-_]+)\\b\W*$", |
Jim Stichnoth | 0a9e126 | 2015-04-21 09:59:21 -0700 | [diff] [blame] | 125 | r"\2" + pnaclbinpath + "/" + r"\4", |
Jan Voung | a2703ae | 2015-02-19 11:27:44 -0800 | [diff] [blame] | 126 | tool) |
| 127 | config.substitutions.append((tool, substitution)) |
| 128 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 129 | # Add a feature to detect the Python version. |
| 130 | config.available_features.add("python%d.%d" % (sys.version_info[0], |
| 131 | sys.version_info[1])) |
| 132 | |
| 133 | # Debugging output |
| 134 | def dbg(s): |
| 135 | print '[DBG] %s' % s |
| 136 | |
| 137 | dbg('bin_root = %s' % bin_root) |
Jim Stichnoth | 0a9e126 | 2015-04-21 09:59:21 -0700 | [diff] [blame] | 138 | dbg('pnaclbinpath = %s' % pnaclbinpath) |
Jim Stichnoth | fa0cfa5 | 2015-02-26 09:42:36 -0800 | [diff] [blame] | 139 | dbg("Build attributes = %s" % pnacl_sz_atts) |