Use a separate feature for cpp std option on Windows
Copybara Import from https://github.com/bazelbuild/rules_cc/pull/443
BEGIN_PUBLIC
Use a separate feature for cpp std option on Windows (#443)
Address https://github.com/bazelbuild/rules_cc/pull/440#issuecomment-3077629397
We should probably do the same for unix cc toolchains, but that needs more clean up first.
Closes #443
END_PUBLIC
COPYBARA_INTEGRATE_REVIEW=https://github.com/bazelbuild/rules_cc/pull/443 from meteorcloudy:msvc_cpp_std 3c4f7b33a73bbd4ef78440a88fd07d684aac6ff7
PiperOrigin-RevId: 784153348
Change-Id: Ibc1b580d7b5b6df34ec1db70f2005b9002585f65
diff --git a/cc/private/toolchain/windows_cc_toolchain_config.bzl b/cc/private/toolchain/windows_cc_toolchain_config.bzl
index 8102947..5d0d40f 100644
--- a/cc/private/toolchain/windows_cc_toolchain_config.bzl
+++ b/cc/private/toolchain/windows_cc_toolchain_config.bzl
@@ -728,6 +728,21 @@
],
)
+ default_cpp_std_feature = feature(
+ name = "default_cpp_std",
+ enabled = True,
+ flag_sets = [
+ flag_set(
+ actions = all_cpp_compile_actions,
+ flag_groups = [
+ flag_group(
+ flags = ["/std:c++17"],
+ ),
+ ],
+ ),
+ ],
+ )
+
default_compile_flags_feature = feature(
name = "default_compile_flags",
enabled = True,
@@ -751,7 +766,6 @@
flag_groups = [
flag_group(
flags = [
- "/std:c++17",
"/DNOMINMAX",
"/D_WIN32_WINNT=0x0601",
"/D_CRT_SECURE_NO_DEPRECATE",
@@ -1274,6 +1288,7 @@
no_stripping_feature,
targets_windows_feature,
copy_dynamic_libraries_to_binary_feature,
+ default_cpp_std_feature,
default_compile_flags_feature,
msvc_env_feature,
msvc_compile_env_feature,
diff --git a/tests/simple_binary/BUILD b/tests/simple_binary/BUILD
index 259e5a5..7a0fb01 100644
--- a/tests/simple_binary/BUILD
+++ b/tests/simple_binary/BUILD
@@ -26,3 +26,14 @@
srcs = ["foo.cc"],
linkshared = 1,
)
+
+# Regression test for building C code
+# https://github.com/bazelbuild/rules_cc/pull/440#issuecomment-3075519628
+cc_binary(
+ name = "bar",
+ srcs = ["bar.c"],
+ copts = select({
+ "@platforms//os:windows": ["/std:c11"],
+ "//conditions:default": [],
+ }),
+)
diff --git a/tests/simple_binary/bar.c b/tests/simple_binary/bar.c
new file mode 100644
index 0000000..cc38c10
--- /dev/null
+++ b/tests/simple_binary/bar.c
@@ -0,0 +1,15 @@
+// Copyright 2023 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+int main(int argc, char *argv[]) { return 0; }