|
1 | 1 | from enum import Enum |
2 | 2 | import subprocess |
3 | 3 | import sys |
4 | | -import tempfile |
5 | 4 | import pathlib |
6 | 5 | import platform |
7 | 6 | from typing import List, Optional, Tuple |
@@ -75,44 +74,32 @@ def mpy_cross_compile( |
75 | 74 | ... |
76 | 75 |
|
77 | 76 | """ |
78 | | - with tempfile.TemporaryDirectory() as tmp_dir_name: |
79 | | - tmp_dir = pathlib.Path(tmp_dir_name) |
| 77 | + args: list[str] = [MPY_CROSS_PATH, "-", "-s", file_name] |
80 | 78 |
|
81 | | - with open(tmp_dir / "tmp.py", "w") as in_file: |
82 | | - in_file.write(file_contents) |
| 79 | + if optimization_level is not None: |
| 80 | + if optimization_level not in range(4): |
| 81 | + raise ValueError("optimization_level must be between 0 and 3") |
83 | 82 |
|
84 | | - args: list[str] = [MPY_CROSS_PATH, in_file.name, "-s", file_name] |
| 83 | + args.append(f"-O{optimization_level}") |
85 | 84 |
|
86 | | - if optimization_level is not None: |
87 | | - if optimization_level not in range(4): |
88 | | - raise ValueError("optimization_level must be between 0 and 3") |
| 85 | + if small_number_bits is not None: |
| 86 | + args.append(f"-msmall-int-bits={small_number_bits}") |
89 | 87 |
|
90 | | - args.append(f"-O{optimization_level}") |
| 88 | + if arch is not None: |
| 89 | + args.append(f"-march={arch.value}") |
91 | 90 |
|
92 | | - if small_number_bits is not None: |
93 | | - args.append(f"-msmall-int-bits={small_number_bits}") |
| 91 | + if emit is not None: |
| 92 | + args += ["-X", f"emit={emit.value}"] |
94 | 93 |
|
95 | | - if arch is not None: |
96 | | - args.append(f"-march={arch.value}") |
| 94 | + if heap_size is not None: |
| 95 | + args += ["-X", f"heapsize={heap_size}"] |
97 | 96 |
|
98 | | - if emit is not None: |
99 | | - args += ["-X", f"emit={emit.value}"] |
| 97 | + if extra_args: |
| 98 | + args += extra_args |
100 | 99 |
|
101 | | - if heap_size is not None: |
102 | | - args += ["-X", f"heapsize={heap_size}"] |
| 100 | + process = subprocess.run(args, capture_output=True, input=file_contents.encode()) |
103 | 101 |
|
104 | | - if extra_args: |
105 | | - args += extra_args |
106 | | - |
107 | | - process = subprocess.run(args, capture_output=True) |
108 | | - |
109 | | - try: |
110 | | - with open(tmp_dir / "tmp.mpy", "rb") as out_file: |
111 | | - data = out_file.read() |
112 | | - except OSError: |
113 | | - data = None |
114 | | - |
115 | | - return process, data |
| 102 | + return process, process.stdout if process.returncode == 0 else None |
116 | 103 |
|
117 | 104 |
|
118 | 105 | def mpy_cross_version() -> str: |
|
0 commit comments