write_file: support different line endings (#150)

The user can specify which line endings they want
write_file to use. This helps avoiding line ending
mismatches with diff_test.

Example: diff_test verifies that a rule generates
correct output by comparing it to a checked-in
"golden" file. Both files are text files, and the
user builds on Windows but the golden file was
written on Linux and git checkout preserved
original line endings.

Without explicitly specifying which line endings
to use, this diff_test would fail on an otherwise
good output.

With explicit line endings we don't need to check
in the golden file to git, we can just generate it
with "auto" line endings.
diff --git a/docs/write_file_doc.md b/docs/write_file_doc.md
index ecd96bd..d8f098e 100755
--- a/docs/write_file_doc.md
+++ b/docs/write_file_doc.md
@@ -1,7 +1,7 @@
 ## write_file
 
 <pre>
-write_file(<a href="#write_file-name">name</a>, <a href="#write_file-out">out</a>, <a href="#write_file-content">content</a>, <a href="#write_file-is_executable">is_executable</a>, <a href="#write_file-kwargs">kwargs</a>)
+write_file(<a href="#write_file-name">name</a>, <a href="#write_file-out">out</a>, <a href="#write_file-content">content</a>, <a href="#write_file-is_executable">is_executable</a>, <a href="#write_file-newline">newline</a>, <a href="#write_file-kwargs">kwargs</a>)
 </pre>
 
 Creates a UTF-8 encoded text file.
@@ -47,9 +47,20 @@
       <td>
         optional. default is <code>False</code>
         <p>
-          A boolean. Whether to make the output file executable. When
-    True, the rule's output can be executed using `bazel run` and can be
-    in the srcs of binary and test rules that require executable sources.
+          A boolean. Whether to make the output file executable.
+    When True, the rule's output can be executed using `bazel run` and can
+    be in the srcs of binary and test rules that require executable
+    sources.
+        </p>
+      </td>
+    </tr>
+    <tr id="write_file-newline">
+      <td><code>newline</code></td>
+      <td>
+        optional. default is <code>"auto"</code>
+        <p>
+          one of ["auto", "unix", "windows"]: line endings to use. "auto"
+    for platform-determined, "unix" for LF, and "windows" for CRLF.
         </p>
       </td>
     </tr>
@@ -58,7 +69,7 @@
       <td>
         optional.
         <p>
-          further keyword arguments, e.g. `visibility`
+          further keyword arguments, e.g. <code>visibility</code>
         </p>
       </td>
     </tr>
diff --git a/rules/private/write_file_private.bzl b/rules/private/write_file_private.bzl
index 64a3361..ac8ce9c 100644
--- a/rules/private/write_file_private.bzl
+++ b/rules/private/write_file_private.bzl
@@ -18,11 +18,18 @@
 '_write_xfile' marks the resulting file executable, '_write_file' does not.
 """
 
-def _common_impl(ctx, is_executable):
+def _common_impl(ctx, is_windows, is_executable):
+    if ctx.attr.newline == "auto":
+        newline = "\r\n" if is_windows else "\n"
+    elif ctx.attr.newline == "windows":
+        newline = "\r\n"
+    else:
+        newline = "\n"
+
     # ctx.actions.write creates a FileWriteAction which uses UTF-8 encoding.
     ctx.actions.write(
         output = ctx.outputs.out,
-        content = "\n".join(ctx.attr.content) if ctx.attr.content else "",
+        content = newline.join(ctx.attr.content) if ctx.attr.content else "",
         is_executable = is_executable,
     )
     files = depset(direct = [ctx.outputs.out])
@@ -33,14 +40,16 @@
         return [DefaultInfo(files = files, runfiles = runfiles)]
 
 def _impl(ctx):
-    return _common_impl(ctx, False)
+    return _common_impl(ctx, ctx.attr.is_windows, False)
 
 def _ximpl(ctx):
-    return _common_impl(ctx, True)
+    return _common_impl(ctx, ctx.attr.is_windows, True)
 
 _ATTRS = {
     "out": attr.output(mandatory = True),
     "content": attr.string_list(mandatory = False, allow_empty = True),
+    "newline": attr.string(values = ["unix", "windows", "auto"], default = "auto"),
+    "is_windows": attr.bool(mandatory = True),
 }
 
 _write_file = rule(
@@ -56,7 +65,13 @@
     attrs = _ATTRS,
 )
 
-def write_file(name, out, content = [], is_executable = False, **kwargs):
+def write_file(
+        name,
+        out,
+        content = [],
+        is_executable = False,
+        newline = "auto",
+        **kwargs):
     """Creates a UTF-8 encoded text file.
 
     Args:
@@ -64,16 +79,24 @@
       out: Path of the output file, relative to this package.
       content: A list of strings. Lines of text, the contents of the file.
           Newlines are added automatically after every line except the last one.
-      is_executable: A boolean. Whether to make the output file executable. When
-          True, the rule's output can be executed using `bazel run` and can be
-          in the srcs of binary and test rules that require executable sources.
-      **kwargs: further keyword arguments, e.g. `visibility`
+      is_executable: A boolean. Whether to make the output file executable.
+          When True, the rule's output can be executed using `bazel run` and can
+          be in the srcs of binary and test rules that require executable
+          sources.
+      newline: one of ["auto", "unix", "windows"]: line endings to use. "auto"
+          for platform-determined, "unix" for LF, and "windows" for CRLF.
+      **kwargs: further keyword arguments, e.g. <code>visibility</code>
     """
     if is_executable:
         _write_xfile(
             name = name,
             content = content,
             out = out,
+            newline = newline or "auto",
+            is_windows = select({
+                "@bazel_tools//src/conditions:host_windows": True,
+                "//conditions:default": False,
+            }),
             **kwargs
         )
     else:
@@ -81,5 +104,10 @@
             name = name,
             content = content,
             out = out,
+            newline = newline or "auto",
+            is_windows = select({
+                "@bazel_tools//src/conditions:host_windows": True,
+                "//conditions:default": False,
+            }),
             **kwargs
         )
diff --git a/tests/write_file/BUILD b/tests/write_file/BUILD
index 321bcc0..9ea3609 100644
--- a/tests/write_file/BUILD
+++ b/tests/write_file/BUILD
@@ -32,6 +32,7 @@
 #   field of it, so we assert that that field contains the output file of the
 #   rule
 
+load("//rules:diff_test.bzl", "diff_test")
 load("//rules:write_file.bzl", "write_file")
 
 licenses(["notice"])
@@ -117,3 +118,49 @@
     ],
     is_executable = True,
 )
+
+write_file(
+    name = "newline_unix_actual",
+    out = "out/newline_unix_actual.txt",
+    content = [
+        "ab",
+        "cd",
+        "ef",
+    ],
+    newline = "unix",
+)
+
+write_file(
+    name = "newline_unix_exp",
+    out = "out/newline_unix_exp.txt",
+    content = ["ab\ncd\nef"],
+)
+
+diff_test(
+    name = "unix_line_ending_test",
+    file1 = ":newline_unix_actual",
+    file2 = ":newline_unix_exp",
+)
+
+write_file(
+    name = "newline_win_actual",
+    out = "out/newline_win_actual.txt",
+    content = [
+        "ab",
+        "cd",
+        "ef",
+    ],
+    newline = "windows",
+)
+
+write_file(
+    name = "newline_win_exp",
+    out = "out/newline_win_exp.txt",
+    content = ["ab\r\ncd\r\nef"],
+)
+
+diff_test(
+    name = "win_line_ending_test",
+    file1 = ":newline_win_actual",
+    file2 = ":newline_win_exp",
+)