Skip to content

Commit

Permalink
mock toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Apr 26, 2023
1 parent 139929d commit dce3fe7
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,105 @@ load(
"with_feature_set",
)
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
load("@rules_testing//lib:util.bzl", "util")

def _mock_cc_toolchain_config_impl(ctx):
return cc_common.create_cc_toolchain_config_info(
ctx = ctx,
features = features,
action_configs = action_configs,
artifact_name_patterns = artifact_name_patterns,
cxx_builtin_include_directories = ctx.attr.cxx_builtin_include_directories,
toolchain_identifier = ctx.attr.toolchain_identifier,
host_system_name = ctx.attr.host_system_name,
target_system_name = ctx.attr.target_system_name,
target_cpu = ctx.attr.cpu,
target_libc = ctx.attr.target_libc,
compiler = ctx.attr.compiler,
abi_version = ctx.attr.abi_version,
abi_libc_version = ctx.attr.abi_libc_version,
tool_paths = tool_paths,
builtin_sysroot = ctx.attr.builtin_sysroot,
action_configs = [
action_config(
action_name = ACTION_NAMES.cpp_link_static_library,
enabled = True,
tools = [
tool(
path = "my_ar",
execution_requirements = ["my_requirement"],
),
],
),
],
features = [
feature(
name = "archiver_flags",
enabled = True,
env_sets = [
env_set(
actions = [ACTION_NAMES.cpp_link_static_library],
env_entries = [
env_entry(
key = "MY_KEY",
value = "my_value",
),
],
),
],
flag_sets = [
flag_set(
actions = [ACTION_NAMES.cpp_link_static_library],
flag_groups = [
flag_group(flags = ["abc"]),
flag_group(
flags = ["/MY_OUT:%{output_execpath}"],
expand_if_available = "output_execpath",
),
],
),
],
),
],
artifact_name_patterns = [
artifact_name_pattern(
category_name = "static_library",
prefix = "prefix",
extension = ".lib",
),
],
toolchain_identifier = "mock_toolchain",
host_system_name = "local",
target_system_name = "local",
target_cpu = "local",
target_libc = "local",
compiler = "compiler",
)

mock_cc_toolchain_config = rule(
implementation = _impl,
_mock_cc_toolchain_config = rule(
implementation = _mock_cc_toolchain_config_impl,
provides = [CcToolchainConfigInfo],
)

def mock_cc_toolchain(name):
_mock_cc_toolchain_config(
name = name + "_config",
)

empty = name + "_empty"
native.filegroup(
name = empty,
)

archiver = util.empty_file(
name = name + "_my_ar",
)

all_files = name + "_all_files"
native.filegroup(
name = all_files,
srcs = [archiver],
)

native.cc_toolchain(
name = name + "_cc_toolchain",
toolchain_config = name + "_config",
all_files = all_files,
dwp_files = empty,
compiler_files = empty,
linker_files = empty,
objcopy_files = empty,
strip_files = empty,
)

native.toolchain(
name = name,
toolchain = name + "_cc_toolchain",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
load("@rules_testing//lib:util.bzl", "util")
load(":mock_toolchain.bzl", "mock_cc_toolchain")

def _test_output_groups(name):
def _set_up_subject(name):
util.helper_target(
native.cc_import,
name = name + "_dynamic_import",
Expand Down Expand Up @@ -78,6 +79,9 @@ def _test_output_groups(name):
name + "_imports",
],
)

def _test_output_groups(name):
_set_up_subject(name)
analysis_test(
name = name,
impl = _test_output_groups_impl,
Expand All @@ -99,18 +103,49 @@ def _test_output_groups_impl(env, target):
]).in_order()

subject.output_group("linkopts").contains_exactly([path_prefix + "_linkopts.txt"])
subject.action_generating(path_prefix + "_linkopts.txt").content().split("\n").contains_exactly([]).in_order()
subject.action_generating(path_prefix + "_linkopts.txt").content().split("\n").contains_exactly([
"dep_1_arg_1",
"dep_1_arg_2",
"dep_1_arg_1",
"linkopts_only_arg_1",
"linkopts_only_arg_2",
"linkopts_only_arg_1",
"",
]).in_order()

subject.output_group("targets").contains_exactly([path_prefix + "_targets.txt"])
subject.action_generating(path_prefix + "_targets.txt").content().split("\n").contains_exactly([
base_label + "_dep_1",
"",
]).in_order()

def _test_default_outputs(name):
_set_up_subject(name)
mock_cc_toolchain(name + "_toolchain")
analysis_test(
name = name,
impl = _test_default_outputs_impl,
target = name + "_subject",
config_settings = {
"//command_line_option:extra_toolchains": [str(native.package_relative_label(name + "_toolchain"))],
"//command_line_option:incompatible_enable_cc_toolchain_resolution": True,
},
)

def _test_default_outputs_impl(env, target):
subject_path = target.label.package + "/" + "prefix" + target.label.name + ".lib"

env.expect.that_target(target).default_outputs().contains_exactly([subject_path])

action = env.expect.that_target(target).action_generating(subject_path)
action.mnemonic().equals("CppTransitiveArchive")
action.argv().contains_exactly([])

def analysis_test_suite(name):
test_suite(
name = name,
tests = [
_test_default_outputs,
_test_output_groups,
],
)

0 comments on commit dce3fe7

Please sign in to comment.