|
Expand Up
|
@@ -26,23 +26,14 @@ def test_debug_op(self) -> None: |
|
|
|
|
|
|
|
|
class TestHeaderDeps(unittest.TestCase): |
|
|
"""Tests for the header-dependency tracking used to build |
|
|
`Extension.depends`, which drives setuptools' `newer_group` decision |
|
|
about whether to recompile a .o file on incremental builds. |
|
|
|
|
|
The critical case is cross-group export-table headers: each module's |
|
|
`__native_internal_<mod>.h` does `#include <other_group/__native_other.h>`, |
|
|
and the consumer's compiled .o file bakes in byte offsets into that |
|
|
header's `export_table_<group>` struct. If we miss this header in the |
|
|
deps list, struct-layout changes in `other_group` won't trigger a |
|
|
rebuild of the consumer, and its baked-in offsets will silently resolve |
|
|
to whatever now occupies those slots. |
|
|
""" |
|
|
Tests for the header-dependency tracking used to build `Extension.depends`, which drives |
|
|
setuptools' `newer_group` decision about whether to recompile a .o file on incremental builds. |
|
|
""" |
|
|
|
|
|
def test_get_header_deps_quoted_includes(self) -> None: |
|
|
# Quoted includes — the historical form. Used by the .c file to |
|
|
# reach its own __native_<mod>.h / __native_internal_<mod>.h. The |
|
|
# `False` in each tuple marks the include as non-angled, which |
|
|
# Quoted includes: the historical form. Used by the .c file to reach its own __native_<mod>.h / |
|
|
# __native_internal_<mod>.h. The `False` in each tuple marks the include as non-angled, which |
|
|
# `resolve_cfile_deps` uses to search the includer's directory. |
|
|
cfile = '#include "__native_caller.h"\n#include "__native_internal_caller.h"\n' |
|
|
assert get_header_deps([("caller.c", cfile)]) == [ |
|
Expand All
|
@@ -51,51 +42,46 @@ def test_get_header_deps_quoted_includes(self) -> None: |
|
|
] |
|
|
|
|
|
def test_get_header_deps_angle_bracket_includes(self) -> None: |
|
|
# Angle-bracket includes are also matched, and reported with |
|
|
# is_angled=True so that the resolver skips the includer's dir |
|
|
# for them (matching the C preprocessor). The cross-group export |
|
|
# header is reached via `#include <other_group/__native_other.h>` |
|
|
# in __native_internal_<mod>.h. Before this was matched the dep |
|
|
# was missed entirely and the consumer's .o was never invalidated |
|
|
# when the other group's struct layout shifted. |
|
|
# Angle-bracket includes are also matched, and reported with is_angled=True so that the resolver |
|
|
# skips the includer's dir for them (matching the C preprocessor). The cross-group export header |
|
|
# is reached via `#include <other_group/__native_other.h>` in __native_internal_<mod>.h. Before |
|
|
# this was matched the dep was missed entirely and the consumer's .o was never invalidated when |
|
|
# the other group's struct layout shifted. |
|
|
cfile = "#include <Python.h>\n#include <lib/__native_functions.h>\n" |
|
|
assert get_header_deps([("caller.c", cfile)]) == [ |
|
|
(True, "Python.h"), |
|
|
(True, "lib/__native_functions.h"), |
|
|
] |
|
|
|
|
|
def test_get_header_deps_mixed_and_whitespace(self) -> None: |
|
|
# The preprocessor tolerates whitespace and the leading-hash form. |
|
|
# `get_header_deps` returns sorted tuples — non-angled (False) sorts |
|
|
# before angled (True), then alphabetical within each kind. |
|
|
# The preprocessor tolerates whitespace and the leading-hash form. `get_header_deps` returns sorted |
|
|
# tuples — non-angled (False) sorts before angled (True), then alphabetical within each kind. |
|
|
cfile = '# include "a.h"\n# include <b.h>\n#include\t"c.h"\n' |
|
|
assert get_header_deps([("x.c", cfile)]) == [(False, "a.h"), (False, "c.h"), (True, "b.h")] |
|
|
|
|
|
def test_resolve_walks_transitively_through_headers(self) -> None: |
|
|
# Reproduces the bug scenario: caller's .c only directly includes |
|
|
# caller's own headers, but caller's __native_internal_caller.h |
|
|
# includes the cross-group export header. The resolver must follow |
|
|
# that chain so setuptools sees the cross-group header as a dep. |
|
|
# Reproduces the bug scenario: caller's .c only directly includes caller's own headers, but |
|
|
# caller's __native_internal_caller.h includes the cross-group export header. The resolver |
|
|
# must follow that chain so setuptools sees the cross-group header as a dep. |
|
|
with tempfile.TemporaryDirectory() as tmp: |
|
|
build_dir = tmp |
|
|
os.makedirs(os.path.join(build_dir, "lib")) |
|
|
os.makedirs(os.path.join(build_dir, "other_group")) |
|
|
|
|
|
# caller.c's directly-included headers — both live alongside |
|
|
# caller.c's directly-included headers, both live alongside |
|
|
# caller.c under build/ (resolved via target_dir). |
|
|
internal_h = os.path.join(build_dir, "__native_internal_caller.h") |
|
|
caller_h = os.path.join(build_dir, "__native_caller.h") |
|
|
cross_group_h = os.path.join(build_dir, "lib", "__native_functions.h") |
|
|
unrelated_h = os.path.join(build_dir, "other_group", "__native_other.h") |
|
|
|
|
|
with open(caller_h, "w") as f: |
|
|
# lib-rt headers don't exist on disk under build/, so they |
|
|
# get dropped during resolution and aren't recursed into. |
|
|
# Headers outside build/ (CPython's <Python.h>, lib-rt's <CPy.h>) don't resolve under |
|
|
# target_dir, so they get dropped during resolution and aren't recursed into. |
|
|
f.write("#include <Python.h>\n#include <CPy.h>\n") |
|
|
with open(internal_h, "w") as f: |
|
|
# The smoking gun: this header includes a header in another |
|
|
# group via angle brackets. Pre-fix, this dep was invisible |
|
|
# to setuptools. |
|
|
# This header includes a header in another group via angle brackets. Pre-fix, this dep |
|
|
# was invisible to setuptools. |
|
|
f.write( |
|
|
"#include <Python.h>\n" |
|
|
'#include "__native_caller.h"\n' |
|
Expand All
|
@@ -104,15 +90,12 @@ def test_resolve_walks_transitively_through_headers(self) -> None: |
|
|
with open(cross_group_h, "w") as f: |
|
|
f.write("struct export_table_lib___functions { int x; };\n") |
|
|
with open(unrelated_h, "w") as f: |
|
|
# Sibling group not reached from caller's chain — must |
|
|
# NOT appear in the resolved set. |
|
|
# Sibling group not reached from caller's chain => must NOT appear in the resolved set. |
|
|
f.write("struct unrelated { int x; };\n") |
|
|
|
|
|
# caller.c is in build_dir, so its includer-dir is build_dir. |
|
|
# Both directly-included headers are quoted (`False`); the |
|
|
# cross-group header that __native_internal_caller.h reaches |
|
|
# via `<lib/__native_functions.h>` is found by the recursive |
|
|
# walk re-reading the on-disk header. |
|
|
# caller.c is in build_dir, so its includer-dir is build_dir. Both directly-included headers |
|
|
# are quoted (`False`); the cross-group header that __native_internal_caller.h reaches via |
|
|
# `<lib/__native_functions.h>` is found by the recursive walk re-reading the on-disk header. |
|
|
deps = resolve_cfile_deps( |
|
|
cfile_dir=build_dir, |
|
|
direct_includes=[ |
|
Expand All
|
@@ -128,11 +111,9 @@ def test_resolve_walks_transitively_through_headers(self) -> None: |
|
|
) |
|
|
|
|
|
def test_resolve_drops_unresolvable_includes(self) -> None: |
|
|
# `<Python.h>`, `<CPy.h>`, etc. don't live under target_dir, so |
|
|
# they're dropped from depends. They never change between builds, |
|
|
# so this is the right behavior — and crucially it stops |
|
|
# setuptools' `missing="newer"` from treating them as always-newer |
|
|
# and force-rebuilding every translation unit. |
|
|
# `<Python.h>`, `<CPy.h>`, etc. don't live under target_dir, so they're dropped from depends. They |
|
|
# never change between builds, so this is the right behavior. Crucially, it stops setuptools' |
|
|
# `missing="newer"` from treating them as always-newer and force-rebuilding every translation unit. |
|
|
with tempfile.TemporaryDirectory() as tmp: |
|
|
cfile_dir = tmp |
|
|
deps = resolve_cfile_deps( |
|
Expand All
|
@@ -143,11 +124,9 @@ def test_resolve_drops_unresolvable_includes(self) -> None: |
|
|
assert deps == set() |
|
|
|
|
|
def test_resolve_search_order_matches_preprocessor(self) -> None: |
|
|
# When the same header name exists both next to the includer and |
|
|
# under target_dir, the C preprocessor picks the includer-dir copy |
|
|
# for `#include "shared.h"` and the target_dir copy for |
|
|
# `#include <shared.h>`. The resolver must record the same path |
|
|
# the compiler will actually consume, otherwise mtimes of the |
|
|
# When the same header name exists both next to the includer and under target_dir, the C preprocessor |
|
|
# picks the includer-dir copy for `#include "shared.h"` and the target_dir copy for `#include <shared.h>`. |
|
|
# The resolver must record the same path the compiler will actually consume, otherwise mtimes of the |
|
|
# wrong file drive incremental rebuild decisions. |
|
|
with tempfile.TemporaryDirectory() as tmp: |
|
|
includer = os.path.join(tmp, "groupA") |
|
Expand Down
|
|