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
3 changes: 3 additions & 0 deletions news/current-py-cc-headers-dynamic-deps.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(cc) {obj}`current_py_cc_headers` and {obj}`current_py_cc_headers_abi3` no
longer drop the Windows Python import libraries when used by a `cc_binary` with
`dynamic_deps`.
32 changes: 28 additions & 4 deletions python/private/current_py_cc_headers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,30 @@
"""

load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
load("@rules_cc//cc/common:cc_shared_library_hint_info.bzl", "CcSharedLibraryHintInfo")
load("//python/private:toolchain_types.bzl", "PY_CC_TOOLCHAIN_TYPE")

def _forward_headers(ctx, headers):
# Report the owners of forwarded libraries (e.g. the Windows import
# libraries) so cc_binary with dynamic_deps doesn't drop them.
providers = headers.providers_map
owners = {
linker_input.owner: None
for linker_input in providers["CcInfo"].linking_context.linker_inputs.to_list()
}
return providers.values() + [CcSharedLibraryHintInfo(
attributes = [],
owners = owners.keys() or [ctx.label],
)]

def _current_py_cc_headers_impl(ctx):
py_cc_toolchain = ctx.toolchains[PY_CC_TOOLCHAIN_TYPE].py_cc_toolchain
return py_cc_toolchain.headers.providers_map.values()
return _forward_headers(ctx, py_cc_toolchain.headers)

current_py_cc_headers = rule(
implementation = _current_py_cc_headers_impl,
toolchains = [PY_CC_TOOLCHAIN_TYPE],
provides = [CcInfo],
provides = [CcInfo, CcSharedLibraryHintInfo],
doc = """
Provides the currently active Python toolchain's C headers.
Expand All @@ -41,6 +55,11 @@ cc_library(
deps = ["@rules_python//python/cc:current_py_cc_headers"]
)
```
:::{versionchanged} VERSION_NEXT_PATCH
The Windows import libraries are no longer dropped by `cc_binary` with
`dynamic_deps`.
:::
""",
)

Expand All @@ -54,12 +73,12 @@ def _current_py_cc_headers_abi3_impl(ctx):
).format(
PY_CC_TOOLCHAIN_TYPE,
))
return py_cc_toolchain.headers_abi3.providers_map.values()
return _forward_headers(ctx, py_cc_toolchain.headers_abi3)

current_py_cc_headers_abi3 = rule(
implementation = _current_py_cc_headers_abi3_impl,
toolchains = [PY_CC_TOOLCHAIN_TYPE],
provides = [CcInfo],
provides = [CcInfo, CcSharedLibraryHintInfo],
doc = """
Provides the currently active Python toolchain's C ABI3 headers.
Expand All @@ -78,5 +97,10 @@ cc_library(
:::{versionadded} 1.7.0
:::
:::{versionchanged} VERSION_NEXT_PATCH
The Windows import libraries are no longer dropped by `cc_binary` with
`dynamic_deps`.
:::
""",
)
83 changes: 83 additions & 0 deletions tests/cc/current_py_cc_headers/current_py_cc_headers_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@

"""Tests for current_py_cc_headers."""

load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_cc//cc:cc_shared_library.bzl", "cc_shared_library")
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
load("@rules_testing//lib:truth.bzl", "matching")
load("@rules_testing//lib:util.bzl", "util")
load("//python/cc:py_cc_toolchain.bzl", "py_cc_toolchain")
load("//tests/support:cc_info_subject.bzl", "cc_info_subject")
load("//tests/support:support.bzl", "CC_TOOLCHAIN")

Expand Down Expand Up @@ -110,6 +115,84 @@ def _test_current_toolchain_headers_abi3_impl(env, target):

_tests.append(_test_current_toolchain_headers_abi3)

# On Windows the headers targets carry the Python import libraries. Check that
# they are still linked into a cc_binary with dynamic_deps.
def _setup_binary_with_dynamic_deps(name, headers_target):
util.helper_target(
cc_library,
name = name + ".python_lib",
srcs = ["python_lib.c"],
)
util.helper_target(
py_cc_toolchain,
name = name + ".py_cc_toolchain",
headers = ":" + name + ".python_lib",
headers_abi3 = ":" + name + ".python_lib",
python_version = "3.999",
)
util.helper_target(
native.toolchain,
name = name + ".toolchain",
toolchain = ":" + name + ".py_cc_toolchain",
toolchain_type = "//python/cc:toolchain_type",
)
util.helper_target(
cc_library,
name = name + ".dep",
srcs = ["python_lib.c"],
)
util.helper_target(
cc_shared_library,
name = name + ".dep_shared",
deps = [":" + name + ".dep"],
)
util.helper_target(
cc_binary,
name = name + ".bin",
linkshared = True,
dynamic_deps = [":" + name + ".dep_shared"],
deps = [
":" + name + ".dep",
headers_target,
],
)
analysis_test(
name = name,
impl = _test_binary_with_dynamic_deps_impl,
target = name + ".bin",
config_settings = {
# This transition replaces the C++ toolchain supplied by RBE.
"//command_line_option:extra_toolchains": [
str(native.package_relative_label(":" + name + ".toolchain")),
str(Label("//tests/support/cc_toolchains:linux_toolchain_definition")),
str(Label("//tests/support/cc_toolchains:mac_toolchain_definition")),
str(Label("//tests/support/cc_toolchains:windows_toolchain_definition")),
],
},
)

def _test_binary_with_dynamic_deps_impl(env, target):
test_name = target.label.name.removesuffix(".bin")
link_inputs = env.expect.that_target(target).action_named("CppLink").inputs()
link_inputs.contains_predicate(
matching.file_basename_contains(test_name + ".python_lib"),
)

# :dep is linked through :dep_shared, not statically.
link_inputs.not_contains_predicate(
matching.file_basename_contains(test_name + ".dep."),
)

def _test_headers_binary_with_dynamic_deps(name):
_setup_binary_with_dynamic_deps(name, "//python/cc:current_py_cc_headers")

_tests.append(_test_headers_binary_with_dynamic_deps)

def _test_headers_abi3_binary_with_dynamic_deps(name):
_setup_binary_with_dynamic_deps(name, "//python/cc:current_py_cc_headers_abi3")

_tests.append(_test_headers_abi3_binary_with_dynamic_deps)

def current_py_cc_headers_test_suite(name):
test_suite(
name = name,
Expand Down
1 change: 1 addition & 0 deletions tests/cc/current_py_cc_headers/python_lib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int python_library_symbol(void) { return 0; }
Loading