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