Add bp2build support for cpp_std.
This converts cpp_std and gnu_extensions into a -std copt, if cpp_std is
specified or gnu_extensions is false if cpp_std is not specified.
I chose to go with this copts approach because the tradeoff is a much
simpler setting than adding a new attr(s) everywhere that uses features
to set the flag.
This approach limits the number of user-configurable knobs (since users
would then be able to set std in _both_ copts and the new attr). But it
does rely on the user copt overriding the toolchain's default gnu++17
version, which can mean a `-std` flag showing up twice in the action.
Fixes: b/202462232
Test: b build //system/libziparchive:libziparchive
Change-Id: I81dad029059461739b91f318d662e089edb46b84
diff --git a/cc/bp2build.go b/cc/bp2build.go
index ebba9d1..ec15b26 100644
--- a/cc/bp2build.go
+++ b/cc/bp2build.go
@@ -277,7 +277,24 @@
srcs.SetSelectValue(axis, config, srcsList)
}
- archVariantCopts := parseCommandLineFlags(baseCompilerProps.Cflags)
+ var archVariantCopts []string
+ if axis == bazel.NoConfigAxis {
+ // If cpp_std is not specified, don't generate it in the
+ // BUILD file. For readability purposes, cpp_std and gnu_extensions are
+ // combined into a single -std=<version> copt, except in the
+ // default case where cpp_std is nil and gnu_extensions is true or unspecified,
+ // then the toolchain's default "gnu++17" will be used.
+ if baseCompilerProps.Cpp_std != nil {
+ // TODO(b/202491296): Handle C_std.
+ // These transformations are shared with compiler.go.
+ cppStdVal := parseCppStd(baseCompilerProps.Cpp_std)
+ _, cppStdVal = maybeReplaceGnuToC(baseCompilerProps.Gnu_extensions, "", cppStdVal)
+ archVariantCopts = append(archVariantCopts, "-std="+cppStdVal)
+ } else if baseCompilerProps.Gnu_extensions != nil && !*baseCompilerProps.Gnu_extensions {
+ archVariantCopts = append(archVariantCopts, "-std=c++17")
+ }
+ }
+ archVariantCopts = append(archVariantCopts, parseCommandLineFlags(baseCompilerProps.Cflags)...)
archVariantAsflags := parseCommandLineFlags(baseCompilerProps.Asflags)
localIncludeDirs := baseCompilerProps.Local_include_dirs