##
# This script includes windows specific config (MSVS/MSVC)
##
Import('env')
import os.path

# Set common flags
if env['CC'] == 'cl':
    if env.get('UWP_APP') == '1':
        # Currently only supports VS2015 (14.0)
        supported_uwp_msvc_versions = ['14.0']
        # If MSVC_VERSION is not supported for UWP, exit on error
        if env.get('MSVC_VERSION') not in supported_uwp_msvc_versions:
            print '\nError: Trying to Build UWP binaries with unsupported Visual Studio version\n'
            Exit(1)

    #  - warning C4133: incompatible type conversion
    env.AppendUnique(CCFLAGS=['/we4133'])

    # Disable the following warnings:
    #  - warning C4127: conditional expression is constant
    #    - Disabled due to the widespread usage in IoTivity
    #  - warning C4200: zero-sized array in struct/union.
    #    - It is an acceptable approach for variable size structs.
    #  - warning C4201: nameless struct/union
    #    - Disabled due to IoTivity not being ANSI compatible
    #  - warning C4204: nonstandard extension used: non-constant aggregate initializer
    #    - Disabled due to IoTivity not being ANSI compatible and this is an appropriate way to intialize
    #      structs for non-legacy compilers.
    #  - warning C4214:  bit field types other than int
    #    - Disabled due to IoTivity not being ANSI compatible
    #  - warning C4221: nonstandard extension used: 'identifier' cannot be initialized using address of automatic variable
    #    - Disabled due to IoTivity not being ANSI compatible
    #  - warning C4232: nonstandard extension used: 'read': address of dllimport 'fread' is not static, identity not guaranteed
    #    - fread, frwrite, etc are provided by the platform and cannot be changed.
    #  - warning C4706: assignment within conditional expression
    #    - Disabled due to the widespread usage in IoTivity and low impact.
    env.AppendUnique(CCFLAGS=['/wd4127', '/wd4200', '/wd4201', '/wd4204', '/wd4214', '/wd4221', '/wd4232', '/wd4706'])

    env.AppendUnique(CCFLAGS=['/EHsc'])

    # Set release/debug flags
    if env.get('RELEASE'):
        env.AppendUnique(CCFLAGS = ['/MD', '/O2', '/GF'])
        env.AppendUnique(CPPDEFINES = ['NDEBUG'])
    else:
        env.AppendUnique(CCFLAGS = ['/MDd', '/Od', '/RTC1'])
        env.AppendUnique(LINKFLAGS = ['/debug'])

    env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])

    # Work around [IOT-1986]
    # During some Windows multi-threaded builds, SCons/Python/Pywin32 appear to try
    # linking with these static libraries while another SCons thread started executing
    # InstallTarget() for this static LIB, but didn't finish yet. That behavior results
    # in linker errors. Work around this issue by linking with the source of InstallTarget(),
    # rather than the target.
    env.PrependUnique(LIBPATH = [os.path.join(env.get('BUILD_DIR'), 'resource', 'src')])
    env.PrependUnique(LIBPATH = [os.path.join(env.get('BUILD_DIR'), 'resource', 'c_common', 'windows')])
    env.PrependUnique(LIBPATH = [os.path.join(env.get('BUILD_DIR'), 'resource', 'oc_logger')])
    env.PrependUnique(LIBPATH = [os.path.join(env.get('BUILD_DIR'), 'resource', 'csdk', 'resource-directory')])

    env.AppendUnique(PATH = os.environ['PATH'])
    env['PDB'] = '${TARGET.base}.pdb'
    env.Append(LINKFLAGS=['/PDB:${TARGET.base}.pdb'])

    # Visual Studio compiler complains that functions like strncpy are unsafe. We
    # are aware that it's possible to create a non-null terminated string using the
    # strncpy function.  However, the str*_s functions are not standard and thus
    # will not work on all systems supported by IoTivity. This will prevent Visual
    # Studio from displaying unwanted warnings.
    # See https://msdn.microsoft.com/en-us/library/ttcz0bys.aspx for more details.
    env.AppendUnique(CPPDEFINES=['_CRT_SECURE_NO_WARNINGS', '_CRT_NONSTDC_NO_WARNINGS'])

    if env.get('UWP_APP') != '1':
        # Add Desktop specific libraries
        env.AppendUnique(LIBS = ['bcrypt', 'ws2_32', 'advapi32', 'iphlpapi', 'crypt32', 'kernel32'])
    else:
        # Add Windows Universal Platform specific libraries and flags
        # Note: We technically should set WINAPI_FAMILY=WINAPI_FAMILY_APP, but cannot
        #       due to [IOT-2312]. All APIs used are store/UWP compatible at this time.
        env.AppendUnique(CPPDEFINES=['UWP_APP', '__WRL_NO_DEFAULT_LIB__'])
        env.AppendUnique(LINKFLAGS=['/MANIFEST:NO', '/WINMD:NO', '/APPCONTAINER'])
        env.AppendUnique(LIBS = ['WindowsApp', 'bcrypt', 'ws2_32', 'iphlpapi', 'crypt32'])

elif env['CC'] == 'gcc':
    print "\nError: gcc not supported on Windows.  Use Visual Studio!\n"
    Exit(1);

