-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
327d61b
commit 2154464
Showing
7 changed files
with
175 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# 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 rule that 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 | ||
We cannot use genrule or copy_file rule to copy a source file to bazel-bin with | ||
the same relative path, because both rules create label for the output | ||
artifact, which is conflict with the source file label. map_to_output rule | ||
achieves that by avoiding creating label for the output artifact. | ||
""" | ||
|
||
load( | ||
"//rules/private:map_to_output_private.bzl", | ||
_map_to_output = "map_to_output", | ||
) | ||
|
||
map_to_output = _map_to_output |
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,60 @@ | ||
# 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. | ||
|
||
"""map_to_output() rule implementations. | ||
This rule copies a source file to bazel-bin at the same execroot path, we avoid | ||
label conflict with the source file by not creating label for the output | ||
artifact. | ||
""" | ||
|
||
load("//rules/private:copy_file_private.bzl", "copy_bash", "copy_cmd") | ||
|
||
def _map_to_output_impl(ctx): | ||
src = ctx.file.src | ||
if not src.is_source: | ||
fail("A source file must be specified in map_to_output 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) | ||
return DefaultInfo(files = depset([dst]), runfiles = ctx.runfiles(files = [dst])) | ||
|
||
_map_to_output = rule( | ||
implementation = _map_to_output_impl, | ||
attrs = { | ||
"src": attr.label(mandatory = True, allow_single_file = True), | ||
"is_windows": attr.bool(mandatory = True), | ||
} | ||
) | ||
|
||
def map_to_output(name, src, **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 Label. The file to to copy. | ||
**kwargs: further keyword arguments, e.g. `visibility` | ||
""" | ||
_map_to_output( | ||
name = name, | ||
src = src, | ||
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("//rules:map_to_output.bzl", "map_to_output") | ||
|
||
licenses(["notice"]) | ||
|
||
package(default_testonly = 1) | ||
|
||
sh_test( | ||
name = "map_to_output_tests", | ||
srcs = ["map_to_output_tests.sh"], | ||
data = [ | ||
":a", | ||
"//tests:unittest.bash", | ||
], | ||
deps = ["@bazel_tools//tools/bash/runfiles"], | ||
) | ||
|
||
map_to_output( | ||
name = "a", | ||
src = "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,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
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 bazel_skylib/tests/unittest.bash)" \ | ||
|| { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; } | ||
|
||
function test_map_to_output() { | ||
echo "$(rlocation bazel_skylib/tests/map_to_output/foo/bar/a.txt)" >"$TEST_log" | ||
# Test the foo/bar/a.txt is copied to bazel-out/ | ||
expect_log 'bazel-out/' | ||
cat "$(rlocation bazel_skylib/tests/map_to_output/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" |