Skip to content

Commit 681e9e8

Browse files
authored
Add a clean subcommand to Tools/wasm/wasi.py (GH-114274)
1 parent 229ee5b commit 681e9e8

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

Tools/wasm/wasi.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@
1717

1818

1919
CHECKOUT = pathlib.Path(__file__).parent.parent.parent
20+
2021
CROSS_BUILD_DIR = CHECKOUT / "cross-build"
2122
BUILD_DIR = CROSS_BUILD_DIR / "build"
2223
HOST_TRIPLE = "wasm32-wasi"
2324
HOST_DIR = CROSS_BUILD_DIR / HOST_TRIPLE
2425

26+
LOCAL_SETUP = CHECKOUT / "Modules" / "Setup.local"
27+
LOCAL_SETUP_MARKER = "# Generated by Tools/wasm/wasi.py\n".encode("utf-8")
28+
2529

2630
def updated_env(updates={}):
2731
"""Create a new dict representing the environment to use.
@@ -119,12 +123,11 @@ def build_python_path():
119123
@subdir(BUILD_DIR, clean_ok=True)
120124
def configure_build_python(context, working_dir):
121125
"""Configure the build/host Python."""
122-
local_setup = CHECKOUT / "Modules" / "Setup.local"
123-
if local_setup.exists():
124-
print(f"👍 {local_setup} exists ...")
126+
if LOCAL_SETUP.exists():
127+
print(f"👍 {LOCAL_SETUP} exists ...")
125128
else:
126-
print(f"📝 Touching {local_setup} ...")
127-
local_setup.touch()
129+
print(f"📝 Touching {LOCAL_SETUP} ...")
130+
LOCAL_SETUP.write_bytes(LOCAL_SETUP_MARKER)
128131

129132
configure = [os.path.relpath(CHECKOUT / 'configure', working_dir)]
130133
if context.args:
@@ -260,6 +263,17 @@ def build_all(context):
260263
for step in steps:
261264
step(context)
262265

266+
def clean_contents(context):
267+
"""Delete all files created by this script."""
268+
if CROSS_BUILD_DIR.exists():
269+
print(f"🧹 Deleting {CROSS_BUILD_DIR} ...")
270+
shutil.rmtree(CROSS_BUILD_DIR)
271+
272+
if LOCAL_SETUP.exists():
273+
with LOCAL_SETUP.open("rb") as file:
274+
if file.read(len(LOCAL_SETUP_MARKER)) == LOCAL_SETUP_MARKER:
275+
print(f"🧹 Deleting generated {LOCAL_SETUP} ...")
276+
263277

264278
def main():
265279
default_host_runner = (f"{shutil.which('wasmtime')} run "
@@ -290,11 +304,13 @@ def main():
290304
"Python)")
291305
make_host = subcommands.add_parser("make-host",
292306
help="Run `make` for the host/WASI")
307+
clean = subcommands.add_parser("clean", help="Delete files and directories "
308+
"created by this script")
293309
for subcommand in build, configure_build, make_build, configure_host, make_host:
294310
subcommand.add_argument("--quiet", action="store_true", default=False,
295311
dest="quiet",
296312
help="Redirect output from subprocesses to a log file")
297-
for subcommand in build, configure_build, configure_host:
313+
for subcommand in configure_build, configure_host:
298314
subcommand.add_argument("--clean", action="store_true", default=False,
299315
dest="clean",
300316
help="Delete any relevant directories before building")
@@ -319,7 +335,8 @@ def main():
319335
"make-build-python": make_build_python,
320336
"configure-host": configure_wasi_python,
321337
"make-host": make_wasi_python,
322-
"build": build_all}
338+
"build": build_all,
339+
"clean": clean_contents}
323340
dispatch[context.subcommand](context)
324341

325342

0 commit comments

Comments
 (0)