# 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('env')

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

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

env = env.Clone()

is_shared = env.has_key('SHAREDLIBS')

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

env.PrependUnique(LIBPATH = [
    '$BUILDDIR/fea',
    '$BUILDDIR/fea/data_plane/managers',
    '$BUILDDIR/fea/data_plane/fibconfig',
    '$BUILDDIR/fea/data_plane/ifconfig',
    '$BUILDDIR/fea/data_plane/io',
    '$BUILDDIR/fea/data_plane/control_socket',
    '.',
    '$BUILDDIR/mrt',
    '$BUILDDIR/cli',
    '$BUILDDIR/cli/libtecla',
    '$BUILDDIR/xrl/interfaces',
    '$BUILDDIR/xrl/targets',
    '$BUILDDIR/libfeaclient',
    '$BUILDDIR/libproto',
    '$BUILDDIR/libxipc',
    '$BUILDDIR/libxorp',
    '$BUILDDIR/libcomm',
    ])

libxfdp_srcs = [
     'fea_data_plane_manager.cc',
     'fibconfig_forwarding.cc',
     'ifconfig_property.cc',
     'iftree.cc',
     'io_ip.cc',
     'io_link.cc',
     'io_tcpudp.cc',
]

libxorp_fea_linkorder = [
    'xorp_fea_data_plane_managers',
    'xorp_fea_fibconfig',
    'xorp_fea_io',
    'xorp_fea_control_socket',
    'xorp_fea_ifconfig',
    ]

if not is_shared:
    libxorp_fea_linkorder.append('libxorp_fea_data_plane_base')

libxorp_fea_linkorder += [
    'xif_fea_fib_client',
    'xif_fea_rawlink_client',
    'xif_fea_rawpkt4_client',
    'xif_socket4_user',
    'xif_finder_event_notifier',
    'xorp_cli',
    'xif_cli_processor',
    'xorp_fea_client',
    'xif_fea_ifmgr_mirror',   # XXX?
    'xif_fea_ifmgr_replicator', # XXX?
    'xst_fea_ifmgr_mirror', # XXX?
    'xif_cli_manager',
    'xif_mfea_client',
    'xst_cli',
    'xst_fea',
    'xst_mfea',
    'xorp_mrt',
    'xorp_proto',
    'xorp_ipc',
    'xorp_comm',
    'xorp_core'
    ]

if not (env.has_key('disable_libtecla') and env['disable_libtecla']):
    libxorp_fea_linkorder += [
        'xorp_tecla',
        ]
else:
    # External tecla library
    libxorp_fea_linkorder += [
        'tecla',
        ]

if not (env.has_key('disable_ipv6') and env['disable_ipv6']):
    libxorp_fea_linkorder += [ 'xif_socket6_user',
                               'xif_fea_rawpkt6_client',
                               ]

if not (env.has_key('disable_fw') and env['disable_fw']):
    env.PrependUnique(LIBPATH = [ '$BUILDDIR/fea/data_plane/firewall' ])
    libxorp_fea_linkorder += [ 'xorp_fea_firewall' ]

if not (env.has_key('disable_profile') and env['disable_profile']):
    libxorp_fea_linkorder += [ 'xif_profile_client' ]

# Internal libraries.
# Note special use of conditional above.
env.PrependUnique(LIBS = libxorp_fea_linkorder)

# External libraries.
if env.has_key('has_libpcap') and env['has_libpcap']:
    env.AppendUnique(LIBS = [
        'pcap'
        ])

# Report unresolved symbol references when building the FEA.
if is_shared:
    env.AppendUnique(LINKFLAGS = [ '-Wl,-z,defs', ])

else:
    env.AppendUnique(LIBS = [
        "crypto",
        ])

    if not (env.has_key('mingw') and env['mingw']):
        env.AppendUnique(LIBS = [
            "rt",
            ])

if (env.has_key('mingw') and env['mingw']):
    env.AppendUnique(LIBS = [
        'ws2_32',
        'iphlpapi',
        'mprapi',
        'regex',
        'winmm',
        ])

libxorp_fea_srcs = [
     'fea_io.cc',
     'fea_node.cc',
     'fibconfig.cc',
     'fibconfig_transaction.cc',
     'ifconfig.cc',
     'ifconfig_reporter.cc',
     'ifconfig_transaction.cc',
     'io_ip_manager.cc',
     'io_link_manager.cc',
     'io_tcpudp_manager.cc',
     'libfeaclient_bridge.cc',
     'mfea_config.cc',
     'mfea_dataflow.cc',
     'mfea_mrouter.cc',
     'mfea_node.cc',
     'mfea_node_cli.cc',
     'mfea_vif.cc',
     'nexthop_port_mapper.cc',
     'xrl_fea_io.cc',
     'xrl_fea_node.cc',
     'xrl_fea_target.cc',
     'xrl_fib_client_manager.cc',
     'xrl_io_ip_manager.cc',
     'xrl_io_link_manager.cc',
     'xrl_io_tcpudp_manager.cc',
     'xrl_mfea_node.cc'
    ]

if not (env.has_key('disable_fw') and env['disable_fw']):
    libxorp_fea_srcs += [ 'firewall_entry.cc',
                          'firewall_manager.cc',
                          'firewall_transaction.cc',
                          ]

if not (env.has_key('disable_profile') and env['disable_profile']):
    libxorp_fea_srcs += [ 'profile_vars.cc' ]


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

if is_shared:
    libxorp_fea_srcs += libxfdp_srcs
    libxorp_fea = env.SharedLibrary(target = 'libxorp_fea',
			       source = libxorp_fea_srcs)
    if env['rtld_origin']:
        for obj in libxorp_fea:
            env.AddPostAction(libxorp_fea,
                env.Symlink(obj.abspath,
                            os.path.join(env['xorp_alias_libdir'], str(obj))))
    env.Alias('install',
              env.InstallLibrary(env['xorp_libdir'], libxorp_fea))
else:
    # Use an intermediate static lib to deal with some dependencies.
    libxfdp = env.StaticLibrary(target = 'libxorp_fea_data_plane_base',
			        source = libxfdp_srcs)
    env.PrependUnique(LIBS = [ libxfdp ])
    libxorp_fea = env.StaticLibrary(target = 'libxorp_fea',
				    source = libxorp_fea_srcs)

#######################

env = env.Clone()

if is_shared:
    env.PrependUnique(LIBS = [
        'xorp_fea',		# -lxorp_fea, not the xorp_fea executable
    ])
else:
    env.PrependUnique(LIBS = [ libxorp_fea ])

feasrcs = [
          'xorp_fea.cc',
          ]

fea = env.Program(target = 'xorp_fea', source = feasrcs)
if env['enable_builddirrun']:
    for obj in fea:
        env.AddPostAction(fea,
            env.Symlink(obj.abspath,
                os.path.join(env['xorp_alias_moduledir'], str(obj))))
env.Alias('install', env.InstallProgram(env['xorp_moduledir'], fea))

#######################

if env['enable_fea_dummy']:
    env = env.Clone()

    env['OBJPREFIX'] = 'dummy-'

    env.AppendUnique(CPPDEFINES = 'FEA_DUMMY=1')

    feadummysrcs = [
        'xorp_fea.cc',
        ]

    feadummy = env.Program(target = 'xorp_fea_dummy', source = feadummysrcs)
    if env['enable_builddirrun']:
        for obj in feadummy:
            env.AddPostAction(feadummy,
                env.Symlink(obj.abspath,
                            os.path.join(env['xorp_alias_moduledir'], str(obj))))
    env.Alias('install', env.InstallProgram(env['xorp_moduledir'], feadummy))

#######################

if env['enable_click']:
    clickgen = 'fea_click_config_generator'
    env.Alias('install', env.InstallScript(env['xorp_tooldir'], clickgen))

#######################

# Install scripts
env.Alias('install', env.InstallScript('$exec_prefix/sbin/', env.Entry('fea_xrl_shell_funcs.sh')))

if env['enable_fea_dummy']:
    Default(libxorp_fea, fea, feadummy)
else:
    Default(libxorp_fea, fea)
