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

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

  Copyright (C) 2003-2008 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: makefile_defs_dmars_common.bkl 1190 2008-01-28 10:09:13Z vaclavslavik $

-->
<makefile>

    <!-- the value in win32.bkl is too high for DMC: -->
    <set var="LIB_PAGESIZE" overwrite="0">512</set>

    <include file="win32.bkl"/>

    <set var="__DEFINE_ARG">-D</set>
    <set var="__INCLUDE_ARG">-I</set>
    <set var="__LIB_ARG">%s.lib</set>

    <set var="DMARS_MEM_POOL_SIZE">99</set>
    <set var="_DMARS_MEM_POOL_FLAG" eval="0">-HP$(DMARS_MEM_POOL_SIZE)</set>

    <set var="__DEFAULT_CC">dmc</set>
    <set var="__DEFAULT_CXX">dmc</set>
    <set var="__DEFAULT_CPPFLAGS"></set>
    <set var="__DEFAULT_CFLAGS">$(_DMARS_MEM_POOL_FLAG)</set>
    <set var="__DEFAULT_CXXFLAGS">$(_DMARS_MEM_POOL_FLAG)</set>
    <set var="__DEFAULT_LDFLAGS"></set>

    <set var="__LINKER">link /NOLOGO /SILENT /NOI /DELEXECUTABLE</set>
    <set var="__LINKER_CC">$(__LINKER) /EXETYPE:NT</set>
    <set var="__LINKER_CXX">$(__LINKER) /EXETYPE:NT</set>
    <set var="__DLL_LINKER_CC">$(__LINKER)</set>
    <set var="__DLL_LINKER_CXX">$(__LINKER)</set>
    <set var="__MODULE_LINKER_CC">$(__DLL_LINKER_CC)</set>
    <set var="__MODULE_LINKER_CXX">$(__DLL_LINKER_CXX)</set>

    <!-- basic compiler/linker flags: -->
    <set var="__FLAG_EXE_CONSOLE"></set>
    <set var="__FLAG_EXE_GUI">/su:windows:4.0</set>
    <set var="__FLAG_DEBUG_ON">-g</set>
    <set var="__FLAG_DEBUG_ON_LINKER">/DEBUG /CODEVIEW</set>
    <set var="__FLAG_DEBUG_OFF"></set>
    <set var="__FLAG_DEBUG_OFF_LINKER"></set>
    <set var="__FLAG_OPTIMIZE_OFF">-o+none</set>
    <set var="__FLAG_OPTIMIZE_SIZE">-o+space</set>
    <set var="__FLAG_OPTIMIZE_SPEED">-o</set>
    <set var="__FLAG_ARCH_I386">-3</set>
    <set var="__FLAG_ARCH_I486">-4</set>
    <set var="__FLAG_ARCH_I586">-5</set>
    <set var="__FLAG_ARCH_I686">-6</set>
    <set var="__FLAG_SINGLE_THREADING_LD"/>
    <set var="__FLAG_MULTI_THREADING_LD"/>
    <set var="__FLAG_SINGLE_THREADING_CPP"/>
    <set var="__FLAG_MULTI_THREADING_CPP"/>
    <set var="__FLAG_WARNINGS_NO">-w</set>
    <set var="__FLAG_WARNINGS_DEFAULT"/>
    <set var="__FLAG_WARNINGS_MAX">-w-</set>

    <set var="__FLAG_RTTI_ON">-Ar</set>
    <set var="__FLAG_RTTI_OFF"></set>
    <set var="__FLAG_EXCEPTIONS_ON">-Ae</set>
    <set var="__FLAG_EXCEPTIONS_OFF"></set>

    <!-- rules building blocks: -->

    <template id="__commands_templ_C">
        <set var="__COMPILE_C_CMD" eval="0">
            $(CC) -mn -c -o$@ $(__cflags) $(src)
        </set>
        <!-- FIXME: temporary hack, $(src) should be used only if there are
                    object-dependencies, otherwise $** should be used -->
    </template>

    <template id="__commands_templ_CXX">
        <set var="__COMPILE_CXX_CMD" eval="0">
            $(CXX) -mn -c -cpp -o$@ $(__cxxflags) $(src)
        </set>
        <!-- FIXME: temporary hack, $(src) should be used only if there are
                    object-dependencies, otherwise $** should be used -->
    </template>

    <define-tag name="runtime-libs" rules="exe,dll,module,lib">
        <cppflags>
            $(substituteFromDict(value, {'static':'', 'dynamic':'-ND'}))
        </cppflags>
    </define-tag>

    <define-tag name="app-type" rules="exe">
        <cppflags>
            $(substituteFromDict(value, {'console':'', 'gui':'-WA'}))
        </cppflags>
    </define-tag>

    <define-tag name="lib-path" rules="exe,dll,module">
        <set var="__ldlibs" append="1">$(nativePaths(value))\</set>
    </define-tag>

    <set var="__exe_baser">__dmars_comp</set>
    <set var="__dll_baser">__dmars_dll</set>
    <set var="__lib_baser">__dmars_comp</set>


    <!-- Precompiled headers support: -->
    <tag-info name="precomp-headers-header"
              position="before:precomp-headers-file"/>
    <tag-info name="precomp-headers-header"
              position="before:precomp-headers"/>
    <tag-info name="precomp-headers-file"
              position="before:precomp-headers"/>
    <tag-info name="precomp-headers-location"
              position="before:precomp-headers"/>

    <define-rule name="__dmars_comp">
        <template>
            <set var="__pch_header"/>
            <set var="__pch_location">$(nativePaths(SRCDIR))$(DIRSEP)</set>
            <set var="__pch_file">hdr</set>
        </template>

        <define-tag name="precomp-headers-file" rules="__compiled">
            <set var="__pch_file">$(value)</set>
        </define-tag>

        <define-tag name="precomp-headers-location" rules="__compiled">
            <set var="__pch_location">
                $(nativePaths(SRCDIR))$(DIRSEP)$(nativePaths(value))$(DIRSEP)
            </set>
        </define-tag>

        <define-tag name="precomp-headers-header" rules="__compiled">
            <set var="__pch_header">$(value)</set>
        </define-tag>

        <define-tag name="precomp-headers">
            <if cond="value=='on'">
                <if cond="__pch_header==''">
                    <error>
                    Precompiled header must explicitly be provided using &lt;precomp-headers-header&gt; for DMC.
                    </error>
                </if>

                <!--
                 DMC generates both .sym file with pch data and .obj file
                 with code for the header; this object file must be linked in:
                 -->
                <set var="__pch_sym_tgt">pch_$(__pch_file).sym</set>
                <set var="__pch_sym_file">
                    $(BUILDDIR)$(DIRSEP)$(__pch_sym_tgt)
                </set>

                <!-- FIXME: this is a hack in absense of PCH exclusion:
                     it should be cppflags and there should be a way to
                     exclude certain files from using PCH -->
                <cxxflags>
                    $(substituteFromDict(value,
                            {'on':'-H -HO- -HH$(__pch_sym_file)',
                             'off':''}))
                </cxxflags>

                <!-- FIXME: the part that includes -cpp below is only correct
                     when the PCH file will be used _only_ by C++; real solution
                     would be (for all formats) to have different PCH files
                     for different languages, one per language -->
                <set var="__pch_cmd" eval="0">
                    $(ifthenelse(__uses_cxx,CXX,CC)+' '+ifthenelse(__uses_cxx,
                                 '$(%s_CXXFLAGS)' % id.upper(),
                                 '$(%s_CFLAGS)' % id.upper())) -mn $(ifthenelse(__uses_cxx,'-cpp','')) -HF$(__pch_sym_file) $(__pch_location)$(nativePaths(__pch_header))
                </set>

                <if cond="__pch_sym_tgt not in targets">
                    <add-target target="$(__pch_sym_tgt)" type="action"/>
                    <modify-target target="$(__pch_sym_tgt)">
                        <set var="__targetname">$(__pch_sym_file)</set>
                        <command>
                            if not exist $(BUILDDIR) mkdir $(BUILDDIR)
                            $(ref("__pch_cmd",id))
                        </command>
                        <depends-on-file>
                            $(__pch_location)$(nativePaths(__pch_header))
                        </depends-on-file>
                    </modify-target>
                </if>

                <objects-depend>$(__pch_sym_tgt)</objects-depend>
            </if> <!-- value=on -->
        </define-tag>

    </define-rule>

    <define-rule name="__dmars_dll" extends="__dmars_comp">
        <template>
            <cppflags>-WD</cppflags>
            <sys-lib>kernel32</sys-lib>
        </template>
    </define-rule>

    <template id="__commands_templ">
        <set var="__win32res"/>
        <set var="__mapfile" eval="0">$(__targetdir)$(__name).map</set>
        <set var="__LINK_EXE_CMD" eval="0">
            $(__linker) $(__ldflags_all) $(__objects_var),$@,$(__mapfile),$(__ldlibs),,$(__win32res)
        </set>
        <set var="__LINK_LIB_CMD" eval="0">
            lib -p$(LIB_PAGESIZE) -c -n $@ $(__objects_var)
        </set>
        <set var="__LINK_DLL_CMD" eval="0">
            $(__dll_linker) $(__ldflags_all) /IMPLIB:$(__targetdir)$(__libname).lib $(__objects_var),$@,$(__mapfile),$(__ldlibs),,$(__win32res)
        </set>
        <set var="__LINK_MODULE_CMD" eval="0">
            $(__module_linker) $(__ldflags_all) $(__objects_var),$@,$(__mapfile),$(__ldlibs),,$(__win32res)
        </set>
    </template>

</makefile>
