xref: /aosp_15_r20/external/bazelbuild-rules_python/gazelle/modules_mapping/def.bzl (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1*60517a1eSAndroid Build Coastguard Worker# Copyright 2023 The Bazel Authors. All rights reserved.
2*60517a1eSAndroid Build Coastguard Worker#
3*60517a1eSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*60517a1eSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*60517a1eSAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*60517a1eSAndroid Build Coastguard Worker#
7*60517a1eSAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
8*60517a1eSAndroid Build Coastguard Worker#
9*60517a1eSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*60517a1eSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*60517a1eSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*60517a1eSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*60517a1eSAndroid Build Coastguard Worker# limitations under the License.
14*60517a1eSAndroid Build Coastguard Worker
15*60517a1eSAndroid Build Coastguard Worker"""Definitions for the modules_mapping.json generation.
16*60517a1eSAndroid Build Coastguard Worker
17*60517a1eSAndroid Build Coastguard WorkerThe modules_mapping.json file is a mapping from Python modules to the wheel
18*60517a1eSAndroid Build Coastguard Workernames that provide those modules. It is used for determining which wheel
19*60517a1eSAndroid Build Coastguard Workerdistribution should be used in the `deps` attribute of `py_*` targets.
20*60517a1eSAndroid Build Coastguard Worker
21*60517a1eSAndroid Build Coastguard WorkerThis mapping is necessary when reading Python import statements and determining
22*60517a1eSAndroid Build Coastguard Workerif they are provided by third-party dependencies. Most importantly, when the
23*60517a1eSAndroid Build Coastguard Workermodule name doesn't match the wheel distribution name.
24*60517a1eSAndroid Build Coastguard Worker"""
25*60517a1eSAndroid Build Coastguard Worker
26*60517a1eSAndroid Build Coastguard Workerdef _modules_mapping_impl(ctx):
27*60517a1eSAndroid Build Coastguard Worker    modules_mapping = ctx.actions.declare_file(ctx.attr.modules_mapping_name)
28*60517a1eSAndroid Build Coastguard Worker    args = ctx.actions.args()
29*60517a1eSAndroid Build Coastguard Worker    all_wheels = depset(
30*60517a1eSAndroid Build Coastguard Worker        [whl for whl in ctx.files.wheels],
31*60517a1eSAndroid Build Coastguard Worker        transitive = [dep[DefaultInfo].files for dep in ctx.attr.wheels] + [dep[DefaultInfo].data_runfiles.files for dep in ctx.attr.wheels],
32*60517a1eSAndroid Build Coastguard Worker    )
33*60517a1eSAndroid Build Coastguard Worker    args.add("--output_file", modules_mapping.path)
34*60517a1eSAndroid Build Coastguard Worker    args.add_all("--exclude_patterns", ctx.attr.exclude_patterns)
35*60517a1eSAndroid Build Coastguard Worker    args.add_all("--wheels", [whl.path for whl in all_wheels.to_list()])
36*60517a1eSAndroid Build Coastguard Worker    ctx.actions.run(
37*60517a1eSAndroid Build Coastguard Worker        inputs = all_wheels.to_list(),
38*60517a1eSAndroid Build Coastguard Worker        outputs = [modules_mapping],
39*60517a1eSAndroid Build Coastguard Worker        executable = ctx.executable._generator,
40*60517a1eSAndroid Build Coastguard Worker        arguments = [args],
41*60517a1eSAndroid Build Coastguard Worker        use_default_shell_env = False,
42*60517a1eSAndroid Build Coastguard Worker    )
43*60517a1eSAndroid Build Coastguard Worker    return [DefaultInfo(files = depset([modules_mapping]))]
44*60517a1eSAndroid Build Coastguard Worker
45*60517a1eSAndroid Build Coastguard Workermodules_mapping = rule(
46*60517a1eSAndroid Build Coastguard Worker    _modules_mapping_impl,
47*60517a1eSAndroid Build Coastguard Worker    attrs = {
48*60517a1eSAndroid Build Coastguard Worker        "exclude_patterns": attr.string_list(
49*60517a1eSAndroid Build Coastguard Worker            default = ["^_|(\\._)+"],
50*60517a1eSAndroid Build Coastguard Worker            doc = "A set of regex patterns to match against each calculated module path. By default, exclude the modules starting with underscores.",
51*60517a1eSAndroid Build Coastguard Worker            mandatory = False,
52*60517a1eSAndroid Build Coastguard Worker        ),
53*60517a1eSAndroid Build Coastguard Worker        "modules_mapping_name": attr.string(
54*60517a1eSAndroid Build Coastguard Worker            default = "modules_mapping.json",
55*60517a1eSAndroid Build Coastguard Worker            doc = "The name for the output JSON file.",
56*60517a1eSAndroid Build Coastguard Worker            mandatory = False,
57*60517a1eSAndroid Build Coastguard Worker        ),
58*60517a1eSAndroid Build Coastguard Worker        "wheels": attr.label_list(
59*60517a1eSAndroid Build Coastguard Worker            allow_files = True,
60*60517a1eSAndroid Build Coastguard Worker            doc = "The list of wheels, usually the 'all_whl_requirements' from @<pip_repository>//:requirements.bzl",
61*60517a1eSAndroid Build Coastguard Worker            mandatory = True,
62*60517a1eSAndroid Build Coastguard Worker        ),
63*60517a1eSAndroid Build Coastguard Worker        "_generator": attr.label(
64*60517a1eSAndroid Build Coastguard Worker            cfg = "exec",
65*60517a1eSAndroid Build Coastguard Worker            default = "//modules_mapping:generator",
66*60517a1eSAndroid Build Coastguard Worker            executable = True,
67*60517a1eSAndroid Build Coastguard Worker        ),
68*60517a1eSAndroid Build Coastguard Worker    },
69*60517a1eSAndroid Build Coastguard Worker    doc = "Creates a modules_mapping.json file for mapping module names to wheel distribution names.",
70*60517a1eSAndroid Build Coastguard Worker)
71