xref: /aosp_15_r20/external/bazelbuild-rules_cc/examples/custom_toolchain/toolchain_config.bzl (revision eed53cd41c5909d05eedc7ad9720bb158fd93452)
1"""Sample Starlark definition defining a C++ toolchain's behavior.
2
3When you build a cc_* rule, this logic defines what programs run for what
4build steps (e.g. compile / link / archive) and how their command lines are
5structured.
6
7This is a proof-of-concept simple implementation. It doesn't construct fancy
8command lines and uses mock shell scripts to compile and link
9("sample_compiler" and "sample_linker"). See
10https://docs.bazel.build/versions/main/cc-toolchain-config-reference.html and
11https://docs.bazel.build/versions/main/tutorial/cc-toolchain-config.html for
12advanced usage.
13"""
14
15load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "tool_path")
16
17def _impl(ctx):
18    tool_paths = [
19        tool_path(
20            name = "ar",
21            path = "sample_linker",
22        ),
23        tool_path(
24            name = "cpp",
25            path = "not_used_in_this_example",
26        ),
27        tool_path(
28            name = "gcc",
29            path = "sample_compiler",
30        ),
31        tool_path(
32            name = "gcov",
33            path = "not_used_in_this_example",
34        ),
35        tool_path(
36            name = "ld",
37            path = "sample_linker",
38        ),
39        tool_path(
40            name = "nm",
41            path = "not_used_in_this_example",
42        ),
43        tool_path(
44            name = "objdump",
45            path = "not_used_in_this_example",
46        ),
47        tool_path(
48            name = "strip",
49            path = "not_used_in_this_example",
50        ),
51    ]
52
53    # Documented at
54    # https://docs.bazel.build/versions/main/skylark/lib/cc_common.html#create_cc_toolchain_config_info.
55    #
56    # create_cc_toolchain_config_info is the public interface for registering
57    # C++ toolchain behavior.
58    return cc_common.create_cc_toolchain_config_info(
59        ctx = ctx,
60        toolchain_identifier = "custom-toolchain-identifier",
61        host_system_name = "local",
62        target_system_name = "local",
63        target_cpu = "sample_cpu",
64        target_libc = "unknown",
65        compiler = "gcc",
66        abi_version = "unknown",
67        abi_libc_version = "unknown",
68        tool_paths = tool_paths,
69    )
70
71cc_toolchain_config = rule(
72    implementation = _impl,
73    # You can alternatively define attributes here that make it possible to
74    # instantiate different cc_toolchain_config targets with different behavior.
75    attrs = {},
76    provides = [CcToolchainConfigInfo],
77)
78