paul@233 | 1 | #!/usr/bin/env python |
paul@233 | 2 | |
paul@233 | 3 | """ |
paul@233 | 4 | Command option processing and other utilities for compiling and testing |
paul@233 | 5 | programs. |
paul@253 | 6 | |
paul@253 | 7 | Copyright (C) 2009 Paul Boddie <paul@boddie.org.uk> |
paul@253 | 8 | |
paul@253 | 9 | This program is free software; you can redistribute it and/or modify it under |
paul@253 | 10 | the terms of the GNU General Public License as published by the Free Software |
paul@253 | 11 | Foundation; either version 3 of the License, or (at your option) any later |
paul@253 | 12 | version. |
paul@253 | 13 | |
paul@253 | 14 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@253 | 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@253 | 16 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@253 | 17 | details. |
paul@253 | 18 | |
paul@253 | 19 | You should have received a copy of the GNU General Public License along with |
paul@253 | 20 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@233 | 21 | """ |
paul@233 | 22 | |
paul@499 | 23 | from os.path import exists, join |
paul@233 | 24 | import micropython |
paul@233 | 25 | |
paul@233 | 26 | def parse_optimisations(args): |
paul@233 | 27 | |
paul@233 | 28 | "Parse 'args' for optimisation flags." |
paul@233 | 29 | |
paul@233 | 30 | if "-omax" in args: |
paul@233 | 31 | requested_optimisations = micropython.Program.supported_optimisations |
paul@233 | 32 | else: |
paul@233 | 33 | requested_optimisations = [] |
paul@233 | 34 | for arg in args: |
paul@233 | 35 | if arg.startswith("-o"): |
paul@233 | 36 | for arg_part in arg[2:].split(","): |
paul@233 | 37 | requested_optimisations.append(arg_part) |
paul@233 | 38 | |
paul@233 | 39 | return requested_optimisations |
paul@233 | 40 | |
paul@268 | 41 | def show_optimisations(): |
paul@268 | 42 | |
paul@268 | 43 | "Show available optimisation flags." |
paul@268 | 44 | |
paul@268 | 45 | return ",".join(micropython.Program.supported_optimisations) |
paul@268 | 46 | |
paul@314 | 47 | def program(path, requested_optimisations, verbose=0): |
paul@233 | 48 | |
paul@233 | 49 | """ |
paul@314 | 50 | Return a program object for the given module search 'path' and |
paul@314 | 51 | 'requested_optimisations'. |
paul@233 | 52 | """ |
paul@233 | 53 | |
paul@233 | 54 | i = micropython.Importer(path, verbose, requested_optimisations) |
paul@233 | 55 | p = micropython.Program(i, requested_optimisations) |
paul@233 | 56 | |
paul@499 | 57 | for d in path: |
paul@499 | 58 | builtins = join(d, "builtins.py") |
paul@499 | 59 | if exists(builtins): |
paul@499 | 60 | i.load_from_file(builtins, "__builtins__") |
paul@499 | 61 | break |
paul@233 | 62 | return p |
paul@233 | 63 | |
paul@233 | 64 | # Convenience functions. |
paul@233 | 65 | |
paul@314 | 66 | def get_program(path, args): |
paul@233 | 67 | |
paul@233 | 68 | """ |
paul@314 | 69 | Return the program object for the given module search 'path' and command |
paul@314 | 70 | 'args'. |
paul@233 | 71 | """ |
paul@233 | 72 | |
paul@233 | 73 | requested_optimisations = parse_optimisations(args) |
paul@314 | 74 | return program(path, requested_optimisations, "-v" in args) |
paul@233 | 75 | |
paul@233 | 76 | # vim: tabstop=4 expandtab shiftwidth=4 |