"""Compile the Quirrel command line tool (`sq`) from a source checkout. The source list is read from the checkout's own CMakeLists.txt files, so a newer Quirrel checkout builds without editing this file. CMake itself is not needed: MSVC's `cl` is driven directly through a response file. The checkout is expected at build/quirrel-source (git-ignored); docs/implementation notes records which commit it is pinned to. """ from __future__ import annotations import re import subprocess from pathlib import Path DEFAULT_VCVARS = Path("C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Auxiliary/Build/vcvars64.bat") CHECKER_NAME = "sq.exe" # (folder, CMake variable) pairs whose files make up the command line tool. _SOURCE_LISTS = ( ("squirrel", "SQUIRREL_SRC"), ("squirrel/compiler", "COMPILER_SRC"), ("sqstdlib", "SQSTDLIB_SRC"), ("sqmodules", "SQMODULES_SRC"), ) # Chosen by an if() in CMake rather than listed: we build without variable tracing. _EXTRA_SOURCES = ("squirrel/vartracestub.cpp", "sq/sq.cpp", "sq/sq_test_natives.cpp") _INCLUDE_DIRS = ("include", "internal", "squirrel", "squirrel/compiler", "helpers", "sqrat/include", "sqmodules", ".") _FLAGS = ( "/nologo", "/utf-8", "/EHsc", "/std:c++17", "/O1", "/MP", "/D_SQ64", "/DSQ_VAR_TRACE_ENABLED=0", "/D_CRT_SECURE_NO_WARNINGS", "/wd4244", "/wd4018", "/wd4267", ) class CheckerBuildError(Exception): pass def build_checker(source: Path, out_dir: Path, vcvars: Path = DEFAULT_VCVARS) -> Path: """Compile `sq.exe` into `out_dir` and return its path. Raises CheckerBuildError.""" if not vcvars.is_file(): raise CheckerBuildError(f"MSVC environment script not found: {vcvars}") sources = collect_sources(source) out_dir.mkdir(parents=True, exist_ok=True) response = out_dir / "sources.rsp" response.write_text("\n".join(_response_lines(source, sources)) + "\n", encoding="utf-8") # A single string, not a list: cmd.exe needs the inner quotes verbatim, and /s makes it # strip only the outermost pair. command = f'cmd /d /s /c "call "{vcvars}" >nul && cl @sources.rsp"' completed = subprocess.run(command, cwd=out_dir, capture_output=True, text=True) checker = out_dir / CHECKER_NAME if completed.returncode != 0 or not checker.is_file(): tail = "\n".join((completed.stdout + completed.stderr).splitlines()[-30:]) raise CheckerBuildError(f"compiling the checker failed (exit {completed.returncode}):\n{tail}") return checker def collect_sources(source: Path) -> list[Path]: files: list[Path] = [] for folder, variable in _SOURCE_LISTS: files.extend(_cmake_list(source / folder, variable)) files.extend(source / item for item in _EXTRA_SOURCES) missing = [path for path in files if not path.is_file()] if missing: raise CheckerBuildError(f"source checkout is incomplete, missing: {', '.join(map(str, missing[:5]))}") return files def _cmake_list(folder: Path, variable: str) -> list[Path]: cmake = folder / "CMakeLists.txt" if not cmake.is_file(): raise CheckerBuildError(f"not a Quirrel source checkout: {cmake} is missing") match = re.search(rf"set\({variable}\s+([^)]*)\)", cmake.read_text("utf-8")) if match is None: raise CheckerBuildError(f"{cmake} does not define {variable}") return [folder / name for name in match[1].split()] def _response_lines(source: Path, sources: list[Path]) -> list[str]: includes = [f'/I"{(source / item).resolve()}"' for item in _INCLUDE_DIRS] files = [f'"{path.resolve()}"' for path in sources] return [*_FLAGS, *includes, *files, f"/Fe:{CHECKER_NAME}"]