• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""
2THIS IS THE EXTERNAL-ONLY VERSION OF THIS FILE. G3 HAS ITS OWN.
3
4The macro defined in this file allows us to generate .cpp files for header files. The content
5is a copy of the header file.
6
7This allows us to actually "compile" the header file, which allows tools like IWYU to properly
8analyze a header file on its own.
9
10"""
11
12def generate_cpp_files_for_headers(name, headers, to_generate):
13    """Generate a filegroup containing generate .cpp files for the given header files
14
15    Args:
16        name: The name of the filegroup to hold the generated files.
17        headers: A list of labels that contain the files listed in to_generate (it can contain
18                 more; they will be ignored).
19        to_generate: A list of header files, from anywhere in the Skia source tree, that should
20                 have a .cpp file generated for them that includes the header. If a header already
21                 has a .cpp file, it should not be in this list. The generated files will not be
22                 checked into the Skia repo, they will exist in Bazel's cache folder.
23    """
24    rules = []
25    for hdr in to_generate:
26        cpp = hdr + ".cpp"
27        native.genrule(
28            name = "gen_" + cpp,
29            srcs = headers,
30            outs = ["gen/" + cpp],
31            # Copy the header as the output .cpp file
32            # https://bazel.build/reference/be/make-variables#predefined_genrule_variables
33            cmd = "cp %s $@" % hdr,
34        )
35        rules.append(":gen/" + cpp)
36
37    native.filegroup(
38        name = name,
39        srcs = rules,
40    )
41
42def generate_cpp_files_for_header_list(name, headers, visibility = None):
43    """Generate a filegroup containing generate .cpp files for the given header files
44
45    Args:
46        name: The name of the filegroup to hold the generated files.
47        headers: A list of header files, from this package, that should have a .cpp file generated
48                 for them that includes the header. If a header already has a .cpp file, it can be
49                 generally be in this list, it will just get analyzed twice (although this sometimes
50                 tickles bugs with IWYU). The generated files will not be checked into the Skia
51                 repo, they will exist in Bazel's cache folder.
52        visibility: A list of packages which can use the generated filegroup. Defaults to private.
53    """
54    if not visibility:
55        visibility = ["//visibility:private"]
56    rules = []
57    for hdr in headers:
58        cpp = hdr + ".cpp"
59        native.genrule(
60            name = "gen_" + name + cpp,
61            srcs = [hdr],
62            outs = ["gen_" + name + "/" + cpp],
63            # Copy the header as the output .cpp file
64            # https://bazel.build/reference/be/make-variables#predefined_genrule_variables
65            cmd = "cp $< $@",
66        )
67        rules.append(":gen_" + name + "/" + cpp)
68
69    native.filegroup(
70        name = name,
71        srcs = rules,
72        visibility = visibility,
73    )
74