pytorch
81 строка · 2.6 Кб
1# This Bazel rules file is derived from https://github.com/tensorflow/tensorflow/blob/master/third_party/common.bzl
2
3# Rule for simple expansion of template files. This performs a simple
4# search over the template file for the keys in substitutions,
5# and replaces them with the corresponding values.
6#
7# Typical usage:
8# load("/tools/build_rules/template_rule", "template_rule")
9# template_rule(
10# name = "ExpandMyTemplate",
11# src = "my.template",
12# out = "my.txt",
13# substitutions = {
14# "$VAR1": "foo",
15# "$VAR2": "bar",
16# }
17# )
18#
19# Args:
20# name: The name of the rule.
21# template: The template file to expand
22# out: The destination of the expanded file
23# substitutions: A dictionary mapping strings to their substitutions
24
25def template_rule_impl(ctx):
26ctx.actions.expand_template(
27template = ctx.file.src,
28output = ctx.outputs.out,
29substitutions = ctx.attr.substitutions,
30)
31
32template_rule = rule(
33attrs = {
34"out": attr.output(mandatory = True),
35"src": attr.label(
36mandatory = True,
37allow_single_file = True,
38),
39"substitutions": attr.string_dict(mandatory = True),
40},
41# output_to_genfiles is required for header files.
42output_to_genfiles = True,
43implementation = template_rule_impl,
44)
45
46# Header template rule is an extension of template substitution rule
47# That also makes this header a valid dependency for cc_library
48# From https://stackoverflow.com/a/55407399
49def header_template_rule_impl(ctx):
50ctx.actions.expand_template(
51template = ctx.file.src,
52output = ctx.outputs.out,
53substitutions = ctx.attr.substitutions,
54)
55return [
56# create a provider which says that this
57# out file should be made available as a header
58CcInfo(compilation_context = cc_common.create_compilation_context(
59
60# pass out the include path for finding this header
61system_includes = depset([ctx.attr.include, ctx.outputs.out.dirname, ctx.bin_dir.path]),
62
63# and the actual header here.
64headers = depset([ctx.outputs.out]),
65)),
66]
67
68header_template_rule = rule(
69attrs = {
70"include": attr.string(),
71"out": attr.output(mandatory = True),
72"src": attr.label(
73mandatory = True,
74allow_single_file = True,
75),
76"substitutions": attr.string_dict(mandatory = True),
77},
78# output_to_genfiles is required for header files.
79output_to_genfiles = True,
80implementation = header_template_rule_impl,
81)
82