40
40
import webbrowser
41
41
42
42
# for Python 3.8
43
- from typing import Any , Callable , Dict , Iterable , List , Optional , Tuple , Union
43
+ from typing import (
44
+ cast ,
45
+ Any ,
46
+ Callable ,
47
+ Dict ,
48
+ Iterable ,
49
+ List ,
50
+ Optional ,
51
+ Tuple ,
52
+ Union ,
53
+ )
44
54
45
55
logger = logging .getLogger ("wasm_build" )
46
56
64
74
(3 , 1 , 16 ): "https://github.com/emscripten-core/emscripten/issues/17393" ,
65
75
(3 , 1 , 20 ): "https://github.com/emscripten-core/emscripten/issues/17720" ,
66
76
}
67
- _MISSING = pathlib .PurePath ("MISSING" )
77
+ _MISSING = pathlib .Path ("MISSING" )
68
78
69
79
WASM_WEBSERVER = WASMTOOLS / "wasm_webserver.py"
70
80
109
119
110
120
def parse_emconfig (
111
121
emconfig : pathlib .Path = EM_CONFIG ,
112
- ) -> Tuple [pathlib .PurePath , pathlib .PurePath ]:
122
+ ) -> Tuple [pathlib .Path , pathlib .Path ]:
113
123
"""Parse EM_CONFIG file and lookup EMSCRIPTEN_ROOT and NODE_JS.
114
124
115
125
The ".emscripten" config file is a Python snippet that uses "EM_CONFIG"
@@ -150,11 +160,11 @@ def read_python_version(configure: pathlib.Path = CONFIGURE) -> str:
150
160
151
161
152
162
class ConditionError (ValueError ):
153
- def __init__ (self , info : str , text : str ):
163
+ def __init__ (self , info : str , text : str ) -> None :
154
164
self .info = info
155
165
self .text = text
156
166
157
- def __str__ (self ):
167
+ def __str__ (self ) -> str :
158
168
return f"{ type (self ).__name__ } : '{ self .info } '\n { self .text } "
159
169
160
170
@@ -180,19 +190,19 @@ class Platform:
180
190
name : str
181
191
pythonexe : str
182
192
config_site : Optional [pathlib .PurePath ]
183
- configure_wrapper : Optional [pathlib .PurePath ]
193
+ configure_wrapper : Optional [pathlib .Path ]
184
194
make_wrapper : Optional [pathlib .PurePath ]
185
- environ : dict
195
+ environ : Dict [ str , Any ]
186
196
check : Callable [[], None ]
187
197
# Used for build_emports().
188
198
ports : Optional [pathlib .PurePath ]
189
199
cc : Optional [pathlib .PurePath ]
190
200
191
- def getenv (self , profile : "BuildProfile" ) -> dict :
201
+ def getenv (self , profile : "BuildProfile" ) -> Dict [ str , Any ] :
192
202
return self .environ .copy ()
193
203
194
204
195
- def _check_clean_src ():
205
+ def _check_clean_src () -> None :
196
206
candidates = [
197
207
SRCDIR / "Programs" / "python.o" ,
198
208
SRCDIR / "Python" / "frozen_modules" / "importlib._bootstrap.h" ,
@@ -202,7 +212,7 @@ def _check_clean_src():
202
212
raise DirtySourceDirectory (os .fspath (candidate ), CLEAN_SRCDIR )
203
213
204
214
205
- def _check_native ():
215
+ def _check_native () -> None :
206
216
if not any (shutil .which (cc ) for cc in ["cc" , "gcc" , "clang" ]):
207
217
raise MissingDependency ("cc" , INSTALL_NATIVE )
208
218
if not shutil .which ("make" ):
@@ -234,12 +244,12 @@ def _check_native():
234
244
)
235
245
236
246
237
- def _check_emscripten ():
247
+ def _check_emscripten () -> None :
238
248
if EMSCRIPTEN_ROOT is _MISSING :
239
249
raise MissingDependency ("Emscripten SDK EM_CONFIG" , INSTALL_EMSDK )
240
250
# sanity check
241
251
emconfigure = EMSCRIPTEN .configure_wrapper
242
- if not emconfigure .exists ():
252
+ if emconfigure is not None and not emconfigure .exists ():
243
253
raise MissingDependency (os .fspath (emconfigure ), INSTALL_EMSDK )
244
254
# version check
245
255
version_txt = EMSCRIPTEN_ROOT / "emscripten-version.txt"
@@ -250,7 +260,10 @@ def _check_emscripten():
250
260
if version .endswith ("-git" ):
251
261
# git / upstream / tot-upstream installation
252
262
version = version [:- 4 ]
253
- version_tuple = tuple (int (v ) for v in version .split ("." ))
263
+ version_tuple = cast (
264
+ Tuple [int , int , int ],
265
+ tuple (int (v ) for v in version .split ("." ))
266
+ )
254
267
if version_tuple < EMSDK_MIN_VERSION :
255
268
raise ConditionError (
256
269
os .fspath (version_txt ),
@@ -293,7 +306,7 @@ def _check_emscripten():
293
306
)
294
307
295
308
296
- def _check_wasi ():
309
+ def _check_wasi () -> None :
297
310
wasm_ld = WASI_SDK_PATH / "bin" / "wasm-ld"
298
311
if not wasm_ld .exists ():
299
312
raise MissingDependency (os .fspath (wasm_ld ), INSTALL_WASI_SDK )
@@ -400,7 +413,7 @@ class EmscriptenTarget(enum.Enum):
400
413
node_debug = "node-debug"
401
414
402
415
@property
403
- def is_browser (self ):
416
+ def is_browser (self ) -> bool :
404
417
cls = type (self )
405
418
return self in {cls .browser , cls .browser_debug }
406
419
@@ -421,7 +434,7 @@ class SupportLevel(enum.Enum):
421
434
experimental = "experimental, may be broken"
422
435
broken = "broken / unavailable"
423
436
424
- def __bool__ (self ):
437
+ def __bool__ (self ) -> bool :
425
438
cls = type (self )
426
439
return self in {cls .supported , cls .working }
427
440
@@ -500,7 +513,7 @@ def make_cmd(self) -> List[str]:
500
513
cmd .insert (0 , os .fspath (platform .make_wrapper ))
501
514
return cmd
502
515
503
- def getenv (self ) -> dict :
516
+ def getenv (self ) -> Dict [ str , Any ] :
504
517
"""Generate environ dict for platform"""
505
518
env = os .environ .copy ()
506
519
env .setdefault ("MAKEFLAGS" , f"-j{ os .cpu_count ()} " )
@@ -529,7 +542,7 @@ def _run_cmd(
529
542
cmd : Iterable [str ],
530
543
args : Iterable [str ] = (),
531
544
cwd : Optional [pathlib .Path ] = None ,
532
- ):
545
+ ) -> int :
533
546
cmd = list (cmd )
534
547
cmd .extend (args )
535
548
if cwd is None :
@@ -541,46 +554,46 @@ def _run_cmd(
541
554
env = self .getenv (),
542
555
)
543
556
544
- def _check_execute (self ):
557
+ def _check_execute (self ) -> None :
545
558
if self .is_browser :
546
559
raise ValueError (f"Cannot execute on { self .target } " )
547
560
548
- def run_build (self , * args ) :
561
+ def run_build (self , * args : str ) -> None :
549
562
"""Run configure (if necessary) and make"""
550
563
if not self .makefile .exists ():
551
564
logger .info ("Makefile not found, running configure" )
552
565
self .run_configure (* args )
553
566
self .run_make ("all" , * args )
554
567
555
- def run_configure (self , * args ) :
568
+ def run_configure (self , * args : str ) -> int :
556
569
"""Run configure script to generate Makefile"""
557
570
os .makedirs (self .builddir , exist_ok = True )
558
571
return self ._run_cmd (self .configure_cmd , args )
559
572
560
- def run_make (self , * args ) :
573
+ def run_make (self , * args : str ) -> int :
561
574
"""Run make (defaults to build all)"""
562
575
return self ._run_cmd (self .make_cmd , args )
563
576
564
- def run_pythoninfo (self , * args ) :
577
+ def run_pythoninfo (self , * args : str ) -> int :
565
578
"""Run 'make pythoninfo'"""
566
579
self ._check_execute ()
567
580
return self .run_make ("pythoninfo" , * args )
568
581
569
- def run_test (self , target : str , testopts : Optional [str ] = None ):
582
+ def run_test (self , target : str , testopts : Optional [str ] = None ) -> int :
570
583
"""Run buildbottests"""
571
584
self ._check_execute ()
572
585
if testopts is None :
573
586
testopts = self .default_testopts
574
587
return self .run_make (target , f"TESTOPTS={ testopts } " )
575
588
576
- def run_py (self , * args ) :
589
+ def run_py (self , * args : str ) -> int :
577
590
"""Run Python with hostrunner"""
578
591
self ._check_execute ()
579
- self .run_make (
592
+ return self .run_make (
580
593
"--eval" , f"run: all; $(HOSTRUNNER) ./$(PYTHON) { shlex .join (args )} " , "run"
581
594
)
582
595
583
- def run_browser (self , bind = "127.0.0.1" , port = 8000 ):
596
+ def run_browser (self , bind : str = "127.0.0.1" , port : int = 8000 ) -> None :
584
597
"""Run WASM webserver and open build in browser"""
585
598
relbuilddir = self .builddir .relative_to (SRCDIR )
586
599
url = f"http://{ bind } :{ port } /{ relbuilddir } /python.html"
@@ -611,15 +624,15 @@ def run_browser(self, bind="127.0.0.1", port=8000):
611
624
except KeyboardInterrupt :
612
625
pass
613
626
614
- def clean (self , all : bool = False ):
627
+ def clean (self , all : bool = False ) -> None :
615
628
"""Clean build directory"""
616
629
if all :
617
630
if self .builddir .exists ():
618
631
shutil .rmtree (self .builddir )
619
632
elif self .makefile .exists ():
620
633
self .run_make ("clean" )
621
634
622
- def build_emports (self , force : bool = False ):
635
+ def build_emports (self , force : bool = False ) -> None :
623
636
"""Pre-build emscripten ports."""
624
637
platform = self .host .platform
625
638
if platform .ports is None or platform .cc is None :
@@ -829,7 +842,7 @@ def build_emports(self, force: bool = False):
829
842
)
830
843
831
844
832
- def main ():
845
+ def main () -> None :
833
846
args = parser .parse_args ()
834
847
logging .basicConfig (
835
848
level = logging .INFO if args .verbose else logging .ERROR ,
0 commit comments