@#
@#  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: dmars.empy 1020 2007-03-05 13:52:29Z vaclavslavik $
@#
@{
# Order make variables so that vars used to define values of other vars
# are before the vars dependent on them:
#
# FIXME: this algorithm is O(n^2), it can easily be O(n*m) where m
#        is max number of references to other variables in var definition

def orderByDeps(vars):
    d = {}
    for v in make_vars:
        d[v] = []
    for v in make_vars:
        ref = '$(%s)' % v.name
        for v2 in make_vars:
            if v2.value.find(ref) != -1:
                d[v2].append(v)

    def stream(list, v):
        if d[v] != None:
            for u in d[v]:
                stream(list, u)
            d[v] = None
            list.append(v)

    list = []
    for v in make_vars:
        stream(list, v)
    return list
}@
@empy.include(os.path.join(RULESDIR, '_hdr_makefile.empy'))
@[if OPTIONS_FILE != '']include @OPTIONS_FILE
@[end if]@
@[if OPTIONS_FILE == '']@
@empy.include(os.path.join(RULESDIR, 'dmars-opts.empy'))
@[end if]@

# -------------------------------------------------------------------------
# Do not modify the rest of this file!
# -------------------------------------------------------------------------

### Variables: ###

@[for v in orderByDeps(make_vars)]@
@utils.wrapLongLine('%s = ' % v.name, v.value, ' \\', variable=v.name)
@[end for]@

### Targets: ###
@[for t in targets]
@t.__targetdir@t.__targetname : @t.__deps
@[for cmd in t.__command.splitlines()]@
@('\t'+cmd.strip())
@[end for]@
@[end for]@

@[for f in fragments]
@f.content
@[end for]@
