paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | from errors import * |
paul@442 | 4 | from os import rename |
paul@0 | 5 | from os.path import abspath, exists, join, split |
paul@442 | 6 | from subprocess import Popen, PIPE |
paul@0 | 7 | from time import time |
paul@126 | 8 | import importer, deducer, optimiser, generator, translator |
paul@0 | 9 | import sys |
paul@0 | 10 | |
paul@0 | 11 | libdirs = [ |
paul@0 | 12 | join(split(__file__)[0], "lib"), |
paul@0 | 13 | "/usr/share/lichen/lib" |
paul@0 | 14 | ] |
paul@0 | 15 | |
paul@0 | 16 | def load_module(filename, module_name): |
paul@0 | 17 | for libdir in libdirs: |
paul@0 | 18 | path = join(libdir, filename) |
paul@0 | 19 | if exists(path): |
paul@0 | 20 | return i.load_from_file(path, module_name) |
paul@0 | 21 | return None |
paul@0 | 22 | |
paul@41 | 23 | def show_missing(missing): |
paul@41 | 24 | missing = list(missing) |
paul@41 | 25 | missing.sort() |
paul@41 | 26 | for module_name, name in missing: |
paul@41 | 27 | print >>sys.stderr, "Module %s references an unknown object: %s" % (module_name, name) |
paul@41 | 28 | |
paul@0 | 29 | def stopwatch(activity, now): |
paul@0 | 30 | print >>sys.stderr, "%s took %.2f seconds" % (activity, time() - now) |
paul@0 | 31 | return time() |
paul@0 | 32 | |
paul@442 | 33 | def call(tokens, verbose=False): |
paul@442 | 34 | out = not verbose and PIPE or None |
paul@442 | 35 | cmd = Popen(tokens, stdout=out, stderr=out) |
paul@442 | 36 | stdout, stderr = cmd.communicate() |
paul@442 | 37 | return cmd.wait() |
paul@442 | 38 | |
paul@0 | 39 | # Main program. |
paul@0 | 40 | |
paul@0 | 41 | if __name__ == "__main__": |
paul@442 | 42 | args = sys.argv[1:] |
paul@0 | 43 | path = libdirs + sys.path[:] |
paul@0 | 44 | |
paul@442 | 45 | # Determine the options and arguments. |
paul@442 | 46 | |
paul@442 | 47 | verbose = False |
paul@442 | 48 | reset = False |
paul@442 | 49 | debug = False |
paul@442 | 50 | make = True |
paul@442 | 51 | make_verbose = True |
paul@442 | 52 | |
paul@442 | 53 | filenames = [] |
paul@442 | 54 | outputs = [] |
paul@442 | 55 | |
paul@442 | 56 | # Obtain program filenames by default. |
paul@442 | 57 | |
paul@442 | 58 | l = filenames |
paul@442 | 59 | |
paul@442 | 60 | for arg in args: |
paul@442 | 61 | if arg == "-v": verbose = True |
paul@442 | 62 | elif arg == "-r": reset = True |
paul@442 | 63 | elif arg == "-g": debug = True |
paul@442 | 64 | elif arg == "-c": make = False |
paul@442 | 65 | elif arg == "-q": make_verbose = False |
paul@442 | 66 | elif arg == "-o": l = outputs |
paul@442 | 67 | else: |
paul@442 | 68 | l.append(arg) |
paul@442 | 69 | |
paul@442 | 70 | # Revert to collecting program filenames after obtaining the output |
paul@442 | 71 | # executable filename. |
paul@442 | 72 | |
paul@442 | 73 | if l is outputs: l = filenames |
paul@442 | 74 | |
paul@442 | 75 | # Obtain the program filename. |
paul@442 | 76 | |
paul@442 | 77 | if len(filenames) > 1: |
paul@442 | 78 | print >>sys.stderr, "Only one main program file can be specified." |
paul@442 | 79 | sys.exit(1) |
paul@442 | 80 | |
paul@442 | 81 | filename = abspath(filenames[0]) |
paul@0 | 82 | path.append(split(filename)[0]) |
paul@0 | 83 | |
paul@442 | 84 | # Obtain the output filename. |
paul@442 | 85 | |
paul@442 | 86 | output = outputs and outputs[0] or "main" |
paul@442 | 87 | |
paul@442 | 88 | # Define the output data directories. |
paul@442 | 89 | |
paul@442 | 90 | datadir = "_lplc" |
paul@442 | 91 | cache_dir = join(datadir, "_cache") |
paul@442 | 92 | deduced_dir = join(datadir, "_deduced") |
paul@442 | 93 | output_dir = join(datadir, "_output") |
paul@442 | 94 | generated_dir = join(datadir, "_generated") |
paul@0 | 95 | |
paul@0 | 96 | # Load the program. |
paul@0 | 97 | |
paul@0 | 98 | try: |
paul@0 | 99 | start = now = time() |
paul@0 | 100 | |
paul@442 | 101 | i = importer.Importer(path, cache_dir, verbose) |
paul@0 | 102 | m = i.initialise(filename, reset) |
paul@41 | 103 | success = i.finalise() |
paul@0 | 104 | |
paul@0 | 105 | now = stopwatch("Inspection", now) |
paul@0 | 106 | |
paul@41 | 107 | # Check for success, indicating missing references otherwise. |
paul@41 | 108 | |
paul@41 | 109 | if not success: |
paul@41 | 110 | show_missing(i.missing) |
paul@275 | 111 | sys.exit(1) |
paul@41 | 112 | |
paul@442 | 113 | d = deducer.Deducer(i, deduced_dir) |
paul@44 | 114 | d.to_output() |
paul@44 | 115 | |
paul@44 | 116 | now = stopwatch("Deduction", now) |
paul@44 | 117 | |
paul@442 | 118 | o = optimiser.Optimiser(i, d, output_dir) |
paul@92 | 119 | o.to_output() |
paul@92 | 120 | |
paul@92 | 121 | now = stopwatch("Optimisation", now) |
paul@92 | 122 | |
paul@442 | 123 | g = generator.Generator(i, o, generated_dir) |
paul@183 | 124 | g.to_output(debug) |
paul@126 | 125 | |
paul@127 | 126 | now = stopwatch("Generation", now) |
paul@126 | 127 | |
paul@442 | 128 | t = translator.Translator(i, d, o, generated_dir) |
paul@113 | 129 | t.to_output() |
paul@113 | 130 | |
paul@442 | 131 | now = stopwatch("Translation", now) |
paul@442 | 132 | |
paul@442 | 133 | # Compile the program unless otherwise indicated. |
paul@442 | 134 | |
paul@442 | 135 | if make: |
paul@442 | 136 | make_clean_cmd = ["make", "-C", generated_dir, "clean"] |
paul@442 | 137 | make_cmd = make_clean_cmd[:-1] |
paul@442 | 138 | |
paul@442 | 139 | retval = call(make_clean_cmd, make_verbose) |
paul@442 | 140 | if not retval: |
paul@442 | 141 | retval = call(make_cmd, make_verbose) |
paul@442 | 142 | |
paul@442 | 143 | if not retval: |
paul@442 | 144 | stopwatch("Compilation", now) |
paul@442 | 145 | else: |
paul@442 | 146 | sys.exit(retval) |
paul@442 | 147 | |
paul@442 | 148 | # Move the executable into the current directory. |
paul@442 | 149 | |
paul@442 | 150 | rename(join(generated_dir, "main"), output) |
paul@113 | 151 | |
paul@0 | 152 | # Report any errors. |
paul@0 | 153 | |
paul@0 | 154 | except ProcessingError, exc: |
paul@0 | 155 | print exc |
paul@0 | 156 | if "-tb" in args: |
paul@0 | 157 | raise |
paul@275 | 158 | sys.exit(1) |
paul@0 | 159 | |
paul@0 | 160 | else: |
paul@275 | 161 | sys.exit(0) |
paul@0 | 162 | |
paul@0 | 163 | # vim: tabstop=4 expandtab shiftwidth=4 |