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@445 | 6 | from pyparser import error |
paul@442 | 7 | from subprocess import Popen, PIPE |
paul@0 | 8 | from time import time |
paul@126 | 9 | import importer, deducer, optimiser, generator, translator |
paul@0 | 10 | import sys |
paul@0 | 11 | |
paul@0 | 12 | libdirs = [ |
paul@0 | 13 | join(split(__file__)[0], "lib"), |
paul@0 | 14 | "/usr/share/lichen/lib" |
paul@0 | 15 | ] |
paul@0 | 16 | |
paul@0 | 17 | def load_module(filename, module_name): |
paul@0 | 18 | for libdir in libdirs: |
paul@0 | 19 | path = join(libdir, filename) |
paul@0 | 20 | if exists(path): |
paul@0 | 21 | return i.load_from_file(path, module_name) |
paul@0 | 22 | return None |
paul@0 | 23 | |
paul@41 | 24 | def show_missing(missing): |
paul@41 | 25 | missing = list(missing) |
paul@41 | 26 | missing.sort() |
paul@41 | 27 | for module_name, name in missing: |
paul@41 | 28 | print >>sys.stderr, "Module %s references an unknown object: %s" % (module_name, name) |
paul@41 | 29 | |
paul@445 | 30 | def show_syntax_error(exc): |
paul@445 | 31 | print >>sys.stderr, "Syntax error at column %d on line %d in file %s:" % (exc.offset, exc.lineno, exc.filename) |
paul@445 | 32 | print >>sys.stderr |
paul@445 | 33 | print >>sys.stderr, exc.text.rstrip() |
paul@445 | 34 | print >>sys.stderr, " " * exc.offset + "^" |
paul@445 | 35 | |
paul@0 | 36 | def stopwatch(activity, now): |
paul@0 | 37 | print >>sys.stderr, "%s took %.2f seconds" % (activity, time() - now) |
paul@0 | 38 | return time() |
paul@0 | 39 | |
paul@442 | 40 | def call(tokens, verbose=False): |
paul@442 | 41 | out = not verbose and PIPE or None |
paul@442 | 42 | cmd = Popen(tokens, stdout=out, stderr=out) |
paul@442 | 43 | stdout, stderr = cmd.communicate() |
paul@442 | 44 | return cmd.wait() |
paul@442 | 45 | |
paul@558 | 46 | def start_arg_list(l, arg, prefix, needed): |
paul@558 | 47 | |
paul@558 | 48 | """ |
paul@558 | 49 | Add to 'l' any value given as part of 'arg' having the given option |
paul@558 | 50 | 'prefix'. The 'needed' number of values is provided in case no value is |
paul@558 | 51 | found. |
paul@558 | 52 | |
paul@558 | 53 | Return 'l' and 'needed' decremented by 1 together in a tuple. |
paul@558 | 54 | """ |
paul@558 | 55 | |
paul@558 | 56 | s = arg[len(prefix):].strip() |
paul@558 | 57 | if s: |
paul@558 | 58 | l.append(s) |
paul@558 | 59 | return l, needed - 1 |
paul@558 | 60 | else: |
paul@558 | 61 | return l, needed |
paul@558 | 62 | |
paul@0 | 63 | # Main program. |
paul@0 | 64 | |
paul@0 | 65 | if __name__ == "__main__": |
paul@558 | 66 | basename = split(sys.argv[0])[-1] |
paul@442 | 67 | args = sys.argv[1:] |
paul@525 | 68 | path = libdirs |
paul@0 | 69 | |
paul@558 | 70 | if "--help" in args or not args: |
paul@558 | 71 | print >>sys.stderr, """\ |
paul@558 | 72 | Usage: %s [ <options> ] <filename> |
paul@558 | 73 | |
paul@558 | 74 | Compile the program whose principal file is given in place of <filename>. |
paul@558 | 75 | The following options may be specified: |
paul@558 | 76 | |
paul@558 | 77 | -c Only partially compile the program; do not attempt to build or link it |
paul@558 | 78 | -g Generate debugging information for the built executable |
paul@558 | 79 | -q Silence messages produced when building an executable |
paul@558 | 80 | -r Reset (discard) cached program information; inspect the whole program again |
paul@558 | 81 | -tb Provide a traceback for any internal errors (development only) |
paul@558 | 82 | -v Report compiler activities in a verbose fashion (development only) |
paul@558 | 83 | |
paul@558 | 84 | Some options may be followed by values, either immediately after the option |
paul@558 | 85 | (without any space between) or in the arguments that follow them: |
paul@558 | 86 | |
paul@558 | 87 | -o Indicate the output executable name |
paul@558 | 88 | -W Show warnings on the topics indicated |
paul@558 | 89 | |
paul@558 | 90 | Currently, the following warnings are supported: |
paul@558 | 91 | |
paul@558 | 92 | all Show all possible warnings |
paul@558 | 93 | |
paul@558 | 94 | args Show invocations where a callable may be involved that cannot accept the |
paul@558 | 95 | arguments provided |
paul@558 | 96 | """ % basename |
paul@558 | 97 | sys.exit(1) |
paul@558 | 98 | |
paul@442 | 99 | # Determine the options and arguments. |
paul@442 | 100 | |
paul@442 | 101 | debug = False |
paul@442 | 102 | make = True |
paul@442 | 103 | make_verbose = True |
paul@525 | 104 | reset = False |
paul@474 | 105 | traceback = False |
paul@525 | 106 | verbose = False |
paul@558 | 107 | warnings = [] |
paul@442 | 108 | |
paul@442 | 109 | filenames = [] |
paul@442 | 110 | outputs = [] |
paul@442 | 111 | |
paul@442 | 112 | # Obtain program filenames by default. |
paul@442 | 113 | |
paul@442 | 114 | l = filenames |
paul@558 | 115 | needed = None |
paul@442 | 116 | |
paul@442 | 117 | for arg in args: |
paul@525 | 118 | if arg == "-c": make = False |
paul@442 | 119 | elif arg == "-g": debug = True |
paul@442 | 120 | elif arg == "-q": make_verbose = False |
paul@525 | 121 | elif arg == "-r": reset = True |
paul@474 | 122 | elif arg == "-tb": traceback = True |
paul@558 | 123 | elif arg.startswith("-o"): l, needed = start_arg_list(outputs, arg, "-o", 1) |
paul@525 | 124 | elif arg == "-v": verbose = True |
paul@558 | 125 | elif arg.startswith("-W"): l, needed = start_arg_list(warnings, arg, "-W", 1) |
paul@442 | 126 | else: |
paul@442 | 127 | l.append(arg) |
paul@558 | 128 | if needed: |
paul@558 | 129 | needed -= 1 |
paul@442 | 130 | |
paul@558 | 131 | if needed == 0: |
paul@558 | 132 | l = filenames |
paul@442 | 133 | |
paul@442 | 134 | # Obtain the program filename. |
paul@442 | 135 | |
paul@558 | 136 | if len(filenames) != 1: |
paul@558 | 137 | print >>sys.stderr, "One main program file must be specified." |
paul@442 | 138 | sys.exit(1) |
paul@442 | 139 | |
paul@442 | 140 | filename = abspath(filenames[0]) |
paul@0 | 141 | path.append(split(filename)[0]) |
paul@0 | 142 | |
paul@442 | 143 | # Obtain the output filename. |
paul@442 | 144 | |
paul@558 | 145 | if outputs and not make: |
paul@558 | 146 | print >>sys.stderr, "Output specified but building disabled." |
paul@558 | 147 | |
paul@449 | 148 | output = outputs and outputs[0] or "_main" |
paul@442 | 149 | |
paul@442 | 150 | # Define the output data directories. |
paul@442 | 151 | |
paul@442 | 152 | datadir = "_lplc" |
paul@442 | 153 | cache_dir = join(datadir, "_cache") |
paul@442 | 154 | deduced_dir = join(datadir, "_deduced") |
paul@442 | 155 | output_dir = join(datadir, "_output") |
paul@442 | 156 | generated_dir = join(datadir, "_generated") |
paul@0 | 157 | |
paul@0 | 158 | # Load the program. |
paul@0 | 159 | |
paul@0 | 160 | try: |
paul@0 | 161 | start = now = time() |
paul@0 | 162 | |
paul@558 | 163 | i = importer.Importer(path, cache_dir, verbose, warnings) |
paul@0 | 164 | m = i.initialise(filename, reset) |
paul@41 | 165 | success = i.finalise() |
paul@0 | 166 | |
paul@0 | 167 | now = stopwatch("Inspection", now) |
paul@0 | 168 | |
paul@41 | 169 | # Check for success, indicating missing references otherwise. |
paul@41 | 170 | |
paul@41 | 171 | if not success: |
paul@41 | 172 | show_missing(i.missing) |
paul@275 | 173 | sys.exit(1) |
paul@41 | 174 | |
paul@442 | 175 | d = deducer.Deducer(i, deduced_dir) |
paul@44 | 176 | d.to_output() |
paul@44 | 177 | |
paul@44 | 178 | now = stopwatch("Deduction", now) |
paul@44 | 179 | |
paul@442 | 180 | o = optimiser.Optimiser(i, d, output_dir) |
paul@92 | 181 | o.to_output() |
paul@92 | 182 | |
paul@92 | 183 | now = stopwatch("Optimisation", now) |
paul@92 | 184 | |
paul@442 | 185 | g = generator.Generator(i, o, generated_dir) |
paul@183 | 186 | g.to_output(debug) |
paul@126 | 187 | |
paul@127 | 188 | now = stopwatch("Generation", now) |
paul@126 | 189 | |
paul@442 | 190 | t = translator.Translator(i, d, o, generated_dir) |
paul@113 | 191 | t.to_output() |
paul@113 | 192 | |
paul@442 | 193 | now = stopwatch("Translation", now) |
paul@442 | 194 | |
paul@442 | 195 | # Compile the program unless otherwise indicated. |
paul@442 | 196 | |
paul@442 | 197 | if make: |
paul@442 | 198 | make_clean_cmd = ["make", "-C", generated_dir, "clean"] |
paul@442 | 199 | make_cmd = make_clean_cmd[:-1] |
paul@442 | 200 | |
paul@442 | 201 | retval = call(make_clean_cmd, make_verbose) |
paul@442 | 202 | if not retval: |
paul@442 | 203 | retval = call(make_cmd, make_verbose) |
paul@442 | 204 | |
paul@442 | 205 | if not retval: |
paul@442 | 206 | stopwatch("Compilation", now) |
paul@442 | 207 | else: |
paul@442 | 208 | sys.exit(retval) |
paul@442 | 209 | |
paul@442 | 210 | # Move the executable into the current directory. |
paul@442 | 211 | |
paul@442 | 212 | rename(join(generated_dir, "main"), output) |
paul@113 | 213 | |
paul@0 | 214 | # Report any errors. |
paul@0 | 215 | |
paul@445 | 216 | except error.SyntaxError, exc: |
paul@445 | 217 | show_syntax_error(exc) |
paul@474 | 218 | if traceback: |
paul@445 | 219 | raise |
paul@445 | 220 | sys.exit(1) |
paul@445 | 221 | |
paul@0 | 222 | except ProcessingError, exc: |
paul@0 | 223 | print exc |
paul@474 | 224 | if traceback: |
paul@0 | 225 | raise |
paul@275 | 226 | sys.exit(1) |
paul@0 | 227 | |
paul@0 | 228 | else: |
paul@275 | 229 | sys.exit(0) |
paul@0 | 230 | |
paul@0 | 231 | # vim: tabstop=4 expandtab shiftwidth=4 |