Skip to content

Commit 0cd9bfa

Browse files
authored
tests: make py_cc_toolchain test of headers/includes work with Bazel 9 (#3276)
Bazel 9 has a small change in cc_library behavior: the includes attribute populates the `.includes` instead of `.system_includes`. This is OK for rules_python, since both result in the includes being added as system include paths. To fix, change the test to look at both; as long as the include paths are in one, then its OK. Fixes #3239
1 parent ac177a6 commit 0cd9bfa

File tree

6 files changed

+45
-29
lines changed

6 files changed

+45
-29
lines changed

tests/cc/current_py_cc_headers/current_py_cc_headers_tests.bzl

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ def _test_current_toolchain_headers(name):
3131
"//command_line_option:extra_toolchains": [CC_TOOLCHAIN],
3232
},
3333
attrs = {
34-
"header": attr.label(
35-
default = "//tests/support/cc_toolchains:fake_header.h",
36-
allow_single_file = True,
34+
"header_files": attr.label_list(
35+
default = [
36+
"//tests/support/cc_toolchains:py_header_files",
37+
],
38+
allow_files = True,
3739
),
3840
},
3941
)
@@ -44,17 +46,23 @@ def _test_current_toolchain_headers_impl(env, target):
4446
CcInfo,
4547
factory = cc_info_subject,
4648
).compilation_context()
47-
compilation_context.direct_headers().contains_exactly([
48-
env.ctx.file.header,
49-
])
50-
compilation_context.direct_public_headers().contains_exactly([
51-
env.ctx.file.header,
52-
])
49+
compilation_context.direct_headers().contains_exactly(
50+
env.ctx.files.header_files,
51+
)
52+
compilation_context.direct_public_headers().contains_exactly(
53+
env.ctx.files.header_files,
54+
)
55+
56+
# NOTE: Bazel 8 and lower put cc_library.includes into `.system_includes`,
57+
# while Bazel 9 put it in `.includes`. Both result in the includes being
58+
# added as system includes, so either is acceptable for the expected
59+
# `#include <Python.h>` to work.
60+
includes = compilation_context.actual.includes.to_list() + compilation_context.actual.system_includes.to_list()
5361

5462
# NOTE: The include dir gets added twice, once for the source path,
5563
# and once for the config-specific path.
56-
compilation_context.system_includes().contains_at_least_predicates([
57-
matching.str_matches("*/fake_include"),
64+
env.expect.that_collection(includes).contains_at_least_predicates([
65+
matching.str_matches("*/py_include"),
5866
])
5967

6068
# Check that the forward DefaultInfo looks correct

tests/cc/py_cc_toolchain/py_cc_toolchain_tests.bzl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ def _test_py_cc_toolchain(name):
2828
impl = _test_py_cc_toolchain_impl,
2929
target = "//tests/support/cc_toolchains:fake_py_cc_toolchain_impl",
3030
attrs = {
31-
"header": attr.label(
32-
default = "//tests/support/cc_toolchains:fake_header.h",
33-
allow_single_file = True,
31+
"header_files": attr.label_list(
32+
default = ["//tests/support/cc_toolchains:py_header_files"],
33+
allow_files = True,
3434
),
3535
},
3636
)
@@ -50,17 +50,17 @@ def _test_py_cc_toolchain_impl(env, target):
5050
cc_info = headers_providers.get("CcInfo", factory = cc_info_subject)
5151

5252
compilation_context = cc_info.compilation_context()
53-
compilation_context.direct_headers().contains_exactly([
54-
env.ctx.file.header,
55-
])
56-
compilation_context.direct_public_headers().contains_exactly([
57-
env.ctx.file.header,
58-
])
53+
compilation_context.direct_headers().contains_exactly(
54+
env.ctx.files.header_files,
55+
)
56+
compilation_context.direct_public_headers().contains_exactly(
57+
env.ctx.files.header_files,
58+
)
5959

6060
# NOTE: The include dir gets added twice, once for the source path,
6161
# and once for the config-specific path, but we don't care about that.
6262
compilation_context.system_includes().contains_at_least_predicates([
63-
matching.str_matches("*/fake_include"),
63+
matching.str_matches("*/py_include"),
6464
])
6565

6666
default_info = headers_providers.get("DefaultInfo", factory = subjects.default_info)
@@ -87,7 +87,7 @@ def _test_libs_optional(name):
8787
py_cc_toolchain(
8888
name = name + "_subject",
8989
libs = None,
90-
headers = "//tests/support/cc_toolchains:fake_headers",
90+
headers = "//tests/support/cc_toolchains:py_headers",
9191
python_version = "4.5",
9292
)
9393
analysis_test(

tests/support/cc_info_subject.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ def _compilation_context_subject_new(info, *, meta):
8181

8282
# buildifier: disable=uninitialized
8383
public = struct(
84+
actual = info,
8485
# go/keep-sorted start
8586
direct_headers = lambda *a, **k: _compilation_context_subject_direct_headers(self, *a, **k),
8687
direct_public_headers = lambda *a, **k: _compilation_context_subject_direct_public_headers(self, *a, **k),
@@ -156,6 +157,7 @@ def _linking_context_subject_new(info, meta):
156157

157158
# buildifier: disable=uninitialized
158159
public = struct(
160+
actual = info,
159161
# go/keep-sorted start
160162
linker_inputs = lambda *a, **k: _linking_context_subject_linker_inputs(self, *a, **k),
161163
# go/keep-sorted end

tests/support/cc_toolchains/BUILD.bazel

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
load("@rules_cc//cc:cc_library.bzl", "cc_library")
1516
load("@rules_cc//cc/toolchains:cc_toolchain.bzl", "cc_toolchain")
1617
load("@rules_cc//cc/toolchains:cc_toolchain_suite.bzl", "cc_toolchain_suite")
1718
load("@rules_testing//lib:util.bzl", "PREVENT_IMPLICIT_BUILDING_TAGS")
@@ -20,7 +21,14 @@ load(":fake_cc_toolchain_config.bzl", "fake_cc_toolchain_config")
2021

2122
package(default_visibility = ["//:__subpackages__"])
2223

23-
exports_files(["fake_header.h"])
24+
# Factored out for testing
25+
filegroup(
26+
name = "py_header_files",
27+
srcs = [
28+
"py_header.h",
29+
"py_include/py_include.h",
30+
],
31+
)
2432

2533
filegroup(
2634
name = "libpython",
@@ -37,22 +45,20 @@ toolchain(
3745

3846
py_cc_toolchain(
3947
name = "fake_py_cc_toolchain_impl",
40-
headers = ":fake_headers",
48+
headers = ":py_headers",
4149
libs = ":fake_libs",
4250
python_version = "3.999",
4351
tags = PREVENT_IMPLICIT_BUILDING_TAGS,
4452
)
4553

46-
# buildifier: disable=native-cc
4754
cc_library(
48-
name = "fake_headers",
49-
hdrs = ["fake_header.h"],
55+
name = "py_headers",
56+
hdrs = [":py_header_files"],
5057
data = ["data.txt"],
51-
includes = ["fake_include"],
58+
includes = ["py_include"],
5259
tags = PREVENT_IMPLICIT_BUILDING_TAGS,
5360
)
5461

55-
# buildifier: disable=native-cc
5662
cc_library(
5763
name = "fake_libs",
5864
srcs = ["libpython3.so"],

tests/support/cc_toolchains/py_header.h

Whitespace-only changes.

tests/support/cc_toolchains/py_include/py_include.h

Whitespace-only changes.

0 commit comments

Comments
 (0)