from os.path import basename


def threadLibs(env, subdir):
    env.AppendUnique(
        CPPPATH=['.', '../..'],
        CCFLAGS=['-ffast-math', '-fpic'],
        )

    def libs(target, parts, extraLibs):
        sources = [part + '.c' for part in parts]
        target += '${REENTRANT}'
        env.TwoLibraries(target, sources, LIBS=extraLibs)

    parts = ['../countdown', 'hijack', '../initialize', '../registry', '../report', '../timestamps-set', '../verbose', '../wrap_fork' ]
    libs('sampler', parts, 'dl')

    randomizers = {'fixed': [], 'offline': [], 'online': ['m']}
    if subdir == 'no':
        randomizers['always'] = []

    for when, extraLibs in randomizers.iteritems():
        src = '../random-%s' % when
        libs('sampler-%s' % when, [src], extraLibs)

    Alias('install', env.Install('$DESTDIR$threadsdir/%s' % subdir, ['lock.h', 'once.h']))


Import('env', 'excludedSources')

headers = [
    'atomic-increment.h',
    'countdown.h',
    'local.h',
    'random-fixed.h',
    'random-offline.h',
    'random-offline-size.h',
    'random-online.h',
    ]

Install('#driver/sampler/threads', headers)
Alias('install', env.Install('$DESTDIR$threadsdir', headers))

File([
        'lifetime.h',
        'random.h',
        'report.h',
        'verbose.h',
        ])

excludedSources |= set([
        'lib/threads/hijack.c',
        'lib/threads/precompute.c',
        ])

SConscript(dirs=['no', 'yes'], exports=['env', 'threadLibs'])
