diff options
-rwxr-xr-x | deheader | 28 |
1 files changed, 16 insertions, 12 deletions
@@ -4,6 +4,7 @@ deheader -- find (optionally remove) unneeded includes in C or C++ sourcefiles. Usage: deheader [-h] [-v] [-r] [-i str] [sourcefiles] -h, --help Emit this help message and quit. + -m, --maker Set the build command (by default "make") -i, --ignore Ignore (don't remove) headers matching the argument regexp -r, --remove Remove the final set of unneeded headers -v, --verbose Be chatty about what you're doing. @@ -126,7 +127,7 @@ class SaveForModification: def forget(self): "Disable reversion." os.remove(self.original) - def __del__(self): + def revert(self): "Revert modifications on the file at the end of this object's lifetime." if os.path.exists(self.original): try: @@ -145,13 +146,13 @@ def trim(line): else: return `line` -def compile(source, msg="", verbosity=0): +def testcompile(source, maker, msg="", verbosity=0): "Test-compile a sourcefile. Return the status and the compilation time" (stem, suffix) = os.path.splitext(source) derived = stem + ".o" if os.path.exists(derived): os.remove(derived) - command = "make " + derived + command = maker + " " + derived if verbosity < 2: command += " >/dev/null 2>&1" start = time.time() @@ -168,7 +169,7 @@ def compile(source, msg="", verbosity=0): % (sourcefile, msg, explain, end-start) return (status, end - start) -def c_analyze(source, includes, verbosity): +def c_analyze(source, maker, includes, verbosity): "Given a C file and a list of includes, return those that can be omitted." # We'll remove headers in reverse order, because later unnecessary # headers might depend on earlier ones @@ -176,13 +177,13 @@ def c_analyze(source, includes, verbosity): unneeded = [] if verbosity == 0: baton = Baton(sourcefile + ": ", "Done") - saveit = SaveForModification(sourcefile) try: + saveit = SaveForModification(sourcefile) while True: keepgoing = False for header in includes: saveit.remove_headers([header]) - (st, t) = compile(sourcefile, " without %s" % trim(header), verbosity) + (st, t) = testcompile(sourcefile, maker, " without %s" % trim(header), verbosity) if st == 0: unneeded.append(header) includes.remove(header) @@ -192,19 +193,19 @@ def c_analyze(source, includes, verbosity): if not keepgoing: break finally: - del saveit + saveit.revert() if verbosity == 0: baton.end() return unneeded -def deheader(sourcefile, includes, remove, verbose): +def deheader(sourcefile, maker, includes, remove, verbose): # Sanity check against broken sourcefiles; we want this to # complain visibly if the sourcefile won't build at all. - (st, t) = compile(sourcefile, min(1, verbose)) + (st, t) = testcompile(sourcefile, maker, min(1, verbose)) if st == 0: # Now do the analysis if sourcefile.endswith(".c") or sourcefile.endswith(".cpp"): - unneeded = c_analyze(sourcefile, includes, verbose) + unneeded = c_analyze(sourcefile, maker, includes, verbose) if unneeded: for line in unneeded: print "deheader: remove %s from %s" % (trim(line), sourcefile) @@ -215,9 +216,10 @@ def deheader(sourcefile, includes, remove, verbose): del remove_it if __name__ == "__main__": - (options, arguments) = getopt.getopt(sys.argv[1:], "hrvi:", + (options, arguments) = getopt.getopt(sys.argv[1:], "hm:rvi:", ["help", "ignore", "remove", "verbose",]) + maker = "make" verbose = 0 remove = False ignores = [] @@ -227,6 +229,8 @@ if __name__ == "__main__": sys.exit(0) elif switch in ('-i', '--ignore'): ignores.append(val) + elif switch in ('-m', '--maker'): + maker = val elif switch in ('-r', '--remove'): remove = True elif switch in ('-v', '--verbose'): @@ -237,6 +241,6 @@ if __name__ == "__main__": inclusion_map = InclusionMap(arguments, ignore, verbose) for sourcefile in inclusion_map.c_to_h: - deheader(sourcefile, inclusion_map.c_to_h[sourcefile], remove, verbose) + deheader(sourcefile, maker, inclusion_map.c_to_h[sourcefile], remove, verbose) # End |