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