bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
22.97k stars 4.03k forks source link

Toolchain config Windows #16247

Open Josar opened 2 years ago

Josar commented 2 years ago

Description of the bug:

I try to get MinGw to work on Windows. I try to use the local installed MinGw and configure it as toolchain. This is only the first step to understand how an embedded toolchain could be set up. But i fail when triyng to follow this tutorial and try to adapt it.

load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
load(
    "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
    "feature",
    "flag_group",
    "flag_set",
    "tool_path",
    "action_config",
    "tool",
)

all_link_actions = [
    ACTION_NAMES.cpp_link_executable,
    ACTION_NAMES.cpp_link_dynamic_library,
    ACTION_NAMES.cpp_link_nodeps_dynamic_library,
]

def _impl(ctx):
    tool_paths = [
        tool_path(
            name = "gcc",
            path = "false",
            #path = "C:\\MinGW\\bin\\gcc.exe", # Error 1
            #path = "C:/MinGW/bin/gcc.exe", # Error 2
        ),
        tool_path(
            name = "ld",
            path = "false",
            #path = "C:\\MinGW\\bin\\ld.exe",  # Error 1
            #path = "C:/MinGW/bin/ld.exe",  # Error 2
        ),
        tool_path(
            name = "ar",
            path = "false",
        ),
        tool_path(
            name = "cpp",
            path = "false",
        ),
        tool_path(
            name = "gcov",
            path = "false",
        ),
        tool_path(
            name = "nm",
            path = "false",
        ),
        tool_path(
            name = "objdump",
            path = "false",
        ),
        tool_path(
            name = "strip",
            path = "false",
        ),
    ]

    features = [
        feature(
            name = "default_linker_flags",
            enabled = True,
            flag_sets = [
                flag_set(
                    actions = all_link_actions,
                    flag_groups = ([
                        flag_group(
                            flags = [
                                "-lstdc++",
                            ],
                        ),
                    ]),
                ),
            ],
        ),
    ]

    action_configs = [
        action_config (
            action_name = ACTION_NAMES.c_compile,
            tools = [
                # NEW label type tool field introduced in #10967
                tool(
                    # tool = ctx.executable.my_ld),
                    path = "C:\\MinGW\\bin\\gcc.exe" 
                )
            ],
        ),
        action_config (
            action_name = ACTION_NAMES.cpp_link_executable,
            tools = [
                # NEW label type tool field introduced in #10967
                tool(
                    # tool = ctx.executable.my_ld,
                    path = "C:\\MinGW\\bin\\ld.exe"
                )
            ],
        ),
    ]

    return cc_common.create_cc_toolchain_config_info(
        ctx = ctx,
        features = features, # NEW
        cxx_builtin_include_directories = [
            "C:\\MinGW\\lib\\gcc\\mingw32\\6.3.0\\include",
            "C:\\MinGW\\include",
        ],
        toolchain_identifier = "local",
        host_system_name = "local",
        target_system_name = "local",
        target_cpu = "k8",
        target_libc = "unknown",
        compiler = "mingw",
        abi_version = "unknown",
        abi_libc_version = "unknown",
        tool_paths = tool_paths,
        action_configs = action_configs,
    )

cc_toolchain_config = rule(
    implementation = _impl,
    attrs = {},
    provides = [CcToolchainConfigInfo],
)

Trying to change the path to #Error 2

D:\BazelBuild\examples\cpp-tutorial\stage1>bazelisk build --config=mingw_config --cpu=x86 //main:hello-world
Starting local Bazel server and connecting to it...
INFO: Analyzed target //main:hello-world (38 packages loaded, 162 targets configured).
INFO: Found 1 target...
ERROR: D:/bazelbuild/examples/cpp-tutorial/stage1/main/BUILD:3:10: Compiling main/hello-world.cc failed: missing input file '//toolchain:empty'
ERROR: D:/bazelbuild/examples/cpp-tutorial/stage1/main/BUILD:3:10: Compiling main/hello-world.cc failed: 1 input file(s) do not exist
Target //main:hello-world failed to build
Use --verbose_failures to see the command lines of failed build steps.
ERROR: D:/bazelbuild/examples/cpp-tutorial/stage1/main/BUILD:3:10 Compiling main/hello-world.cc failed: 1 input file(s) do not exist
INFO: Elapsed time: 2.897s, Critical Path: 0.03s
INFO: 2 processes: 2 internal.
FAILED: Build did NOT complete successfully

when i try to provide the path to the tool_path.path i get # error 1.

D:\BazelBuild\examples\cpp-tutorial\stage1>bazel build --config=mingw_config //main:hello-world
Analyzing: target //main:hello-world (0 packages loaded, 0 targets configured)
FATAL: bazel crashed due to an internal error. Printing stack trace:
java.lang.RuntimeException: Unrecoverable error while evaluating node 'ConfiguredTargetKey{label=//toolchain:x86_toolchain_config, config=BuildConfigurationValue.Key[02e79dd9de174d25399e85bf1b0833ded09d49738442a403231bf1994cd72e28]}' (requested by nodes 'ConfiguredTargetKey{label=//toolchain:x86_toolchain, config=BuildConfigurationValue.Key[02e79dd9de174d25399e85bf1b0833ded09d49738442a403231bf1994cd72e28]}')
        at com.google.devtools.build.skyframe.AbstractParallelEvaluator$Evaluate.run(AbstractParallelEvaluator.java:674)
        at com.google.devtools.build.lib.concurrent.AbstractQueueVisitor$WrappedRunnable.run(AbstractQueueVisitor.java:382)
        at java.base/java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(Unknown Source)
        at java.base/java.util.concurrent.ForkJoinTask.doExec(Unknown Source)
        at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(Unknown Source)
        at java.base/java.util.concurrent.ForkJoinPool.scan(Unknown Source)
        at java.base/java.util.concurrent.ForkJoinPool.runWorker(Unknown Source)
        at java.base/java.util.concurrent.ForkJoinWorkerThread.run(Unknown Source)
Caused by: net.starlark.java.eval.Starlark$UncheckedEvalException: IllegalStateException thrown during Starlark evaluation (//toolchain:x86_toolchain_config)
        at <starlark>.create_cc_toolchain_config_info(<builtin>:0)
        at <starlark>._impl(D:/bazelbuild/examples/cpp-tutorial/stage1/toolchain/cc_toolchain_config.bzl:73)
Caused by: java.lang.IllegalStateException: com.google.protobuf.TextFormat$ParseException: 4:16: Invalid escape sequence: '\M'
        at com.google.devtools.build.lib.rules.cpp.CppActionConfigs.getLegacyActionConfigs(CppActionConfigs.java:1372)
        at com.google.devtools.build.lib.rules.cpp.CcModule.ccToolchainConfigInfoFromStarlark(CcModule.java:1250)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.base/java.lang.reflect.Method.invoke(Unknown Source)
        at net.starlark.java.eval.MethodDescriptor.call(MethodDescriptor.java:162)
        at net.starlark.java.eval.BuiltinFunction.fastcall(BuiltinFunction.java:77)
        at net.starlark.java.eval.Starlark.fastcall(Starlark.java:619)
        at net.starlark.java.eval.Eval.evalCall(Eval.java:672)
        at net.starlark.java.eval.Eval.eval(Eval.java:489)
        at net.starlark.java.eval.Eval.execReturn(Eval.java:249)
        at net.starlark.java.eval.Eval.exec(Eval.java:288)
        at net.starlark.java.eval.Eval.execStatements(Eval.java:82)
        at net.starlark.java.eval.Eval.execFunctionBody(Eval.java:66)
        at net.starlark.java.eval.StarlarkFunction.fastcall(StarlarkFunction.java:191)
        at net.starlark.java.eval.Starlark.fastcall(Starlark.java:619)
        at com.google.devtools.build.lib.analysis.starlark.StarlarkRuleConfiguredTargetUtil.buildRule(StarlarkRuleConfiguredTargetUtil.java:99)
        at com.google.devtools.build.lib.analysis.ConfiguredTargetFactory.createRule(ConfiguredTargetFactory.java:353)
...

When i try to provide it by the actions i get this.

D:\BazelBuild\examples\cpp-tutorial\stage1_toolchain_1>bazelisk build --config=asmjs //main:hello-world
Starting local Bazel server and connecting to it...
INFO: Analyzed target //main:hello-world (38 packages loaded, 162 targets configured).
INFO: Found 1 target...
ERROR: D:/bazelbuild/examples/cpp-tutorial/stage1_toolchain_1/main/BUILD:3:10: Compiling main/hello-world.cc failed: missing input file '//toolchain:empty'
ERROR: D:/bazelbuild/examples/cpp-tutorial/stage1_toolchain_1/main/BUILD:3:10: Compiling main/hello-world.cc failed: 1 input file(s) do not exist
Target //main:hello-world failed to build
Use --verbose_failures to see the command lines of failed build steps.
ERROR: D:/bazelbuild/examples/cpp-tutorial/stage1_toolchain_1/main/BUILD:3:10 Compiling main/hello-world.cc failed: 1 input file(s) do not exist
INFO: Elapsed time: 3.749s, Critical Path: 0.02s
INFO: 1 process: 1 internal.
FAILED: Build did NOT complete successfully

What went wrong? If you could point my to the right direction it would be great.

A second questions would be, is there any tutorial thats shows how to use local installed tools/exe, without wrapping it in a script?

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

No response

Which operating system are you running Bazel on?

Windows 11

What is the output of bazel info release?

release 5.3.0

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?

No response

Have you found anything relevant by searching the web?

My own question. https://stackoverflow.com/questions/73614004/use-local-mingw-as-toolchain-on-windows

Any other information, logs, or outputs that you want to share?

No response

crillion commented 1 year ago

I have had this problem on windows. I experienced the crash too. it seems that bazel interprets the backslashes as escape characters. by converting them to normal slashes (as in the unix/linux platform) it works... and it give to me other errors (but at least that problem is passed). Maybe the bazel developers will fix this sooner or later...

chickenandpork commented 1 year ago

missing input file '//toolchain:empty'

This seems to be a default value added to something after the tutorial was written.

I created a bank file "toolchain/empty" and it seems to satisfy.

Josar commented 1 year ago

BUILD

filegroup(name = "empty")

This seems to expect a file named empty. So adding a blank file to the folder of the BUILD file solves that. Thanks @chickenandpork.

Or declaring the srcs with an empty array also solves this.

filegroup(
    name = "empty",
    srcs = [],
)
Fasten90 commented 1 year ago

missing input file '//toolchain:empty'

This seems to be a default value added to something after the tutorial was written.

I created a bank file "toolchain/empty" and it seems to satisfy.

In this case this ticket is a similar topic to the https://github.com/bazelbuild/bazel/issues/12856