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