|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# This file is part of the MicroPython project, http://micropython.org/ |
| 4 | +# |
| 5 | +# The MIT License (MIT) |
| 6 | +# |
| 7 | +# Copyright (c) 2022 Andrew Leech |
| 8 | +# Copyright (c) 2022 Jim Mussared |
| 9 | +# |
| 10 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | +# of this software and associated documentation files (the "Software"), to deal |
| 12 | +# in the Software without restriction, including without limitation the rights |
| 13 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | +# copies of the Software, and to permit persons to whom the Software is |
| 15 | +# furnished to do so, subject to the following conditions: |
| 16 | +# |
| 17 | +# The above copyright notice and this permission notice shall be included in |
| 18 | +# all copies or substantial portions of the Software. |
| 19 | +# |
| 20 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 26 | +# THE SOFTWARE. |
| 27 | + |
| 28 | +from __future__ import print_function |
| 29 | +import os |
| 30 | +import stat |
| 31 | +import subprocess |
| 32 | + |
| 33 | +NATIVE_ARCH_X86 = "x86" |
| 34 | +NATIVE_ARCH_X64 = "x64" |
| 35 | +NATIVE_ARCH_ARMV6 = "armv6" |
| 36 | +NATIVE_ARCH_ARMV6M = "armv6m" |
| 37 | +NATIVE_ARCH_ARMV7M = "armv7m" |
| 38 | +NATIVE_ARCH_ARMV7EM = "armv7em" |
| 39 | +NATIVE_ARCH_ARMV7EMSP = "armv7emsp" |
| 40 | +NATIVE_ARCH_ARMV7EMDP = "armv7emdp" |
| 41 | +NATIVE_ARCH_XTENSA = "xtensa" |
| 42 | +NATIVE_ARCH_XTENSAWIN = "xtensawin" |
| 43 | + |
| 44 | +NATIVE_ARCHS = [ |
| 45 | + NATIVE_ARCH_X86, |
| 46 | + NATIVE_ARCH_X64, |
| 47 | + NATIVE_ARCH_ARMV6, |
| 48 | + NATIVE_ARCH_ARMV6M, |
| 49 | + NATIVE_ARCH_ARMV7M, |
| 50 | + NATIVE_ARCH_ARMV7EM, |
| 51 | + NATIVE_ARCH_ARMV7EMSP, |
| 52 | + NATIVE_ARCH_ARMV7EMDP, |
| 53 | + NATIVE_ARCH_XTENSA, |
| 54 | + NATIVE_ARCH_XTENSAWIN, |
| 55 | +] |
| 56 | + |
| 57 | +__all__ = ["compile", "run", "CrossCompileError"] |
| 58 | + |
| 59 | + |
| 60 | +class CrossCompileError(Exception): |
| 61 | + pass |
| 62 | + |
| 63 | + |
| 64 | +def find_mpy_cross_binary(mpy_cross): |
| 65 | + if mpy_cross: |
| 66 | + return mpy_cross |
| 67 | + return os.path.abspath(os.path.join(os.path.dirname(__file__), "../mpy-cross")) |
| 68 | + |
| 69 | + |
| 70 | +def compile(src, dest=None, src_path=None, opt=None, march=None, mpy_cross=None, extra_args=None): |
| 71 | + if not src: |
| 72 | + raise ValueError("src is required") |
| 73 | + if not os.path.exists(src): |
| 74 | + raise CrossCompileError("Input .py file not found: {}.".format(src_py)) |
| 75 | + |
| 76 | + args = [] |
| 77 | + |
| 78 | + if src_path: |
| 79 | + args += ["-s", src_path] |
| 80 | + |
| 81 | + if dest: |
| 82 | + args += ["-o", dest] |
| 83 | + |
| 84 | + if march: |
| 85 | + args += ["-march", march] |
| 86 | + |
| 87 | + if opt is not None: |
| 88 | + args += ["-O{}".format(opt)] |
| 89 | + |
| 90 | + if extra_args: |
| 91 | + args += extra_args |
| 92 | + |
| 93 | + args += [src] |
| 94 | + |
| 95 | + run(args, mpy_cross) |
| 96 | + |
| 97 | + |
| 98 | +def run(args, mpy_cross=None): |
| 99 | + mpy_cross = find_mpy_cross_binary(mpy_cross) |
| 100 | + |
| 101 | + if not os.path.exists(mpy_cross): |
| 102 | + raise CrossCompileError("mpy-cross binary not found at {}.".format(mpy_cross)) |
| 103 | + |
| 104 | + try: |
| 105 | + st = os.stat(mpy_cross) |
| 106 | + os.chmod(mpy_cross, st.st_mode | stat.S_IEXEC) |
| 107 | + except OSError: |
| 108 | + pass |
| 109 | + |
| 110 | + try: |
| 111 | + subprocess.check_output([mpy_cross] + args, stderr=subprocess.STDOUT) |
| 112 | + except subprocess.CalledProcessError as er: |
| 113 | + raise CrossCompileError(er.output) |
0 commit comments