Debian Codegraph @

cross-project-tests/dtlto/test_temps.py

raw
1 """
2 This script works in tandem with local_codegen_and_wait.py. By coordinating
3 via the "send-signal*" files, the scripts ensure that the requested action is
4 performed after all DTLTO backend compilations have completed but before
5 DTLTO itself finishes. At this point, DTLTO temporary files have been
6 created but have not yet been cleaned up.
7
8 Usage:
9 %python test_temps.py <output_dir> <action> <command...>
10
11 Run <command>, which must be a ThinLTO link invocation that uses
12 local_codegen_and_wait.py as the ThinLTO distributor. The script waits for
13 the "send-signal1" file to appear, performs <action>, and then creates the
14 "send-signal2" file to allow the link to continue.
15
16 Actions:
17 kill Send an interrupt to cause <command> to terminate after the
18 ThinLTO backend compilations complete.
19 lock (Windows only) Hold open handles to DTLTO temporary files in
20 <output_dir> to prevent their deletion after the ThinLTO backend
21 compilations complete. This action is not supported on Linux, as
22 there is no reliable mechanism to prevent file deletion that is
23 guaranteed to be released when the script exits (AFAICT).
24 remove Delete non-essential files in <output_dir> after the ThinLTO
25 backend compilations complete.
26 """
27
28 import os
29 import signal
30 import subprocess
31 import sys
32 import time
33 from pathlib import Path
34
35 IS_WIN = os.name == "nt"
36 SIGNAL1 = Path("send-signal1")
37 SIGNAL2 = Path("send-signal2")
38
39 if IS_WIN:
40 import ctypes
41 from ctypes import wintypes
42
43 CreateFileW = ctypes.WinDLL("kernel32", use_last_error=True).CreateFileW
44 CreateFileW.argtypes = [
45 wintypes.LPCWSTR, # lpFileName
46 wintypes.DWORD, # dwDesiredAccess
47 wintypes.DWORD, # dwShareMode
48 wintypes.LPVOID, # lpSecurityAttributes
49 wintypes.DWORD, # dwCreationDisposition
50 wintypes.DWORD, # dwFlagsAndAttributes
51 wintypes.HANDLE, # hTemplateFile
52 ]
53 CreateFileW.restype = wintypes.HANDLE
54
55
56 def lock_no_delete_share(path):
57 # Windows-specific: deny FILE_SHARE_DELETE by omitting it from dwShareMode.
58 h = CreateFileW(
59 path,
60 0x80000000, # GENERIC_READ
61 0x00000003, # FILE_SHARE_READ/WRITE (no FILE_SHARE_DELETE)
62 None, # lpSecurityAttributes
63 3, # OPEN_EXISTING
64 0, # dwFlagsAndAttributes
65 None, # hTemplateFile
66 )
67 if h == wintypes.HANDLE(-1).value:
68 err = ctypes.get_last_error()
69 raise OSError(err, f"CreateFileW failed ({err}) for: {path}")
70 return h
71
72
73 output_dir = Path(sys.argv[1])
74 action = sys.argv[2]
75
76 # "lock" is Windows-only; fail early if invoked elsewhere.
77 if action == "lock" and not IS_WIN:
78 print("error: action 'lock' is only supported on Windows", file=sys.stderr)
79 sys.exit(1)
80
81 # Remove any pre-existing signal files from previous script runs.
82 SIGNAL1.unlink(missing_ok=True)
83 SIGNAL2.unlink(missing_ok=True)
84
85 kwargs = {}
86 if action == "kill":
87 if IS_WIN:
88 # CREATE_NEW_PROCESS_GROUP is used so that p.send_signal(CTRL_BREAK_EVENT)
89 # does not get sent to the LIT processes that are running the test.
90 kwargs = {"creationflags": subprocess.CREATE_NEW_PROCESS_GROUP}
91 else:
92 # Makes the child a process-group leader so os.killpg(p.pid, SIGINT) works.
93 kwargs = {"start_new_session": True}
94
95 p = subprocess.Popen(sys.argv[3:], **kwargs)
96
97 while not SIGNAL1.exists() and p.poll() is None:
98 time.sleep(0.05)
99 if p.poll() is not None:
100 sys.exit(1)
101
102 if action == "kill":
103 if IS_WIN:
104 # Note that CTRL_C_EVENT does not appear to work for clang.
105 p.send_signal(signal.CTRL_BREAK_EVENT)
106 else:
107 os.killpg(p.pid, signal.SIGINT)
108
109 if action == "lock":
110 print("Lock any files in the output directory.")
111 for f in output_dir.iterdir():
112 if f.is_file():
113 lock_no_delete_share(str(f))
114
115 if action == "remove":
116 print("Remove non-essential files in the output directory.")
117 for f in output_dir.iterdir():
118 if f.is_file() and not f.name.endswith("native.o"):
119 f.unlink()
120
121 SIGNAL2.touch()
122
123 if action == "kill":
124 # Expect termination: succeed if the child fails, fail if it exits cleanly.
125 p.wait()
126 sys.exit(0 if p.returncode != 0 else 1)
127
128 sys.exit(p.wait())