Bazel,Windows 10 工具链配置与 mingw64 编译器

Bazel, Windows 10 toolchain configuration with mingw64 compiler

我需要用Bazel构建自己的工具链,我想强制Bazel使用mingw64的g++编译器。我按照 https://docs.bazel.build/versions/master/tutorial/cc-toolchain-config.html 中的说明尝试使它们适应我的 Windows 10 机器。 Bazel 能够编译我的 Hello-world.cc 并创建 hello-world.exe,如果我 运行

bazel-bin/main/hello-world

我得到了正确的输出。 然而,Bazel 会抛出以下错误:

bazel build --config=mingw64_config -s //main:hello-world --verbose_failures
Starting local Bazel server and connecting to it...
INFO: Analyzed target //main:hello-world (15 packages loaded, 47 targets configured).
INFO: Found 1 target...
SUBCOMMAND: # //main:hello-world [action 'Compiling main/hello-world.cc', configuration: e711ec75bfc5b6e1d77ea144cef912e655797b748714c6396a6b8cd3625eebee, execution platform: @local_config_platform//:host]
cd C:/users/_bazel_user/bcge3shs/execroot/__main__
  SET PATH=c:\tools\msys64\usr\bin;c:\tools\msys64\bin;C:\WINDOWS;C:\WINDOWS\System32;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\Program Files\Haskell\bin;C:\Program Files\Haskell Platform.6.5\lib\extralibs\bin;C:\Program Files\Haskell Platform.6.5\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Haskell Platform.6.5\mingw\bin;C:\Program Files\dotnet\;C:\Program Files\Git\cmd;C:\Program Files\TortoiseSVN\bin;C:\Users\Documents\Bazel\bazel_binary;C:\mingw64\bin;C:\Users\AppData\Roaming\cabal\bin;C:\Users\AppData\Roaming\local\bin;C:\Users\AppData\Local\Microsoft\WindowsApps;C:\Program Files\LLVM\bin
    SET PWD=/proc/self/cwd
    SET RUNFILES_MANIFEST_ONLY=1
  C:/mingw64/bin/g++ -MD -MF bazel-out/x64_windows-fastbuild/bin/main/_objs/hello-world/hello-world.d -frandom-seed=bazel-out/x64_windows-fastbuild/bin/main/_objs/hello-world/hello-world.o -iquote . -iquote bazel-out/x64_windows-fastbuild/bin -iquote external/bazel_tools -iquote bazel-out/x64_windows-fastbuild/bin/external/bazel_tools -c main/hello-world.cc -o bazel-out/x64_windows-fastbuild/bin/main/_objs/hello-world/hello-world.o
SUBCOMMAND: # //main:hello-world [action 'Linking main/hello-world', configuration: e711ec75bfc5b6e1d77ea144cef912e655797b748714c6396a6b8cd3625eebee, execution platform: @local_config_platform//:host]
cd C:/users/_bazel_user/bcge3shs/execroot/__main__
  SET PATH=c:\tools\msys64\usr\bin;c:\tools\msys64\bin;C:\WINDOWS;C:\WINDOWS\System32;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\Program Files\Haskell\bin;C:\Program Files\Haskell Platform.6.5\lib\extralibs\bin;C:\Program Files\Haskell Platform.6.5\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Haskell Platform.6.5\mingw\bin;C:\Program Files\dotnet\;C:\Program Files\Git\cmd;C:\Program Files\TortoiseSVN\bin;C:\Users\Documents\Bazel\bazel_binary;C:\mingw64\bin;C:\Users\AppData\Roaming\cabal\bin;C:\Users\AppData\Roaming\local\bin;C:\Users\AppData\Local\Microsoft\WindowsApps;C:\Program Files\LLVM\bin
    SET PWD=/proc/self/cwd
    SET RUNFILES_MANIFEST_ONLY=1
  C:/mingw64/bin/g++ -o bazel-out/x64_windows-fastbuild/bin/main/hello-world bazel-out/x64_windows-fastbuild/bin/main/_objs/hello-world/hello-world.o -Wl,-S -lstdc++
ERROR: C:/users/documents/bazel/mingw64__compiler/main/BUILD:3:10: output 'main/hello-world' was not created
ERROR: C:/users/documents/bazel/mingw64__compiler/main/BUILD:3:10: not all outputs were created or valid
Target //main:hello-world failed to build
INFO: Elapsed time: 4.171s, Critical Path: 0.31s
INFO: 2 processes: 2 local.
FAILED: Build did NOT complete successfully

下面是我的cc_toolchain_config.bzl:

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

all_link_actions = [ # NEW
   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 = "C:/mingw64/bin/g++",
       ),
       tool_path(
           name = "ld",
           path = "C:/mingw64/bin/ld",
       ),
       tool_path(
           name = "ar",
           path = "C:/mingw64/bin/ar",
       ),
       tool_path(
           name = "cpp",
           path = "C:/mingw64/bin/cpp",
       ),
       tool_path(
           name = "gcov",
           path = "C:/mingw64/bin/gcov",
       ),
       tool_path(
           name = "nm",
           path = "C:/mingw64/bin/nm",
       ),
       tool_path(
           name = "objdump",
           path = "C:/mingw64/bin/objdump",
       ),
       tool_path(
           name = "strip",
           path = "C:/mingw64/bin/strip",
       ),
   ]

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

   return cc_common.create_cc_toolchain_config_info(
       ctx = ctx,
       features = features, # NEW
       cxx_builtin_include_directories = [
           "C:/mingw64/include",
           "C:/mingw64/x86_64-w64-mingw32/include",
           "C:/mingw64/lib/gcc/x86_64-w64-mingw32/5.1.0/include-fixed",
           "C:/mingw64/lib/gcc/x86_64-w64-mingw32/5.1.0/include",
           "C:/mingw64/lib/gcc/x86_64-w64-mingw32/5.1.0",
       ],
       toolchain_identifier = "local",
       host_system_name = "local",
       target_system_name = "local",
       target_cpu = "x64_windows",
       target_libc = "unknown",
       compiler = "g++",
       abi_version = "unknown",
       abi_libc_version = "unknown",
       tool_paths = tool_paths,
   )

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

对可能出现的问题有什么建议吗?

这个问题的答案可以在这里找到:

从@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl加载artifact_name_pattern并设置cc_common.create_cc_toolchain_config_info的artifact_name_patterns属性就足够了():

artifact_name_patterns = [
artifact_name_pattern(
    category_name = "executable",
    prefix = "",
    extension = ".exe",
),]