Convert cpp_std to be an attribute instead of a copt.
This attribute is then converted into a feature in cc_library_static.
Fixes: 202518741
Test: CI
Change-Id: I070b56a1e96680ffad0466d085caaab2e9308ebc
diff --git a/bp2build/cc_library_conversion_test.go b/bp2build/cc_library_conversion_test.go
index 76f83e7..134e1d7 100644
--- a/bp2build/cc_library_conversion_test.go
+++ b/bp2build/cc_library_conversion_test.go
@@ -1692,7 +1692,7 @@
}
-func TestCcLibraryCppStdWithGnuExtensions_ConvertstoCopt(t *testing.T) {
+func TestCcLibraryCppStdWithGnuExtensions_ConvertsToFeatureAttr(t *testing.T) {
type testCase struct {
cpp_std string
gnu_extensions string
@@ -1751,7 +1751,7 @@
}
bazelCppStdAttr := ""
if tc.bazel_cpp_std != "" {
- bazelCppStdAttr = fmt.Sprintf("\n copts = [\"-std=%s\"],", tc.bazel_cpp_std)
+ bazelCppStdAttr = fmt.Sprintf("\n cpp_std = \"%s\",", tc.bazel_cpp_std)
}
runCcLibraryTestCase(t, bp2buildTestCase{
@@ -1772,5 +1772,43 @@
name = "a",%s
)`, bazelCppStdAttr)},
})
+
+ runCcLibraryStaticTestCase(t, bp2buildTestCase{
+ description: fmt.Sprintf(
+ "cc_library_static with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
+ moduleTypeUnderTest: "cc_library_static",
+ moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
+ moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
+ blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
+cc_library_static {
+ name: "a",
+%s // cpp_std: *string
+%s // gnu_extensions: *bool
+ include_build_directory: false,
+}
+`, cppStdAttr, gnuExtensionsAttr),
+ expectedBazelTargets: []string{fmt.Sprintf(`cc_library_static(
+ name = "a",%s
+)`, bazelCppStdAttr)},
+ })
+
+ runCcLibrarySharedTestCase(t, bp2buildTestCase{
+ description: fmt.Sprintf(
+ "cc_library_shared with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
+ moduleTypeUnderTest: "cc_library_shared",
+ moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
+ moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
+ blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
+cc_library_shared {
+ name: "a",
+%s // cpp_std: *string
+%s // gnu_extensions: *bool
+ include_build_directory: false,
+}
+`, cppStdAttr, gnuExtensionsAttr),
+ expectedBazelTargets: []string{fmt.Sprintf(`cc_library_shared(
+ name = "a",%s
+)`, bazelCppStdAttr)},
+ })
}
}
diff --git a/cc/bp2build.go b/cc/bp2build.go
index 0b2c133..df938d2 100644
--- a/cc/bp2build.go
+++ b/cc/bp2build.go
@@ -226,7 +226,10 @@
srcs bazel.LabelListAttribute
rtti bazel.BoolAttribute
- stl *string
+
+ // Not affected by arch variants
+ stl *string
+ cppStd *string
localIncludes bazel.StringListAttribute
absoluteIncludes bazel.StringListAttribute
@@ -242,6 +245,8 @@
var rtti bazel.BoolAttribute
var localIncludes bazel.StringListAttribute
var absoluteIncludes bazel.StringListAttribute
+ var stl *string = nil
+ var cppStd *string = nil
parseCommandLineFlags := func(soongFlags []string) []string {
var result []string
@@ -277,7 +282,6 @@
srcs.SetSelectValue(axis, config, srcsList)
}
- 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
@@ -289,11 +293,14 @@
// These transformations are shared with compiler.go.
cppStdVal := parseCppStd(baseCompilerProps.Cpp_std)
_, cppStdVal = maybeReplaceGnuToC(baseCompilerProps.Gnu_extensions, "", cppStdVal)
- archVariantCopts = append(archVariantCopts, "-std="+cppStdVal)
+ cppStd = &cppStdVal
} else if baseCompilerProps.Gnu_extensions != nil && !*baseCompilerProps.Gnu_extensions {
- archVariantCopts = append(archVariantCopts, "-std=c++17")
+ cppStdVal := "c++17"
+ cppStd = &cppStdVal
}
}
+
+ var archVariantCopts []string
archVariantCopts = append(archVariantCopts, parseCommandLineFlags(baseCompilerProps.Cflags)...)
archVariantAsflags := parseCommandLineFlags(baseCompilerProps.Asflags)
@@ -339,7 +346,6 @@
srcs, cSrcs, asSrcs := groupSrcsByExtension(ctx, srcs)
- var stl *string = nil
stlPropsByArch := module.GetArchVariantProperties(ctx, &StlProperties{})
for _, configToProps := range stlPropsByArch {
for _, props := range configToProps {
@@ -367,6 +373,7 @@
cppFlags: cppFlags,
rtti: rtti,
stl: stl,
+ cppStd: cppStd,
localIncludes: localIncludes,
absoluteIncludes: absoluteIncludes,
}
diff --git a/cc/library.go b/cc/library.go
index 0cffd56..00e215b 100644
--- a/cc/library.go
+++ b/cc/library.go
@@ -248,7 +248,9 @@
Linkopts bazel.StringListAttribute
Use_libcrt bazel.BoolAttribute
Rtti bazel.BoolAttribute
- Stl *string
+
+ Stl *string
+ Cpp_std *string
// This is shared only.
Version_script bazel.LabelAttribute
@@ -328,6 +330,7 @@
Use_libcrt: linkerAttrs.useLibcrt,
Rtti: compilerAttrs.rtti,
Stl: compilerAttrs.stl,
+ Cpp_std: compilerAttrs.cppStd,
Version_script: linkerAttrs.versionScript,
@@ -2403,6 +2406,7 @@
Use_libcrt: linkerAttrs.useLibcrt,
Rtti: compilerAttrs.rtti,
Stl: compilerAttrs.stl,
+ Cpp_std: compilerAttrs.cppStd,
Export_includes: exportedIncludes.Includes,
Export_system_includes: exportedIncludes.SystemIncludes,
Local_includes: compilerAttrs.localIncludes,
@@ -2427,6 +2431,7 @@
Use_libcrt: linkerAttrs.useLibcrt,
Rtti: compilerAttrs.rtti,
Stl: compilerAttrs.stl,
+ Cpp_std: compilerAttrs.cppStd,
Export_includes: exportedIncludes.Includes,
Export_system_includes: exportedIncludes.SystemIncludes,
@@ -2462,6 +2467,7 @@
Use_libcrt bazel.BoolAttribute
Rtti bazel.BoolAttribute
Stl *string
+ Cpp_std *string
Export_includes bazel.StringListAttribute
Export_system_includes bazel.StringListAttribute
@@ -2489,6 +2495,7 @@
Use_libcrt bazel.BoolAttribute
Rtti bazel.BoolAttribute
Stl *string
+ Cpp_std *string
Export_includes bazel.StringListAttribute
Export_system_includes bazel.StringListAttribute