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@233 | 23 | import micropython |
paul@233 | 24 | |
paul@233 | 25 | def parse_optimisations(args): |
paul@233 | 26 | |
paul@233 | 27 | "Parse 'args' for optimisation flags." |
paul@233 | 28 | |
paul@233 | 29 | if "-omax" in args: |
paul@233 | 30 | requested_optimisations = micropython.Program.supported_optimisations |
paul@233 | 31 | else: |
paul@233 | 32 | requested_optimisations = [] |
paul@233 | 33 | for arg in args: |
paul@233 | 34 | if arg.startswith("-o"): |
paul@233 | 35 | for arg_part in arg[2:].split(","): |
paul@233 | 36 | requested_optimisations.append(arg_part) |
paul@233 | 37 | |
paul@233 | 38 | return requested_optimisations |
paul@233 | 39 | |
paul@268 | 40 | def show_optimisations(): |
paul@268 | 41 | |
paul@268 | 42 | "Show available optimisation flags." |
paul@268 | 43 | |
paul@268 | 44 | return ",".join(micropython.Program.supported_optimisations) |
paul@268 | 45 | |
paul@314 | 46 | def program(path, requested_optimisations, verbose=0): |
paul@233 | 47 | |
paul@233 | 48 | """ |
paul@314 | 49 | Return a program object for the given module search 'path' and |
paul@314 | 50 | 'requested_optimisations'. |
paul@233 | 51 | """ |
paul@233 | 52 | |
paul@233 | 53 | i = micropython.Importer(path, verbose, requested_optimisations) |
paul@233 | 54 | p = micropython.Program(i, requested_optimisations) |
paul@233 | 55 | |
paul@233 | 56 | i.load_from_file("lib/builtins.py", "__builtins__") |
paul@233 | 57 | return p |
paul@233 | 58 | |
paul@233 | 59 | # Convenience functions. |
paul@233 | 60 | |
paul@314 | 61 | def get_program(path, args): |
paul@233 | 62 | |
paul@233 | 63 | """ |
paul@314 | 64 | Return the program object for the given module search 'path' and command |
paul@314 | 65 | 'args'. |
paul@233 | 66 | """ |
paul@233 | 67 | |
paul@233 | 68 | requested_optimisations = parse_optimisations(args) |
paul@314 | 69 | return program(path, requested_optimisations, "-v" in args) |
paul@233 | 70 | |
paul@233 | 71 | # vim: tabstop=4 expandtab shiftwidth=4 |