Skip to content

Commit

Permalink
Support windows (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyawk authored Nov 2, 2024
1 parent 814a4d1 commit a9d50bb
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 37 deletions.
1 change: 1 addition & 0 deletions .bcr/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ bcr_test_module:
- "debian10"
- "ubuntu2004"
- "macos"
- "windows"
bazel:
- "6.x"
- "7.x"
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ jobs:
- ubuntu-22.04
- macos-13
- macos-14
# TODO: Currently runtime error occurs on windows
# - windows-2019
# - windows-2022
- windows-2019
- windows-2022
runs-on: ${{ matrix.runner }}
steps:
- name: Checkout
Expand Down
21 changes: 15 additions & 6 deletions lang/cc/build_error.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,12 @@ def _try_compile(ctx):
inputs = [ctx.file.src]

# Arguments for `try_build.bash`
try_build_executable = get_executable_file(ctx.attr._try_build)
if type(try_build_executable) != "File":
fail("{} must correspond to an executable".format(ctx.attr._try_build))

args = ctx.actions.args()
args.add(try_build_executable)
args.add("-e", compile_stderr)
args.add("-o", compile_stdout)
args.add("-n", compile_output)
Expand All @@ -161,12 +166,12 @@ def _try_compile(ctx):
args.add("-c", ctx.file.src)
args.add("-o", compile_output)

ctx.actions.run(
ctx.actions.run_shell(
outputs = [compile_output, compile_stdout, compile_stderr],
inputs = inputs,
executable = get_executable_file(ctx.attr._try_build),
arguments = [args],
tools = cc_toolchain.all_files,
command = "$@",
tools = cc_toolchain.all_files.to_list() + [try_build_executable],
)

return struct(
Expand Down Expand Up @@ -296,7 +301,11 @@ def _try_link(ctx, compile_output):
inputs = [compile_output] + ctx.attr.additional_linker_inputs

# Arguments for `try_build.bash`
try_build_executable = get_executable_file(ctx.attr._try_build)
if type(try_build_executable) != "File":
fail("{} must correspond to an executable".format(ctx.attr._try_build))
args = ctx.actions.args()
args.add(try_build_executable)
args.add("-e", link_stderr)
args.add("-o", link_stdout)
args.add("-n", link_output)
Expand Down Expand Up @@ -325,12 +334,12 @@ def _try_link(ctx, compile_output):
args.add("-o", link_output)
args.add(compile_output)

ctx.actions.run(
ctx.actions.run_shell(
outputs = [link_output, link_stdout, link_stderr],
inputs = inputs,
executable = get_executable_file(ctx.attr._try_build),
arguments = [args],
tools = cc_toolchain.all_files,
command = "$@",
tools = cc_toolchain.all_files.to_list() + [try_build_executable],
)

return struct(
Expand Down
29 changes: 21 additions & 8 deletions lang/private/general_build_actions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def check_build_error(
check_emptiness):
"""Check if a build error occured.
Force internal `ctx.actions.run` execution to fail if the previous build error
Force internal `ctx.actions.run_shell` execution to fail if the previous build error
did NOT occur, otherwise, create an empty text file as a marker for the action.
This marker file has to be surely evaluated by the rule.
Expand All @@ -49,20 +49,32 @@ def check_build_error(
ctx.label.name + "/marker_check_build_error",
)

# Create a text file to contain the error message
# in order to easily escape its characters
error_message_file = ctx.actions.declare_file(
ctx.label.name + "/error_message_file_check_build_error",
)
ctx.actions.write(
output = error_message_file,
content = error_message,
)

# Arguments for `check_emptiness`
args = ctx.actions.args()
args.add(check_emptiness)

for file_to_check in files_to_check:
args.add("-f", file_to_check)

args.add("-m", error_message)
args.add("-m", error_message_file)
args.add("-n", marker_check_build_error)

ctx.actions.run(
ctx.actions.run_shell(
outputs = [marker_check_build_error],
inputs = files_to_check,
executable = check_emptiness,
inputs = files_to_check + [error_message_file],
command = "$@",
arguments = [args],
tools = [check_emptiness],
)

return marker_check_build_error
Expand Down Expand Up @@ -133,17 +145,18 @@ def check_each_message(
content = pattern,
)

ctx.actions.run(
ctx.actions.run_shell(
outputs = [marker_file],
inputs = [message_file, pattern_file],
executable = checker,
command = "$@",
arguments = [
checker.path,
matcher.path,
pattern_file.path,
message_file.path,
marker_file.path,
],
tools = [matcher],
tools = [checker, matcher],
)

return marker_file
6 changes: 3 additions & 3 deletions lang/private/script/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ package(
default_visibility = ["//lang:__subpackages__"],
)

sh_binary(
filegroup(
name = "check_each_message",
srcs = ["check_each_message.bash"],
)

sh_binary(
filegroup(
name = "check_emptiness",
srcs = ["check_emptiness.bash"],
)

sh_binary(
filegroup(
name = "try_build",
srcs = ["try_build.bash"],
)
5 changes: 2 additions & 3 deletions lang/private/script/check_each_message.bash
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env bash

set -euo pipefail

usage() {
Expand All @@ -8,7 +6,8 @@ Check each message file.
Exits with an error if the pattern is not found in the message file.
Usage: $0 MATCHER PATTERN_FILE MESSAGE_FILE [MARKER_FILE ...]
Note that this script is supposed to be invoked by 'ctx.actions.run_shell'.
Usage: bash -c '\$@' '' $0 MATCHER PATTERN_FILE MESSAGE_FILE [MARKER_FILE ...]
MATCHER
Executable to check if the pattern string is inside the message file
Expand Down
19 changes: 11 additions & 8 deletions lang/private/script/check_emptiness.bash
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
#!/usr/bin/env bash

set -euo pipefail

usage() {
cat <<EOS >&2
Check if any of the files are empty.
Usage: $0 [OPTIONS]
Note that this script is supposed to be invoked by 'ctx.actions.run_shell'.
Usage: bash -c '\$@' '' $0 [OPTIONS]
OPTIONS
-f FILE_TO_CHECK
Successfully exit if the file is empty
-h
Show usage and exit
-m MESSAGE
Error message when no files are empty
-m MESSAGE_FILE
Text file containing the error message when no files are empty
-n NEW_FILE_PATH
If specified, create a new empty file before exiting the script
EOS
Expand Down Expand Up @@ -46,7 +45,6 @@ exit_if_containing_an_empty_file() {

files_to_check=()
files_to_touch=()
error_message="ERROR: No files are empty"

while getopts "f:hm:n:" opt; do
case "${opt}" in
Expand All @@ -58,7 +56,7 @@ while getopts "f:hm:n:" opt; do
exit 0
;;
m)
error_message="${OPTARG}"
error_message_file="${OPTARG}"
;;
n)
files_to_touch+=("${OPTARG}")
Expand All @@ -67,6 +65,11 @@ while getopts "f:hm:n:" opt; do
done
shift $((OPTIND -1))

if [[ ! -n "${error_message_file:-}" ]]; then
echo "ERROR: Option '-m' must be set" >&2
exit 1
fi

# Make sure the required files are touched before exiting
if [[ "${#files_to_touch[@]}" -gt 0 ]]; then
trap 'touch "${files_to_touch[@]}"' EXIT
Expand All @@ -77,5 +80,5 @@ if [[ "${#files_to_check[@]}" -gt 0 ]]; then
fi

# Exit with error if there's no empty file
echo "${error_message}" >&2
cat "${error_message_file}" >&2
exit 1
5 changes: 2 additions & 3 deletions lang/private/script/try_build.bash
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env bash

set -euo pipefail

usage() {
Expand All @@ -12,7 +10,8 @@ without executing the build command.
Even if the build command fails, this script won't exit with an error.
Usage: $0 [OPTIONS] COMMAND
Note that this script is supposed to be invoked by 'ctx.actions.run_shell'.
Usage: bash -c '\$@' '' $0 [OPTIONS] COMMAND
OPTIONS
-f FILE_TO_CHECK
Expand Down
1 change: 0 additions & 1 deletion matcher/executable/contains_basic_regex.bash
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env bash
#
# Matcher executable for `contains_basic_regex`.
#
Expand Down
1 change: 0 additions & 1 deletion matcher/executable/contains_extended_regex.bash
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env bash
#
# Matcher executable for `contains_extended_regex`.
#
Expand Down
1 change: 0 additions & 1 deletion matcher/executable/has_substr.bash
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env bash
#
# Matcher executable for `has_substr`.
#
Expand Down

0 comments on commit a9d50bb

Please sign in to comment.