-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge_licenses genrule: reimplement in Starlark
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
1 parent
5609d14
commit cc6158d
Showing
3 changed files
with
81 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/main/java/com/google/devtools/build/lib/merge_licenses.bzl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
src/main/java/com/google/devtools/build/lib/merge_licenses.sh
This file was deleted.
Oops, something went wrong.