<commit_before>#!/usr/bin/env python

from __future__ import with_statement
import os
import os.path
import json
from sh import Command, make, which, ocamlbuild
import sys

# TODO: make pch: `g++ -x c++-header -I ../ImageStack/src test_plugin.h`
EPSILON = 0.001

verbose = False

def status(name, s):
    print '%s: %s' % (name, s)

def remove(filename):
    try: os.remove(filename)
    except: pass

proj_root = os.path.join('..', '..')
llvm_path = os.path.join(proj_root, 'llvm', 'Release+Asserts', 'bin')
llc = Command(os.path.join(llvm_path, 'llc'))
clang = Command(os.path.join(llvm_path, 'clang++'))
imagestack = Command(os.path.join(proj_root, 'ImageStack', 'bin', 'ImageStack'))

cxx = None
gcc_versions = [which('g++')]
for gcc in gcc_versions:
    cxx = Command(gcc)
    try:
        cxx('--version')
    except:
        continue
    break
if not cxx:
    print 'Halide requires g++ to be in the path.'
    sys.exit(-1)

platform = sys.platform.lower()
if 'linux' in platform:
    platform = 'linux'
elif 'darwin' in platform:
    platform = 'darwin'
elif 'cygwin' in platform:
    platform = 'cygwin'
else:
    print 'System platform not recognized: ' + platform
    sys.exit(-1)

failures = []

def test_cpp(name):
    
    global failures
    
    # status(name, "Building libHalide.a")
    # Make sure Halide.a is built
    make('-C', '../cpp_bindings/', 'libHalide.a')

    curdir = os.getcwd()

    # Dive in
    os.chdir(name)

    srcfile = "test.cpp"
    logfile = "log.txt"
    errfile = "err.txt"
    
    # Clean up old stuff
    remove("generated.o")
    remove("generated.bc")
    remove("passes.txt")
    remove("a.out")
    remove(logfile)
    remove(errfile)
    
    # Threading these through as file handles shared by all sh tasks makes
    # sure each task appends to the main logs, and flushes on exception.
    with open(logfile, "wt") as log:
        with open(errfile, "wt") as err:
            # status(name, "Compiling %s" % srcfile)
            compile_cmd = ["-I../../../cpp_bindings",
                           "-Wno-format",
                           "-fPIC",
                           srcfile]
            if platform in ['linux', 'darwin']:
                compile_cmd.append("../../../cpp_bindings/libHalide.a")
            compile_cmd.extend(["-ldl", "-lpthread"])
            if platform is 'linux':
                compile_cmd.append("-rdynamic")

            if os.path.exists("/usr/local/cuda"):
                compile_cmd.extend(["-L/usr/local/cuda/lib", "-lcuda"])
                if platform is 'linux':
                    compile_cmd.append("-L/usr/lib/nvidia-current")
            
            try:
                # Build the test
                # status(name, " ".join(compile_cmd))
                if platform is 'cygwin':
                    objfile = srcfile[:-srcfile[::-1].find('.')] + "o"
                    compile_cmd.append("-c")
                    compile_cmd.append("-o "+objfile)
                    cxx(compile_cmd, _out=log, _err=err)
                    flexlink = Command("flexlink")
                    link_cmd = ["../../../cpp_bindings/libHalide.a",
                                objfile,
                                "-chain cygwin",
                                "-exe",
                                "-lstdc++",
                                "-o a.out",
								"--",
								"-export-all-symbols"] 
                    flexlink(link_cmd, _out=log, _err=err)
                else:
				    cxx(compile_cmd, _out=log, _err=err)
                # Run the test
                # status(name, "Running test")
                tester = Command("./a.out")
                tester(_out=log, _err=err)
                sys.stdout.write(".")
                sys.stdout.flush()
            except:
                sys.stdout.write("E")
                sys.stdout.flush()
                failures.append(name)

    # Pop back up
    os.chdir(curdir)

def test(name):
    if name.startswith('cpp/'): return test_cpp(name)

    # status(name, 'Building bitcode')

    # Set up filenames
    cfgfile = "%s.json" % name

    bcfile = "%s.bc" % name
    asmfile = "%s.s" % name
    sofile = "%s.so" % name

    outfile = "%s.png" % name
    tmpfile = "%s.tmp" % name
    logfile = "%s.log" % name
    errfile = "%s.err" % name
    timefile = "%s.time" % name<commit_msg>Fix test runner to catch segfaults<commit_after>#!/usr/bin/env python

from __future__ import with_statement
import os
import os.path
import json
from sh import Command, make, which, ocamlbuild
import sys

# TODO: make pch: `g++ -x c++-header -I ../ImageStack/src test_plugin.h`
EPSILON = 0.001

verbose = False

def status(name, s):
    print '%s: %s' % (name, s)

def remove(filename):
    try: os.remove(filename)
    except: pass

proj_root = os.path.join('..', '..')
llvm_path = os.path.join(proj_root, 'llvm', 'Release+Asserts', 'bin')
llc = Command(os.path.join(llvm_path, 'llc'))
clang = Command(os.path.join(llvm_path, 'clang++'))
imagestack = Command(os.path.join(proj_root, 'ImageStack', 'bin', 'ImageStack'))

cxx = None
gcc_versions = [which('g++')]
for gcc in gcc_versions:
    cxx = Command(gcc)
    try:
        cxx('--version')
    except:
        continue
    break
if not cxx:
    print 'Halide requires g++ to be in the path.'
    sys.exit(-1)

platform = sys.platform.lower()
if 'linux' in platform:
    platform = 'linux'
elif 'darwin' in platform:
    platform = 'darwin'
elif 'cygwin' in platform:
    platform = 'cygwin'
else:
    print 'System platform not recognized: ' + platform
    sys.exit(-1)

failures = []

def test_cpp(name):
    
    global failures
    
    # status(name, "Building libHalide.a")
    # Make sure Halide.a is built
    make('-C', '../cpp_bindings/', 'libHalide.a')

    curdir = os.getcwd()

    # Dive in
    os.chdir(name)

    srcfile = "test.cpp"
    logfile = "log.txt"
    errfile = "err.txt"
    
    # Clean up old stuff
    remove("generated.o")
    remove("generated.bc")
    remove("passes.txt")
    remove("a.out")
    remove(logfile)
    remove(errfile)
    
    # Threading these through as file handles shared by all sh tasks makes
    # sure each task appends to the main logs, and flushes on exception.
    with open(logfile, "wt") as log:
        with open(errfile, "wt") as err:
            # status(name, "Compiling %s" % srcfile)
            compile_cmd = ["-I../../../cpp_bindings",
                           "-Wno-format",
                           "-fPIC",
                           srcfile]
            if platform in ['linux', 'darwin']:
                compile_cmd.append("../../../cpp_bindings/libHalide.a")
            compile_cmd.extend(["-ldl", "-lpthread"])
            if platform is 'linux':
                compile_cmd.append("-rdynamic")

            if os.path.exists("/usr/local/cuda"):
                compile_cmd.extend(["-L/usr/local/cuda/lib", "-lcuda"])
                if platform is 'linux':
                    compile_cmd.append("-L/usr/lib/nvidia-current")
            
            try:
                # Build the test
                # status(name, " ".join(compile_cmd))
                if platform is 'cygwin':
                    objfile = srcfile[:-srcfile[::-1].find('.')] + "o"
                    compile_cmd.append("-c")
                    compile_cmd.append("-o "+objfile)
                    cxx(compile_cmd, _out=log, _err=err)
                    flexlink = Command("flexlink")
                    link_cmd = ["../../../cpp_bindings/libHalide.a",
                                objfile,
                                "-chain cygwin",
                                "-exe",
                                "-lstdc++",
                                "-o a.out",
								"--",
								"-export-all-symbols"] 
                    flexlink(link_cmd, _out=log, _err=err)
                else:
				    cxx(compile_cmd, _out=log, _err=err)
                # Run the test
                # status(name, "Running test")
                tester = Command("./a.out")
                e = tester(_out=log, _err=err)
                if e.exit_code:
                    # Sometimes nonzero exit codes get to here, because SH
                    # silently accepts death due to signals like SEGFAULT.
                    # We re-raise to be sure these report as failure.
                    raise sh.ErrorReturnCode(tester.command, None, None)
                sys.stdout.write(".")
                sys.stdout.flush()
            except:
                sys.stdout.write("E")
                sys.stdout.flush()
                failures.append(name)

    # Pop back up
    os.chdir(curdir)

def test(name):
    if name.startswith('cpp/'): return test_cpp(name)

    # status(name, 'Building bitcode')

    # Set up filenames
    cfgfile = "%s.json" % name

    bcfile = "%s.bc" % name
    asmfile = "%s.s" % name
    sofile = "%s.so" % name

    outfile = "%s.png" % name
    tmpfile = "%s.tmp" % name
    logfile = "%s.log" % name
    errfile = "%s.err" % name
    timefile = "%s.time" % name
{'fraction_non_alphanumeric': 0.14153029632905795, 'fraction_numerical': 0.0015479876160990713, 'mean_word_length': 1.6927657040785948, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4286572', 'n_tokens_mistral': 2954, 'n_tokens_neox': 2800, 'n_words': 679}