toolchains_llvm icon indicating copy to clipboard operation
toolchains_llvm copied to clipboard

feat: cross-compile to windows msvc

Open f0rmiga opened this issue 1 year ago • 4 comments

So, I had this code on my computer for a long time. I found it and wanted to share it with the community. It's not ready for a thorough review, but I wanted to spark a discussion about whether such a thing is desirable.

My motivation is simple: I don't want to have a Windows machine, much less one for development, but I want to distribute my software to run on Windows machines.

To accomplish this, I untangled some code around cross-compilation, which should also pave the way to cross-compile targeting macOS.

If we don't make any assumptions about whether we are cross-compiling, we can have a single pipeline for the targets. E.g. exec Linux or macOS targeting Linux should have no difference in which tools are used to compile and link. Notice that my code only uses the lld already in the downloaded toolchain.

One way to obtain the Windows sysroot is by using https://github.com/Jake-Shadle/xwin. At this point, I didn't give the search paths much thought and instead hard-coded the directories I had on my sysroot.

A very important piece of this puzzle is adding the windows_cc_toolchain_config.bzl file with a few fixes you can see here and here. What I had to change makes me believe that nobody has ever tried to set a Windows sysroot using msvc under Bazel. There's no way the original windows_cc_toolchain_config.bzl could have worked with a sysroot. It's also possible that I am missing something...

I hope to get some feedback, and I'm happy to answer any questions you may have.

f0rmiga avatar Sep 17 '24 05:09 f0rmiga

@fmeum, any thoughts?

f0rmiga avatar Sep 24 '24 08:09 f0rmiga

I generally like this a lot and can review when you think it's ready. We should strive to make the change in a way that somewhat obviously doesn't change command lines for non-Windows target platforms.

fmeum avatar Sep 24 '24 09:09 fmeum

I was about to put up a very similar implementation. With a couple key differences.

  1. I am supporting x86_64 and aarch64 builds.
  2. I am not embedding windows_cc_toolchain_config, but loading it directly from rules_cc
  3. I am not using the winsysroot style. Instead I have separate crt and sdk directories (I also use xwin)
  4. I have split the unix and windows cc_toolchain_config functions (it seems less confusing that way)
  5. I am not creating a new cc_wrapper for clang-cl. (This is not needed since clang-cl is already a symlink.)

I would be interested in combining efforts.

kkass avatar Feb 14 '25 19:02 kkass

@kkass Any chance you can share your working copy (either as repo/branch link or PR on this repo)?

gr1mpatr0n avatar Mar 31 '25 13:03 gr1mpatr0n