#!/usr/bin/env python
# coding: utf-8

import os

def options(opt):
    opt.load('compiler_c compiler_cxx')
    opt.add_option('--debug', action='store_true', help='"Debug" build', dest='debug')
    opt.add_option('--nodebug', action='store_true', help='Release build (Default)', dest='nodebug')
    opt.add_option('--static', action='store_true', help='Enable static linkage (Default)', dest='static')
    opt.add_option('--nostatic', action='store_true', help='Disable static linkage', dest='nostatic')

def ReorderCompiler(str, complier):
    list = str.split()
    list.remove('msvc')
    list += 'msvc'
    return ' '.join(list)

def configure(conf):
    import waflib.Tools.compiler_c as compiler_c
    import waflib.Tools.compiler_cxx as compiler_cxx
    import waflib.Errors

    conf.env.MSVC_TARGETS = 'x86'

    compiler_cxx.configure(conf)

    conf.env.debug = conf.options.debug and not conf.options.nodebug
    conf.env.static = conf.options.static or not conf.options.nostatic

    conf.env.cshlib_PATTERN = conf.env.cxxshlib_PATTERN = '%s.qdp'

    msvc = 'msvc' in conf.env.CC_NAME
    if not msvc:
        cxxflags = ['-m32', '-march=i686', '-Wall', '-g', '-O3', '--std=gnu++11']
    else:
        cxxflags = ['/Ox', '/Zi', '/FS', '/MT']
    conf.env.append_value('CXXFLAGS', cxxflags)

def build(bld):
    if bld.options.debug and bld.options.nodebug:
        bld.fatal('Both --debug and --nodebug were specified')
    debug = (bld.env.debug or bld.options.debug) and not bld.options.nodebug
    if bld.options.static and bld.options.nostatic:
        bld.fatal('Both --static and --nostatic were specified')
    static = (bld.options.static or bld.env.static) and not bld.options.nostatic
    msvc = 'msvc' in bld.env.CC_NAME

    cflags = bld.env.CFLAGS
    cxxflags = bld.env.CXXFLAGS
    linkflags = []
    defines = []
    includes = []
    if debug:
        defines += ['DEBUG']

    if not msvc:
        cxxflags += ['-fno-exceptions']
        linkflags += ['-Wl,--enable-stdcall-fixup', '-Wl,--shared']
        if not debug:
            cflags += ['-ffunction-sections', '-fdata-sections']
            linkflags += ['-Wl,--gc-sections']
        if static:
            linkflags += ['-static']
            bld.env.SHLIB_MARKER = ''
    else:
        cxxflags += ['/EHsc']
        defines += ['_HAS_EXCEPTIONS=0']
        linkflags += ['/DEBUG']
        if not debug:
            linkflags += ['/OPT:REF', '/OPT:ICF']

    cxxflags += cflags

    src = ['mpqdraft.cpp']
    src = ['src/' + file for file in src]


    if msvc:
        bld.shlib(source=src + ['wmode_binary.cpp'], cflags=cflags0x401964, defines=defines, linkflags=linkflags,
                includes=includes, stlib=['user32'], defs=['mpqd.def'], features='cxx cxxshlib', target='wmode')
    else:
        bld.objects(source=src, cflags=cflags, defines=defines, includes=includes, target='obj')
        bld.shlib(source='wmode.bwl.o', linkflags=linkflags, target='wmode', use='obj',
            defs=['mpqd.def'], features='cxx cxxshlib', install_path=None)

    bld.install_as('${PREFIX}/wmode.qdp', 'wmode.qdp')
