using bazel with mingw-w64

1,435 views
Skip to first unread message

jose....@gmail.com

unread,
Dec 20, 2016, 9:11:52 PM12/20/16
to bazel-discuss
Hello! Have anyone here tried to use mingw to build C++ code with bazel?

I recently started to use bazel on Windows and I happened to find the related thread below:

https://groups.google.com/forum/#!topic/bazel-discuss/t-ruQRMis8A

which suggests to create BUILD files for a whole new toolchain contained in an external repository, making me end up at this tutorial: https://github.com/bazelbuild/bazel/wiki/Building-with-a-custom-toolchain

Do I need to go all through this? I just want to make bazel to use my mingw compiler at
C:\mingw-dists\mingw-w64\x86_64-5.1.0-posix-seh-rt_v4-rev0\mingw64

Any help is appreciated. Thanks in advance!!!

jose....@gmail.com

unread,
Dec 20, 2016, 10:02:11 PM12/20/16
to bazel-discuss, jose....@gmail.com
I also tried to change the tools/cpp/CROSSTOOL file and then I rebuild bazel with compile.sh, but it looks like it had no effect. I am passing the --compiler=windows_msys64 flag to bazel build, but it insists on using C:/tools/msys64/ path instead of C:/mingw-dists/mingw-w64/x86_64-5.4.0-release-posix-seh-rt_v5-rev0/mingw64/

Here are my changes:

462,467c462,467
< tool_path { name: "ar" path: "C:/tools/msys64/mingw64/bin/ar" }
< tool_path { name: "compat-ld" path: "C:/tools/msys64/mingw64/bin/ld" }
< tool_path { name: "cpp" path: "C:/tools/msys64/mingw64/bin/cpp" }
< tool_path { name: "dwp" path: "C:/tools/msys64/mingw64/bin/dwp" }
< tool_path { name: "gcc" path: "C:/tools/msys64/mingw64/bin/gcc" }
< cxx_flag: "-std=c++0x"
---
> tool_path { name: "ar" path: "C:/mingw-dists/mingw-w64/x86_64-5.4.0-release-posix-seh-rt_v5-rev0/mingw64/bin/ar" }
> tool_path { name: "compat-ld" path: "C:/mingw-dists/mingw-w64/x86_64-5.4.0-release-posix-seh-rt_v5-rev0/mingw64/bin/ld" }
> tool_path { name: "cpp" path: "C:/mingw-dists/mingw-w64/x86_64-5.4.0-release-posix-seh-rt_v5-rev0/mingw64/bin/cpp" }
> tool_path { name: "dwp" path: "C:/mingw-dists/mingw-w64/x86_64-5.4.0-release-posix-seh-rt_v5-rev0/mingw64/bin/dwp" }
> tool_path { name: "gcc" path: "C:/mingw-dists/mingw-w64/x86_64-5.4.0-release-posix-seh-rt_v5-rev0/mingw64/bin/gcc" }
> cxx_flag: "-std=c++1y"
472,476c472,476
< cxx_builtin_include_directory: "C:/tools/msys64/mingw64/x86_64-w64-mingw32/include"
< tool_path { name: "gcov" path: "C:/tools/msys64/mingw64/bin/gcov" }
< tool_path { name: "ld" path: "C:/tools/msys64/mingw64/bin/ld" }
< tool_path { name: "nm" path: "C:/tools/msys64/mingw64/bin/nm" }
< tool_path { name: "objcopy" path: "C:/tools/msys64/mingw64/bin/objcopy" }
---
> cxx_builtin_include_directory: "C:/mingw-dists/mingw-w64/x86_64-5.4.0-release-posix-seh-rt_v5-rev0/mingw64/x86_64-w64-mingw32/include"
> tool_path { name: "gcov" path: "C:/mingw-dists/mingw-w64/x86_64-5.4.0-release-posix-seh-rt_v5-rev0/mingw64/bin/gcov" }
> tool_path { name: "ld" path: "C:/mingw-dists/mingw-w64/x86_64-5.4.0-release-posix-seh-rt_v5-rev0/mingw64/bin/ld" }
> tool_path { name: "nm" path: "C:/mingw-dists/mingw-w64/x86_64-5.4.0-release-posix-seh-rt_v5-rev0/mingw64/bin/nm" }
> tool_path { name: "objcopy" path: "C:/mingw-dists/mingw-w64/x86_64-5.4.0-release-posix-seh-rt_v5-rev0/mingw64/bin/objcopy" }
479,480c479,480
< tool_path { name: "objdump" path: "C:/tools/msys64/mingw64/bin/objdump" }
< tool_path { name: "strip" path: "C:/tools/msys64/mingw64/bin/strip" }
---
> tool_path { name: "objdump" path: "C:/mingw-dists/mingw-w64/x86_64-5.4.0-release-posix-seh-rt_v5-rev0/mingw64/bin/objdump" }
> tool_path { name: "strip" path: "C:/mingw-dists/mingw-w64/x86_64-5.4.0-release-posix-seh-rt_v5-rev0/mingw64/bin/strip" }
532,535c532,535
< tool_path { name: "ar" path: "C:/tools/msys64/usr/bin/ar" }
< tool_path { name: "compat-ld" path: "C:/tools/msys64/usr/bin/ld" }
< tool_path { name: "cpp" path: "C:/tools/msys64/usr/bin/cpp" }
< tool_path { name: "dwp" path: "C:/tools/msys64/usr/bin/dwp" }
---
> tool_path { name: "ar" path: "C:/mingw-dists/mingw-w64/x86_64-5.4.0-release-posix-seh-rt_v5-rev0/mingw64/bin/ar" }
> tool_path { name: "compat-ld" path: "C:/mingw-dists/mingw-w64/x86_64-5.4.0-release-posix-seh-rt_v5-rev0/mingw64/bin/ld" }
> tool_path { name: "cpp" path: "C:/mingw-dists/mingw-w64/x86_64-5.4.0-release-posix-seh-rt_v5-rev0/mingw64/bin/cpp" }
> tool_path { name: "dwp" path: "C:/mingw-dists/mingw-w64/x86_64-5.4.0-release-posix-seh-rt_v5-rev0/mingw64/bin/dwp" }
537c537
< tool_path { name: "gcc" path: "C:/tools/msys64/usr/bin/gcc" }
---
> tool_path { name: "gcc" path: "C:/mingw-dists/mingw-w64/x86_64-5.4.0-release-posix-seh-rt_v5-rev0/mingw64/bin/gcc" }
544c544
< cxx_builtin_include_directory: "C:/tools/msys64/"
---
> cxx_builtin_include_directory: "C:/mingw-dists/mingw-w64/x86_64-5.4.0-release-posix-seh-rt_v5-rev0/mingw64/"
546,549c546,549
< tool_path { name: "gcov" path: "C:/tools/msys64/usr/bin/gcov" }
< tool_path { name: "ld" path: "C:/tools/msys64/usr/bin/ld" }
< tool_path { name: "nm" path: "C:/tools/msys64/usr/bin/nm" }
< tool_path { name: "objcopy" path: "C:/tools/msys64/usr/bin/objcopy" }
---
> tool_path { name: "gcov" path: "C:/mingw-dists/mingw-w64/x86_64-5.4.0-release-posix-seh-rt_v5-rev0/mingw64/bin/gcov" }
> tool_path { name: "ld" path: "C:/mingw-dists/mingw-w64/x86_64-5.4.0-release-posix-seh-rt_v5-rev0/mingw64/bin/ld" }
> tool_path { name: "nm" path: "C:/mingw-dists/mingw-w64/x86_64-5.4.0-release-posix-seh-rt_v5-rev0/mingw64/bin/nm" }
> tool_path { name: "objcopy" path: "C:/mingw-dists/mingw-w64/x86_64-5.4.0-release-posix-seh-rt_v5-rev0/mingw64/bin/objcopy" }
552,553c552,553
< tool_path { name: "objdump" path: "C:/tools/msys64/usr/bin/objdump" }
< tool_path { name: "strip" path: "C:/tools/msys64/usr/bin/strip" }
---
> tool_path { name: "objdump" path: "C:/mingw-dists/mingw-w64/x86_64-5.4.0-release-posix-seh-rt_v5-rev0/mingw64/bin/objdump" }
> tool_path { name: "strip" path: "C:/mingw-dists/mingw-w64/x86_64-5.4.0-release-posix-seh-rt_v5-rev0/mingw64/bin/strip" }

Marcel Hlopko

unread,
Dec 21, 2016, 4:38:51 AM12/21/16
to jose....@gmail.com, bazel-discuss
Hi Jose,

To be perfectly honest I'm not sure what do we use tools/cpp/CROSSTOOL for, maybe only testing. What you could do is to tweak cc_configure.bzl and CROSSTOOL.tpl. The first is Skylark script auto-guessing C++ toolchain configuration, and the second is template file that is expanded by cc_configure. That should work for you. Let me know if it doesn't. 

Cheers!
Marcel

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/c6074f32-d87a-45ee-bd52-9a1dcb537451%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
-- 
Marcel Hlopko | Software Engineer | hlo...@google.com | 

Google Germany GmbH | Erika-Mann-Str. 33  | 80636 München | Germany | Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle | Registergericht und -nummer: Hamburg, HRB 86891

jose....@gmail.com

unread,
Dec 21, 2016, 7:34:27 PM12/21/16
to bazel-discuss, jose....@gmail.com
thanks for the answer, but I honestly have no idea on how to change these files to make bazel to use my compiler at C:\mingw-dists\mingw-w64\x86_64-5.1.0-posix-seh-rt_v4-rev0\mingw64

sigh :(

Marcel Hlopko

unread,
Dec 22, 2016, 12:37:23 PM12/22/16
to jose....@gmail.com, bazel-discuss
So I would edit the CROSSTOOL.tpl file, and modify paths in vc_14_0_x64 toolchain in all tool_paths sections and in the all action_config.tool.tool_path messages. They are currently set to batch scripts that call python that translated gcc/clang flags into msvc flags. With mingw there is a chance that gcc/clang flags will work out of the box :)

Or wait. We are making the Windows bazel experience better every day now :) But it will still take a few months.

Cheers!
m.


For more options, visit https://groups.google.com/d/optout.

jose....@gmail.com

unread,
Dec 22, 2016, 9:41:42 PM12/22/16
to bazel-discuss, jose....@gmail.com
do I also need to change the flags in the action_config messages?
for example, by replacing /c with -c in line 238?
flag_set {
flag_group {
flag: '/c'
flag: '%{source_file}'
}
}


also how can I use environment variables there? for example I want to expand the value of MINGW_HOME variable inside the tool_path messages

thanks!

jose....@gmail.com

unread,
Dec 23, 2016, 12:20:51 AM12/23/16
to bazel-discuss, jose....@gmail.com
On Thursday, December 22, 2016 at 12:37:23 PM UTC-5, Marcel Hlopko wrote:
bah! I've changed CROSSTOOL.tpl a lot, then I rebuilt bazel but the paths to mingw tools still aren't sticking up.

Do I have to do something else in addition to change CROSSTOOL.tpl ? Deleting / touching another file, perhaps?

Marcel Hlopko

unread,
Jan 5, 2017, 7:11:10 AM1/5/17
to jose....@gmail.com, bazel-discuss
Hi Jose,

So I have no idea what flags mingw expects, so yes, if it expects /c instead of -c, then yes, you have to change, or maybe change the scripts we use for msvc. Take a look at CppLinkActionConfigs.java there are some hard-coded defaults, which you might need to override in your CROSSTOOL too. Regarding environment variables, you can access them in skylark:

repository_ctx.os.environ["MINGW_HOME"]

We do that for example here.

You need to recompile bazel after every change to tools/cpp/cc_configure.bzl or tools/cpp/CROSSTOOL.tpl. It will be more comfortable for you if you'll create your own remote repository for the crosstool and will not use the built-in one. An inspiration can be taken from our blog post on the topic.

We know writing Crosstools is not simple, especially for the windows. There's a lot going on in this area right now, so it should be easier soon.

Cheers!
Marcel


For more options, visit https://groups.google.com/d/optout.

edouar...@gmail.com

unread,
Jan 23, 2018, 1:51:52 PM1/23/18
to bazel-discuss
@Marcel. Any idea when to expect progress on this so that i can decide whether i should wait or bite the bullet like Jose.

@Jose, if you get mingw to work any chance you can share your work ;-) ?

Diego

unread,
Jan 23, 2018, 4:21:45 PM1/23/18
to edouar...@gmail.com, bazel-discuss
I gave up ages ago and I am using MSVC (which is not totally terrible, though).


--
You received this message because you are subscribed to a topic in the Google Groups "bazel-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/bazel-discuss/6T-sTMt4DA4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to bazel-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/65af112c-14d8-49fd-aa84-4224e47eae4d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
https://github.com/thinlizzy/ - C++11 and C++14 open source wrappers and libraries for various purposes (XML, image, webserver... and even a MTG implementation)
http://baudejogos.net - o único site brasileiro decente de jogos!!

Yun Peng

unread,
Jan 24, 2018, 4:17:56 AM1/24/18
to Diego, edouar...@gmail.com, bazel-discuss
Hi,

It is possible to write a CROSSTOOL for mingw-w64. And with Bazel 0.10.0, you will be able to use mingw-w64 in MSYS2 directly. Please check this https://github.com/bazelbuild/bazel/commit/e9f4090c67a89247166b7c9607394b350a7c4ef6


To unsubscribe from this group and all its topics, send an email to bazel-discus...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
https://github.com/thinlizzy/ - C++11 and C++14 open source wrappers and libraries for various purposes (XML, image, webserver... and even a MTG implementation)
http://baudejogos.net - o único site brasileiro decente de jogos!!

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.

edouar...@gmail.com

unread,
Jan 24, 2018, 8:37:16 AM1/24/18
to bazel-discuss
Hi Yun,

that is great news!
i tried the procedure in your mail using bazel 0.10.0 RC5

I am getting an error as gcc command line includes windows-like options ( /W3 /WX /wd4005 /wd4068 /wd4244 /wd4267 /wd4800 /DNOMINMAX /DWIN32_LEAN_AND_MEAN /D_CRT_SECURE_NO_WARNINGS)

maybe your change is not included in RC5 yet? Any ideas?

edouar...@gmail.com

unread,
Jan 26, 2018, 8:57:27 AM1/26/18
to bazel-discuss
Yun, in fact it works for some libs. I have an abseil-specific problem: there is this statement:
ABSL_DEFAULT_COPTS = select({
"//absl:windows": MSVC_FLAGS,
"//absl:llvm_compiler": LLVM_FLAGS,
"//conditions:default": GCC_FLAGS,
})

which gives me MSVC_FLAGS when i compile using --compiler=mingw-gcc and then the mingw gcc fails.

Do you believe that it is a problem in the BUILD file from abseil or a bazel mingw problem?

Yun Peng

unread,
Jan 26, 2018, 9:01:37 AM1/26/18
to edouar...@gmail.com, bazel-discuss
Yes, I think you should add a config_setting under abs/BUILD.
config_setting(
  name = "windows_mingw",
      values = {
        "compiler": "mingw-gcc",
    },
)
then use this to select GCC_FLAGS

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages