buck2 icon indicating copy to clipboard operation
buck2 copied to clipboard

'clang++' is not recognized as an internal or external command, operable program or batch file.

Open steveklabnik opened this issue 2 years ago • 22 comments
trafficstars

Hey there!

Trying to get a buck "hello world" building on Windows. I have this BUCK file:

rust_binary(
    name = "hello_world",
    srcs = ["hello.rs"],
    crate_root = "hello.rs",
)

and this toolchains\BUCK:

load("@prelude//toolchains:rust.bzl", "system_rust_toolchain")
load("@prelude//toolchains:genrule.bzl", "system_genrule_toolchain")
load("@prelude//toolchains:cxx.bzl", "system_cxx_toolchain")
load("@prelude//toolchains:python.bzl", "system_python_bootstrap_toolchain")

system_genrule_toolchain(
    name = "genrule",
    visibility = ["PUBLIC"],
)

system_rust_toolchain(
    name = "rust",
    default_edition = "2021",
    visibility = ["PUBLIC"],
)

system_cxx_toolchain(
    name = "cxx",
    visibility = ["PUBLIC"],
)

system_python_bootstrap_toolchain(
    name = "python_bootstrap",
    visibility = ["PUBLIC"],
)

When I try to buck2 build //:hello_world, I get this:

Local command returned non-zero exit code 1
Reproduce locally: `"buck-out\\v2\\gen\\prelude\\fb50fd37ce946800\\python_bootstrap\\tools\\__win_python_wrapper__\\win_ ...<omitted>... \\gen\\root\\fb50fd37ce946800\\__hello_world__\\bin-pic-static_pic-link\\hello_world-link-diag.args" (run `buck2 log what-failed` to get the full command)`
stdout:
stderr:
error: linking with `buck-out\v2\gen\root\fb50fd37ce946800\__hello_world__\__linker_wrapper.bat` failed: exit code: 1
  |
  = note: "cmd" "/c" "buck-out\\v2\\gen\\root\\fb50fd37ce946800\\__hello_world__\\__linker_wrapper.bat" "-fno-use-linker-plugin" "-Wl,--dynamicbase" "-Wl,--disable-auto-image-base" "-m64" "-Wl,--high-entropy-va" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\self-contained\\crt2.o" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\rsbegin.o" "C:\\Users\\steve\\Documents\\GitHub\\buck-rust-hello\\buck-out\\v2\\tmp\\root\\fb50fd37ce946800\\__hello_world__\\rustc\\_buck_309d5f84a6053e4a\\rustcciRgVe\\symbols.o" "buck-out\\v2\\gen\\root\\fb50fd37ce946800\\__hello_world__\\bin-pic-static_pic-link\\extras\\hello_world\\hello_world.hello_world.aeb6c6e0-cgu.0.rcgu.o" "buck-out\\v2\\gen\\root\\fb50fd37ce946800\\__hello_world__\\bin-pic-static_pic-link\\extras\\hello_world\\hello_world.hello_world.aeb6c6e0-cgu.1.rcgu.o" "buck-out\\v2\\gen\\root\\fb50fd37ce946800\\__hello_world__\\bin-pic-static_pic-link\\extras\\hello_world\\hello_world.hello_world.aeb6c6e0-cgu.2.rcgu.o" "buck-out\\v2\\gen\\root\\fb50fd37ce946800\\__hello_world__\\bin-pic-static_pic-link\\extras\\hello_world\\hello_world.hello_world.aeb6c6e0-cgu.3.rcgu.o" "buck-out\\v2\\gen\\root\\fb50fd37ce946800\\__hello_world__\\bin-pic-static_pic-link\\extras\\hello_world\\hello_world.hello_world.aeb6c6e0-cgu.4.rcgu.o" "buck-out\\v2\\gen\\root\\fb50fd37ce946800\\__hello_world__\\bin-pic-static_pic-link\\extras\\hello_world\\hello_world.hello_world.aeb6c6e0-cgu.5.rcgu.o" "buck-out\\v2\\gen\\root\\fb50fd37ce946800\\__hello_world__\\bin-pic-static_pic-link\\extras\\hello_world\\hello_world.1l3753u24tlzpzc9.rcgu.o" "-L" "buck-out\\v2\\gen\\root\\fb50fd37ce946800\\__hello_world__\\bin-pic-static_pic-link-deps-0" "-L" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib" "-Wl,-Bstatic" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libstd-e363be82127e72d4.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libpanic_unwind-271c0a4c2400bd0e.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libobject-3b3a88ddf57ad9b8.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libmemchr-c38acbaaa0512e61.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libaddr2line-a777dde688506f47.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libgimli-00e812c5215e2bb4.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\librustc_demangle-9824443ffde90bb7.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libstd_detect-c9cae9f57d72c5d8.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libhashbrown-80b5e088fad27661.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libminiz_oxide-25b744457ec6a6b9.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libadler-b662208514509737.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\librustc_std_workspace_alloc-70e1db2cbff7c5e3.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libunwind-bc622eac43f92150.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcfg_if-da38528f9991ea5d.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\liblibc-0217604e5fc185ea.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\liballoc-094368c19a10127d.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\librustc_std_workspace_core-9310325d5d5607bd.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcore-5c3fe6fc6388f93c.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcompiler_builtins-d765c9bc514400ee.rlib" "-Wl,-Bdynamic" "-lkernel32" "-ladvapi32" "-luserenv" "-lkernel32" "-lws2_32" "-lbcrypt" "-lgcc_eh" "-l:libpthread.a" "-lmsvcrt" "-lmingwex" "-lmingw32" "-lgcc" "-lmsvcrt" "-luser32" "-lkernel32" "-Wl,--nxcompat" "-nostartfiles" "-L" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib" "-L" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\self-contained" "-o" "buck-out\\v2\\gen\\root\\fb50fd37ce946800\\__hello_world__\\static_pic\\hello_world.exe" "-Wl,--gc-sections" "-no-pie" "-nodefaultlibs" "@buck-out\\v2\\gen\\root\\fb50fd37ce946800\\__hello_world__\\bin-pic-static_pic-link\\__hello_world-link_linker_args.txt" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\rsend.o"
  = note: 'clang++' is not recognized as an internal or external command,
          operable program or batch file.



error: aborting due to previous error


Build ID: 767daac4-04b8-4412-8f4b-9953bafed010
Jobs completed: 3. Time elapsed: 0.5s. Cache hits: 0%. Commands: 1 (cached: 0, remote: 0, local: 1)
BUILD FAILED
Failed to build 'root//:hello_world (prelude//platforms:default#fb50fd37ce946800)'

Now, what's weird about this is that clang++ is in my path:

$ clang++
clang++: error: no input files

I am not sure what I am doing wrong here, but any pointers would be greatly appreciated.

steveklabnik avatar Apr 10 '23 21:04 steveklabnik

Is clang++ on your windows path, or only your mingw/shell path? E.g. if you start a vanilla cmd shell, is clang++ on your path then? We run things like clang++ directly through cmd as that gives best compatibility, but if your .bashrc or similar sets up clang++ it won't be found.

ndmitchell avatar Apr 11 '23 08:04 ndmitchell

You can also run this command manually to debug this issue outside Buck:

cmd "/c" "buck-out\v2\gen\root\fb50fd37ce946800\__hello_world__\__linker_wrapper.bat" <other args>

KapJI avatar Apr 11 '23 10:04 KapJI

Is clang++ on your windows path, or only your mingw/shell path?

I don't use mingw, I use native Windows. The above is in my normal shell (nushell), but also

〉cmd /c "clang++"
clang++: error: no input files

which is part of why it's so confusing!

You can also run this command manually to debug this issue outside Buck:

Thanks @KapJI! I thought I had tried this, but I must have gotten some of the arguments wrong.

> cmd /c "buck-out\\v2\\gen\\prelude\\fb50fd37ce946800\\python_bootstrap\\tools\\__win_python_wrapper__\\win_python_wrapper.bat" "buck-out\\v2\\gen\\prelude\\fb50fd37ce946800\\rust\\tools\\__rustc_action__\\__rustc_action__" python "buck-out\\v2\\gen\\prelude\\fb50fd37ce946800\\rust\\tools\\__rustc_action__\\rustc_action.py" "@buck-out\\v2\\gen\\root\\fb50fd37ce946800\\__hello_world__\\bin-pic-static_pic-link\\hello_world-link-diag.args"
WARN rustc_codegen_ssa::back::link Linker does not support -no-pie command line option. Retrying without.

error: linking with `buck-out\v2\gen\root\fb50fd37ce946800\__hello_world__\__linker_wrapper.bat` failed: exit code: 1
  |
  = note: "cmd" "/c" "buck-out\\v2\\gen\\root\\fb50fd37ce946800\\__hello_world__\\__linker_wrapper.bat" "-fno-use-linker-plugin" "-Wl,--dynamicbase" "-Wl,--disable-auto-image-base" "-m64" "-Wl,--high-entropy-va" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\self-contained\\crt2.o" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\rsbegin.o" "C:\\Users\\steve\\AppData\\Local\\Temp\\rustccz58eA\\symbols.o" "buck-out\\v2\\gen\\root\\fb50fd37ce946800\\__hello_world__\\bin-pic-static_pic-link\\extras\\hello_world\\hello_world.hello_world.aeb6c6e0-cgu.0.rcgu.o" "buck-out\\v2\\gen\\root\\fb50fd37ce946800\\__hello_world__\\bin-pic-static_pic-link\\extras\\hello_world\\hello_world.hello_world.aeb6c6e0-cgu.1.rcgu.o" "buck-out\\v2\\gen\\root\\fb50fd37ce946800\\__hello_world__\\bin-pic-static_pic-link\\extras\\hello_world\\hello_world.hello_world.aeb6c6e0-cgu.2.rcgu.o" "buck-out\\v2\\gen\\root\\fb50fd37ce946800\\__hello_world__\\bin-pic-static_pic-link\\extras\\hello_world\\hello_world.hello_world.aeb6c6e0-cgu.3.rcgu.o" "buck-out\\v2\\gen\\root\\fb50fd37ce946800\\__hello_world__\\bin-pic-static_pic-link\\extras\\hello_world\\hello_world.hello_world.aeb6c6e0-cgu.4.rcgu.o" "buck-out\\v2\\gen\\root\\fb50fd37ce946800\\__hello_world__\\bin-pic-static_pic-link\\extras\\hello_world\\hello_world.hello_world.aeb6c6e0-cgu.5.rcgu.o" "buck-out\\v2\\gen\\root\\fb50fd37ce946800\\__hello_world__\\bin-pic-static_pic-link\\extras\\hello_world\\hello_world.1l3753u24tlzpzc9.rcgu.o" "-L" "buck-out\\v2\\gen\\root\\fb50fd37ce946800\\__hello_world__\\bin-pic-static_pic-link-deps-0" "-L" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib" "-Wl,-Bstatic" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libstd-e363be82127e72d4.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libpanic_unwind-271c0a4c2400bd0e.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libobject-3b3a88ddf57ad9b8.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libmemchr-c38acbaaa0512e61.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libaddr2line-a777dde688506f47.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libgimli-00e812c5215e2bb4.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\librustc_demangle-9824443ffde90bb7.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libstd_detect-c9cae9f57d72c5d8.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libhashbrown-80b5e088fad27661.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libminiz_oxide-25b744457ec6a6b9.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libadler-b662208514509737.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\librustc_std_workspace_alloc-70e1db2cbff7c5e3.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libunwind-bc622eac43f92150.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcfg_if-da38528f9991ea5d.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\liblibc-0217604e5fc185ea.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\liballoc-094368c19a10127d.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\librustc_std_workspace_core-9310325d5d5607bd.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcore-5c3fe6fc6388f93c.rlib" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcompiler_builtins-d765c9bc514400ee.rlib" "-Wl,-Bdynamic" "-lkernel32" "-ladvapi32" "-luserenv" "-lkernel32" "-lws2_32" "-lbcrypt" "-lgcc_eh" "-l:libpthread.a" "-lmsvcrt" "-lmingwex" "-lmingw32" "-lgcc" "-lmsvcrt" "-luser32" "-lkernel32" "-Wl,--nxcompat" "-nostartfiles" "-L" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib" "-L" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\self-contained" "-o" "buck-out\\v2\\gen\\root\\fb50fd37ce946800\\__hello_world__\\static_pic\\hello_world.exe" "-Wl,--gc-sections" "-nodefaultlibs" "@buck-out\\v2\\gen\\root\\fb50fd37ce946800\\__hello_world__\\bin-pic-static_pic-link\\__hello_world-link_linker_args.txt" "C:\\Users\\steve\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\rsend.o"
  = note: lld-link: warning: ignoring unknown argument '--dynamicbase', did you mean '-dynamicbase'
          lld-link: warning: ignoring unknown argument '--disable-auto-image-base'
          lld-link: warning: ignoring unknown argument '--high-entropy-va'
          lld-link: warning: ignoring unknown argument '-Bstatic'
          lld-link: warning: ignoring unknown argument '-Bdynamic'
          lld-link: warning: ignoring unknown argument '--nxcompat', did you mean '-nxcompat'
          lld-link: warning: ignoring unknown argument '--gc-sections'
          lld-link: error: could not open 'gcc_eh.lib': no such file or directory
          lld-link: error: could not open ':libpthread.a.lib': no such file or directory
          lld-link: error: could not open 'mingwex.lib': no such file or directory
          lld-link: error: could not open 'mingw32.lib': no such file or directory
          lld-link: error: could not open 'gcc.lib': no such file or directory
          clang++: error: linker command failed with exit code 1 (use -v to see invocation)

given this error and the comment above, is it expected to be using this inside of mingw? This reads like lld being passed ld flags to me...

I poked around noticed that the -msvc version of Rust isn't supported yet, so maybe that is actually my issue here. (I set up a rustup override to use x86_64-pc-windows-gnu in this directory because of it) I'm still not 100% sure why invoking the wrapper myself produces a more useful error.

steveklabnik avatar Apr 11 '23 15:04 steveklabnik

It seems the issue here is we don't support lld-link on Windows. We use MSVC flavoured llvm-link internally.

llvm-link should work if cxx_toolchain has linker_type = "windows".

Basically Windows support in Buck2 is quite limited right now, especially in OSS version, but we're working on improving it.

KapJI avatar Apr 11 '23 15:04 KapJI

Basically Windows support in Buck2 is quite limited right now, especially in OSS version, but we're working on improving it.

Totally understand, that's partly why I'm poking at it, I'd like to help make it better, if possible :)

I would be happy to use whatever setup makes this actually work.

cxx.bzl has this

def _system_cxx_toolchain_impl(ctx):
    """
    A very simple toolchain that is hardcoded to the current environment.
    """
    archiver_type = "gnu"
    linker_type = "gnu"
    if host_info().os.is_macos:
        linker_type = "darwin"
    elif host_info().os.is_windows:
        linker_type = "windows"
        archiver_type = "windows"

so it seems to be setting that unconditionally. But then later, as one of the arguments to CxxToolchainInfo:

       linker_flags = ["-fuse-ld=lld"] + ctx.attrs.link_flags,

so it feels like this is currently hard-coded to use lld. I'm guessing that this means there's a divergence between the prelude inside of the company? How would I set up MSVC flavored llvm-link?

Even beyond that though, yinz are building a rust project on Windows in CI https://github.com/facebook/buck2/blob/main/.circleci/config.yml#L201 so this should work. I'm gonna continue to poke at how that environment differs from mine, I think.

steveklabnik avatar Apr 11 '23 16:04 steveklabnik

Internally we have quite different toolchains - but the entire divergence is in the toolchain rule, so if you just take _system_cxx_toolchain_impl and make it spit out whatever toolchain you want, complete with as many hardcoding as you want, that will work just fine.

ndmitchell avatar Apr 11 '23 16:04 ndmitchell

Yes, system_cxx_toolchain rule is very basic version of toolchain configuration we created for open source. Internally we construct cxx_toolchain rule which has much more control on different parameters: https://github.com/facebook/buck2/blob/main/prelude/cxx/cxx_toolchain.bzl

KapJI avatar Apr 11 '23 16:04 KapJI

Ah that makes sense, and yeah, if I build the no_prelude example, it builds just fine, so that all fits!

Given all that, that unsticks me in the moment, but I am unsure if I should just close this out, or if it's useful to track enhancements to that basic toolchain config. Are you all interested in enhancements there, or is the idea that each project should be mostly coming up with its own configurations, rather than using the prelude?

steveklabnik avatar Apr 11 '23 16:04 steveklabnik

We are definitely interested in making example toolchains to work on all platforms :)

KapJI avatar Apr 11 '23 16:04 KapJI

Given that this does work internally, I expect the work to get this working externally is small. Roughly:

  1. Remove the if in https://github.com/facebook/buck2/blob/main/examples/prelude/rust/BUCK so these targets are enabled.
  2. Fix the Rust and C++ toolchains, partly as you've observed.

I expect the diff to be a few lines long. @KapJI - do we target msvc by default for Rust?

ndmitchell avatar Apr 11 '23 21:04 ndmitchell

Yes, MSVC toolchain is the default one.

KapJI avatar Apr 11 '23 21:04 KapJI

I spent some time over a video call with @davidbarsky, who was very helpful in showing me some things, but we weren't able to figure out the root cause.

I'd love to help fix up those toolchains, but given I'm sorta stuck, I don't know how. Current open questions/thoughts:

  1. I still am not sure why clang++ cannot be found. I'm assuming there's some sort of isolation doing its job a bit too well, but I'm unsure where or how to investigate this.
  2. if I run the command manually, it does find clang++, but:
  3. it appears that ld -style linker args are being passed to lld, causing warnings that they're being ignored. It's not clear to me how this is happening.
  4. it cannot find a few different .lib files, this is probably something wrong with my system, as I don't use the -gnu Rust often.

steveklabnik avatar Apr 11 '23 22:04 steveklabnik

(As it turns out, my knowledge ends at the boundaries of Rust rules on Linux/Mac platforms 😅)

davidbarsky avatar Apr 11 '23 23:04 davidbarsky

I ran into the same issue and made a comment over at https://github.com/facebook/buck2-prelude/issues/6

If you change the linker to link in cxx.bzl and change the _DEFAULT_TRIPLE to the -msvc targets, everything seems to work

kcking avatar Apr 12 '23 05:04 kcking

Can confirm! Works for me as well. Thanks so much @kcking !

steveklabnik avatar Apr 12 '23 05:04 steveklabnik

@kcking @steveklabnik - can you confirm exactly how you reproduce this? E.g. what pieces of the Rust toolchain you put on the PATH, how you get clang and which version etc? I'd like to replicate. Once you have that, does compiling the C++ examples on Windows also work?

ndmitchell avatar Apr 12 '23 08:04 ndmitchell

E.g. what pieces of the Rust toolchain you put on the PATH

Just the stuff that rustup does normally, .cargo/bin.

how you get clang and which version etc?

Unlike many targets, this doesn't invoke the linker through the compiler, You don't use clang in this configuration, you're using link.exe, the MSVC linker. This is the default for the MSVC target. People used to have to download it and install it, but IIRC recently, rustup will fetch it for you, so you don't have to do that either.

Once you have that, does compiling the C++ examples on Windows also work?

I haven't tried it yet but will later today.

steveklabnik avatar Apr 12 '23 17:04 steveklabnik

So yes, this still leads to a "clang not found" error:

cxx_binary(
    name = "main",
    srcs = ["main.cpp"],
    link_style = "static",
)
#include<iostream>

int main() {
  std::cout << "Hello, world!" << std::endl;
}

invoking buck2 build //...:

image

Still cannot find clang, still works in my shell though. Tricky!

steveklabnik avatar Apr 12 '23 18:04 steveklabnik

Oh, as for installing clang: I usually use vcpkg, but for some reason it had issues building, so I used scoop, another package manager. All of the llvm tools live in C:\Users\steve\scoop\apps\llvm\current\bin\, which is in my PATH.

steveklabnik avatar Apr 12 '23 20:04 steveklabnik

To get rust working, I overrode the linker to be link on windows in cxx.bzl. When trying @steveklabnik's cpp example, I get a similar not found error, but for link:

Local command returned non-zero exit code <no exit code>
Reproduce locally: `link "-fuse-ld=lld" /Brepro "/OUT:buck-out\\v2\\gen\\root\\fb50fd37ce946800\\__maincpp__\\maincpp" "@buck-out\\v2\\gen\\root\\fb50fd37ce946800\\__maincpp__\\maincpp.linker.argsfile"`
stdout:
stderr:
Spawning executable `link` failed: program not found
Build ID: 982cd683-cd62-49d7-809c-ac2c9d92fadb
Jobs completed: 10. Time elapsed: 0.4s. Cache hits: 0%. Commands: 2 (cached: 0, remote: 0, local: 2)
BUILD FAILED
Failed to build 'root//:maincpp (prelude//platforms:default#fb50fd37ce946800)'

Rust is somehow able to find link even though it isn't on my path by default.

~ ❯ link
link: The term 'link' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Perhaps buck2 should copy how rust searches for the linker on windows (I couldn't find that code after searching for a few minutes), or the prelude should explicitly add the Visual Studio tools bin to its path.

kcking avatar Apr 12 '23 23:04 kcking

Perhaps buck2 should copy how rust searches for the linker on windows

That sounds like a reasonable path forward.

(I couldn't find that code after searching for a few minutes),

I am not an expert, but I believe that is here:

https://github.com/rust-lang/rust/blob/e4dae0dac76436ada630b519f5fbf0b373bc5974/compiler/rustc_codegen_ssa/src/back/link.rs#L884-L887

(that is calling https://docs.rs/cc/latest/cc/windows_registry/fn.find_tool.html)

steveklabnik avatar Apr 13 '23 18:04 steveklabnik

While the "change it to link" hack did work for making a rust binary, it falls apart when trying to build a build script, ported over from a cargo project with reindeer. Which... is also a rust binary, so it's not immediately obvious to me what the difference is here.

Additionally, when building a different project (dtolnay/cxx) that has a working Rust & C++ build on Linux, I get failures like the above, but also

clang++: error: unsupported option '-fPIC' for target 'x86_64-pc-windows-msvc'

So, I'm looking forward to @lmvasquezg's work mentioned over in #163 :)

steveklabnik avatar Apr 14 '23 14:04 steveklabnik

It seems like at this point, it can find my clang++, and #163 is a more general "MSVC issues" issue, so I'm going to close this one in favor of that one.

Thank you all so much for the help on this.

steveklabnik avatar May 02 '23 16:05 steveklabnik