|
| 1 | +# This file is part of the MicroPython project, http://micropython.org/ |
| 2 | +# |
| 3 | +# The MIT License (MIT) |
| 4 | +# |
| 5 | +# Copyright (c) 2022 Andrew Leech |
| 6 | +# Copyright (c) 2023 Jim Mussared |
| 7 | +# |
| 8 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | +# of this software and associated documentation files (the "Software"), to deal |
| 10 | +# in the Software without restriction, including without limitation the rights |
| 11 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +# copies of the Software, and to permit persons to whom the Software is |
| 13 | +# furnished to do so, subject to the following conditions: |
| 14 | +# |
| 15 | +# The above copyright notice and this permission notice shall be included in |
| 16 | +# all copies or substantial portions of the Software. |
| 17 | +# |
| 18 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | +# THE SOFTWARE. |
| 25 | + |
| 26 | +import os |
| 27 | + |
| 28 | + |
| 29 | +class CrossCompileError(Exception): |
| 30 | + pass |
| 31 | + |
| 32
+ |
| 33 | +class CompilerNotFoundError(Exception): |
| 34 | + pass |
| 35 | + |
| 36 | + |
| 37 | +EMIT_BYTECODE = "bytecode" |
| 38 | +EMIT_NATIVE = "native" |
| 39 | +EMIT_VIPER = "viper" |
| 40 | + |
| 41 | + |
| 42 | +def emit_id(name): |
| 43 | + return { |
| 44 | + EMIT_BYTECODE: 1, |
| 45 | + EMIT_NATIVE: 2, |
| 46 | + EMIT_VIPER: 3, |
| 47 | + }[name] |
| 48 | + |
| 49 | + |
| 50 | +NATIVE_ARCHS = { |
| 51 | + "NATIVE_ARCH_NONE": "", |
| 52 | + "NATIVE_ARCH_X86": "x86", |
| 53 | + "NATIVE_ARCH_X64": "x64", |
| 54 | + "NATIVE_ARCH_ARMV6": "armv6", |
| 55 | + "NATIVE_ARCH_ARMV6M": "armv6m", |
| 56 | + "NATIVE_ARCH_ARMV7M": "armv7m", |
| 57 | + "NATIVE_ARCH_ARMV7EM": "armv7em", |
| 58 | + "NATIVE_ARCH_ARMV7EMSP": "armv7emsp", |
| 59 | + "NATIVE_ARCH_ARMV7EMDP": "armv7emdp", |
| 60 | + "NATIVE_ARCH_XTENSA": "xtensa", |
| 61 | + "NATIVE_ARCH_XTENSAWIN": "xtensawin", |
| 62 | +} |
| 63 | + |
| 64 | +globals().update(NATIVE_ARCHS) |
| 65 | + |
| 66 | + |
| 67 | +def native_arch_id(name): |
| 68 | + if name is None: |
| 69 | + return 0 |
| 70 | + |
| 71 | + return { |
| 72 | + "": 0, |
| 73 | + "x86": 1, |
| 74 | + "x64": 2, |
| 75 | + "armv6": 3, |
| 76 | + "armv6m": 4, |
| 77 | + "armv7m": 5, |
| 78 | + "armv7em": 6, |
| 79 | + "armv7emsp": 7, |
| 80 | + "armv7emdp": 8, |
| 81 | + "xtensa": 9, |
| 82 | + "xtensawin": 10, |
| 83 | + }[name] |
| 84 | + |
| 85 | + |
| 86 | +class Compiler: |
| 87 | + def mpy_version(self): |
| 88 | + """ |
| 89 | + Get the version and sub-version of the .mpy file format generated by this compiler. |
| 90 | +
|
| 91 | + Returns: A tuple of `(mpy_version, mpy_sub_version)` |
| 92 | + """ |
| 93 | + raise NotImplementedError() |
| 94 | + |
| 95 | + def version(self): |
| 96 | + """ |
| 97 | + Get the version string from this compiler. |
| 98 | +
|
| 99 | + Returns: A string, typically "MicroPython <git tag> on <build date>". |
| 100 | + """ |
| 101 | + raise NotImplementedError() |
| 102 | + |
| 103 | + def compile(self, src, *, dest=None, src_path=None, opt=0, march=None, emit=EMIT_BYTECODE): |
| 104 | + """ |
| 105 | + Compile the specified .py file with mpy-cross. |
| 106 | +
|
| 107 | + Returns: Standard output from mpy-cross as a string. |
| 108 | +
|
| 109 | + Required arguments: |
| 110 | + - src: The path to the .py file |
| 111 | +
|
| 112 | + Optional keyword arguments: |
| 113 | + - dest: The output .mpy file. Defaults to `src` (with .mpy extension) |
| 114 | + - src_path: The path to embed in the .mpy file (defaults to `src`) |
| 115 | + - opt: Optimisation level (0-3, default 0) |
| 116 | + - march: One of the `NATIVE_ARCH_*` constants (defaults to |
| 117 | + None, which is treated as NATIVE_ARCH_NONE). Architecture to |
| 118 | + use when generating code for @native/@viper functions. |
| 119 | + - emit: One of the `EMIT_*` constants (defaults to |
| 120 | + EMIT_BYTECODE). This sets the default emitter to use for |
| 121 | + functions that do not have a decorator. |
| 122 | + """ |
| 123 | + raise NotImplementedError() |
| 124 | + |
| 125 | + def description(self): |
| 126 | + """ |
| 127 | + Returns: A string containing a description of this compiler. |
| 128 | + """ |
| 129 | + raise NotImplementedError() |
| 130 | + |
| 131 | + def _find_binary(self, binary=None, *, name="mpy-cross", ext="", build_dir="build"): |
| 132 | + # If an explicit binary is given, use that. |
| 133 | + if binary and os.path.exists(binary): |
| 134 | + return binary |
| 135 | + |
| 136 | + # If we're running the installed version, then the binary will be |
| 137 | + # alongside the Python files. |
| 138 | + install_path = os.path.dirname(__file__) |
| 139 | + wheel_binary = os.path.abspath(os.path.join(install_path, name + ext)) |
| 140 | + if os.path.exists(wheel_binary): |
| 141 | + return wheel_binary |
| 142 | + |
| 143 | + # Otherwise, running a locally-built copy, grab it from the build |
| 144 | + # directory. |
| 145 | + build_binary = os.path.abspath(os.path.join(install_path, "..", build_dir, name + ext)) |
| 146 | + if os.path.exists(build_binary): |
| 147 | + return build_binary |
| 148 | + |
| 149 | + raise CompilerNotFoundError("Unable to find {}{} binary.".format(name, ext)) |
| 150 | + |
| 151 | + |
| 152 | +def default_compiler(binary=None): |
| 153 | + """ |
| 154 | + Finds an available compiler. |
| 155 | +
|
| 156 | + Optional arguments: |
| 157 | + - binary: The path to a native or wasm binary. If this is specified then |
| 158 | + no search will be performed, and only this binary will be tried. |
| 159 | + """ |
| 160 | + |
| 161 | + if not binary or not binary.lower().endswith(".wasm"): |
| 162 | + try: |
| 163 | + from .compiler_native import CompilerNative |
| 164 | + |
| 165 | + return CompilerNative(binary=binary) |
| 166 | + except CompilerNotFoundError: |
| 167 | + pass |
| 168 | + |
| 169 | + if not binary or binary.lower().endswith(".wasm"): |
| 170 | + try: |
| 171 | + from .compiler_pywasm3 import CompilerPywasm3 |
| 172 | + |
| 173 | + return CompilerPywasm3(binary=binary) |
| 174 | + except CompilerNotFoundError: |
| 175 | + pass |
| 176 | + |
| 177 | + try: |
| 178 | + from .compiler_wasmtime import CompilerWasmTime |
| 179 | + |
| 180 | + return CompilerWasmTime(binary=binary) |
| 181 | + except CompilerNotFoundError: |
| 182 | + pass |
| 183 | + |
| 184 | + try: |
| 185 | + from .compiler_wasmer import CompilerWasmer |
| 186 | + |
| 187 | + return CompilerWasmer(binary=binary) |
| 188 | + except CompilerNotFoundError: |
| 189 | + pass |
| 190 | + |
| 191 | + if binary: |
| 192 | + raise RuntimeError("Unable to instantiate compiler for {}.".format(binary)) |
| 193 | + else: |
| 194 | + raise RuntimeError("No compiler installed/available.") |
| 195 | + |
| 196 | + |
| 197 | +__all__ = [ |
| 198 | + "CrossCompileError", |
| 199 | + "Compiler", |
| 200 | + "default_compiler", |
| 201 | + "EMIT_BYTECODE", |
| 202 | + "EMIT_NATIVE", |
| 203 | + "EMIT_VIPER", |
| 204 | +] + list(NATIVE_ARCHS.keys()) |
0 commit comments