# Copyright (c) 2009-2011 XORP, Inc and Others
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, Version 2, June
# 1991 as published by the Free Software Foundation. Redistribution
# and/or modification of this program under the terms of any other
# version of the GNU General Public License is not permitted.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
# see the GNU General Public License, Version 2, a copy of which can be
# found in the XORP LICENSE.gpl file.
#
# XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
# http://xorp.net
#
# $XORP$
 
import os
import string
Import("env")
 
env = env.Clone()
is_shared = env.has_key('SHAREDLIBS')
 
env.AppendUnique(CPPPATH = [ "#" ])
env.AppendUnique(LIBPATH = [
    '$BUILDDIR/libxipc',
    '$BUILDDIR/libcomm',
    '$BUILDDIR/libxorp',
    ])
 
env.AppendUnique(LIBS = [
    'xorp_ipc',
    'xorp_comm',
    'xorp_core',
    ])
 
env['TGTGEN_CPPPATH'] = [ '#/xrl/interfaces', '$BUILDDIR' ]
 
tgts = [
    'cli.tgt',
    'coord.tgt',
    'fea.tgt',
    'fea_ifmgr_mirror.tgt',
    'fib2mrib.tgt',
    'finder.tgt',
    'finder_client.tgt',
    'mfea.tgt',
    'mld6igmp.tgt',
    'pim.tgt',
    'policy.tgt',
    'rib.tgt',
    'ribclient.tgt',
    'rtrmgr.tgt',
    'show_distances.tgt',
    'show_routes.tgt',
    'static_routes.tgt',
    'test.tgt',
    'test_fea_ifmgr_mirror.tgt',
    'test_fea_rawlink.tgt',
    'test_finder_events.tgt',
    'test_peer.tgt',
    'test_socket4.tgt',
    'test_xrls.tgt',
    ]

if env['enable_bgp']:
    tgts.append('bgp.tgt')

if not (env.has_key('disable_ipv6') and env['disable_ipv6']):
    tgts.append('test_socket6.tgt')
    if env['enable_ospf']:
        tgts.append('ospfv3.tgt')

if env['enable_olsr']:
    tgts.append('olsr4.tgt')

if env['enable_ospf']:
    tgts.append('ospfv2.tgt')

if env['enable_rip']:
    tgts.append('rip.tgt')
    tgts.append('ripng.tgt')

if env['enable_vrrp']:
    tgts.append('vrrp.tgt')

if env['enable_xorpsh']:
    tgts.append('xorpsh.tgt')

if not (env.has_key('disable_profile') and env['disable_profile']):
    tgts.append('profiler.tgt')

#xrls_path = '$datadir'
xrlspath = '$exec_prefix/xrl/targets'

# Not sure this is working right...I want some way to make the
# targets depend on the xorp_config.h so we can enable/disable various
# features w/out having to re-compile everything to get the changes to
# take affect. --Ben
Depends('fea.tgt', '../../xorp_config.h');

def BuildXrlTarget(tgt):
    lib = None
    base = tgt[:-len('.tgt')]
    target = 'libxst_' + base
    source = base + '_base.cc'

    env.TGTGEN(tgt)

    if is_shared:
        lib = env.SharedLibrary(target, source)
        # symlink an alias for each library when $ORIGIN is in use.
        if env['rtld_origin']:
            for obj in lib:
                env.AddPostAction(lib,
                    env.Symlink(obj.abspath,
                                os.path.join(env['xorp_alias_libdir'], str(obj))))
    else:
        lib = env.StaticLibrary(target, source)

    xrls = '$BUILDDIR/xrl/targets/' + base + '.xrls'

    if env['enable_tests'] or base[:4] != "test":
        if is_shared:
            env.Alias('install', env.InstallLibrary(env['xorp_libdir'], lib))
        if env['debug_xrldb']:
            env.Alias('install', env.InstallData(env['xorp_xrlsdir'], xrls))

    return lib

all_tgt_libs = []
for tgt in tgts:
    all_tgt_libs.append(BuildXrlTarget(tgt))

Default(all_tgt_libs)
