# 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")

env = env.Clone();

libxipc_env = env.Clone()

if env['enable_tests']:
    subdirs = [ 'tests' ]
    SConscript(dirs=subdirs, exports='env')

is_shared = env.has_key('SHAREDLIBS')

#
# XIPC Library
#

libxipc_env.AppendUnique(CPPPATH = [
    "#",
    "$BUILDDIR",
    ])

libxipc_env.PrependUnique(LIBPATH = [
    '$BUILDDIR/libcomm',
    '$BUILDDIR/libxorp'
    ])

# Internal libraries
libxipc_env.AppendUnique(LIBS = [
    'xorp_comm',
    'xorp_core'
    ])

# External libraries
libxipc_env.AppendUnique(LIBS = [
    'crypto'
    ])


libxipc_sources = [
    'finder_client.cc',
    'finder_client_observer.cc',
    'finder_client_xrl_target.cc',
    'finder_messenger.cc',
    'finder_msgs.cc',
    'finder_tcp.cc',
    'finder_tcp_messenger.cc',
    'hmac.cc',
    'hmac_md5.c',
    'permits.cc',
    'sockutil.cc',
    'xrl.cc',
    'xrl_args.cc',
    'xrl_atom.cc',
    'xrl_atom_encoding.cc',
    'xrl_atom_list.cc',
    'xrl_cmd_map.cc',
    'xrl_dispatcher.cc',
    'xrl_error.cc',
    'xrl_parser.cc',
    'xrl_parser_input.cc',
    'xrl_pf.cc',
    'xrl_pf_factory.cc',
    'xrl_pf_stcp.cc',
    'xrl_pf_stcp_ph.cc',
    'xrl_pf_unix.cc',
    'xrl_router.cc',
    'xrl_std_router.cc',
    'xrl_tokens.cc',
    'xuid.cc',				# only for udp (and fea tcpudp mgr)
    'fp64serial.c',
    ]

# deal with shared objects
if is_shared:
    libxipc_sources += [
        '$BUILDDIR/xrl/targets/finder_client_base$SHOBJSUFFIX',
        '$BUILDDIR/xrl/interfaces/finder_xif$SHOBJSUFFIX',
        '$BUILDDIR/xrl/interfaces/common_xif$SHOBJSUFFIX',
    ]
else:
    obj_fcb = libxipc_env.StaticObject(
	target = 'libxipc_finder_client_base',
	source = '$BUILDDIR/xrl/targets/finder_client_base.cc')
    obj_fxif = libxipc_env.StaticObject(
	target = 'libxipc_finder_xif',
	source = '$BUILDDIR/xrl/interfaces/finder_xif.cc')
    obj_cxif = libxipc_env.StaticObject(
	target = 'libxipc_common_xif',
	source = '$BUILDDIR/xrl/interfaces/common_xif.cc')
    libxipc_sources += [ obj_fcb, obj_fxif, obj_cxif ]

# actual library target
if is_shared:
    libxipc = libxipc_env.SharedLibrary(target = 'libxorp_ipc',
                                        source = libxipc_sources)
    # build symlink for resolving links whilst in BUILDDIR
    if env['rtld_origin']:
        for obj in libxipc:
            env.AddPostAction(libxipc,
                env.Symlink(obj.abspath,
                            os.path.join(env['xorp_alias_libdir'], str(obj))))
    env.Alias('install',
              env.InstallLibrary(env['xorp_libdir'], libxipc))
else:
    libxipc = libxipc_env.StaticLibrary(target = 'libxorp_ipc',
                                        source = libxipc_sources)

#
# Finder library
#
libfinder_env = env.Clone()

libfinder_env.AppendUnique(CPPPATH = [
    "#",
    "$BUILDDIR",
    ])

libfinder_env.AppendUnique(LIBPATH = [
    '$BUILDDIR/libxipc',
    '$BUILDDIR/libcomm',
    '$BUILDDIR/libxorp'
    ])

libfinder_env.AppendUnique(LIBS = [
    'xorp_ipc',
    'xorp_comm',
    'xorp_core'
    ])

libfinder_sources = [
    'finder.cc',
    'finder_server.cc',
    'finder_xrl_queue.cc',
    'finder_xrl_target.cc',
    ]

# deal with shared/non-shared source/objects
if is_shared:
    libfinder_sources += [
        '$BUILDDIR/xrl/targets/finder_base$SHOBJSUFFIX',
        '$BUILDDIR/xrl/interfaces/finder_client_xif$SHOBJSUFFIX',
        '$BUILDDIR/xrl/interfaces/finder_event_observer_xif$SHOBJSUFFIX',
    ]
else:
    obj_f_base = libfinder_env.StaticObject(
	target = 'libfinder_finder_client_base',
	source = '$BUILDDIR/xrl/targets/finder_base.cc')
    obj_f_cxif = libfinder_env.StaticObject(
	target = 'libfinder_finder_client_xif',
	source = '$BUILDDIR/xrl/interfaces/finder_client_xif.cc')
    obj_f_eoxif = libfinder_env.StaticObject(
	target = 'libfinder_finder_event_observer_xif',
	source = '$BUILDDIR/xrl/interfaces/finder_event_observer_xif.cc')
    libfinder_sources += [ obj_f_base, obj_f_cxif, obj_f_eoxif ]

# library target
if is_shared:
    libfinder = libfinder_env.SharedLibrary(target = 'libxorp_finder',
                                            source = libfinder_sources)
    # build symlink for resolving links whilst in BUILDDIR
    if env['rtld_origin']:
        for obj in libfinder:
            env.AddPostAction(libfinder,
                env.Symlink(obj.abspath,
                            os.path.join(env['xorp_alias_libdir'], str(obj))))
    env.Alias('install',
              env.InstallLibrary(env['xorp_libdir'], libfinder))
else:
    libfinder = libfinder_env.StaticLibrary(target = 'libxorp_finder',
                                            source = libfinder_sources)

#
# finder, call_xrl, finder, etc. executables
#
env = env.Clone()

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

env.AppendUnique(LIBPATH = [
    '$BUILDDIR/libxipc',
    '$BUILDDIR/libcomm',
    '$BUILDDIR/libxorp'
    ])

env.AppendUnique(LIBS = [
    'xorp_finder',
    'xorp_ipc',
    'xorp_comm',
    'xorp_core'
    ])

if not is_shared:
    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',
        'winmm',
        ])


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

xorp_finder = env.Program(target = 'xorp_finder',
                     source = [ 'finder_main.cc' ])

call_xrl = env.Program(target = 'call_xrl',
                       source = [ 'call_xrl.cc'])

# call_xrl is not required for normal use, but can be useful for
# applications wanting to configure XRL via shell scripts.
env.Alias('install', env.InstallProgram('$exec_prefix/sbin/', call_xrl))

if env['enable_tests']:
    if env['enable_builddirrun']:
        for obj in xorp_finder:
            env.AddPostAction(xorp_finder,
                env.Symlink(obj.abspath,
                    os.path.join(env['xorp_alias_moduledir'], str(obj))))
    env.Alias('install', env.InstallProgram(env['xorp_moduledir'], xorp_finder))
    
Default(libxipc, libfinder, call_xrl, xorp_finder)

