Lines Matching +full:python +full:- +full:pathlib
2 """Build script for Python on WebAssembly platforms.
4 $ ./Tools/wasm/wasm_builder.py emscripten-browser build repl
5 $ ./Tools/wasm/wasm_builder.py emscripten-node-dl build test
8 Primary build targets are "emscripten-node-dl" (NodeJS, dynamic linking),
9 "emscripten-browser", and "wasi".
16 and falls back to /opt/wasi-sdk.
18 The 'build' Python interpreter must be rebuilt every time Python's byte code
21 ./Tools/wasm/wasm_builder.py --clean build build
29 import pathlib
42 # for Python 3.8
47 SRCDIR = pathlib.Path(__file__).parent.parent.parent.absolute()
55 # path to WASI-SDK root
56 WASI_SDK_PATH = pathlib.Path(os.environ.get("WASI_SDK_PATH", "/opt/wasi-sdk"))
59 # auto-detect's EMSDK in /opt/emsdk without ". emsdk_env.sh".
60 EM_CONFIG = pathlib.Path(os.environ.setdefault("EM_CONFIG", "/opt/emsdk/.emscripten"))
63 (3, 1, 14): "https://github.com/emscripten-core/emscripten/issues/17338",
64 (3, 1, 16): "https://github.com/emscripten-core/emscripten/issues/17393",
65 (3, 1, 20): "https://github.com/emscripten-core/emscripten/issues/17720",
67 _MISSING = pathlib.PurePath("MISSING")
73 run "make clean -C '{SRCDIR}'".
77 Builds require a C compiler (gcc, clang), make, pkg-config, and development
80 Debian/Ubuntu: sudo apt install build-essential git curl pkg-config zlib1g-dev
81 Fedora/CentOS: sudo dnf install gcc make git-core curl pkgconfig zlib-devel
85 wasm32-emscripten builds need Emscripten SDK. Please follow instructions at
89 git clone https://github.com/emscripten-core/emsdk.git /path/to/emsdk
97 wasm32-wasi builds need WASI SDK. Please fetch the latest SDK from
98 https://github.com/WebAssembly/wasi-sdk/releases and install it to
99 "/opt/wasi-sdk". Alternatively you can install the SDK in a different location
105 wasm32-wasi tests require wasmtime on PATH. Please follow instructions at
111 emconfig: pathlib.Path = EM_CONFIG,
112 ) -> Tuple[pathlib.PurePath, pathlib.PurePath]:
115 The ".emscripten" config file is a Python snippet that uses "EM_CONFIG"
121 with open(emconfig, encoding="utf-8") as f:
123 # EM_CONFIG file is a Python snippet
126 emscripten_root = pathlib.Path(local["EMSCRIPTEN_ROOT"])
127 node_js = pathlib.Path(local["NODE_JS"])
134 def read_python_version(configure: pathlib.Path = CONFIGURE) -> str:
141 with configure.open(encoding="utf-8") as f:
171 """Platform-specific settings
173 - CONFIG_SITE override
174 - configure wrapper (e.g. emconfigure)
175 - make wrapper (e.g. emmake)
176 - additional environment variables
177 - check function to verify SDK
182 config_site: Optional[pathlib.PurePath]
183 configure_wrapper: Optional[pathlib.PurePath]
184 make_wrapper: Optional[pathlib.PurePath]
188 ports: Optional[pathlib.PurePath]
189 cc: Optional[pathlib.PurePath]
191 def getenv(self, profile: "BuildProfile") -> dict:
197 SRCDIR / "Programs" / "python.o",
198 SRCDIR / "Python" / "frozen_modules" / "importlib._bootstrap.h",
211 # skip pkg-config check on macOS
212 if not shutil.which("pkg-config"):
213 raise MissingDependency("pkg-config", INSTALL_NATIVE)
217 subprocess.check_call(["pkg-config", "--exists", devel])
225 # macOS has python.exe
226 pythonexe=sysconfig.get_config_var("BUILDPYTHON") or "python",
245 version_txt = EMSCRIPTEN_ROOT / "emscripten-version.txt"
250 if version.endswith("-git"):
251 # git / upstream / tot-upstream installation
252 version = version[:-4]
280 pythonexe="python.js",
281 config_site=WASMTOOLS / "config.site-wasm32-emscripten",
287 # workaround for https://github.com/emscripten-core/emscripten/issues/17635
297 wasm_ld = WASI_SDK_PATH / "bin" / "wasm-ld"
308 pythonexe="python.wasm",
309 config_site=WASMTOOLS / "config.site-wasm32-wasi",
310 configure_wrapper=WASMTOOLS / "wasi-env",
316 # workaround for https://github.com/python/cpython/issues/95952
319 "--env PYTHONPATH=/{relbuilddir}/build/lib.wasi-wasm32-{version}:/Lib "
320 "--mapdir /::{srcdir} --"
331 wasm32_emscripten = "wasm32-unknown-emscripten"
332 wasm64_emscripten = "wasm64-unknown-emscripten"
333 wasm32_wasi = "wasm32-unknown-wasi"
334 wasm64_wasi = "wasm64-unknown-wasi"
339 def platform(self) -> Platform:
348 def is_emscripten(self) -> bool:
353 def is_wasi(self) -> bool:
357 def get_extra_paths(self) -> Iterable[pathlib.PurePath]:
358 """Host-specific os.environ["PATH"] entries.
360 Emscripten's Node version 14.x works well for wasm32-emscripten.
361 wasm64-emscripten requires more recent v8 version, e.g. node 16.x.
374 def emport_args(self) -> List[str]:
375 """Host-specific port args (Emscripten)."""
378 return ["-sMEMORY64=1"]
380 return ["-sMEMORY64=0"]
385 def embuilder_args(self) -> List[str]:
386 """Host-specific embuilder args (Emscripten)."""
389 return ["--wasm64"]
395 """Emscripten-specific targets (--with-emscripten-target)"""
398 browser_debug = "browser-debug"
400 node_debug = "node-debug"
408 def emport_args(self) -> List[str]:
409 """Target-specific port args."""
412 # some libs come in debug and non-debug builds
413 return ["-O0"]
415 return ["-O2"]
437 default_testopts: str = "-j2"
440 def is_browser(self) -> bool:
445 def builddir(self) -> pathlib.Path:
450 def python_cmd(self) -> pathlib.Path:
451 """Path to python executable"""
455 def makefile(self) -> pathlib.Path:
460 def configure_cmd(self) -> List[str]:
463 # pathlib.Path.relative_to() does not work here.
465 cmd = [configure, "-C"]
470 cmd.append(f"--host={self.host.value}")
471 cmd.append(f"--build={Host.build.value}")
475 cmd.append(f"--with-emscripten-target={self.target.value}")
480 cmd.append(f"--{opt}-wasm-dynamic-linking")
485 cmd.append(f"--{opt}-wasm-pthreads")
488 cmd.append(f"--with-build-python={BUILD.python_cmd}")
496 def make_cmd(self) -> List[str]:
504 def getenv(self) -> dict:
507 env.setdefault("MAKEFLAGS", f"-j{os.cpu_count()}")
514 new_path: List[pathlib.PurePath] = []
532 cwd: Optional[pathlib.Path] = None,
578 """Run Python with hostrunner"""
581 "--eval", f"run: all; $(HOSTRUNNER) ./$(PYTHON) {shlex.join(args)}", "run"
587 url = f"http://{bind}:{port}/{relbuilddir}/python.html"
591 "--bind",
593 "--port",
624 """Pre-build emscripten ports."""
632 embuilder_cmd.append("--force")
641 ports_cmd.append("-sMAIN_MODULE")
642 embuilder_cmd.append("--pic")
645 # Trigger multi-threaded build.
646 ports_cmd.append("-sUSE_PTHREADS")
648 # Pre-build libbz2, libsqlite3, libz, and some system libs.
649 ports_cmd.extend(["-sUSE_ZLIB", "-sUSE_BZIP2", "-sUSE_SQLITE3"])
650 # Multi-threaded sqlite3 has different suffix
652 ["build", "bzip2", "sqlite3-mt" if self.pthreads else "sqlite3", "zlib"]
657 with tempfile.TemporaryDirectory(suffix="-py-emport") as tmpdir:
658 tmppath = pathlib.Path(tmpdir)
665 "-o",
671 # native build (build Python)
680 # wasm32-emscripten
682 "emscripten-browser",
689 "emscripten-browser-debug",
696 "emscripten-node-dl",
703 "emscripten-node-dl-debug",
710 "emscripten-node-pthreads",
717 "emscripten-node-pthreads-debug",
725 "emscripten-node-dl-pthreads-debug",
732 # wasm64-emscripten (requires Emscripten >= 3.1.21)
734 "wasm64-emscripten-node-debug",
742 # wasm32-wasi
750 # "wasm64-wasi",
765 "--clean",
766 "-c",
772 "--verbose",
773 "-v",
779 "--silent",
785 "--testopts",
788 "--testopts='-v test_os'."
804 build="auto build (build 'build' Python, emports, configure, compile)",
839 cm_args = ("--silent",) if args.silent else ()
858 # auto-build
860 # check and create build Python
862 logger.info("Auto-building 'build' Python.")