Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions tests/test_stale_build_include_dirs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
"""stale_build: a quoted #include is followed through the CMake include directories too (STALE-BUILD-INCLUDE-DIRS).

python3 -I tests/test_stale_build_include_dirs.py

A program's header in runtime/src is a source of the program: editing it must mark the built program stale. The
trees are temporary and hold placeholder text only; the one smoke test reads the real repository's sources.
"""
import os
import sys
import tempfile
import unittest

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(ROOT, "tools"))
import stale_build # noqa: E402


def write(root, rel, text):
path = os.path.join(root, *rel.split("/"))
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w", encoding="utf-8", newline="\n") as f:
f.write(text)
return path


class IncludeDirsTest(unittest.TestCase):
def setUp(self):
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
self.root = tmp.name

def sources(self, name="prog"):
return sorted(os.path.relpath(p, self.root).replace(os.sep, "/")
for p in stale_build.program_sources(self.root, name))

def test_header_in_runtime_src_is_a_source(self):
write(self.root, "runtime/test/prog.cpp", '#include "helper.h"\n')
write(self.root, "runtime/src/helper.h", "// helper\n")
self.assertEqual(self.sources(), ["runtime/src/helper.h", "runtime/test/prog.cpp"])

def test_editing_runtime_src_header_marks_exe_stale(self):
source = write(self.root, "runtime/test/prog.cpp", '#include "helper.h"\n')
header = write(self.root, "runtime/src/helper.h", "// helper\n")
exe = write(self.root, "runtime/build/prog", "exe\n")
os.utime(source, (500_000_000, 500_000_000))
os.utime(exe, (1_000_000_000, 1_000_000_000))
os.utime(header, (2_000_000_000, 2_000_000_000))
self.assertEqual(stale_build.newer_sources(exe, self.root, "prog"), ["runtime/src/helper.h"])

def test_header_next_to_program_wins_over_runtime_src(self):
write(self.root, "runtime/test/prog.cpp", '#include "helper.h"\n')
write(self.root, "runtime/test/helper.h", "// local\n")
write(self.root, "runtime/src/helper.h", "// shared\n")
self.assertEqual(self.sources(), ["runtime/test/helper.h", "runtime/test/prog.cpp"])

def test_every_copy_in_the_include_directories_is_a_source(self):
# CMake orders the directories per target (`target_include_directories(prog PRIVATE shims src)` takes the
# shims copy), so a header present in both must count whichever one a given program compiles.
write(self.root, "runtime/test/prog.cpp", '#include "helper.h"\n')
write(self.root, "runtime/src/helper.h", "// src\n")
write(self.root, "runtime/shims/helper.h", "// shims\n")
self.assertEqual(self.sources(), ["runtime/shims/helper.h", "runtime/src/helper.h", "runtime/test/prog.cpp"])

def test_editing_only_the_shims_copy_marks_the_exe_stale(self):
write(self.root, "runtime/test/prog.cpp", '#include "selection.h"\n')
write(self.root, "runtime/src/selection.h", "// src\n")
write(self.root, "runtime/shims/selection.h", "// shims\n")
exe = write(self.root, "build/prog.exe", "exe")
for rel in ("runtime/test/prog.cpp", "runtime/src/selection.h", "build/prog.exe"):
os.utime(os.path.join(self.root, *rel.split("/")), (1000, 1000))
os.utime(os.path.join(self.root, "runtime", "shims", "selection.h"), (5000, 5000))
self.assertEqual(stale_build.newer_sources(exe, self.root, "prog"), ["runtime/shims/selection.h"])

def test_header_found_in_runtime_shims(self):
write(self.root, "runtime/test/prog.cpp", '#include "shim.h"\n')
write(self.root, "runtime/shims/shim.h", "// shim\n")
self.assertEqual(self.sources(), ["runtime/shims/shim.h", "runtime/test/prog.cpp"])

def test_header_found_nowhere_is_skipped(self):
write(self.root, "runtime/test/prog.cpp", '#include "missing.h"\n#include <vector>\n')
self.assertEqual(self.sources(), ["runtime/test/prog.cpp"])

def test_cycle_through_fallback_directories_terminates(self):
write(self.root, "runtime/test/prog.cpp", '#include "a.h"\n')
write(self.root, "runtime/src/a.h", '#include "b.h"\n#pragma once\n')
write(self.root, "runtime/shims/b.h", '#include "a.h"\n#include "b.h"\n')
self.assertEqual(self.sources(), ["runtime/shims/b.h", "runtime/src/a.h", "runtime/test/prog.cpp"])

def test_relative_resolution_is_unchanged(self):
write(self.root, "runtime/test/prog.cpp", '#include "inc/a.h"\n')
write(self.root, "runtime/test/inc/a.h", '#include "b.h"\n#include "../c.h"\n')
write(self.root, "runtime/test/inc/b.h", "// b\n")
write(self.root, "runtime/test/c.h", "// c\n")
self.assertEqual(self.sources(), ["runtime/test/c.h", "runtime/test/inc/a.h", "runtime/test/inc/b.h",
"runtime/test/prog.cpp"])

def test_missing_program_is_empty(self):
self.assertEqual(stale_build.program_sources(self.root, "nope"), [])


class RealTreeTest(unittest.TestCase):
def test_pe_loader_test_follows_pe_loader_header(self):
sources = stale_build.program_sources(ROOT, "pe_loader_test")
rels = {os.path.relpath(p, ROOT).replace(os.sep, "/") for p in sources}
self.assertIn("runtime/src/pe_loader.h", rels)


if __name__ == "__main__":
unittest.main()
32 changes: 30 additions & 2 deletions tools/stale_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,41 @@
make_game.py (vk_pipeline_check) and first_light.py (vk_harness) take a program already in runtime/build when there
is one. After a pull that changed the program, that copy is from the old sources and would run silently. The
sources are runtime/test/<name>.cpp and the headers it pulls in with #include "..." (followed through other
headers): a change to any of them recompiles and relinks the program, so a current build is never older than them.
headers, each looked up next to the including file, then in the include directories of runtime/CMakeLists.txt): a
change to any of them recompiles and relinks the program, so a current build is never older than them.
runtime/CMakeLists.txt isn't one: a change there that doesn't concern the program leaves it as it was.
"""
import os
import re

_INCLUDE = re.compile(r'^\s*#\s*include\s*"([^"]+)"', re.MULTILINE)

# The include directories runtime/CMakeLists.txt gives the runtime/test programs, searched after the directory of the
# including file: runtime/src and runtime/shims (target_include_directories of pe_loader_test, lines 657-660, which
# other test programs repeat), then the xtaskqueue ones (lines 90-92, 1206-1210 and 1227-1229). Paths are relative to
# the repository root. The list is the union over the programs, not one target's order: no header name exists in two
# of these directories today, and if one did, every copy would count as a source of the program.
_INCLUDE_DIRS = (
"runtime/src",
"runtime/shims",
"runtime/shims/xtaskqueue",
"third_party/xtaskqueue/Source/Task",
"third_party/xtaskqueue/Source/Common",
"third_party/xtaskqueue/Include",
)


def _resolve_include(root, path, inc):
"""The files a quoted `#include inc` in `path` may name: the one next to `path` when there is one (the compiler
looks there first and nowhere else), otherwise every match in _INCLUDE_DIRS. CMake orders those directories per
target, so which match the compiler takes can differ from one program to the next; following all of them can add a
rebuild but never miss one. Empty when nothing has it (a system or generated header)."""
local = os.path.join(os.path.dirname(path), inc)
if os.path.isfile(local):
return [local]
matches = (os.path.join(root, directory, inc) for directory in _INCLUDE_DIRS)
return [candidate for candidate in matches if os.path.isfile(candidate)]


def program_sources(root, name):
"""The files program `name` of runtime/test is built from, as far as they exist in this checkout."""
Expand All @@ -29,7 +56,8 @@ def program_sources(root, name):
text = f.read()
except OSError:
continue
pending.extend(os.path.join(os.path.dirname(path), inc) for inc in _INCLUDE.findall(text))
for inc in _INCLUDE.findall(text):
pending.extend(_resolve_include(root, path, inc))
return found


Expand Down