c++windowscross-compilingbazeltoolchain

Proper use of toolchain_type in Bazel?


I need to implement a custom toolchain in Bazel to use a custom compiler ccppc.
I'm following this, but it seems like Bazel is having trouble with //ccppc_tools:toolchain_type.


My command:

bazel build --platforms=//src:my_platform //src:TEST_OBJECT

The errors:

ERROR: C:/tools/dev/test_partition/src/BUILD:11:14: While resolving toolchains for target //src:TEST_OBJECT: No matching toolchains found for types //ccppc_tools:toolchain_type.
To debug, rerun with --toolchain_resolution_debug='//ccppc_tools:toolchain_type'
If platforms or toolchains are a new concept for you, we'd encourage reading https://bazel.build/concepts/platforms-intro.
ERROR: Analysis of target '//src:TEST_OBJECT' failed; build aborted:
INFO: Elapsed time: 0.564s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (2 packages loaded, 3 targets configured)

Here is my setup:

Project
  | 
  + ccppc_tools/ -- rules.bzl BUILD
  + src/ -- command.c BUILD
  + BUILD
  + WORKSPACE

ccppc_tools/rules.bzl:

##################################################################### TOOLCHAIN PART

# Declare the information provider
CCPPCInfo = provider(
    doc = "Information about how to invoke the ccppc compiler.",
    # In the real world, compiler_path and system_lib might hold File objects,
    # but for simplicity they are strings for this example. arch_flags is a list
    # of strings.
    fields = ["compiler_path", "system_lib", "arch_flags"],
)

# Rules for the CCPPC
def _ccppc_toolchain_impl(ctx):
    toolchain_info = platform_common.ToolchainInfo(
        ccppcinfo = CCPPCInfo(
            compiler_path = ctx.attr.compiler_path,
            system_lib = ctx.attr.system_lib,
            arch_flags = ctx.attr.arch_flags,
        ),
    )
    return [toolchain_info]

ccppc_toolchain = rule(
    implementation = _ccppc_toolchain_impl,
    attrs = {
        "compiler_path": attr.string(),
        "system_lib": attr.label(
                    mandatory = False,
                    cfg = "target",
                ),
        "arch_flags": attr.string_list(),
    },
)


##################################################################### USER RULES PART

# Rule for creating objects with CCPPC
def _ccppc_objects_impl(ctx):
    info = ctx.toolchains["//ccppc_tools:toolchain_type"].ccppcinfo

    command = "%s %s -c %s" % (
        info.compiler_path,
        info.system_lib,
        " ".join(info.arch_flags),
    )
    print("CALLED CCPPC OBJECTS: ")
    print(command)



ccppc_objects = rule(
    implementation = _ccppc_objects_impl,
    attrs = {
        "srcs": attr.label_list(allow_files = True)
    },
    toolchains = ["//ccppc_tools:toolchain_type"],
)

ccppc_tools/BUILD:

load("//ccppc_tools:rules.bzl","ccppc_toolchain")

toolchain_type(name = "toolchain_type")

# Declare a ccppc toolchain
ccppc_toolchain(
    name = "ccppc_windows",
    arch_flags = [
        "--arch=WINDOWS",
        "--DTEST",
    ],
    compiler_path = "ccppc",
)


# Declare a toolchain from the ccppc_toolchain
toolchain(
    name = "ccppc_windows_toolchain",
    exec_compatible_with = [
        "@platforms//os:windows",
        "@platforms//cpu:x86_64",
    ],
    target_compatible_with = [
        "@platforms//os:linux",
        "@platforms//cpu:ppc",
    ],
    toolchain = ":ccppc_windows",
    toolchain_type = ":toolchain_type",
)

src/BUILD:

load("//:ccppc_tools/rules.bzl","ccppc_objects")

platform(
    name = "my_platform",
    constraint_values = [
        "@platforms//os:linux",
    ],
)

ccppc_objects(
    name = "TEST_OBJECT",
    srcs = ["src/command.c"]
)

WORKSPACE:

register_toolchains(
    "//ccppc_tools:ccppc_windows_toolchain",
)

I tried reading through project and documentations see what I'm doing wrong but couldn't find anything


Solution

  • You need to add @platforms//cpu:ppc to the constraint_values of //src:my_platform.

    There may be other things preventing that toolchain from being selected, see my comment about adding output with --toolchain_resolution_debug='//ccppc_tools:toolchain_type' to include to debug further.