Skip to content

Commit

Permalink
fix: correctly parse a space inside command-line option (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyawk authored Dec 20, 2024
1 parent e5083b5 commit 7cdc546
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 5 deletions.
7 changes: 4 additions & 3 deletions lang/cc/build_error.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ load(
load(
"//lang/private:general_build_actions.bzl",
"DEFAULT_MATCHER",
"LIST_ALL_ARGS",
"check_build_error",
"check_each_message",
"get_executable_file",
Expand Down Expand Up @@ -171,7 +172,7 @@ def _try_compile(ctx):
outputs = [compile_output, compile_stdout, compile_stderr],
inputs = inputs,
arguments = [args],
command = "$@",
command = LIST_ALL_ARGS,
tools = cc_toolchain.all_files.to_list() + [try_build_executable],
)

Expand Down Expand Up @@ -340,7 +341,7 @@ def _try_link(ctx, compile_output):
outputs = [link_output, link_stdout, link_stderr],
inputs = inputs,
arguments = [args],
command = "$@",
command = LIST_ALL_ARGS,
tools = cc_toolchain.all_files.to_list() + [try_build_executable],
)

Expand Down Expand Up @@ -651,7 +652,7 @@ def _create_test_impl(ctx):
ctx.actions.run_shell(
outputs = [executable],
inputs = cc_build_error_info.markers + [executable_template],
command = "cp $1 $2",
command = 'cp "$1" "$2"',
arguments = [
executable_template.path,
executable.path,
Expand Down
6 changes: 4 additions & 2 deletions lang/private/general_build_actions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ DEFAULT_MATCHER = struct(
pattern = None,
)

LIST_ALL_ARGS = '"$@"'

def get_executable_file(label):
"""Get executable file if the label is not None.
Expand Down Expand Up @@ -72,7 +74,7 @@ def check_build_error(
ctx.actions.run_shell(
outputs = [marker_check_build_error],
inputs = files_to_check + [error_message_file],
command = "$@",
command = LIST_ALL_ARGS,
arguments = [args],
tools = [check_emptiness],
)
Expand Down Expand Up @@ -148,7 +150,7 @@ def check_each_message(
ctx.actions.run_shell(
outputs = [marker_file],
inputs = [message_file, pattern_file],
command = "$@",
command = LIST_ALL_ARGS,
arguments = [
checker.path,
matcher.path,
Expand Down
71 changes: 71 additions & 0 deletions tests/cc/c_compile_error_options_with_space/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("//matcher:defs.bzl", "matcher")
load("//tests/cc:utils.bzl", "check_build_and_test")

check_build_and_test(
name = "using_copts_plain",
src = "c_compile_error.c",
copts = ['-DCAUSE_COMPILATION_ERROR=static_assert(0, "Compile error message for c_compile_error.c")'],
)

check_build_and_test(
name = "using_copts_with_substr_matcher",
src = "c_compile_error.c",
compile_stderr = matcher.has_substr("for c_compile_error.c"),
copts = ['-DCAUSE_COMPILATION_ERROR=static_assert(0, "Compile error message for c_compile_error.c")'],
)

check_build_and_test(
name = "using_copts_with_basic_regex_matcher",
src = "c_compile_error.c",
compile_stderr = matcher.contains_basic_regex(r"for[[:space:]]c_compile_error\.\(c\|cxx\)"),
copts = ['-DCAUSE_COMPILATION_ERROR=static_assert(0, "Compile error message for c_compile_error.c")'],
)

check_build_and_test(
name = "using_copts_with_extended_regex_matcher",
src = "c_compile_error.c",
compile_stderr = matcher.contains_extended_regex(r"for[[:space:]]c_compile_error\.(c|cxx)"),
copts = ['-DCAUSE_COMPILATION_ERROR=static_assert(0, "Compile error message for c_compile_error.c")'],
)

check_build_and_test(
name = "using_local_defines_plain",
src = "c_compile_error.c",
local_defines = ['CAUSE_COMPILATION_ERROR=static_assert(0, "Compile error message for c_compile_error.c")'],
)

check_build_and_test(
name = "using_local_defines_with_substr_matcher",
src = "c_compile_error.c",
compile_stderr = matcher.has_substr("for c_compile_error.c"),
local_defines = ['CAUSE_COMPILATION_ERROR=static_assert(0, "Compile error message for c_compile_error.c")'],
)

check_build_and_test(
name = "using_local_defines_with_basic_regex_matcher",
src = "c_compile_error.c",
compile_stderr = matcher.contains_basic_regex(r"for[[:space:]]c_compile_error\.\(c\|cxx\)"),
local_defines = ['CAUSE_COMPILATION_ERROR=static_assert(0, "Compile error message for c_compile_error.c")'],
)

check_build_and_test(
name = "using_local_defines_with_extended_regex_matcher",
src = "c_compile_error.c",
compile_stderr = matcher.contains_extended_regex(r"for[[:space:]]c_compile_error\.(c|cxx)"),
local_defines = ['CAUSE_COMPILATION_ERROR=static_assert(0, "Compile error message for c_compile_error.c")'],
)

build_test(
name = "build_test",
targets = [
":using_copts_plain",
":using_copts_with_substr_matcher",
":using_copts_with_basic_regex_matcher",
":using_copts_with_extended_regex_matcher",
":using_local_defines_plain",
":using_local_defines_with_substr_matcher",
":using_local_defines_with_basic_regex_matcher",
":using_local_defines_with_extended_regex_matcher",
],
)
6 changes: 6 additions & 0 deletions tests/cc/c_compile_error_options_with_space/c_compile_error.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <assert.h>

int main() {
CAUSE_COMPILATION_ERROR;
return 0;
}
71 changes: 71 additions & 0 deletions tests/cc/cpp_compile_error_options_with_space/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("//matcher:defs.bzl", "matcher")
load("//tests/cc:utils.bzl", "check_build_and_test")

check_build_and_test(
name = "using_copts_plain",
src = "cpp_compile_error.cpp",
copts = ['-DCAUSE_COMPILATION_ERROR=static_assert(false, "Compile error message for cpp_compile_error.cpp")'],
)

check_build_and_test(
name = "using_copts_with_substr_matcher",
src = "cpp_compile_error.cpp",
compile_stderr = matcher.has_substr("for cpp_compile_error.cpp"),
copts = ['-DCAUSE_COMPILATION_ERROR=static_assert(false, "Compile error message for cpp_compile_error.cpp")'],
)

check_build_and_test(
name = "using_copts_with_basic_regex_matcher",
src = "cpp_compile_error.cpp",
compile_stderr = matcher.contains_basic_regex(r"for[[:space:]]cpp_compile_error\.\(cpp\|cxx\)"),
copts = ['-DCAUSE_COMPILATION_ERROR=static_assert(false, "Compile error message for cpp_compile_error.cpp")'],
)

check_build_and_test(
name = "using_copts_with_extended_regex_matcher",
src = "cpp_compile_error.cpp",
compile_stderr = matcher.contains_extended_regex(r"for[[:space:]]cpp_compile_error\.(cpp|cxx)"),
copts = ['-DCAUSE_COMPILATION_ERROR=static_assert(false, "Compile error message for cpp_compile_error.cpp")'],
)

check_build_and_test(
name = "using_local_defines_plain",
src = "cpp_compile_error.cpp",
local_defines = ['CAUSE_COMPILATION_ERROR=static_assert(false, "Compile error message for cpp_compile_error.cpp")'],
)

check_build_and_test(
name = "using_local_defines_with_substr_matcher",
src = "cpp_compile_error.cpp",
compile_stderr = matcher.has_substr("for cpp_compile_error.cpp"),
local_defines = ['CAUSE_COMPILATION_ERROR=static_assert(false, "Compile error message for cpp_compile_error.cpp")'],
)

check_build_and_test(
name = "using_local_defines_with_basic_regex_matcher",
src = "cpp_compile_error.cpp",
compile_stderr = matcher.contains_basic_regex(r"for[[:space:]]cpp_compile_error\.\(cpp\|cxx\)"),
local_defines = ['CAUSE_COMPILATION_ERROR=static_assert(false, "Compile error message for cpp_compile_error.cpp")'],
)

check_build_and_test(
name = "using_local_defines_with_extended_regex_matcher",
src = "cpp_compile_error.cpp",
compile_stderr = matcher.contains_extended_regex(r"for[[:space:]]cpp_compile_error\.(cpp|cxx)"),
local_defines = ['CAUSE_COMPILATION_ERROR=static_assert(false, "Compile error message for cpp_compile_error.cpp")'],
)

build_test(
name = "build_test",
targets = [
":using_copts_plain",
":using_copts_with_substr_matcher",
":using_copts_with_basic_regex_matcher",
":using_copts_with_extended_regex_matcher",
":using_local_defines_plain",
":using_local_defines_with_substr_matcher",
":using_local_defines_with_basic_regex_matcher",
":using_local_defines_with_extended_regex_matcher",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int main() {
CAUSE_COMPILATION_ERROR;
return 0;
}

0 comments on commit 7cdc546

Please sign in to comment.