<?xml version="1.0" ?>
<!--

  This file is part of Bakefile (http://www.bakefile.org)

  Copyright (C) 2003-2007 Vaclav Slavik

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to
  deal in the Software without restriction, including without limitation the
  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  sell copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  IN THE SOFTWARE.

  $Id: msevc4prj.bkl 1332 2009-12-16 16:20:41Z vaclavslavik $

-->
<makefile>
    
    <!-- ============================================================== -->
    <!--                      CPUs configuration                        -->
    <!-- ============================================================== -->

    <!-- Set this variable to comma-separated list of CPUs you want to
         target in generated project files. x86 emulator is always
         included. Supported CPU types are listed in variable EVC_ALL_CPUS.
         Special value "all" will include all supported CPUs.
    -->
    <set var="EVC_CPUS">all</set>

    <!-- All supported CPUs: -->
    <set var="EVC_ALL_CPUS">
        ARM,ARMV4,ARMV4I,ARMV4T,MIPS16,MIPSII,MIPSII_FP,MIPSIV,MIPSIV_FP,SH3,SH4,x86
    </set>


    <!-- This code ensures that configurations for all CPUs are made: -->
    <set var="_cpu_option_vals">
        <if cond="EVC_CPUS=='all'">$(EVC_ALL_CPUS),emulator</if>
        <if cond="EVC_CPUS!='all'">$(EVC_CPUS),emulator</if>
    </set>
    <set var="_cpu_option_desc">
        $(','.join(['(WCE %s)' % x for x in _cpu_option_vals.split(',')]))
    </set>    
    <option name="EVC_CPU">
        <values>$(_cpu_option_vals)</values>
        <values-description>$(_cpu_option_desc)</values-description>
    </option>

    <set var="CPU">$(substitute(EVC_CPU,lambda x: x.lower()))</set>

    <template id="_evc_config">
        <!-- CPU_ID values from .vcp files for given CPU: -->
        <set var="_CPU_ID">
            $(substituteFromDict(EVC_CPU,{
                'ARM'       : '{D6518FFC-710F-11D3-99F2-00105A0DF099}',
                'ARMV4'     : '{ECBEA43D-CD7B-4852-AD55-D4227B5D624B}',
                'ARMV4I'    : '{DC70F430-E78B-494F-A9D5-62ADC56443B8}',
                'ARMV4T'    : '{F52316A9-3B7C-4FE7-A67F-68350B41240D}',
                'MIPS16'    : '{D6519013-710F-11D3-99F2-00105A0DF099}',
                'MIPSII'    : '{689DDC64-9D9D-11D5-96F8-00207802C01C}',
                'MIPSII_FP' : '{D8AC856C-B213-4895-9E83-9EC51A55201E}',
                'MIPSIV'    : '{0B2FE524-26C5-4194-8CEF-B1582DEB5A98}',
                'MIPSIV_FP' : '{046A430D-7770-48AB-89B5-24C2D300B03F}',
                'SH3'       : '{D6519020-710F-11D3-99F2-00105A0DF099}',
                'SH4'       : '{D6519021-710F-11D3-99F2-00105A0DF099}',
                'x86'       : '{D6518FF3-710F-11D3-99F2-00105A0DF099}',
                'emulator'  : '{32E52003-403E-442D-BE48-DE10F8C6131D}',
            }))
        </set>

        <!-- compiler executable for given CPU: -->
        <set var="_COMPILER">
            $(substituteFromDict(EVC_CPU,{
                'ARM'       : 'clarm.exe',
                'ARMV4'     : 'clarm.exe',
                'ARMV4I'    : 'clarm.exe',
                'ARMV4T'    : 'clthumb.exe',
                'MIPS16'    : 'clmips.exe',
                'MIPSII'    : 'clmips.exe',
                'MIPSII_FP' : 'clmips.exe',
                'MIPSIV'    : 'clmips.exe',
                'MIPSIV_FP' : 'clmips.exe',
                'SH3'       : 'shcl.exe',
                'SH4'       : 'shcl.exe',
                'x86'       : 'cl.exe',
                'emulator'  : 'cl.exe',
            }))
        </set>

        <!-- Typecodes: -->

        <set var="_MSVC_TYPECODE_CONSOLE">ERROR_NOT_SUPPORTED</set>
        <set var="_MSVC_TYPECODE_GUI">
            $(substituteFromDict(EVC_CPU,{
                'ARM'       : '0x8501',
                'ARMV4'     : '0xa301',
                'ARMV4I'    : '0xa501',
                'ARMV4T'    : '0xa401',
                'MIPS16'    : '0x8901',
                'MIPSII'    : '0xa101',
                'MIPSII_FP' : '0xa201',
                'MIPSIV'    : '0x9601',
                'MIPSIV_FP' : '0x9201',
                'SH3'       : '0x8101',
                'SH4'       : '0x8601',
                'x86'       : '0x8301',
                'emulator'  : '0xa601',
            }))
        </set>
        
        <set var="_MSVC_TYPECODE_DLL">
            $(substituteFromDict(EVC_CPU,{
                'ARM'       : '0x8502',
                'ARMV4'     : '0xa302',
                'ARMV4I'    : '0xa502',
                'ARMV4T'    : '0xa402',
                'MIPS16'    : '0x8902',
                'MIPSII'    : '0xa102',
                'MIPSII_FP' : '0xa202',
                'MIPSIV'    : '0x9602',
                'MIPSIV_FP' : '0x9202',
                'SH3'       : '0x8102',
                'SH4'       : '0x8602',
                'x86'       : '0x0302',
                'emulator'  : '0xa602',
            }))
        </set>
        
        <set var="_MSVC_TYPECODE_LIB">
            $(substituteFromDict(EVC_CPU,{
                'ARM'       : '0x8504',
                'ARMV4'     : '0xa304',
                'ARMV4I'    : '0xa504',
                'ARMV4T'    : '0xa404',
                'MIPS16'    : '0x8904',
                'MIPSII'    : '0xa104',
                'MIPSII_FP' : '0xa204',
                'MIPSIV'    : '0x9604',
                'MIPSIV_FP' : '0x9204',
                'SH3'       : '0x8104',
                'SH4'       : '0x8604',
                'x86'       : '0x8304',
                'emulator'  : '0xa604',
            }))
        </set>
        
        <!-- CPU-specific flags: -->
        <set var="_CPU_DEFINES">
            $(substituteFromDict(EVC_CPU,{
            'ARM'       : '/D "ARM" /D "_ARM_"',
            'ARMV4'     : '/D "ARM" /D "_ARM_" /D "ARMV4"',
            'ARMV4I'    : '/D "ARM" /D "_ARM_" /D "ARMV4I"',
            'ARMV4T'    : 
                '/D "ARM" /D "_ARM_" /D "THUMB" /D "_THUMB_" /D "ARMV4T"',
            'MIPS16'    : 
                '/D "MIPS" /D "_MIPS_" /D "R4000" /D "MIPSII" /D "MIPS16" /D "_MIPS16_" /D "MIPS16SUPPORT"',
            'MIPSII'    : '/D "MIPS" /D "_MIPS_" /D "R4000" /D "MIPSII"',
            'MIPSII_FP' : 
                '/D "MIPS" /D "_MIPS_" /D "R4000" /D "MIPSII" /D "MIPSII_FP"',
            'MIPSIV'    : 
                '/D "MIPS" /D "_MIPS_" /D "_MIPS64" /D "R4000" /D "MIPSIV"',
            'MIPSIV_FP' : 
                '/D "MIPS" /D "_MIPS_" /D "_MIPS64" /D "R4000" /D "MIPSIV" /D "MIPSIV_FP"',
            'SH3'       : '/D "SHx" /D "SH3" /D "_SH3_"',
            'SH4'       : '/D "SHx" /D "SH4" /D "_SH4_"',
            'x86'       : '/D "_i386_" /D "i_386_" /D "_X86_" /D "x86"',
            'emulator'  : '/D "i_386_" /D "_X86_" /D "x86"',
            }))
        </set>
        <set var="_CPU_CFLAGS">
            $(substituteFromDict(EVC_CPU,{
            'ARM'       : '',
            'ARMV4'     : '',
            'ARMV4I'    : '/QRarch4T /QRinterwork-return',
            'ARMV4T'    : '/QRarch4T /QRinterwork-return',
            'MIPS16'    : '/QMmips16',
            'MIPSII'    : '/QMmips2 /QMFPE',
            'MIPSII_FP' : '/QMmips2 /QMFPE-',
            'MIPSIV'    : '/QMmips4 /QMn32 /QMFPE',
            'MIPSIV_FP' : '/QMmips4 /QMn32 /QMFPE-',
            'SH3'       : '',
            'SH4'       : '/Qsh4',
            'x86'       : '/Gs8192 /GF',
            'emulator'  : '/Gs8192 /GF',
            }))
        </set>
        <set var="_CPU_LDFLAGS">
            $(substituteFromDict(EVC_CPU,{
            'ARM'       : '/align:"4096" /MACHINE:ARM',
            'ARMV4'     : '/align:"4096" /MACHINE:ARM',
            'ARMV4I'    : '/MACHINE:THUMB',
            'ARMV4T'    : '/MACHINE:THUMB',
            'MIPS16'    : '/MACHINE:MIPS16 /ALIGN:4096',
            'MIPSII'    : '/MACHINE:MIPS',
            'MIPSII_FP' : '/MACHINE:MIPS',
            'MIPSIV'    : '/MACHINE:MIPSFPU',
            'MIPSIV_FP' : '/MACHINE:MIPSFPU',
            'SH3'       : '/MACHINE:SH3 /align:4096',
            'SH4'       : '/MACHINE:SH4',
            'x86'       : '$(DOLLAR)(CEx86Corelibc) /MACHINE:IX86 /nodefaultlib:"OLDNAMES.lib"',
            'emulator'  : '$(DOLLAR)(CEx86Corelibc) /MACHINE:IX86 /nodefaultlib:"OLDNAMES.lib"',
            }))
        </set>

        <!-- CRT flags are different for CPUs, too: -->
        <set var="_CPU_CRT_DBG">
            $(substituteFromDict(EVC_CPU,{
            'ARM'       : '/M$(DOLLAR)(CECrtMTDebug)',
            'ARMV4'     : '/M$(DOLLAR)(CECrtMTDebug)',
            'ARMV4I'    : '/M$(DOLLAR)(CECrtMTDebug)',
            'ARMV4T'    : '/M$(DOLLAR)(CECrtMTDebug)',
            'MIPS16'    : '/M$(DOLLAR)(CECrtMTDebug)',
            'MIPSII'    : '/M$(DOLLAR)(CECrtMTDebug)',
            'MIPSII_FP' : '/M$(DOLLAR)(CECrtMTDebug)',
            'MIPSIV'    : '/M$(DOLLAR)(CECrtMTDebug)',
            'MIPSIV_FP' : '/M$(DOLLAR)(CECrtMTDebug)',
            'SH3'       : '/M$(DOLLAR)(CECrtMTDebug)',
            'SH4'       : '/M$(DOLLAR)(CECrtMTDebug)',
            'x86'       : '',
            'emulator'  : '',
            }))
        </set>
        <set var="_CPU_CRT_REL">
            $(substituteFromDict(EVC_CPU,{
            'ARM'       : '/M$(DOLLAR)(CECrtMT)',
            'ARMV4'     : '/M$(DOLLAR)(CECrtMT)',
            'ARMV4I'    : '/M$(DOLLAR)(CECrtMT)',
            'ARMV4T'    : '/M$(DOLLAR)(CECrtMT)',
            'MIPS16'    : '/M$(DOLLAR)(CECrtMT)',
            'MIPSII'    : '/M$(DOLLAR)(CECrtMT)',
            'MIPSII_FP' : '/M$(DOLLAR)(CECrtMT)',
            'MIPSIV'    : '/M$(DOLLAR)(CECrtMT)',
            'MIPSIV_FP' : '/M$(DOLLAR)(CECrtMT)',
            'SH3'       : '/M$(DOLLAR)(CECrtMT)',
            'SH4'       : '/M$(DOLLAR)(CECrtMT)',
            'x86'       : '',
            'emulator'  : '',
            }))
        </set>

    </template>

    
    <!-- ============================================================== -->
    <!--                           Other code                           -->
    <!-- ============================================================== -->
    
    <set var="COMPILER">evc</set>

    <set var="_MSVC_CPU_NAME">WCE $(EVC_CPU)</set>
 
    <template id="_compiled_baset" template="_evc_config">
        
        <set var="_default_builddir" eval="0">
            $(substituteFromDict(_debug,{'0':EVC_CPU+'Rel','1':EVC_CPU+'Dbg'}))
        </set>
        
        <set var="_defines"/>
        <set var="_ldflags">
            /base:"0x00010000" /stack:0x10000,0x1000 /nodefaultlib:"$(DOLLAR)(CENoDefaultLib)" /subsystem:$(DOLLAR)(CESubsystem) $(_CPU_LDFLAGS)
        </set>
        <set var="_ldlibs">coredll.lib</set>
        <set var="_win32rc_flags">/l 0x409</set>
        <set var="_cppflags"/>

        <define>UNDER_CE=$(DOLLAR)(CEVersion)</define>
        <define>_WIN32_WCE=$(DOLLAR)(CEVersion)</define>
        <!-- Windows CE is Unicode-only OS: -->
        <define>UNICODE</define>
        <define>_UNICODE</define>
        <define>$(DOLLAR)(CePlatform)</define>
        
        <cppflags>$(_CPU_DEFINES) $(_CPU_CFLAGS)</cppflags>
        <set var="_win32rc_flags" append="1">
            $(_CPU_DEFINES.replace('/D','/d'))
        </set>

        <debug-runtime-libs>off</debug-runtime-libs>
    </template>

    <define-rule name="_compiled_specific">
        <define-tag name="debug-info">
            <cppflags>
                $(substituteFromDict(value, {'on':'/Zi', 'off':''}))
            </cppflags>
            <!-- NB: rest of handling is done in msvc_prj_common.bkl -->
        </define-tag>

        <define-tag name="debug-runtime-libs">
            <define>
                $(substituteFromDict(value, {'on':'DEBUG', 'off':''}))
            </define>
            <cppflags>
                $(substituteFromDict(value,
                                     {'on':_CPU_CRT_DBG,'off':_CPU_CRT_REL}))
            </cppflags>
            <!-- See bug #212:
                 Specifying /debug to the linker not only enables PDB
                 generation (which is what we want) but also automatically
                 enables incremental linking and disables all linking
                 optimizations. To correct this we need to add "/opt:ref
                 /opt:icf" to cl command line (but only if debug runtime
                 is not used). -->
            <set var="_extra_debug_flags">
                $(substituteFromDict(value, {'on':'', 'off':'/opt:ref /opt:icf'}))
            </set>
        </define-tag>

        <!-- eVC's RTTI requires additional library and exceptions require
             RTTI. See here for more information:
             http://sourceforge.net/tracker/index.php?func=detail&aid=1086311&group_id=83016&atid=568029
             http://support.microsoft.com/default.aspx?scid=kb;en-us;830482
          -->
        <define-tag name="cxx-rtti">
            <sys-lib>
                $(substituteFromDict(value, {'on':'ccrtrtti','off':''}))
            </sys-lib>
        </define-tag>
        
        <define-tag name="runtime-libs"/>
        <define-tag name="threading"/>

    </define-rule>
    
    
    <template id="_exe_baset">
        <ldflags>/entry:"WinMainCRTStartup"</ldflags>
    </template>
    
    <template id="_dll_baset">
        <ldflags>/entry:"_DllMainCRTStartup"</ldflags>
    </template>

    <!-- Load common code and run it: -->
    <include file="msvc_prj_dsp_common.bkl"/>

    <!-- Windows CE can't do console applications: -->
    <define-tag name="app-type" rules="exe">
        <if cond="value=='console'">
            <error>Windows CE doesn't support console applications.</error>
        </if>
    </define-tag>

    <!-- Output the projects: -->
    <output file="$(OUTPUT_FILE)" writer="msevc4prj.py"/>

</makefile>
