view codeOptimizer.py @ 3:f65c2d63ab66

Rename Options to Config
author Tom Fredrik Blenning Klaussen <bfg@blenning.no>
date Sun, 16 Sep 2012 21:31:41 +0200
parents 10f1d7de9bc3
children 3a56cd936c59
line wrap: on
line source

#!/usr/bin/env python -O

import sys

from Compilable import Compilable
from DepGraph import DepGraph
from Config import Config



def isHppfile(name):
    if name.endswith(".hpp"):
        return True
    if name.endswith(".h"):
        return True
    return False

def isCppfile(name):
    if name.endswith(".cpp"):
        return True
    return False

try:
    options = Config(sys.argv[1])
except:
    print sys.argv[1] + ' is not a valid xml file'
infiles = sys.argv[2:]

files = {}
unknown = []

for file in infiles:
    if isHppfile(file) or isCppfile(file):
        c = Compilable(file)
        c.setFlags(options.getCxxflags(file))
        files[file] = c
    else:
        unknown.append(file)

if len(unknown) > 0:
    str = ", "
    raise SystemExit(str.join(unknown)  + " are of unknown filetype")

depgraph = DepGraph()

for file in files:
    depgraph.add(files[file])

for file in files:
    depgraph.addDependency(files[file], files[file].dependencies())

files = depgraph.directedGraph()

for file in files:
    if not file.worksWithoutModifications():
        print file.path
        raise SystemExit(file.path  + " does not compile at all")

for file in files:
    removable = file.removeRemovableIncludes()
    if removable:
        print 'Removable lines in ' + file.path
        for r in removable:
            print str(r) + ' : ' + file.lines[r].rstrip()

    replacable = file.replaceIncludes(dict.fromkeys(removable, '\n'))
    if replacable:
        print 'Replacable lines in ' + file.path
        for r in replacable:
            print str(r) + ' : ' + file.lines[r].rstrip()