Skip to content

Commit

Permalink
merge_licenses genrule: reimplement in Starlark
Browse files Browse the repository at this point in the history
Reimplement a genrule that Bazel depends on in
Starlark, to reduce the Bazel source tree's
dependency on Bash (when built on Windows).

Closes #7128.

Change-Id: I20a888559008c350e11133fea1255c9b4c396805
PiperOrigin-RevId: 229512144
  • Loading branch information
laszlocsomor authored and Copybara-Service committed Jan 16, 2019
1 parent 5609d14 commit cc6158d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 34 deletions.
10 changes: 4 additions & 6 deletions src/main/java/com/google/devtools/build/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package(
default_visibility = ["//src:__subpackages__"],
)

load(":merge_licenses.bzl", "merge_licenses")

# Generate list of all srcs via:
# bazel query 'filter("srcs", kind("filegroup rule", //src/main/java/com/google/devtools/build/lib/...))' | sort | sed -e "s/^/\"/" | sed -e "s/$/\",/" | fgrep -v "build/lib:srcs"
filegroup(
Expand Down Expand Up @@ -1289,17 +1291,13 @@ java_library(
],
)

genrule(
merge_licenses(
name = "merge_licenses",
srcs = [
"//:LICENSE",
"//third_party:srcs",
],
outs = ["runtime/commands/LICENSE"],
# Use xargs to avoid the shell script complaining that is has been passed too many arguments.
# xargs calls the script multiple times with at most 1000 arguments per call.
cmd = "echo \"$(SRCS)\" | xargs -n1000 $(location merge_licenses.sh) > \"$@\"",
tools = ["merge_licenses.sh"],
out = "runtime/commands/LICENSE",
)

java_library(
Expand Down
77 changes: 77 additions & 0 deletions src/main/java/com/google/devtools/build/lib/merge_licenses.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Copyright 2019 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""A platform-independent build rule that merges license files."""

def _windows_action(ctx, files):
cmd = "(FOR %F IN (%SRCS%) DO ((SET X=%F)&ECHO ===== !X:\\=/! =====&TYPE %F&ECHO.&ECHO.)) > %OUT%"
ctx.actions.run(
inputs = depset(direct = files),
outputs = [ctx.outputs.out],
executable = "cmd.exe",
arguments = ["/V:ON", "/E:ON", "/Q", "/C", cmd],
env = {
"OUT": ctx.outputs.out.path.replace("/", "\\"),
"SRCS": " ".join([f.path.replace("/", "\\") for f in files]),
},
)

def _bash_action(ctx, files):
cmd = "for f in $SRCS; do echo ===== $f ===== && cat $f && echo && echo ; done > $OUT"
ctx.actions.run_shell(
inputs = depset(direct = files),
outputs = [ctx.outputs.out],
command = cmd,
env = {
"OUT": ctx.outputs.out.path,
"SRCS": " ".join([f.path for f in files]),
},
)

def _impl(ctx):
files = []
for src in ctx.files.srcs:
for substr in ["ASSEMBLY_EXCEPTION", "DISCLAIMER", "LICENSE", "license", "THIRD_PARTY_README"]:
if substr in src.path:
files.append(src)
break
if not files:
fail("expected some sources")
if ctx.attr.is_windows:
_windows_action(ctx, files)
else:
_bash_action(ctx, files)

return [DefaultInfo(files = depset(direct = [ctx.outputs.out]))]

_merge_licenses = rule(
implementation = _impl,
attrs = {
"srcs": attr.label_list(allow_files = True, mandatory = True),
"out": attr.output(mandatory = True),
"is_windows": attr.bool(mandatory = True),
},
)

def merge_licenses(name, srcs, out, **kwargs):
_merge_licenses(
name = name,
srcs = srcs,
out = out,
is_windows = select({
"@bazel_tools//src/conditions:windows": True,
"//conditions:default": False,
}),
**kwargs
)
28 changes: 0 additions & 28 deletions src/main/java/com/google/devtools/build/lib/merge_licenses.sh

This file was deleted.

0 comments on commit cc6158d

Please sign in to comment.