#!/usr/bin/python

# This script tests that there are no errors building with any possible build option:

import sys, os, time


a = [
    'NO_LOCKFREEVISITOR',
    'NO_BLOCK_ORDERING',
    'HAVE_PTHREAD',
    'USE_TLS',
    'NO_ASSERT_CHECKS',
    'WITH_BTANDCHECK',
    'WITH_ACCOUNTS',
    'DISABLE_INLINE',
    'WANT_PRINTFSIMPLE',
    'NO_CB_HEAPS',
    'COMPACT_MODE',
    'NO_PTHREAD_EXIT_OVERRIDE',
    'NO_RANKHEAP',
    'NO_HEAP_TRUNCATION',
]

comb = len(a)

resume_s =''
try:
    resume_s = open('RESUME', 'r').read()
except:
    pass

resume = 0
if resume_s:
    resume = int(resume_s)

for i in xrange(1 << comb):
    s = ''
    u = {}
    x = (resume + i) % (1 << comb)
    for j in xrange(comb):
        if (x & (1<<j)):
            s = s + '-D' + a[j] + ' '
            u[a[j]] = 1
    if u.has_key('USE_TLS') and not u.has_key('HAVE_PTHREAD'):
        continue
    if u.has_key('NO_BLOCK_ORDERING') and u.has_key('COMPACT_MODE'):
        continue
    f = open('RESUME', 'w').write(str(x) + '\n')
    sys.stdout.write('Defines:  %d   until => %d   (total=%d)          \r' % (x, (resume + (1 << comb) - 1) % (1 << comb), (1 << comb)))
    sys.stdout.flush()
    cmd = "gcc -c -o dfwmalloc.o dfwmalloc.c -Wall -O0 %s -Wextra -Werror" % s
    if os.system(cmd):
        print 'error%d:  %s' % (x, cmd)
        sys.exit(0)
    if not (i % 10):
        time.sleep(0.1)         # chance to catch Ctrl-C

os.unlink('RESUME')

print 'Success                           '


