• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2024 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""Generates a module map for the include directories."""
15
16load("@bazel_skylib//lib:paths.bzl", "paths")
17load("@bazel_skylib//rules/directory:providers.bzl", "DirectoryInfo")
18
19def _impl(ctx):
20    module_map_file = ctx.actions.declare_file("module.modulemap")
21
22    header_paths = list()
23    files = list()
24    for directory in ctx.attr.include_directories:
25        header_paths.append(directory[DirectoryInfo].path)
26        files.append(directory[DirectoryInfo].transitive_files)
27
28    # The paths in the module map must be relative to the path to the module map
29    # itself, but the header_paths provided as arguments must be relative to
30    # the execroot. This prefix is just enough ../ to transform between the two.
31    prefix = "../" * len(paths.dirname(module_map_file.path).split("/"))
32
33    ctx.actions.run(
34        inputs = depset([], transitive = files),
35        executable = ctx.executable._module_map_generator,
36        arguments = [module_map_file.path, prefix] + header_paths,
37        outputs = [module_map_file],
38    )
39
40    return [DefaultInfo(files = depset([module_map_file]))]
41
42builtin_module_map = rule(
43    implementation = _impl,
44    attrs = {
45        "include_directories": attr.label_list(
46            providers = [DirectoryInfo],
47            doc = """Directories in which to search for builtin headers.""",
48        ),
49        "_module_map_generator": attr.label(
50            default = Label("//pw_toolchain/cc:generate_module_map"),
51            executable = True,
52            cfg = "exec",
53        ),
54    },
55    provides = [DefaultInfo],
56    doc = """Generates a module map for the include directories.""",
57)
58