#!/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

    # Custom compiler preference based on c++14 support
    if conf.options.check_c_compiler is None:
        conf.options.check_c_compiler = 'clang gcc msvc'
    if conf.options.check_cxx_compiler is None:
        conf.options.check_cxx_compiler = 'clang++ g++ msvc'

    compiler_c.configure(conf)
    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'

    cflags = ['-m32', '-march=i686', '-Wall', '-g', '-O3', '-fno-tree-loop-optimize']
    conf.env.append_value('CFLAGS', cflags)
    conf.env.append_value('CXXFLAGS', cflags + ['--std=c++11'])

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

    cflags = bld.env.CFLAGS
    cxxflags = bld.env.CXXFLAGS
    linkflags = []
    defines = []
    includes = []
    libs = ['psapi']
    stlibs = []
    libpath = []
    stlibpath = []
    if debug:
        defines += ['DEBUG']
    else:
        cflags += ['-ffunction-sections', '-fdata-sections']
        linkflags += ['-Wl,--gc-sections']
    if static:
        linkflags += ['-static']
        bld.env.SHLIB_MARKER = ''

    if static:
        stlibs = libs
        stlibpath = libpath
        libs = []
        libpath = []

    cxxflags += cflags

    src = bld.path.ant_glob('src/*.cpp')
    exception_files = ['x86', 'patchmanager']
    exception_src = []
    for e in exception_files:
        exception_src += [path for path in src if e in path.srcpath()]
        src = [path for path in src if not e in path.srcpath()]

    bld.shlib(source='', linkflags=linkflags, target='metaplugin', use='obj exception_obj',
        defs=['mpqd.def'], features='cxx cxxshlib', install_path=None, lib=libs, stlib=stlibs,
        libpath=libpath, stlibpath=stlibpath)

    bld.objects(source=src, cflags=cflags, cxxflags=cxxflags + ['-fno-exceptions'], defines=defines, includes=includes, target='obj')
    bld.objects(source=exception_src, cflags=cflags, cxxflags=cxxflags, defines=defines, includes=includes, target='exception_obj')

    bld(rule='objcopy -S ${SRC} ${TGT}', source='metaplugin.qdp', target='metaplugin_stripped.qdp')

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