# Copyright (c) 2009 XORP, Inc.
#
# 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('env')

subdirs = [
	'tests',
	'tools',
	]

SConscript(dirs = subdirs, exports='env')

e = env.Clone()
is_shared = e.has_key('SHAREDLIBS')

#
# NB: The same sources are used for RIP and RIPng.  We use separate
# scons environments with different CPPDEFINES and SHOBJPREFIXES to
# support this.
#

# RIP/RIPng Library Sources.
libxorp_rip_srcs = [
    'auth.cc',
    'output_table.cc',
    'output_updates.cc',
    'packet_queue.cc',
    'peer.cc',
    'port.cc',
    'redist.cc',
    'rib_notifier_base.cc',
    'rip_varrw.cc',
    'route_db.cc',
    'route_entry.cc',
    'update_queue.cc'
    ]

# RIP/RIPng XRL Library Sources.
libxorp_rip_xrl_srcs = [
    'xrl_config.cc',
    'xrl_port_manager.cc',
    'xrl_port_io.cc',
    'xrl_process_spy.cc',
    'xrl_redist_manager.cc',
    'xrl_rib_notifier.cc'
    ]

# RIP Main/Driver Sources
ripsrcs = [
    'xorp_rip_main.cc',
    'xrl_target_rip.cc'
    ]

# RIPng Main/Driver Sources
ripngsrcs = [
    'xorp_rip_main.cc',
    'xrl_target_ripng.cc'
    ]


#
# RIP
#

e['OBJPREFIX'] = 'rip-'
e['SHOBJPREFIX'] = 'rip-'

e.AppendUnique(CPPPATH = [
    '#',
    '$BUILDDIR'
    ])

e.AppendUnique(CPPDEFINES = [
    'INSTANTIATE_IPV4'
    ])

e.PrependUnique(LIBPATH = [
    '.',
    '$BUILDDIR/policy/backend',
    '$BUILDDIR/policy/common',
    '$BUILDDIR/xrl/interfaces',
    '$BUILDDIR/xrl/targets',
    '$BUILDDIR/libfeaclient',
    '$BUILDDIR/libproto',
    '$BUILDDIR/libxipc',
    '$BUILDDIR/libcomm',
    '$BUILDDIR/libxorp',
    ])

e.PrependUnique(LIBS = [
    'xorp_rip_xrl',
    'xorp_rip',			# Refers to library, not the executable.
    'xst_rip',
    'xif_rib',
    'xif_finder_event_notifier',
    'xorp_fea_client',
    'xif_fea_ifmgr_mirror',
    'xif_fea_ifmgr_replicator',
    'xst_fea_ifmgr_mirror',
    'xif_socket4',
    'xorp_policy_backend',
    'xorp_policy_common',
    'xorp_proto',
    'xorp_ipc',
    'xorp_comm',
    'xorp_core',
])

e.Replace(RPATH = [
    e.Literal(e['xorp_module_rpath'])
])

if is_shared:
    libxorp_rip = e.SharedLibrary(target = 'libxorp_rip',
				  source = libxorp_rip_srcs,
				  LIBS = '')
    if e['rtld_origin']:
        for obj in libxorp_rip:
            e.AddPostAction(libxorp_rip,
                e.Symlink(obj.abspath,
                          os.path.join(env['xorp_alias_libdir'], str(obj))))
    libxorp_rip_xrl = e.SharedLibrary(target = 'libxorp_rip_xrl',
				      source = libxorp_rip_xrl_srcs,
				      LIBS = '')
    if e['rtld_origin']:
        for obj in libxorp_rip_xrl:
            e.AddPostAction(libxorp_rip_xrl,
                e.Symlink(obj.abspath,
                            os.path.join(env['xorp_alias_libdir'], str(obj))))
    e.Alias('install',
                [ e.InstallLibrary(e['xorp_libdir'], libxorp_rip),
                  e.InstallLibrary(e['xorp_libdir'], libxorp_rip_xrl) ])
else:
    libxorp_rip = e.StaticLibrary(target = 'libxorp_rip',
				  source = libxorp_rip_srcs,
				  LIBS = '')
    libxorp_rip_xrl = e.StaticLibrary(target = 'libxorp_rip_xrl',
				      source = libxorp_rip_xrl_srcs,
				      LIBS = '')

rip = e.Program(target = 'xorp_rip', source = ripsrcs)
e.Alias('install', e.InstallProgram(e['xorp_moduledir'], rip))

#
# RIPng
#
e = env.Clone()

e['OBJPREFIX'] = 'ripng-'
e['SHOBJPREFIX'] = 'ripng-'

e.AppendUnique(CPPPATH = [
    '#',
    '$BUILDDIR',
    ])

e.AppendUnique(CPPDEFINES = [
    'INSTANTIATE_IPV6'
    ])

e.PrependUnique(LIBPATH = [
    '$BUILDDIR/libfeaclient',
    '$BUILDDIR/policy/backend',
    '$BUILDDIR/policy/common',
    '$BUILDDIR/xrl/interfaces',
    '$BUILDDIR/xrl/targets',
    '.',
    '$BUILDDIR/libproto',
    '$BUILDDIR/libxipc',
    '$BUILDDIR/libcomm',
    '$BUILDDIR/libxorp',
    ])

e.PrependUnique(LIBS = [
    'xorp_ripng_xrl',
    'xorp_ripng',
    'xst_ripng',
    'xif_rib',
    'xif_finder_event_notifier',
    'xorp_fea_client',
    'xif_fea_ifmgr_mirror',
    'xif_fea_ifmgr_replicator',
    'xst_fea_ifmgr_mirror',
    'xif_socket6',
    'xorp_policy_backend',
    'xorp_policy_common',
    'xorp_proto',
    'xorp_ipc',
    'xorp_comm',
    'xorp_core',
])

e.Replace(RPATH = [
    e.Literal(e['xorp_module_rpath'])
])

if is_shared:
    libxorp_ripng = e.SharedLibrary(target = 'libxorp_ripng',
				    source = libxorp_rip_srcs,
				    LIBS = '')
    if e['rtld_origin']:
        for obj in libxorp_ripng:
            e.AddPostAction(libxorp_ripng,
                e.Symlink(obj.abspath,
                          os.path.join(env['xorp_alias_libdir'], str(obj))))
    libxorp_ripngxrl = e.SharedLibrary(target = 'libxorp_ripng_xrl',
				       source = libxorp_rip_xrl_srcs,
				       LIBS = '')
    if e['rtld_origin']:
        for obj in libxorp_ripngxrl:
            e.AddPostAction(libxorp_ripngxrl,
                e.Symlink(obj.abspath,
                          os.path.join(env['xorp_alias_libdir'], str(obj))))
    e.Alias('install',
                [ e.InstallLibrary(e['xorp_libdir'], libxorp_ripng),
                  e.InstallLibrary(e['xorp_libdir'], libxorp_ripngxrl) ])
else:
    libxorp_ripng = e.StaticLibrary(target = 'libxorp_ripng',
				    source = libxorp_rip_srcs,
				    LIBS = '')
    libxorp_ripngxrl = e.StaticLibrary(target = 'libxorp_ripng_xrl',
				       source = libxorp_rip_xrl_srcs,
				       LIBS = '')

ripng = e.Program(target = 'xorp_ripng', source = ripngsrcs)
e.Alias('install', e.InstallProgram(e['xorp_moduledir'], ripng))

Default(rip, ripng)
