Skip to content

Commit 11be378

Browse files
[mypyc] Add support for building mypyc code on WASM (python#13446)
### Description This PR tweaks two things about how mypyc generates and builds C code to better support WebAssembly. First, we search `sysconfig` for the size of `size_t`, which works much better for cross-compiling. Second, newer versions of clang have `-Wno-unused-but-set-variable` and so it is added to the default list of arguments (this should probably land regardless the decision on merging this PR). ## Test Plan This PR depends on python#13445. To test this PR, you can do the following: *assuming mypy checkout with both PRs applied, must be on Python 3.10(!)* ``` $ pip install pyodide-build $ pyodide build --exports pyinit backend-args --global-option=--use-mypyc ``` Note: you will get a warning about using `--global-option`, you can ignore it for now. I'm trying to find out why `--build-option` isn't working... Co-authored-by: Shantanu <[email protected]>
1 parent 7c14fee commit 11be378

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

mypy/util.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ class FancyFormatter:
525525
def __init__(self, f_out: IO[str], f_err: IO[str], show_error_codes: bool) -> None:
526526
self.show_error_codes = show_error_codes
527527
# Check if we are in a human-facing terminal on a supported platform.
528-
if sys.platform not in ("linux", "darwin", "win32"):
528+
if sys.platform not in ("linux", "darwin", "win32", "emscripten"):
529529
self.dummy_term = True
530530
return
531531
force_color = int(os.getenv("MYPY_FORCE_COLOR", "0"))
@@ -534,6 +534,8 @@ def __init__(self, f_out: IO[str], f_err: IO[str], show_error_codes: bool) -> No
534534
return
535535
if sys.platform == "win32":
536536
self.dummy_term = not self.initialize_win_colors()
537+
elif sys.platform == "emscripten":
538+
self.dummy_term = not self.initialize_vt100_colors()
537539
else:
538540
self.dummy_term = not self.initialize_unix_colors()
539541
if not self.dummy_term:
@@ -545,6 +547,20 @@ def __init__(self, f_out: IO[str], f_err: IO[str], show_error_codes: bool) -> No
545547
"none": "",
546548
}
547549

550+
def initialize_vt100_colors(self) -> bool:
551+
"""Return True if initialization was successful and we can use colors, False otherwise"""
552+
# Windows and Emscripten can both use ANSI/VT100 escape sequences for color
553+
assert sys.platform in ("win32", "emscripten")
554+
self.BOLD = "\033[1m"
555+
self.UNDER = "\033[4m"
556+
self.BLUE = "\033[94m"
557+
self.GREEN = "\033[92m"
558+
self.RED = "\033[91m"
559+
self.YELLOW = "\033[93m"
560+
self.NORMAL = "\033[0m"
561+
self.DIM = "\033[2m"
562+
return True
563+
548564
def initialize_win_colors(self) -> bool:
549565
"""Return True if initialization was successful and we can use colors, False otherwise"""
550566
# Windows ANSI escape sequences are only supported on Threshold 2 and above.
@@ -571,14 +587,7 @@ def initialize_win_colors(self) -> bool:
571587
| ENABLE_WRAP_AT_EOL_OUTPUT
572588
| ENABLE_VIRTUAL_TERMINAL_PROCESSING,
573589
)
574-
self.BOLD = "\033[1m"
575-
self.UNDER = "\033[4m"
576-
self.BLUE = "\033[94m"
577-
self.GREEN = "\033[92m"
578-
self.RED = "\033[91m"
579-
self.YELLOW = "\033[93m"
580-
self.NORMAL = "\033[0m"
581-
self.DIM = "\033[2m"
590+
self.initialize_vt100_colors()
582591
return True
583592
return False
584593

mypyc/build.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -533,10 +533,8 @@ def mypycify(
533533
"-Wno-unused-variable",
534534
"-Wno-unused-command-line-argument",
535535
"-Wno-unknown-warning-option",
536+
"-Wno-unused-but-set-variable",
536537
]
537-
if "gcc" in compiler.compiler[0] or "gnu-cc" in compiler.compiler[0]:
538-
# This flag is needed for gcc but does not exist on clang.
539-
cflags += ["-Wno-unused-but-set-variable"]
540538
elif compiler.compiler_type == "msvc":
541539
# msvc doesn't have levels, '/O2' is full and '/Od' is disable
542540
if opt_level == "0":

mypyc/common.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import sys
4+
import sysconfig
45
from typing import Any, Dict
56
from typing_extensions import Final
67

@@ -30,7 +31,16 @@
3031
# Maximal number of subclasses for a class to trigger fast path in isinstance() checks.
3132
FAST_ISINSTANCE_MAX_SUBCLASSES: Final = 2
3233

33-
IS_32_BIT_PLATFORM: Final = sys.maxsize < (1 << 31)
34+
# Size of size_t, if configured.
35+
SIZEOF_SIZE_T_SYSCONFIG: Final = sysconfig.get_config_var("SIZEOF_SIZE_T")
36+
37+
SIZEOF_SIZE_T: Final = (
38+
int(SIZEOF_SIZE_T_SYSCONFIG)
39+
if SIZEOF_SIZE_T_SYSCONFIG is not None
40+
else (sys.maxsize + 1).bit_length() // 8
41+
)
42+
43+
IS_32_BIT_PLATFORM: Final = int(SIZEOF_SIZE_T) == 4
3444

3545
PLATFORM_SIZE = 4 if IS_32_BIT_PLATFORM else 8
3646

@@ -42,15 +52,16 @@
4252
IS_MIXED_32_64_BIT_BUILD: Final = sys.platform in ["darwin"] and sys.version_info < (3, 6)
4353

4454
# Maximum value for a short tagged integer.
45-
MAX_SHORT_INT: Final = sys.maxsize >> 1
55+
MAX_SHORT_INT: Final = 2 ** (8 * int(SIZEOF_SIZE_T) - 2) - 1
56+
4657
# Minimum value for a short tagged integer.
47-
MIN_SHORT_INT: Final = -(sys.maxsize >> 1) - 1
58+
MIN_SHORT_INT: Final = -(MAX_SHORT_INT) - 1
4859

4960
# Maximum value for a short tagged integer represented as a C integer literal.
5061
#
5162
# Note: Assume that the compiled code uses the same bit width as mypyc, except for
5263
# Python 3.5 on macOS.
53-
MAX_LITERAL_SHORT_INT: Final = sys.maxsize >> 1 if not IS_MIXED_32_64_BIT_BUILD else 2**30 - 1
64+
MAX_LITERAL_SHORT_INT: Final = MAX_SHORT_INT if not IS_MIXED_32_64_BIT_BUILD else 2**30 - 1
5465
MIN_LITERAL_SHORT_INT: Final = -MAX_LITERAL_SHORT_INT - 1
5566

5667
# Decription of the C type used to track the definedness of attributes and

0 commit comments

Comments
 (0)