make.py
author Benjamin Smedberg <benjamin@smedbergs.us>
Fri, 06 Feb 2009 13:51:37 -0500
changeset 51 8c332a8bae92fde914da88ffba14bc327894be2a
parent 50 b046136650d1f5775ad17fddb6e10fecef220854
child 53 3e2e23603c36b4e2274a5d50a213694a9c3eb647
permissions -rwxr-xr-x
Two-part change. I should have done these separately but forgot to commit after the first change: * Don't build non-phony targets which exist, have rules, but don't have prerequisites. This was just a plain logic error. Also as part of this change, make sure all rules have locations so that we can identify particular rules in warning and error message. * Implement conditionals.

#!/usr/bin/env python

"""
make.py

A drop-in or mostly drop-in replacement for GNU make.
"""

import os, subprocess, sys, logging
from optparse import OptionParser
from pymake.data import Makefile, DataError
from pymake.parser import parsestream, parsecommandlineargs, SyntaxError

op = OptionParser()
op.add_option('-f', '--file', '--makefile',
              action='append',
              dest='makefiles',
              default=[])
op.add_option('-v', '--verbose',
              action="store_true",
              dest="verbose", default=True)

options, arguments = op.parse_args()

if options.verbose:
    logging.basicConfig(level=logging.DEBUG)

m = Makefile()
if len(options.makefiles) == 0:
    if os.path.exists('Makefile'):
        options.makefiles.append('Makefile')
    else:
        print "No makefile found"
        sys.exit(2)

try:
    targets = parsecommandlineargs(m, arguments)

    for f in options.makefiles:
        parsestream(open(f), f, m)

    m.finishparsing()

    if len(targets) == 0:
        if m.defaulttarget is None:
            raise Error("No target specified and no default target found.")
        targets = [m.defaulttarget]

    tlist = [m.gettarget(t) for t in targets]
    for t in tlist:
        t.resolvedeps(m, [], [])
    for t in tlist:
        t.make(m)
except (DataError, SyntaxError, subprocess.CalledProcessError), e:
    print e
    sys.exit(2)