-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(builtin): introduce copy_to_bin rule
Based on meteorcloudy@ awesome work in bazelbuild/bazel-skylib#217 That PR isn't getting approved for upstream so we vendor it into our own ruleset
- Loading branch information
Showing
11 changed files
with
1,021 additions
and
51 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
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
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
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,58 @@ | ||
# 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. | ||
|
||
"copy_to_bin() rule" | ||
|
||
load("//third_party/github.com/bazelbuild/bazel-skylib:rules/private/copy_file_private.bzl", "copy_bash", "copy_cmd") | ||
|
||
def _copy_to_bin_impl(ctx): | ||
all_dst = [] | ||
for src in ctx.files.srcs: | ||
if not src.is_source: | ||
fail("A source file must be specified in copy_to_bin rule, %s is not a source file." % src.path) | ||
dst = ctx.actions.declare_file(src.basename, sibling = src) | ||
if ctx.attr.is_windows: | ||
copy_cmd(ctx, src, dst) | ||
else: | ||
copy_bash(ctx, src, dst) | ||
all_dst.append(dst) | ||
return DefaultInfo(files = depset(all_dst), runfiles = ctx.runfiles(files = all_dst)) | ||
|
||
_copy_to_bin = rule( | ||
implementation = _copy_to_bin_impl, | ||
attrs = { | ||
"srcs": attr.label_list(mandatory = True, allow_files = True), | ||
"is_windows": attr.bool(mandatory = True, doc = "Automatically set by macro"), | ||
}, | ||
) | ||
|
||
def copy_to_bin(name, srcs, **kwargs): | ||
"""Copies a source file to bazel-bin at the same execroot path. | ||
Eg. <source_root>/foo/bar/a.txt -> <bazel-bin>/foo/bar/a.txt | ||
Args: | ||
name: Name of the rule. | ||
src: A List of Labels. File(s) to to copy. | ||
**kwargs: further keyword arguments, e.g. `visibility` | ||
""" | ||
_copy_to_bin( | ||
name = name, | ||
srcs = srcs, | ||
is_windows = select({ | ||
"@bazel_tools//src/conditions:host_windows": True, | ||
"//conditions:default": False, | ||
}), | ||
**kwargs | ||
) |
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,20 @@ | ||
load("//internal/common:copy_to_bin.bzl", "copy_to_bin") | ||
|
||
licenses(["notice"]) | ||
|
||
package(default_testonly = 1) | ||
|
||
sh_test( | ||
name = "copy_to_bin_tests", | ||
srcs = ["copy_to_bin_tests.sh"], | ||
data = [ | ||
":a", | ||
"//third_party/github.com/bazelbuild/bazel-skylib:tests/unittest.bash", | ||
], | ||
deps = ["@bazel_tools//tools/bash/runfiles"], | ||
) | ||
|
||
copy_to_bin( | ||
name = "a", | ||
srcs = ["foo/bar/a.txt"], | ||
) |
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,51 @@ | ||
# 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. | ||
|
||
# --- begin runfiles.bash initialization --- | ||
# Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash). | ||
set -euo pipefail | ||
if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then | ||
if [[ -f "$0.runfiles_manifest" ]]; then | ||
export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest" | ||
elif [[ -f "$0.runfiles/MANIFEST" ]]; then | ||
export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST" | ||
elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then | ||
export RUNFILES_DIR="$0.runfiles" | ||
fi | ||
fi | ||
if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then | ||
source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash" | ||
elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then | ||
source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \ | ||
"$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)" | ||
else | ||
echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash" | ||
exit 1 | ||
fi | ||
# --- end runfiles.bash initialization --- | ||
|
||
source "$(rlocation build_bazel_rules_nodejs/third_party/github.com/bazelbuild/bazel-skylib/tests/unittest.bash)" \ | ||
|| { echo "Could not source build_bazel_rules_nodejs/third_party/github.com/bazelbuild/bazel-skylib/tests/unittest.bash" >&2; exit 1; } | ||
|
||
function test_map_to_output() { | ||
echo "$(rlocation build_bazel_rules_nodejs/internal/common/test/foo/bar/a.txt)" >"$TEST_log" | ||
# Test the foo/bar/a.txt is copied to bazel-out/ | ||
expect_log 'bazel-out/' | ||
cat "$(rlocation build_bazel_rules_nodejs/internal/common/test/foo/bar/a.txt)" >"$TEST_log" | ||
# Test the content of foo/bar/a.txt is correct | ||
expect_log '#!/bin/bash' | ||
expect_log '^echo aaa$' | ||
} | ||
|
||
run_suite "map_to_output test suite" |
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,2 @@ | ||
#!/bin/bash | ||
echo aaa |
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
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
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
Oops, something went wrong.