• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#    http://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,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Implementation of write_file macro and underlying rules.
16
17These rules write a UTF-8 encoded text file, using Bazel's FileWriteAction.
18'_write_xfile' marks the resulting file executable, '_write_file' does not.
19"""
20
21def _common_impl(ctx, is_windows, is_executable):
22    if ctx.attr.newline == "auto":
23        newline = "\r\n" if is_windows else "\n"
24    elif ctx.attr.newline == "windows":
25        newline = "\r\n"
26    else:
27        newline = "\n"
28
29    # ctx.actions.write creates a FileWriteAction which uses UTF-8 encoding.
30    ctx.actions.write(
31        output = ctx.outputs.out,
32        content = newline.join(ctx.attr.content) if ctx.attr.content else "",
33        is_executable = is_executable,
34    )
35    files = depset(direct = [ctx.outputs.out])
36    runfiles = ctx.runfiles(files = [ctx.outputs.out])
37    if is_executable:
38        return [DefaultInfo(files = files, runfiles = runfiles, executable = ctx.outputs.out)]
39    else:
40        return [DefaultInfo(files = files, runfiles = runfiles)]
41
42def _impl(ctx):
43    return _common_impl(ctx, ctx.attr.is_windows, False)
44
45def _ximpl(ctx):
46    return _common_impl(ctx, ctx.attr.is_windows, True)
47
48_ATTRS = {
49    "out": attr.output(mandatory = True),
50    "content": attr.string_list(mandatory = False, allow_empty = True),
51    "newline": attr.string(values = ["unix", "windows", "auto"], default = "auto"),
52    "is_windows": attr.bool(mandatory = True),
53}
54
55_write_file = rule(
56    implementation = _impl,
57    provides = [DefaultInfo],
58    attrs = _ATTRS,
59)
60
61_write_xfile = rule(
62    implementation = _ximpl,
63    executable = True,
64    provides = [DefaultInfo],
65    attrs = _ATTRS,
66)
67
68def write_file(
69        name,
70        out,
71        content = [],
72        is_executable = False,
73        newline = "auto",
74        **kwargs):
75    """Creates a UTF-8 encoded text file.
76
77    Args:
78      name: Name of the rule.
79      out: Path of the output file, relative to this package.
80      content: A list of strings. Lines of text, the contents of the file.
81          Newlines are added automatically after every line except the last one.
82      is_executable: A boolean. Whether to make the output file executable.
83          When True, the rule's output can be executed using `bazel run` and can
84          be in the srcs of binary and test rules that require executable
85          sources.
86      newline: one of ["auto", "unix", "windows"]: line endings to use. "auto"
87          for platform-determined, "unix" for LF, and "windows" for CRLF.
88      **kwargs: further keyword arguments, e.g. <code>visibility</code>
89    """
90    if is_executable:
91        _write_xfile(
92            name = name,
93            content = content,
94            out = out,
95            newline = newline or "auto",
96            is_windows = select({
97                "@bazel_tools//src/conditions:host_windows": True,
98                "//conditions:default": False,
99            }),
100            **kwargs
101        )
102    else:
103        _write_file(
104            name = name,
105            content = content,
106            out = out,
107            newline = newline or "auto",
108            is_windows = select({
109                "@bazel_tools//src/conditions:host_windows": True,
110                "//conditions:default": False,
111            }),
112            **kwargs
113        )
114