From 3b53daefeb51a77c54f653d13105580b86444d7b Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Tue, 7 Feb 2023 15:32:08 -0800 Subject: [PATCH] python: Basic Starlark implementation of PyInfo Not yet tested or exercised; to be done in a followup change. Work towards #15897 PiperOrigin-RevId: 507902946 Change-Id: I0b203892d28e6b00f4ee6567e26abda84e6f71bc --- .../builtins_bzl/common/python/providers.bzl | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/main/starlark/builtins_bzl/common/python/providers.bzl b/src/main/starlark/builtins_bzl/common/python/providers.bzl index 813bc595f118fd..af59c378f0650f 100644 --- a/src/main/starlark/builtins_bzl/common/python/providers.bzl +++ b/src/main/starlark/builtins_bzl/common/python/providers.bzl @@ -111,6 +111,60 @@ the same conventions as the standard CPython interpreter. PyRuntimeInfo = _builtins.toplevel.PyRuntimeInfo +def _check_arg_type(name, required_type, value): + value_type = type(value) + if value_type != required_type: + fail("parameter '{}' got value of type '{}', want '{}'".format( + name, + value_type, + required_type, + )) + +def _PyInfo_init( + *, + transitive_sources, + uses_shared_libraries = False, + imports = depset(), + has_py2_only_sources = False, + has_py3_only_sources = False): + _check_arg_type("transitive_sources", "depset", transitive_sources) + _check_arg_type("uses_shared_libraries", "bool", uses_shared_libraries) + _check_arg_type("imports", "depset", imports) + _check_arg_type("has_py2_only_sources", "bool", has_py2_only_sources) + _check_arg_type("has_py2_only_sources", "bool", has_py3_only_sources) + return { + "transitive_sources": transitive_sources, + "imports": imports, + "uses_shared_libraries": uses_shared_libraries, + "has_py2_only_sources": has_py2_only_sources, + "has_py3_only_sources": has_py2_only_sources, + } + +StarlarkPyInfo, _unused_raw_ctor = provider( + "Encapsulates information provided by the Python rules.", + init = _PyInfo_init, + fields = { + "transitive_sources": """\ +A (`postorder`-compatible) depset of `.py` files appearing in the target's +`srcs` and the `srcs` of the target's transitive `deps`. +""", + "uses_shared_libraries": """ +Whether any of this target's transitive `deps` has a shared library file (such +as a `.so` file). + +This field is currently unused in Bazel and may go away in the future. +""", + "imports": """\ +A depset of import path strings to be added to the `PYTHONPATH` of executable +Python targets. These are accumulated from the transitive `deps`. +The order of the depset is not guaranteed and may be changed in the future. It +is recommended to use `default` order (the default). +""", + "has_py2_only_sources": "Whether any of this target's transitive sources requires a Python 2 runtime.", + "has_py3_only_sources": "Whether any of this target's transitive sources requires a Python 3 runtime.", + }, +) + PyInfo = _builtins.toplevel.PyInfo # TODO(b/203567235): Re-implement in Starlark