mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 14:28:40 +00:00
hw_oracle: start build_hw_oracle.cmd as .\ from the work folder
build_command() returned `cmd.exe /d /c <full path of build_hw_oracle.cmd>` for the vcvars toolchain. cmd.exe loses a full path's quotes when it holds & or ^ (and list2cmdline adds none when there is no space), so from a work folder such as `a & b`, `a&b` or `x^y` the script never ran and the oracle reported that hw_oracle.c did not build. The only caller already runs the command with cwd=self.workdir, the folder the script is written to, so it now starts `.\build_hw_oracle.cmd`, which NoDefaultCurrentDirectoryInExePath does not block either. tests/test_hw_oracle.py runs the returned command on a stub vcvars from plain, `sp (x)`, `a & b`, `a&b`, `x^y` and Turkish-named work folders with that variable set, and the tests that read the script open it from the work folder. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -282,7 +282,7 @@ class VcvarsScript(unittest.TestCase):
|
||||
out = os.path.join(work, "hw_oracle.exe")
|
||||
vcvars = os.path.join(work, "vcvars64.bat")
|
||||
argv = runner.build_command("vcvars", vcvars, src, out, work)
|
||||
with open(argv[-1], "rb") as f:
|
||||
with open(os.path.join(work, argv[-1]), "rb") as f: # the script runs with cwd=work
|
||||
return argv, f.read(), (src, out, vcvars)
|
||||
|
||||
def test_paths_outside_ascii_are_written_as_utf8(self):
|
||||
@ -309,7 +309,7 @@ class VcvarsScript(unittest.TestCase):
|
||||
work = os.path.join(tmp, "work")
|
||||
os.makedirs(work)
|
||||
argv = runner.build_command("vcvars", "vcvars64.bat", "hw_oracle.c", "hw_oracle.exe", work)
|
||||
with open(argv[-1], "rb") as f:
|
||||
with open(os.path.join(work, argv[-1]), "rb") as f:
|
||||
lines = f.read().decode("ascii").split("\r\n")
|
||||
self.assertEqual(lines[3], 'call "vcvars64.bat" >nul || exit /b 1')
|
||||
self.assertTrue(lines[4].startswith("cl /nologo /O1 hw_oracle.c /Fehw_oracle.exe /Fo"))
|
||||
@ -323,6 +323,26 @@ class VcvarsScript(unittest.TestCase):
|
||||
self.assertEqual(runner.build_command("msvc", "cl.exe", "a.c", "a.exe", work)[0], "cl.exe")
|
||||
self.assertEqual(os.listdir(work), [])
|
||||
|
||||
@unittest.skipUnless(sys.platform == "win32", "runs the script through cmd.exe")
|
||||
def test_the_script_runs_from_any_work_folder(self):
|
||||
# A full path through `cmd.exe /c` loses its quotes when the folder holds & or ^, and a bare name is not
|
||||
# found with NoDefaultCurrentDirectoryInExePath set. A stub vcvars shows the script got that far.
|
||||
broken = []
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
stub = os.path.join(tmp, "stub_vcvars.bat")
|
||||
with open(stub, "wb") as f:
|
||||
f.write(b'@echo off\r\necho reached> "%LEONIDA_TEST_MARKER%"\r\nexit /b 0\r\n')
|
||||
for name in ("plain", "sp (x)", "a & b", "a&b", "x^y", self.NAME):
|
||||
work = os.path.join(tmp, name)
|
||||
os.makedirs(work)
|
||||
marker = os.path.join(work, "reached.txt")
|
||||
argv = runner.build_command("vcvars", stub, "hw_oracle.c", "hw_oracle.exe", work)
|
||||
env = dict(os.environ, LEONIDA_TEST_MARKER=marker, NoDefaultCurrentDirectoryInExePath="1")
|
||||
subprocess.run(argv, cwd=work, env=env, capture_output=True, timeout=60) # cl itself is absent
|
||||
if not os.path.exists(marker):
|
||||
broken.append(name)
|
||||
self.assertEqual(broken, [], "build_hw_oracle.cmd did not run from these work folders")
|
||||
|
||||
|
||||
class Unavailable(unittest.TestCase):
|
||||
def test_missing_tools_are_reported_not_raised_as_crashes(self):
|
||||
@ -371,7 +391,7 @@ class Unavailable(unittest.TestCase):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
argv = runner.build_command("vcvars", r"C:\VS 2022\vcvars64.bat", "a.c", "o.exe", tmp)
|
||||
self.assertEqual(argv[:3], ["cmd.exe", "/d", "/c"])
|
||||
script = Path(argv[3]).read_text(encoding="ascii")
|
||||
script = (Path(tmp) / argv[3]).read_text(encoding="ascii")
|
||||
self.assertIn('call "C:\\VS 2022\\vcvars64.bat"', script) # quoted: the path has a space
|
||||
self.assertIn("cl /nologo /O1 a.c /Feo.exe", script)
|
||||
self.assertIn("Installer", script) # vcvars needs vswhere on PATH
|
||||
|
||||
@ -125,7 +125,7 @@ def find_cc():
|
||||
|
||||
|
||||
def build_command(kind, cc, src, out, workdir):
|
||||
"""argv that compiles src into out; MSVC's objects and the vcvars script go to workdir."""
|
||||
"""argv that compiles src into out; MSVC's objects and the vcvars script go to workdir, the cwd to run it in."""
|
||||
if kind == "gcc":
|
||||
return [cc, "-O1", src, "-o", out]
|
||||
cl = [cc if kind == "msvc" else "cl", "/nologo", "/O1", src, f"/Fe{out}", f"/Fo{os.path.join(workdir, '')}"]
|
||||
@ -135,13 +135,16 @@ def build_command(kind, cc, src, out, workdir):
|
||||
# vcvars64.bat runs vswhere itself, so its folder goes on PATH first (as scripts/build_llvm_mc.ps1 does).
|
||||
installer = os.path.join(os.environ.get("ProgramFiles(x86)", r"C:\Program Files (x86)"),
|
||||
"Microsoft Visual Studio", "Installer")
|
||||
script = os.path.join(workdir, "build_hw_oracle.cmd")
|
||||
name = "build_hw_oracle.cmd"
|
||||
script = os.path.join(workdir, name)
|
||||
# Written as UTF-8, with chcp 65001 so cmd.exe reads it as UTF-8 on any console code page: the checkout, %TEMP%
|
||||
# and Visual Studio paths in here reach cl intact when one has a character outside ASCII (a user name, say).
|
||||
with open(script, "w", encoding="utf-8", newline="\n") as f:
|
||||
f.write(f'@echo off\r\nchcp 65001 >nul\r\nset "PATH={installer};%PATH%"\r\ncall "{cc}" >nul || exit /b 1\r\n'
|
||||
+ subprocess.list2cmdline(cl) + "\r\n")
|
||||
return ["cmd.exe", "/d", "/c", script]
|
||||
# Relative to workdir, not the full path: cmd.exe /c loses a full path's quotes when it holds & or ^ (and
|
||||
# list2cmdline adds none without a space), and `.\` is still found with NoDefaultCurrentDirectoryInExePath set.
|
||||
return ["cmd.exe", "/d", "/c", "." + os.sep + name]
|
||||
|
||||
|
||||
def detect_arch(clang):
|
||||
|
||||
Reference in New Issue
Block a user