# Copyright AllSeen Alliance. All rights reserved.
#
#    Permission to use, copy, modify, and/or distribute this software for any
#    purpose with or without fee is hereby granted, provided that the above
#    copyright notice and this permission notice appear in all copies.
#
#    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
#    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
#    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
#    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
#    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
#    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
#    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

import os
Import('env')

# Indicate that this SConscript file has been loaded already
env['_ALLJOYNCORE_'] = True

# Header files in common require "dist/cpp/inc/alljoyn" in the include path to find Status.h
env.Append(CPPPATH = ['$DISTDIR/cpp/inc/alljoyn'])

# Dependent Projects
common_hdrs, common_static_objs, common_shared_objs = env.SConscript(['../common/SConscript'])


# Make alljoyn C++ dist a sub-directory of the alljoyn dist.
env['CPP_DISTDIR'] = env['DISTDIR'] + '/cpp'
env['CPP_TESTDIR'] = env['TESTDIR'] + '/cpp'

env['OBJDIR_ALLJOYN_CORE'] = env['OBJDIR'] + '/alljoyn_core'

ajenv = env.Clone()

# manually add dependencies for xml to h, and for files included in the xml
ajenv.Depends('$OBJDIR_ALLJOYN_CORE/Status.h', 'src/Status.xml')
ajenv.Depends('$OBJDIR_ALLJOYN_CORE/Status.h', '../common/src/Status.xml')
ajenv.Depends('$OBJDIR_ALLJOYN_CORE/Status.h', '../build_core/tools/bin/make_status.py')
ajenv.Append(STATUS_FLAGS=['--base=%s' % os.getcwd()])

# Add support for multiple build targets in the same workset
ajenv.VariantDir('$OBJDIR_ALLJOYN_CORE', 'src', duplicate = 0)
ajenv.VariantDir('$OBJDIR_ALLJOYN_CORE/test', 'test', duplicate = 0)
ajenv.VariantDir('$OBJDIR_ALLJOYN_CORE/router', 'router', duplicate = 0)
ajenv.VariantDir('$OBJDIR_ALLJOYN_CORE/samples', 'samples', duplicate = 0)

# AllJoyn Install
ajenv.Install('$OBJDIR_ALLJOYN_CORE', ajenv.File('src/Status.xml'))
ajenv.Status('$OBJDIR_ALLJOYN_CORE/Status')
core_headers = ajenv.Install('$CPP_DISTDIR/inc/alljoyn', '$OBJDIR_ALLJOYN_CORE/Status.h')

core_headers += ajenv.Install('$CPP_DISTDIR/inc/alljoyn', [ h for h in ajenv.Glob('inc/alljoyn/*.h') if h not in ajenv.Glob('inc/alljoyn/Status*.h') ])

for d, h in common_hdrs.items():
    core_headers += ajenv.Install('$CPP_DISTDIR/inc/%s' % d, h)

# Header file includes
#ajenv.Append(CPPPATH = [ajenv.Dir('$CPP_DISTDIR/inc'), ajenv.Dir('$CPP_DISTDIR/inc/alljoyn')])

# Make private headers available
ajenv.Append(CPPPATH = [ajenv.Dir('src')])

# AllJoyn Libraries
libs, static_objs, shared_objs = ajenv.SConscript('$OBJDIR_ALLJOYN_CORE/SConscript', exports = ['ajenv', 'common_static_objs', 'common_shared_objs'])

ajenv.Install('$CPP_DISTDIR/lib', libs)

env.Prepend(LIBS = 'alljoyn')
daemon_progs, ajrlib, srobj, router_objs = ajenv.SConscript('$OBJDIR_ALLJOYN_CORE/router/SConscript')
ajenv.Install('$CPP_DISTDIR/bin', daemon_progs)
ajenv.Install('$CPP_DISTDIR/lib', ajrlib)
router_lib = 'ajrouter'

# The global env needs the 'ajrlib' for the Java binding
env['ajrlib'] = router_lib
env['srobj'] = srobj
env['router_objs'] = router_objs

# Always build C++ version of AboutService
# ajrlib must be set before calling the SConscripts responsible for
# building the AboutService.

about_env = env.Clone()
about_env['OBJDIR_ABOUT'] = env['OBJDIR'] + '/about'
about_env.VariantDir('$OBJDIR_ABOUT/cpp', '../services/about/cpp', duplicate = 0)
about_env.SConscript('$OBJDIR_ABOUT/cpp/SConscript', exports = ['about_env'])

# Always build C++ version of ConfigService
# ajrlib must be set before calling the SConscripts responsible for
# building the ConfigService.

config_env = env.Clone()
config_env['OBJDIR_CONFIG'] = env['OBJDIR'] + '/config'
config_env.VariantDir('$OBJDIR_CONFIG/cpp', '../services/config/cpp', duplicate = 0)
config_env.SConscript('$OBJDIR_CONFIG/cpp/SConscript', exports = ['config_env'])

# only include command line samples, unit test, test programs if we are not
# building for iOS. No support on iOS for command line applications.
if env['OS'] == 'darwin' and env['CPU'] in ['arm', 'armv7', 'armv7s', 'arm64',]:
    progs = []
else:
    # Test programs
    progs = env.SConscript('$OBJDIR_ALLJOYN_CORE/test/SConscript')
    ajenv.Install('$CPP_DISTDIR/bin', progs[0])
    ajenv.Install('$TESTDIR/cpp/bin', progs[1])

    # Build unit Tests
    env.SConscript('unit_test/SConscript', variant_dir='$OBJDIR_ALLJOYN_CORE/unittest', duplicate = 0)

    # Sample programs
    env.SConscript('$OBJDIR_ALLJOYN_CORE/samples/SConscript')

# Release notes and misc. legals
if ajenv['OS_CONF'] == 'darwin':
    if ajenv['CPU'] == 'x86':
        ajenv.InstallAs('$DISTDIR/README.txt', 'docs/README.darwin.txt')
elif ajenv['OS_CONF'] == 'windows':
    ajenv.InstallAs('$DISTDIR/README.txt', 'docs/README.windows.txt')
elif ajenv['OS_CONF'] == 'android':
    ajenv.InstallAs('$DISTDIR/README.txt', 'docs/README.android.txt')
else:  # linux based platforms
    ajenv.InstallAs('$DISTDIR/README.txt', 'docs/README.linux.txt')

if not (ajenv['OS'] == 'darwin' and ajenv['CPU'] in ['arm', 'armv7', 'armv7s', 'arm64',]):
    ajenv.Install('$DISTDIR', 'docs/ReleaseNotes.txt')

# Build docs
installDocs = ajenv.SConscript('docs/SConscript', exports = ['ajenv', 'core_headers'])
