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

Using @platforms//os:windows to assume MSVC won't work with MinGW #249

Open
jesseschalken opened this issue Jan 25, 2022 · 0 comments
Open

Comments

@jesseschalken
Copy link

jesseschalken commented Jan 25, 2022

I don't have a specific reproducible error but I just noticed that the select()s that assume that @platforms//os:windows means MSVC are most likely wrong, such as

"//conditions:default": ["-Wno-shift-negative-value"],

and

"/D_WIN32_WINNT=0x0601",

I use MinGW to cross compile to Windows from Linux, so @platforms//os:windows is active but the compiler wont understand something like /D_WIN32_WINNT=0x0601.

Bazel doesn't document how to select() on the chosen compiler in a way that doesn't require the compiler to be specified as a constraint in target platform and available toolchains, but Abseil has the correct way in its sources here:

https://github.com/abseil/abseil-cpp/blob/46d939a918092ef94498a126dc1244b437100b31/absl/BUILD.bazel#L28

Note that both msvc and clang-cl should be effectively treated as MSVC, so I typically define:

config_setting(
    name = "is_msvc",
    flag_values = {"@bazel_tools//tools/cpp:compiler": "msvc-cl"},
)

config_setting(
    name = "is_clang_cl",
    flag_values = {"@bazel_tools//tools/cpp:compiler": "clang-cl"},
)

selects.config_setting_group(
    name = "is_msvc_like",
    match_any = [":is_msvc", ":is_clang_cl"],
)

And then use:

select({
    ":is_msvc_like": "I'm msvc or clang-cl",
    "//conditions:default": "I'm probably gcc or gcc-compatible",
})

The values for @bazel_tools//tools/cpp:compiler come from the compiler attribute on the cc_toolchain_config. When targeting Windows the possible values are mingw-gcc, msys-gcc, msvc-cl and clang-cl, defined in this file:

https://github.com/bazelbuild/bazel/blob/36116ce73f97a7d2ac528dca07cc77bc772c9a22/tools/cpp/BUILD.windows.tpl#L132

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant