Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow py_cc_toolchain libs to be optional #2197

Merged
merged 1 commit into from
Sep 7, 2024
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ A brief description of the categories of changes:
* (gazelle): Update error messages when unable to resolve a dependency to be more human-friendly.
* (flags) The {obj}`--python_version` flag now also returns
{obj}`config_common.FeatureFlagInfo`.
* (toolchains) {obj}`py_cc_toolchain.libs` and {obj}`PyCcToolchainInfo.libs` is
optional. This is to support situations where only the Python headers are
available.

### Fixed
* (whl_library): Remove `--no-index` and add `--no-build-isolation` to the
Expand Down
4 changes: 2 additions & 2 deletions python/private/py_cc_toolchain_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ Information about the header files, struct with fields:
represents).
""",
"libs": """\
:type: struct
:type: struct | None

Information about C libraries, struct with fields:
If available, information about C libraries, struct with fields:
* providers_map: A dict of string to provider instances. The key should be
a fully qualified name (e.g. `@rules_foo//bar:baz.bzl#MyInfo`) of the
provider to uniquely identify its type.
Expand Down
18 changes: 11 additions & 7 deletions python/private/py_cc_toolchain_rule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,24 @@ load("@rules_cc//cc:defs.bzl", "CcInfo")
load(":py_cc_toolchain_info.bzl", "PyCcToolchainInfo")

def _py_cc_toolchain_impl(ctx):
if ctx.attr.libs:
libs = struct(
providers_map = {
"CcInfo": ctx.attr.libs[CcInfo],
"DefaultInfo": ctx.attr.libs[DefaultInfo],
},
)
else:
libs = None

py_cc_toolchain = PyCcToolchainInfo(
headers = struct(
providers_map = {
"CcInfo": ctx.attr.headers[CcInfo],
"DefaultInfo": ctx.attr.headers[DefaultInfo],
},
),
libs = struct(
providers_map = {
"CcInfo": ctx.attr.libs[CcInfo],
"DefaultInfo": ctx.attr.libs[DefaultInfo],
},
),
libs = libs,
python_version = ctx.attr.python_version,
)
extra_kwargs = {}
Expand All @@ -59,7 +64,6 @@ py_cc_toolchain = rule(
doc = ("Target that provides the Python runtime libraries for linking. " +
"Typically this is a cc_library target of `.so` files."),
providers = [CcInfo],
mandatory = True,
),
"python_version": attr.string(
doc = "The Major.minor Python version, e.g. 3.11",
Expand Down
28 changes: 24 additions & 4 deletions tests/cc/py_cc_toolchain/py_cc_toolchain_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@

load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
load("@rules_testing//lib:truth.bzl", "matching", "subjects")
load("//python/cc:py_cc_toolchain.bzl", "py_cc_toolchain")
load("//tests/support:cc_info_subject.bzl", "cc_info_subject")
load("//tests/support:py_cc_toolchain_info_subject.bzl", "PyCcToolchainInfoSubject")

_tests = []

def _py_cc_toolchain_test(name):
def _test_py_cc_toolchain(name):
analysis_test(
name = name,
impl = _py_cc_toolchain_test_impl,
impl = _test_py_cc_toolchain_impl,
target = "//tests/support/cc_toolchains:fake_py_cc_toolchain_impl",
attrs = {
"header": attr.label(
Expand All @@ -34,7 +35,7 @@ def _py_cc_toolchain_test(name):
},
)

def _py_cc_toolchain_test_impl(env, target):
def _test_py_cc_toolchain_impl(env, target):
env.expect.that_target(target).has_provider(platform_common.ToolchainInfo)

toolchain = PyCcToolchainInfoSubject.new(
Expand Down Expand Up @@ -80,7 +81,26 @@ def _py_cc_toolchain_test_impl(env, target):
matching.str_matches("/libpython3."),
)

_tests.append(_py_cc_toolchain_test)
_tests.append(_test_py_cc_toolchain)

def _test_libs_optional(name):
py_cc_toolchain(
name = name + "_subject",
libs = None,
headers = "//tests/support/cc_toolchains:fake_headers",
python_version = "4.5",
)
analysis_test(
name = name,
target = name + "_subject",
impl = _test_libs_optional_impl,
)

def _test_libs_optional_impl(env, target):
libs = target[platform_common.ToolchainInfo].py_cc_toolchain.libs
env.expect.that_bool(libs == None).equals(True)

_tests.append(_test_libs_optional)

def py_cc_toolchain_test_suite(name):
test_suite(
Expand Down