Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update windows VS detection code to account for new directory #18608

Closed
wants to merge 7 commits into from

Conversation

redsun82
Copy link

@redsun82 redsun82 commented Jun 7, 2023

Windows VS 2022 version 17.6 introduced a new vcpkg directory underneath VC that is throwing off the toolchain autodetection code.

The checks now got renamed to a more approriate _is_vs_2017_or_newer and takes into account the possible presence of this vcpkg directory.

Closes #18592

Windows VS 2022 version 17.6 introduced a new `vspkg` directory
underneath `VC` that is throwing off the toolchain autodetection code.

The checks now got renamed to a more approriate `_is_vs_2017_or_newer`
and takes into account the possible presence of this `vspkg` directory.
@redsun82 redsun82 marked this pull request as ready for review June 14, 2023 11:59
@github-actions github-actions bot added awaiting-review PR is awaiting review from an assigned reviewer team-Rules-CPP Issues for C++ rules labels Jun 14, 2023
@redsun82
Copy link
Author

Tested this successfully on a recent VS 2022 Community install

@Mizux
Copy link

Mizux commented Jun 14, 2023

Should you also change

# 5. Check default directories for VC installation
auto_configure_warning_maybe(repository_ctx, "Looking for default Visual C++ installation directory")
for path in [
"Microsoft Visual Studio\\2019\\Preview\\VC",
"Microsoft Visual Studio\\2019\\BuildTools\\VC",
"Microsoft Visual Studio\\2019\\Community\\VC",
"Microsoft Visual Studio\\2019\\Professional\\VC",
"Microsoft Visual Studio\\2019\\Enterprise\\VC",
"Microsoft Visual Studio\\2017\\BuildTools\\VC",
"Microsoft Visual Studio\\2017\\Community\\VC",
"Microsoft Visual Studio\\2017\\Professional\\VC",
"Microsoft Visual Studio\\2017\\Enterprise\\VC",
"Microsoft Visual Studio 14.0\\VC",
? (to add all 2022 VS C++ flavors)

ps: I'm using "Microsoft Visual Studio\2022\Preview\VC" (2022 Community preview 17.7.0)

@redsun82
Copy link
Author

redsun82 commented Jun 14, 2023

Good catch @Mizux, but now it is kinda surprising me that even though 2022 is not on that list, it still gets picked up on my my machine 🤔

@redsun82
Copy link
Author

Ah, I see now, that is a kind of last resort hard-coded path check, following many other ways of finding out the VC path, which probably usually already work. But yes, then it makes sense to add 2022 to the mix there.

@redsun82 redsun82 closed this Jun 15, 2023
@redsun82 redsun82 reopened this Jun 15, 2023
"Microsoft Visual Studio 14.0\\VC",
"Microsoft Visual Studio\\2019\\Preview\\VC",
Copy link

@Mizux Mizux Jun 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Currently we need 2022\Preview\VC to build google/or-tools ^^;
    or you could add it to the below comprehension list ?
  2. Also order may matter imho i.e. prefer "Preview" before "Enterprise" before "Community" etc...
    as well as for date 2022 > 2019 > 2017

ref:

Copy link
Author

@redsun82 redsun82 Jun 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in previous code the order was "Preview", "BuildTools", "Community", "Professional", "Enterprise". Should I keep that, or reverse it after "Preview" (i.e. "Preview", "Enterprise", "Professional", "Community", "BuildTools")?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I guess adding "Microsoft Visual Studio\\2017\\Preview\\VC" is harmless even though that did not exist AFAIK, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(for the moment I preserved the order prior to this patch wrt editions, and added the 2017\Preview one, but that can still be tweaked)

@redsun82
Copy link
Author

@Mizux

Should you also change

# 5. Check default directories for VC installation
auto_configure_warning_maybe(repository_ctx, "Looking for default Visual C++ installation directory")
for path in [
"Microsoft Visual Studio\\2019\\Preview\\VC",
"Microsoft Visual Studio\\2019\\BuildTools\\VC",
"Microsoft Visual Studio\\2019\\Community\\VC",
"Microsoft Visual Studio\\2019\\Professional\\VC",
"Microsoft Visual Studio\\2019\\Enterprise\\VC",
"Microsoft Visual Studio\\2017\\BuildTools\\VC",
"Microsoft Visual Studio\\2017\\Community\\VC",
"Microsoft Visual Studio\\2017\\Professional\\VC",
"Microsoft Visual Studio\\2017\\Enterprise\\VC",
"Microsoft Visual Studio 14.0\\VC",

? (to add all 2022 VS C++ flavors)
ps: I'm using "Microsoft Visual Studio\2022\Preview\VC" (2022 Community preview 17.7.0)

This change makes //src/test/py/bazel:bazel_windows_cpp_test fail for a reason I can't understand (those tests fail on my local machine already on master, so I'm having difficulties debugging that). This draft PR shows that reverting just that change makes the test happy. I'm not sure I can devote a lot of time debugging/fixing that test.

@lblk
Copy link

lblk commented Jun 17, 2023

@redsun82 can u publish an release binary file for bazel of your branch ? just in you repo's release and name it bazel-vs2022.exe is ok. i'need it for some special work. thank u very much

@Vertexwahn
Copy link
Contributor

Can we work around this issue by setting the BAZEL_VC environment variable?

@mattip
Copy link

mattip commented Jun 18, 2023

Can we work around this issue by setting the BAZEL_VC environment variable?

I did not find one after long hours of debugging. There is no way around the failure other than:

  • remove the offending extra directory
  • fix the code to accept the new layout

@lblk
Copy link

lblk commented Jun 20, 2023

Can we work around this issue by setting the environment variable?BAZEL_VC

this will not work on vs2022

@redsun82
Copy link
Author

@redsun82 can u publish an release binary file for bazel of your branch ? just in you repo's release and name it bazel-vs2022.exe is ok. i'need it for some special work. thank u very much

@lblk I'll launch the release build on my machine and let you know

@redsun82
Copy link
Author

As already noted, setting BAZEL_VC won't work. Another workaround that I just tested consists in

load("//tools/cpp:cc_configure.bzl", "cc_configure")

cc_configure()

This will replace the built-in autodetection with the patched one.

@redsun82
Copy link
Author

@lblk here you go:

https://github.com/redsun82/bazel/releases/tag/vs-2022-fix-0.1

@redsun82
Copy link
Author

@Mizux what do you think about the change to the hardcoded VS paths that makes that one test fail? As I mentioned I cannot unfortunately devote much time to that currently, so either someone could take over trying to figure out that test failure, or for the moment we revert that change to quickly get something out that still works most of the time? Before this issue I don't think anyone has yet complained about VS 2022 detection not working, even though it's missing from that hard-coded list.

@lblk
Copy link

lblk commented Jun 20, 2023

@lblk here you go:

https://github.com/redsun82/bazel/releases/tag/vs-2022-fix-0.1

thank you very much! so kind of u!

Copy link
Member

@meteorcloudy meteorcloudy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing it, sorry for the late reply!

@meteorcloudy
Copy link
Member

Let's me check if I can help with the test failure

vc_path_contents = [d.basename.lower() for d in repository_ctx.path(vc_path).readdir()]
vc_path_contents = sorted(vc_path_contents)
return vc_path_contents == vc_2017_or_2019_contents
vc_path_contents = sorted([d for d in vc_path_contents if d in vc_2017_or_newer_contents])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can simplify the logic here:

vc_2017_or_newer_expected_dirs = ["auxiliary", "redist", "tools"]
for dir in vc_2017_or_newer_expected_dirs:
    if not repository_ctx.path(vc_path).get_child(dir).exists:
        return False
return True

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a comment above, VS2015 has this directory structure

C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO 14.0\VC
├───bin
├───crt
├───include
├───lib
└───redist

So I think anything that checks for tools and auxiliary will work to differentiate VS2017+ from VS2015. The check for tools is the important one, since that is where the binaries are found.

Copy link
Member

@meteorcloudy meteorcloudy Jun 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, then this could be further simplified to

return repository_ctx.path(vc_path).get_child("tools").exists

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@redsun82 Can you overtake the suggested change - would be nice if we could soon merge this in bring it to Bazel 6.3.0

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can fork this and add @meteorcloudy suggestion if @redsun82 does not have time/resources to work on this further...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I create a PR with this change -> #18945 @meteorcloudy, @redsun82

@meteorcloudy
Copy link
Member

Maybe we can also remove support for VS2015 and prior to simplify the code?

@meteorcloudy
Copy link
Member

@bazel-io fork 6.3.0

@mattip
Copy link

mattip commented Jun 28, 2023

Maybe we can also remove support for VS2015 and prior to simplify the code?

That kind of change is beyond my paygrade, but I think it might be too disruptive on the 5.4 branch. Maybe there are some people using really old VS versions?

@meteorcloudy
Copy link
Member

Yeah, that's a breaking change, we probably shouldn't do that if we plan to cherry pick the fix back to 6.x branches.

@meteorcloudy
Copy link
Member

I think the test failure is a bug with the version of VC 2022 build tools we have on CI, I cannot reproduce with the latest VC 2022 build tool locally. We should update the VC 2022 version in the Windows VM.

In the mean time I have a workaround to use older VC version in integration tests to unblock this.

copybara-service bot pushed a commit that referenced this pull request Jun 28, 2023
To work around [test failure](https://buildkite.com/bazel/bazel-bazel-github-presubmit/builds/16353#018901e5-b0f1-41be-a6f4-e12878711ad3) on #18608. This PR enables Bazel to detect the latest VC build tools installed, but apparently there is a test case which is failing with the new toolchain. Setting --test_env=BAZEL_VC ensures the integrations tests also uses the older VC build tools for now.

PiperOrigin-RevId: 544037250
Change-Id: I4625da17ff2168acbe63813aa7a01e49e0cb459a
@SuperKogito
Copy link

When it comes to building Tensorflow from source with VC 2022 here is the result of my debugging:

Although, Bazel does not account for VS2022 yet but the if condition for VS2017 and VS2019 can still do the trick.
The difference between the old version and the new version is the ../Hostx64/.. label. This is lowercase in your case for example and mine too (VS2022). However, the windows_cc_configure.bz script: line 325 looks for an uppercase ../HostX64/.. label.

The solution here is either to edit ../Hostx64/.. label (https://github.com/bazelbuild/bazel/blob/9c96529fca4a135c162e35ce2882834b879438fb/tools/cpp/windows_cc_configure.bzl#L325C1-L326C1) and change the lines to

if _is_vs_2017_or_2019(repository_ctx, vc_path):
    lib = msvc_vars_x64["%{msvc_env_lib_x64}"]
    full_version = _get_vc_full_version(repository_ctx, vc_path)
    tools_path = "%s\\Tools\\MSVC\\%s\\bin\\Hostx64\\%s" % (vc_path, full_version, target_arch)

    # For native windows(10) on arm64 builds host toolchain runs in an emulated x86 environment
    if not repository_ctx.path(tools_path).exists:
        tools_path = "%s\\Tools\\MSVC\\%s\\bin\\Hostx86\\%s" % (vc_path, full_version, target_arch)

or you rename \bin\Hostx64\ to \bin\HostX64\

also it is vital that these environment variables point to the right paths:

BAZEL_SH=C:\msys64\usr\bin\bash.exe
BAZEL_VC=C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC
BAZEL_VC_FULL_VERSION=14.36.32532
This was tested with:
Windows 10
Bazel 6.2.1
Python 3.9.7
Tensorflow 2.12

Hopefully this helps.

@meteorcloudy
Copy link
Member

you rename \bin\Hostx64\ to \bin\HostX64\

Is this really necessary? File name is supposed to be case insensitive on Windows.

copybara-service bot pushed a commit that referenced this pull request Jul 17, 2023
This is the same PR as #18608 but extended by the modification proposed by @meteorcloudy

This should fix #18592

Should also be picked to 6.3.0 -> #18799

Closes #18945.

PiperOrigin-RevId: 548725707
Change-Id: Iff0f972c9fc23491c8070ee2b12ec600a3d1ead9
iancha1992 pushed a commit to iancha1992/bazel that referenced this pull request Jul 17, 2023
This is the same PR as bazelbuild#18608 but extended by the modification proposed by @meteorcloudy

This should fix bazelbuild#18592

Should also be picked to 6.3.0 -> bazelbuild#18799

Closes bazelbuild#18945.

PiperOrigin-RevId: 548725707
Change-Id: Iff0f972c9fc23491c8070ee2b12ec600a3d1ead9
iancha1992 pushed a commit to iancha1992/bazel that referenced this pull request Jul 18, 2023
To work around [test failure](https://buildkite.com/bazel/bazel-bazel-github-presubmit/builds/16353#018901e5-b0f1-41be-a6f4-e12878711ad3) on bazelbuild#18608. This PR enables Bazel to detect the latest VC build tools installed, but apparently there is a test case which is failing with the new toolchain. Setting --test_env=BAZEL_VC ensures the integrations tests also uses the older VC build tools for now.

PiperOrigin-RevId: 544037250
Change-Id: I4625da17ff2168acbe63813aa7a01e49e0cb459a
@SuperKogito
Copy link

SuperKogito commented Jul 19, 2023

Is this really necessary? File name is supposed to be case insensitive on Windows.

Idk, as I opted for editing the path in the code to avoid messing any other dependencies.

iancha1992 added a commit that referenced this pull request Jul 19, 2023
This is the same PR as #18608 but extended by the modification proposed by @meteorcloudy

This should fix #18592

Should also be picked to 6.3.0 -> #18799

Closes #18945.

PiperOrigin-RevId: 548725707
Change-Id: Iff0f972c9fc23491c8070ee2b12ec600a3d1ead9

Co-authored-by: Vertexwahn <julian.amann@tum.de>
@meteorcloudy
Copy link
Member

#18945 has been merged and cherry picked back to 6.3.0, so we can close this one.

@github-actions github-actions bot removed the awaiting-review PR is awaiting review from an assigned reviewer label Jul 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team-Rules-CPP Issues for C++ rules
Projects
None yet
8 participants