Subzero: Remove the need for a separate NaCl SDK installation.

Now it assumes tests are being run from within the Subzero portion of the native_client tree, and sets up PATH to find PNaCl tools relative to there.

It may be necessary to do a one-time setup to be able to build pexes:

    pnacl/build.sh sdk newlib

or the equivalent scons commands.

If the tool chain is updated, propagate the changes via:

    toolchain_build/toolchain_build_pnacl.py llvm_i686_linux --install=toolchain/linux_x86/pnacl_newlib

BUG= none
R=jvoung@chromium.org

Review URL: https://codereview.chromium.org/525603002
diff --git a/Makefile.standalone b/Makefile.standalone
index 7740a40..bac279a 100644
--- a/Makefile.standalone
+++ b/Makefile.standalone
@@ -90,7 +90,7 @@
 	$(LLVM_SRC_PATH)/utils/lit/lit.py -sv tests_lit
 
 check: check-lit
-	(cd crosstest; LLVM_BIN_PATH=$(LLVM_BIN_PATH) ./runtests.sh)
+	(cd crosstest; ./runtests.sh)
 
 # TODO: Fix the use of wildcards.
 # Assumes clang-format is within $PATH.
diff --git a/crosstest/crosstest.py b/crosstest/crosstest.py
index 89124d6..432cb9d 100755
--- a/crosstest/crosstest.py
+++ b/crosstest/crosstest.py
@@ -9,6 +9,7 @@
 
 sys.path.insert(0, '../pydir')
 from utils import shellcmd
+from utils import FindBaseNaCl
 
 if __name__ == '__main__':
     """Builds a cross-test binary that allows functions translated by
@@ -55,11 +56,6 @@
     argparser.add_argument('--dir', required=False, default='.',
                            metavar='OUTPUT_DIR',
                            help='Output directory for all files')
-    argparser.add_argument('--llvm-bin-path', required=False,
-                           default=os.environ.get('LLVM_BIN_PATH'),
-                           metavar='PATH',
-                           help='Path to LLVM executables like llc ' +
-                                '(defaults to $LLVM_BIN_PATH)')
     argparser.add_argument('--crosstest-bitcode', required=False,
                            default=1, type=int,
                            help='Compile non-subzero crosstest object file ' +
@@ -67,10 +63,15 @@
                            'If 0, then compile it straight from source.')
     args = argparser.parse_args()
 
+    nacl_root = FindBaseNaCl()
+    # Prepend host_x86_32/bin to $PATH.
+    os.environ['PATH'] = nacl_root + \
+        '/toolchain/linux_x86/pnacl_newlib/host_x86_32/bin' + \
+        os.pathsep + os.environ['PATH']
+
     objs = []
     remove_internal = re.compile('^define internal ')
     fix_target = re.compile('le32-unknown-nacl')
-    llvm_bin_path = args.llvm_bin_path
     for arg in args.test:
         base, ext = os.path.splitext(arg)
         if ext == '.ll':
@@ -101,7 +102,7 @@
                   '--prefix=' + args.prefix,
                   '-o=' + asm_sz,
                   bitcode])
-        shellcmd([os.path.join(llvm_bin_path, 'llvm-mc'),
+        shellcmd(['llvm-mc',
                   '-arch=' + arch_map[args.target],
                   '-x86-asm-syntax=intel',
                   '-filetype=obj',
@@ -125,7 +126,7 @@
         if not args.crosstest_bitcode:
             objs.append(arg)
         elif use_llc:
-            shellcmd([os.path.join(llvm_bin_path, 'llc'),
+            shellcmd(['llc'
                       '-filetype=obj',
                       '-o=' + obj_llc,
                       bitcode])
@@ -134,6 +135,6 @@
             objs.append(bitcode)
 
     linker = 'clang' if os.path.splitext(args.driver)[1] == '.c' else 'clang++'
-    shellcmd([os.path.join(llvm_bin_path, linker), '-g', '-m32', args.driver] +
+    shellcmd([linker, '-g', '-m32', args.driver] +
              objs +
              ['-lm', '-lpthread', '-o', os.path.join(args.dir, args.output)])
diff --git a/crosstest/runtests.sh b/crosstest/runtests.sh
index eedb3b3..cc353ab 100755
--- a/crosstest/runtests.sh
+++ b/crosstest/runtests.sh
@@ -19,7 +19,6 @@
             --prefix=Subzero_ \
             --target=x8632 \
             --dir="${OUTDIR}" \
-            --llvm-bin-path="${LLVM_BIN_PATH}" \
             --test=simple_loop.c \
             --driver=simple_loop_main.c \
             --output=simple_loop_O${optlevel}_${attribute}
@@ -28,7 +27,6 @@
             --prefix=Subzero_ \
             --target=x8632 \
             --dir="${OUTDIR}" \
-            --llvm-bin-path="${LLVM_BIN_PATH}" \
             --test=mem_intrin.cpp \
             --driver=mem_intrin_main.cpp \
             --output=mem_intrin_O${optlevel}_${attribute}
@@ -37,7 +35,6 @@
             --prefix=Subzero_ \
             --target=x8632 \
             --dir="${OUTDIR}" \
-            --llvm-bin-path="${LLVM_BIN_PATH}" \
             --test=test_arith.cpp \
             --test=test_arith_frem.ll \
             --test=test_arith_sqrt.ll \
@@ -48,7 +45,6 @@
             --prefix=Subzero_ \
             --target=x8632 \
             --dir="${OUTDIR}" \
-            --llvm-bin-path="${LLVM_BIN_PATH}" \
             --test=test_bitmanip.cpp --test=test_bitmanip_intrin.ll \
             --driver=test_bitmanip_main.cpp \
             --output=test_bitmanip_O${optlevel}_${attribute}
@@ -56,7 +52,6 @@
         ./crosstest.py -O${optlevel} --mattr ${attribute} \
             --prefix=Subzero_ --target=x8632 \
             --dir="${OUTDIR}" \
-            --llvm-bin-path="${LLVM_BIN_PATH}" \
             --test=test_calling_conv.cpp \
             --driver=test_calling_conv_main.cpp \
             --output=test_calling_conv_O${optlevel}_${attribute}
@@ -65,7 +60,6 @@
             --prefix=Subzero_ \
             --target=x8632 \
             --dir="${OUTDIR}" \
-            --llvm-bin-path="${LLVM_BIN_PATH}" \
             --test=test_cast.cpp --test=test_cast_to_u1.ll \
             --driver=test_cast_main.cpp \
             --output=test_cast_O${optlevel}_${attribute}
@@ -74,7 +68,6 @@
             --prefix=Subzero_ \
             --target=x8632 \
             --dir="${OUTDIR}" \
-            --llvm-bin-path="${LLVM_BIN_PATH}" \
             --test=test_fcmp.pnacl.ll \
             --driver=test_fcmp_main.cpp \
             --output=test_fcmp_O${optlevel}_${attribute}
@@ -83,7 +76,6 @@
             --prefix=Subzero_ \
             --target=x8632 \
             --dir="${OUTDIR}" \
-            --llvm-bin-path="${LLVM_BIN_PATH}" \
             --test=test_global.cpp \
             --driver=test_global_main.cpp \
             --output=test_global_O${optlevel}_${attribute}
@@ -92,7 +84,6 @@
             --prefix=Subzero_ \
             --target=x8632 \
             --dir="${OUTDIR}" \
-            --llvm-bin-path="${LLVM_BIN_PATH}" \
             --test=test_icmp.cpp --test=test_icmp_i1vec.ll \
             --driver=test_icmp_main.cpp \
             --output=test_icmp_O${optlevel}_${attribute}
@@ -101,7 +92,6 @@
             --prefix=Subzero_ \
             --target=x8632 \
             --dir="${OUTDIR}" \
-            --llvm-bin-path="${LLVM_BIN_PATH}" \
             --test=test_select.ll \
             --driver=test_select_main.cpp \
             --output=test_select_O${optlevel}_${attribute}
@@ -110,7 +100,6 @@
             --prefix=Subzero_ \
             --target=x8632 \
             --dir="${OUTDIR}" \
-            --llvm-bin-path="${LLVM_BIN_PATH}" \
             --test=test_stacksave.c \
             --driver=test_stacksave_main.c \
             --output=test_stacksave_O${optlevel}_${attribute}
@@ -122,7 +111,6 @@
             --prefix=Subzero_ \
             --target=x8632 \
             --dir="${OUTDIR}" \
-            --llvm-bin-path="${LLVM_BIN_PATH}" \
             --test=test_sync_atomic.cpp \
             --crosstest-bitcode=0 \
             --driver=test_sync_atomic_main.cpp \
@@ -131,7 +119,6 @@
         ./crosstest.py -O${optlevel} --mattr ${attribute} \
             --prefix=Subzero_ --target=x8632 \
             --dir="${OUTDIR}" \
-            --llvm-bin-path="${LLVM_BIN_PATH}" \
             --test=test_vector_ops.ll \
             --driver=test_vector_ops_main.cpp \
             --output=test_vector_ops_O${optlevel}_${attribute}
diff --git a/pydir/build-pnacl-ir.py b/pydir/build-pnacl-ir.py
index 9395fc7..608c574 100755
--- a/pydir/build-pnacl-ir.py
+++ b/pydir/build-pnacl-ir.py
@@ -2,35 +2,24 @@
 
 import argparse
 import os
-import sys
 import tempfile
 from utils import shellcmd
+from utils import FindBaseNaCl
 
 if __name__ == '__main__':
     argparser = argparse.ArgumentParser()
     argparser.add_argument('cfile', nargs='+', type=str,
         help='C file(s) to convert')
-    argparser.add_argument('--nacl_sdk_root', nargs='?', type=str,
-        help='Path to NACL_SDK_ROOT')
     argparser.add_argument('--dir', nargs='?', type=str, default='.',
                            help='Output directory')
     argparser.add_argument('--disable-verify', action='store_true')
     args = argparser.parse_args()
 
-    nacl_sdk_root = os.environ.get('NACL_SDK_ROOT', None)
-    if args.nacl_sdk_root:
-        nacl_sdk_root = os.path.expanduser(args.nacl_sdk_root)
-
-    if not nacl_sdk_root or not os.path.exists(nacl_sdk_root):
-        print '''\
-Please set the NACL_SDK_ROOT environment variable or pass the path through
---nacl_sdk_root to point to a valid Native Client SDK installation.'''
-        sys.exit(1)
-
-    includes_path = os.path.join(nacl_sdk_root, 'include')
-    toolchain_path = os.path.join(nacl_sdk_root, 'toolchain', 'linux_pnacl')
-    clang_path = os.path.join(toolchain_path, 'bin64', 'pnacl-clang')
-    opt_path = os.path.join(toolchain_path, 'host_x86_64', 'bin', 'opt')
+    nacl_root = FindBaseNaCl()
+    # Prepend bin to $PATH.
+    os.environ['PATH'] = (
+        nacl_root + '/toolchain/linux_x86/pnacl_newlib/bin' + os.pathsep +
+        os.pathsep + os.environ['PATH'])
 
     tempdir = tempfile.mkdtemp()
 
@@ -40,11 +29,10 @@
         pnaclname = basename + '.pnacl.ll'
         pnaclname = os.path.join(args.dir, pnaclname)
 
-        shellcmd(clang_path + ' -O2 -I{0} -c {1} -o {2}'.format(
-            includes_path, cname, llname))
-        shellcmd(opt_path +
-            ' -pnacl-abi-simplify-preopt -pnacl-abi-simplify-postopt' +
-            ('' if args.disable_verify else
-             ' -verify-pnaclabi-module -verify-pnaclabi-functions') +
-            ' -pnaclabi-allow-debug-metadata -disable-simplify-libcalls'
-            ' {0} -S -o {1}'.format(llname, pnaclname))
+        shellcmd('pnacl-clang -O2 -c {0} -o {1}'.format(cname, llname))
+        shellcmd('pnacl-opt ' +
+                 '-pnacl-abi-simplify-preopt -pnacl-abi-simplify-postopt' +
+                 ('' if args.disable_verify else
+                  ' -verify-pnaclabi-module -verify-pnaclabi-functions') +
+                 ' -pnaclabi-allow-debug-metadata'
+                 ' {0} -S -o {1}'.format(llname, pnaclname))
diff --git a/pydir/utils.py b/pydir/utils.py
index 57849a4..33b58e9 100644
--- a/pydir/utils.py
+++ b/pydir/utils.py
@@ -1,3 +1,4 @@
+import os
 import subprocess
 import sys
 
@@ -10,3 +11,12 @@
     stdout_result = subprocess.check_output(command, shell=True)
     if echo: sys.stdout.write(stdout_result)
     return stdout_result
+
+def FindBaseNaCl():
+    """Find the base native_client/ directory."""
+    nacl = 'native_client'
+    path_list = os.getcwd().split(os.sep)
+    if nacl not in path_list:
+        return None
+    last_index = len(path_list) - path_list[::-1].index(nacl)
+    return os.sep.join(path_list[:last_index])
diff --git a/tests_lit/lit.cfg b/tests_lit/lit.cfg
index 1edf2f9..ba26097 100644
--- a/tests_lit/lit.cfg
+++ b/tests_lit/lit.cfg
@@ -42,8 +42,8 @@
 config.substitutions.append(('%llvm2ice', llvm2icetool))
 config.substitutions.append(('%szdiff', os.path.join(bin_root, 'szdiff.py')))
 
-llvmbintools = [r"\bFileCheck\b", r"\bllvm-mc\b", r"\bllvm-objdump\b",
-                r"\bnot\b", r"\bpnacl-freeze\b"]
+llvmbintools = [r"\bFileCheck\b", r"\bllvm-as\b", r"\bllvm-mc\b",
+                r"\bllvm-objdump\b", r"\bnot\b", r"\bpnacl-freeze\b"]
 
 for tool in llvmbintools:
   # The re.sub() line is adapted from one of LLVM's lit.cfg files.