1#!/usr/bin/env python
2
3import sys
4from itertools import combinations
5from os import uname
6from multiprocessing import cpu_count
7from subprocess import call
8
9# Later, we want to test extended vaddr support.  Apparently, the "real" way of
10# checking this is flaky on OS X.
11bits_64 = sys.maxsize > 2**32
12
13nparallel = cpu_count() * 2
14
15uname = uname()[0]
16
17if "BSD" in uname:
18    make_cmd = 'gmake'
19else:
20    make_cmd = 'make'
21
22def powerset(items):
23    result = []
24    for i in xrange(len(items) + 1):
25        result += combinations(items, i)
26    return result
27
28possible_compilers = []
29for cc, cxx in (['gcc', 'g++'], ['clang', 'clang++']):
30    try:
31        cmd_ret = call([cc, "-v"])
32        if cmd_ret == 0:
33            possible_compilers.append((cc, cxx))
34    except:
35        pass
36possible_compiler_opts = [
37    '-m32',
38]
39possible_config_opts = [
40    '--enable-debug',
41    '--enable-prof',
42    '--disable-stats',
43    '--enable-opt-safety-checks',
44]
45if bits_64:
46    possible_config_opts.append('--with-lg-vaddr=56')
47
48possible_malloc_conf_opts = [
49    'tcache:false',
50    'dss:primary',
51    'percpu_arena:percpu',
52    'background_thread:true',
53]
54
55print 'set -e'
56print 'if [ -f Makefile ] ; then %(make_cmd)s relclean ; fi' % {'make_cmd': make_cmd}
57print 'autoconf'
58print 'rm -rf run_tests.out'
59print 'mkdir run_tests.out'
60print 'cd run_tests.out'
61
62ind = 0
63for cc, cxx in possible_compilers:
64    for compiler_opts in powerset(possible_compiler_opts):
65        for config_opts in powerset(possible_config_opts):
66            for malloc_conf_opts in powerset(possible_malloc_conf_opts):
67                if cc is 'clang' \
68                  and '-m32' in possible_compiler_opts \
69                  and '--enable-prof' in config_opts:
70                    continue
71                config_line = (
72                    'EXTRA_CFLAGS=-Werror EXTRA_CXXFLAGS=-Werror '
73                    + 'CC="{} {}" '.format(cc, " ".join(compiler_opts))
74                    + 'CXX="{} {}" '.format(cxx, " ".join(compiler_opts))
75                    + '../../configure '
76                    + " ".join(config_opts) + (' --with-malloc-conf=' +
77                    ",".join(malloc_conf_opts) if len(malloc_conf_opts) > 0
78                    else '')
79                )
80
81                # We don't want to test large vaddr spaces in 32-bit mode.
82		if ('-m32' in compiler_opts and '--with-lg-vaddr=56' in
83                  config_opts):
84		    continue
85
86                # Per CPU arenas are only supported on Linux.
87                linux_supported = ('percpu_arena:percpu' in malloc_conf_opts \
88                  or 'background_thread:true' in malloc_conf_opts)
89                # Heap profiling and dss are not supported on OS X.
90                darwin_unsupported = ('--enable-prof' in config_opts or \
91                  'dss:primary' in malloc_conf_opts)
92                if (uname == 'Linux' and linux_supported) \
93                  or (not linux_supported and (uname != 'Darwin' or \
94                  not darwin_unsupported)):
95                    print """cat <<EOF > run_test_%(ind)d.sh
96#!/bin/sh
97
98set -e
99
100abort() {
101    echo "==> Error" >> run_test.log
102    echo "Error; see run_tests.out/run_test_%(ind)d.out/run_test.log"
103    exit 255 # Special exit code tells xargs to terminate.
104}
105
106# Environment variables are not supported.
107run_cmd() {
108    echo "==> \$@" >> run_test.log
109    \$@ >> run_test.log 2>&1 || abort
110}
111
112echo "=> run_test_%(ind)d: %(config_line)s"
113mkdir run_test_%(ind)d.out
114cd run_test_%(ind)d.out
115
116echo "==> %(config_line)s" >> run_test.log
117%(config_line)s >> run_test.log 2>&1 || abort
118
119run_cmd %(make_cmd)s all tests
120run_cmd %(make_cmd)s check
121run_cmd %(make_cmd)s distclean
122EOF
123chmod 755 run_test_%(ind)d.sh""" % {'ind': ind, 'config_line': config_line, 'make_cmd': make_cmd}
124                    ind += 1
125
126print 'for i in `seq 0 %(last_ind)d` ; do echo run_test_${i}.sh ; done | xargs -P %(nparallel)d -n 1 sh' % {'last_ind': ind-1, 'nparallel': nparallel}
127