# 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


import os
Import('env')

subdirs = [
	#'config', # FIXME install
	'tests',
    ]

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

env = env.Clone()

# Fedora 19 has broken-ish lex...work around it for now.
env.AppendUnique(CCFLAGS = [
    '-Wno-sign-compare',
    ])

env.AppendUnique(CPPPATH = [
	'.',
	'$BUILDDIR',
	env['xorp_sourcedir'],	#this is needed for lex and yacc generated files
	])

env.PrependUnique(LIBPATH = [
    '$BUILDDIR/libxorp',
    '$BUILDDIR/libcomm',
    '$BUILDDIR/libxipc',
    '$BUILDDIR/libproto',
    '$BUILDDIR/xrl/interfaces',
    '$BUILDDIR/xrl/targets',
    '$BUILDDIR/cli',
    '$BUILDDIR/cli/libtecla',
    '.',
    ])

### libxorp_rtrmgr

libxorp_rtrmgr_env = env.Clone()

# Automatically generate flex and yacc files

#Create yacc files
yacc_env = env.Clone()
# bison on BSD generates headers files with .h suffix,
# while on other OSs it generates header files with .hh suffix
#
# But, on FreeBSD at least, if you don't do the .hh suffix, it doesn't even
# build a .h file of any type --Ben
yacc_env.Replace(YACCHXXFILESUFFIX='.hh')

yacc_env.AppendUnique(YACCFLAGS='-d')

tplt_env_y = yacc_env.Clone()
tplt_env_y.AppendUnique(YACCFLAGS='-ptplt')

tplt_yacc = tplt_env_y.CXXFile(target='y.tplt_tab.cc',
				source='template.yy')

boot_env_y = yacc_env.Clone()
boot_env_y.AppendUnique(YACCFLAGS='-pboot')

boot_yacc = boot_env_y.CXXFile(target='y.boot_tab.cc',
					source='boot.yy')

opcmd_env_y = yacc_env.Clone()
opcmd_env_y.AppendUnique(YACCFLAGS='-popcmd')

opcmd_yacc = opcmd_env_y.CXXFile(target='y.opcmd_tab.cc',
					source='op_commands.yy')

#create lex files
lex_env = env.Clone()

tplt_env_l = lex_env.Clone()
tplt_env_l.AppendUnique(LEXFLAGS='-Ptplt')

tplt_lex = tplt_env_l.CXXFile(target='lex.tplt.cc',
					source='template.ll')

boot_env_l = lex_env.Clone()
boot_env_l.AppendUnique(LEXFLAGS='-Pboot')

boot_lex = boot_env_l.CXXFile(target='lex.boot.cc',
					source='boot.ll')

opcmd_env_l = lex_env.Clone()
opcmd_env_l.AppendUnique(LEXFLAGS='-Popcmd')

opcmd_lex = opcmd_env_l.CXXFile(target='lex.opcmd.cc',
					source='op_commands.ll')

libxorp_rtrmgr_srcs = [
	'command_tree.cc',
	'conf_tree.cc',
	'conf_tree_node.cc',
	'config_operators.cc',
	'generic_module_manager.cc',
	'glob_win32.c',
	tplt_lex[0],
	opcmd_lex[0],
	boot_lex[0],
	opcmd_yacc[0],
	boot_yacc[0],
	tplt_yacc[0],
	'master_conf_tree.cc',
	'master_conf_tree_node.cc',
	'master_template_tree.cc',
	'master_template_tree_node.cc',
	'module_command.cc',
	'module_manager.cc',
	'op_commands.cc',
	'randomness.cc',
	'slave_conf_tree.cc',
	'slave_conf_tree_node.cc',
	'slave_module_manager.cc',
	'task.cc',
	'template_base_command.cc',
	'template_commands.cc',
	'template_tree.cc',
	'template_tree_node.cc',
	'unexpanded_program.cc',
	'unexpanded_xrl.cc',
	'userdb.cc',
	'xorp_client.cc',
	]

# Runtime XRL syntax validation for developers.
if env['debug_xrldb']:
    libxorp_rtrmgr_srcs += [ 'xrldb.cc' ]
    libxorp_rtrmgr_env.AppendUnique(CPPDEFINES = [
        ( 'DEBUG_XRLDB', 1 ),
    ])

# Pushdown for static paths in util.cc.
xorp_paths = {
    "XORP_INSTALL_ROOT" : '\\"' + str(env['prefix']) + '\\"',
    "XORP_BUILD_ROOT"   : '\\"' + str(env['BUILDDIR']) + '\\"',
    "XORP_SRC_ROOT"     : '\\"' + str(env['xorp_sourcedir']) + '\\"',
}


is_shared = env.has_key('SHAREDLIBS')
if not is_shared:
    libxorp_rtrmgr_env.AppendUnique(LIBS = [
        "crypto",
        ])

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

util_cc_env = env.Clone()
util_cc_env.AppendUnique(CPPDEFINES=xorp_paths.items())
obj_util = util_cc_env.StaticObject(source='util.cc')
libxorp_rtrmgr_srcs += [ obj_util ]

libxorp_rtrmgr = libxorp_rtrmgr_env.StaticLibrary(target = 'libxorp_rtrmgr',
					source = libxorp_rtrmgr_srcs)

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

### rtrmgr

rtrmgr_env = env.Clone()

# The Router Manager's main() function is responsible for
# creating the XRLdb, if XRL syntax debugging is enabled.
if env['debug_xrldb']:
    rtrmgr_env.AppendUnique(CPPDEFINES = [
        ( 'DEBUG_XRLDB', 1 ),
    ])

rtrmgr_env.AppendUnique(LIBS = [
    'xorp_rtrmgr',
    'xst_rtrmgr',
    'xif_rtrmgr_client',
    'xif_finder_event_notifier',
    'xorp_finder',
    'xorp_ipc',
    'xorp_comm',
    'xorp_core',
    'crypto'  # Required on Fedora 13.
])

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

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

    rtrmgr_env.Append(LIBS = ['xorp_core', 'crypto'])


rtrmgr_srcs = [
	'main_rtrmgr.cc',
	'xrl_rtrmgr_interface.cc',
	]

rtrmgr = rtrmgr_env.Program(target = 'xorp_rtrmgr', source = rtrmgr_srcs)

env.Alias('install',
          env.InstallProgram(env['xorp_sbindir'], rtrmgr))

### xorpsh

if env['enable_xorpsh']:
    xorpsh_env = env.Clone()

    xorpsh_env.AppendUnique(LIBS = [
        'xorp_rtrmgr',
        'xorp_cli',
        'xst_cli',			# XXX Not picked up automagically?
        'xif_cli_processor',		# XXX ditto?
        'xorp_proto',
        'xorp_tecla',
        'xif_rtrmgr',
        'xst_xorpsh',
        'xif_finder_event_notifier',
        'xorp_ipc',
        'xorp_comm',
        'xorp_core'
        ])

    # Some sort of curses library is required.
    if xorpsh_env.has_key('has_libncurses') and xorpsh_env['has_libncurses']:
        xorpsh_env.AppendUnique(LIBS = [
            'ncurses'
            ])
    elif xorpsh_env.has_key('has_libpdcurses') and xorpsh_env['has_libpdcurses']:
        xorpsh_env.AppendUnique(LIBS = [
            'pdcurses'
            ])
    else:
        # Assume regular curses.
        xorpsh_env.AppendUnique(LIBS = [
            'curses'
            ])

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

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

        xorpsh_env.Append(LIBS = ['xorp_core', 'crypto'])

    xorpsh_srcs = [
	'xorpsh_main.cc',
	'xrl_xorpsh_interface.cc',
	'cli.cc',
	]

    xorpsh = xorpsh_env.Program(target = 'xorpsh', source = xorpsh_srcs)

    env.Alias('install',
              env.InstallProgram(env['xorp_sbindir'], xorpsh))

if not (env.has_key('disable_profile') and env['disable_profile']):
    ### profiler

    profiler_env = env.Clone()

    profiler_env.AppendUnique(LIBS = [
        'xif_profile',
        'xst_profiler',
        'xorp_finder',
        'xorp_ipc',
        'xorp_comm',
        'xorp_core'
        ])

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

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

        profiler_env.Append(LIBS = ['xorp_core', 'crypto'])
        
    profiler_srcs = [
	'profiler.cc',
	]

    profiler = profiler_env.Program(target = 'xorp_profiler',
                                    source = profiler_srcs)

    env.Alias('install',
              env.InstallProgram(env['xorp_sbindir'], profiler))

    if env['enable_xorpsh']:
        Default(rtrmgr, xorpsh, profiler)
    else:
        Default(rtrmgr, profiler)
else:
    if env['enable_xorpsh']:
        Default(rtrmgr, xorpsh)
    else:
        Default(rtrmgr)
    
