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]
44if bits_64:
45    possible_config_opts.append('--with-lg-vaddr=56')
46
47possible_malloc_conf_opts = [
48    'tcache:false',
49    'dss:primary',
50    'percpu_arena:percpu',
51    'background_thread:true',
52]
53
54print 'set -e'
55print 'if [ -f Makefile ] ; then %(make_cmd)s relclean ; fi' % {'make_cmd': make_cmd}
56print 'autoconf'
57print 'rm -rf run_tests.out'
58print 'mkdir run_tests.out'
59print 'cd run_tests.out'
60
61ind = 0
62for cc, cxx in possible_compilers:
63    for compiler_opts in powerset(possible_compiler_opts):
64        for config_opts in powerset(possible_config_opts):
65            for malloc_conf_opts in powerset(possible_malloc_conf_opts):
66                if cc is 'clang' \
67                  and '-m32' in possible_compiler_opts \
68                  and '--enable-prof' in config_opts:
69                    continue
70                config_line = (
71                    'EXTRA_CFLAGS=-Werror EXTRA_CXXFLAGS=-Werror '
72                    + 'CC="{} {}" '.format(cc, " ".join(compiler_opts))
73                    + 'CXX="{} {}" '.format(cxx, " ".join(compiler_opts))
74                    + '../../configure '
75                    + " ".join(config_opts) + (' --with-malloc-conf=' +
76                    ",".join(malloc_conf_opts) if len(malloc_conf_opts) > 0
77                    else '')
78                )
79
80                # We don't want to test large vaddr spaces in 32-bit mode.
81		if ('-m32' in compiler_opts and '--with-lg-vaddr=56' in
82                  config_opts):
83		    continue
84
85                # Per CPU arenas are only supported on Linux.
86                linux_supported = ('percpu_arena:percpu' in malloc_conf_opts \
87                  or 'background_thread:true' in malloc_conf_opts)
88                # Heap profiling and dss are not supported on OS X.
89                darwin_unsupported = ('--enable-prof' in config_opts or \
90                  'dss:primary' in malloc_conf_opts)
91                if (uname == 'Linux' and linux_supported) \
92                  or (not linux_supported and (uname != 'Darwin' or \
93                  not darwin_unsupported)):
94                    print """cat <<EOF > run_test_%(ind)d.sh
95#!/bin/sh
96
97set -e
98
99abort() {
100    echo "==> Error" >> run_test.log
101    echo "Error; see run_tests.out/run_test_%(ind)d.out/run_test.log"
102    exit 255 # Special exit code tells xargs to terminate.
103}
104
105# Environment variables are not supported.
106run_cmd() {
107    echo "==> \$@" >> run_test.log
108    \$@ >> run_test.log 2>&1 || abort
109}
110
111echo "=> run_test_%(ind)d: %(config_line)s"
112mkdir run_test_%(ind)d.out
113cd run_test_%(ind)d.out
114
115echo "==> %(config_line)s" >> run_test.log
116%(config_line)s >> run_test.log 2>&1 || abort
117
118run_cmd %(make_cmd)s all tests
119run_cmd %(make_cmd)s check
120run_cmd %(make_cmd)s distclean
121EOF
122chmod 755 run_test_%(ind)d.sh""" % {'ind': ind, 'config_line': config_line, 'make_cmd': make_cmd}
123                    ind += 1
124
125print '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}
126