Merge "bp2build implementation for c_std"

This commit is contained in:
Christopher Parsons
2021-11-30 15:50:31 +00:00
committed by Gerrit Code Review
5 changed files with 82 additions and 52 deletions

View File

@@ -364,10 +364,6 @@ var (
"platform_tools_properties", "platform_tools_properties",
"build_tools_source_properties", "build_tools_source_properties",
"libminijail", // b/202491296: Uses unsupported c_std property.
"minijail0", // depends on unconverted modules: libminijail
"drop_privs", // depends on unconverted modules: libminijail
// Tests. Handle later. // Tests. Handle later.
"libbionic_tests_headers_posix", // http://b/186024507, cc_library_static, sched.h, time.h not found "libbionic_tests_headers_posix", // http://b/186024507, cc_library_static, sched.h, time.h not found
"libjemalloc5_integrationtest", "libjemalloc5_integrationtest",

View File

@@ -1691,8 +1691,10 @@ cc_library {
func TestCcLibraryCppStdWithGnuExtensions_ConvertsToFeatureAttr(t *testing.T) { func TestCcLibraryCppStdWithGnuExtensions_ConvertsToFeatureAttr(t *testing.T) {
type testCase struct { type testCase struct {
cpp_std string cpp_std string
c_std string
gnu_extensions string gnu_extensions string
bazel_cpp_std string bazel_cpp_std string
bazel_c_std string
} }
testCases := []testCase{ testCases := []testCase{
@@ -1702,45 +1704,58 @@ func TestCcLibraryCppStdWithGnuExtensions_ConvertsToFeatureAttr(t *testing.T) {
// not set, only emit if gnu_extensions is disabled. the default (gnu+17 // not set, only emit if gnu_extensions is disabled. the default (gnu+17
// is set in the toolchain.) // is set in the toolchain.)
{cpp_std: "", gnu_extensions: "", bazel_cpp_std: ""}, {cpp_std: "", gnu_extensions: "", bazel_cpp_std: ""},
{cpp_std: "", gnu_extensions: "false", bazel_cpp_std: "c++17"}, {cpp_std: "", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c99"},
{cpp_std: "", gnu_extensions: "true", bazel_cpp_std: ""}, {cpp_std: "", gnu_extensions: "true", bazel_cpp_std: ""},
// experimental defaults to gnu++2a // experimental defaults to gnu++2a
{cpp_std: "experimental", gnu_extensions: "", bazel_cpp_std: "gnu++2a"}, {cpp_std: "experimental", gnu_extensions: "", bazel_cpp_std: "gnu++2a"},
{cpp_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "c++2a"}, {cpp_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "c++2a", bazel_c_std: "c99"},
{cpp_std: "experimental", gnu_extensions: "true", bazel_cpp_std: "gnu++2a"}, {cpp_std: "experimental", gnu_extensions: "true", bazel_cpp_std: "gnu++2a"},
// Explicitly setting a c++ std does not use replace gnu++ std even if // Explicitly setting a c++ std does not use replace gnu++ std even if
// gnu_extensions is true. // gnu_extensions is true.
// "c++11", // "c++11",
{cpp_std: "c++11", gnu_extensions: "", bazel_cpp_std: "c++11"}, {cpp_std: "c++11", gnu_extensions: "", bazel_cpp_std: "c++11"},
{cpp_std: "c++11", gnu_extensions: "false", bazel_cpp_std: "c++11"}, {cpp_std: "c++11", gnu_extensions: "false", bazel_cpp_std: "c++11", bazel_c_std: "c99"},
{cpp_std: "c++11", gnu_extensions: "true", bazel_cpp_std: "c++11"}, {cpp_std: "c++11", gnu_extensions: "true", bazel_cpp_std: "c++11"},
// "c++17", // "c++17",
{cpp_std: "c++17", gnu_extensions: "", bazel_cpp_std: "c++17"}, {cpp_std: "c++17", gnu_extensions: "", bazel_cpp_std: "c++17"},
{cpp_std: "c++17", gnu_extensions: "false", bazel_cpp_std: "c++17"}, {cpp_std: "c++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c99"},
{cpp_std: "c++17", gnu_extensions: "true", bazel_cpp_std: "c++17"}, {cpp_std: "c++17", gnu_extensions: "true", bazel_cpp_std: "c++17"},
// "c++2a", // "c++2a",
{cpp_std: "c++2a", gnu_extensions: "", bazel_cpp_std: "c++2a"}, {cpp_std: "c++2a", gnu_extensions: "", bazel_cpp_std: "c++2a"},
{cpp_std: "c++2a", gnu_extensions: "false", bazel_cpp_std: "c++2a"}, {cpp_std: "c++2a", gnu_extensions: "false", bazel_cpp_std: "c++2a", bazel_c_std: "c99"},
{cpp_std: "c++2a", gnu_extensions: "true", bazel_cpp_std: "c++2a"}, {cpp_std: "c++2a", gnu_extensions: "true", bazel_cpp_std: "c++2a"},
// "c++98", // "c++98",
{cpp_std: "c++98", gnu_extensions: "", bazel_cpp_std: "c++98"}, {cpp_std: "c++98", gnu_extensions: "", bazel_cpp_std: "c++98"},
{cpp_std: "c++98", gnu_extensions: "false", bazel_cpp_std: "c++98"}, {cpp_std: "c++98", gnu_extensions: "false", bazel_cpp_std: "c++98", bazel_c_std: "c99"},
{cpp_std: "c++98", gnu_extensions: "true", bazel_cpp_std: "c++98"}, {cpp_std: "c++98", gnu_extensions: "true", bazel_cpp_std: "c++98"},
// gnu++ is replaced with c++ if gnu_extensions is explicitly false. // gnu++ is replaced with c++ if gnu_extensions is explicitly false.
// "gnu++11", // "gnu++11",
{cpp_std: "gnu++11", gnu_extensions: "", bazel_cpp_std: "gnu++11"}, {cpp_std: "gnu++11", gnu_extensions: "", bazel_cpp_std: "gnu++11"},
{cpp_std: "gnu++11", gnu_extensions: "false", bazel_cpp_std: "c++11"}, {cpp_std: "gnu++11", gnu_extensions: "false", bazel_cpp_std: "c++11", bazel_c_std: "c99"},
{cpp_std: "gnu++11", gnu_extensions: "true", bazel_cpp_std: "gnu++11"}, {cpp_std: "gnu++11", gnu_extensions: "true", bazel_cpp_std: "gnu++11"},
// "gnu++17", // "gnu++17",
{cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17"}, {cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17"},
{cpp_std: "gnu++17", gnu_extensions: "false", bazel_cpp_std: "c++17"}, {cpp_std: "gnu++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c99"},
{cpp_std: "gnu++17", gnu_extensions: "true", bazel_cpp_std: "gnu++17"}, {cpp_std: "gnu++17", gnu_extensions: "true", bazel_cpp_std: "gnu++17"},
// some c_std test cases
{c_std: "experimental", gnu_extensions: "", bazel_c_std: "gnu11"},
{c_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c11"},
{c_std: "experimental", gnu_extensions: "true", bazel_c_std: "gnu11"},
{c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17", bazel_c_std: "gnu11"},
{c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c11"},
{c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "true", bazel_cpp_std: "gnu++17", bazel_c_std: "gnu11"},
} }
for _, tc := range testCases { for i, tc := range testCases {
name_prefix := fmt.Sprintf("a_%v", i)
cppStdProp := "" cppStdProp := ""
if tc.cpp_std != "" { if tc.cpp_std != "" {
cppStdProp = fmt.Sprintf(" cpp_std: \"%s\",", tc.cpp_std) cppStdProp = fmt.Sprintf(" cpp_std: \"%s\",", tc.cpp_std)
} }
cStdProp := ""
if tc.c_std != "" {
cStdProp = fmt.Sprintf(" c_std: \"%s\",", tc.c_std)
}
gnuExtensionsProp := "" gnuExtensionsProp := ""
if tc.gnu_extensions != "" { if tc.gnu_extensions != "" {
gnuExtensionsProp = fmt.Sprintf(" gnu_extensions: %s,", tc.gnu_extensions) gnuExtensionsProp = fmt.Sprintf(" gnu_extensions: %s,", tc.gnu_extensions)
@@ -1749,61 +1764,67 @@ func TestCcLibraryCppStdWithGnuExtensions_ConvertsToFeatureAttr(t *testing.T) {
if tc.bazel_cpp_std != "" { if tc.bazel_cpp_std != "" {
attrs["cpp_std"] = fmt.Sprintf(`"%s"`, tc.bazel_cpp_std) attrs["cpp_std"] = fmt.Sprintf(`"%s"`, tc.bazel_cpp_std)
} }
if tc.bazel_c_std != "" {
attrs["c_std"] = fmt.Sprintf(`"%s"`, tc.bazel_c_std)
}
runCcLibraryTestCase(t, bp2buildTestCase{ runCcLibraryTestCase(t, bp2buildTestCase{
description: fmt.Sprintf( description: fmt.Sprintf(
"cc_library with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions), "cc_library with c_std: %s, cpp_std: %s and gnu_extensions: %s", tc.c_std, tc.cpp_std, tc.gnu_extensions),
moduleTypeUnderTest: "cc_library", moduleTypeUnderTest: "cc_library",
moduleTypeUnderTestFactory: cc.LibraryFactory, moduleTypeUnderTestFactory: cc.LibraryFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
blueprint: soongCcLibraryPreamble + fmt.Sprintf(` blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
cc_library { cc_library {
name: "a", name: "%s_full",
%s // cpp_std: *string %s // cpp_std: *string
%s // c_std: *string
%s // gnu_extensions: *bool %s // gnu_extensions: *bool
include_build_directory: false, include_build_directory: false,
} }
`, cppStdProp, gnuExtensionsProp), `, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
expectedBazelTargets: []string{ expectedBazelTargets: []string{
makeBazelTarget("cc_library", "a", attrs), makeBazelTarget("cc_library", name_prefix+"_full", attrs),
}, },
}) })
runCcLibraryStaticTestCase(t, bp2buildTestCase{ runCcLibraryStaticTestCase(t, bp2buildTestCase{
description: fmt.Sprintf( description: fmt.Sprintf(
"cc_library_static with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions), "cc_library_static with c_std: %s, cpp_std: %s and gnu_extensions: %s", tc.c_std, tc.cpp_std, tc.gnu_extensions),
moduleTypeUnderTest: "cc_library_static", moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory, moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
blueprint: soongCcLibraryPreamble + fmt.Sprintf(` blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
cc_library_static { cc_library_static {
name: "a", name: "%s_static",
%s // cpp_std: *string %s // cpp_std: *string
%s // c_std: *string
%s // gnu_extensions: *bool %s // gnu_extensions: *bool
include_build_directory: false, include_build_directory: false,
} }
`, cppStdProp, gnuExtensionsProp), `, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
expectedBazelTargets: []string{ expectedBazelTargets: []string{
makeBazelTarget("cc_library_static", "a", attrs), makeBazelTarget("cc_library_static", name_prefix+"_static", attrs),
}, },
}) })
runCcLibrarySharedTestCase(t, bp2buildTestCase{ runCcLibrarySharedTestCase(t, bp2buildTestCase{
description: fmt.Sprintf( description: fmt.Sprintf(
"cc_library_shared with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions), "cc_library_shared with c_std: %s, cpp_std: %s and gnu_extensions: %s", tc.c_std, tc.cpp_std, tc.gnu_extensions),
moduleTypeUnderTest: "cc_library_shared", moduleTypeUnderTest: "cc_library_shared",
moduleTypeUnderTestFactory: cc.LibrarySharedFactory, moduleTypeUnderTestFactory: cc.LibrarySharedFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build, moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
blueprint: soongCcLibraryPreamble + fmt.Sprintf(` blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
cc_library_shared { cc_library_shared {
name: "a", name: "%s_shared",
%s // cpp_std: *string %s // cpp_std: *string
%s // c_std: *string
%s // gnu_extensions: *bool %s // gnu_extensions: *bool
include_build_directory: false, include_build_directory: false,
} }
`, cppStdProp, gnuExtensionsProp), `, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
expectedBazelTargets: []string{ expectedBazelTargets: []string{
makeBazelTarget("cc_library_shared", "a", attrs), makeBazelTarget("cc_library_shared", name_prefix+"_shared", attrs),
}, },
}) })
} }

View File

@@ -252,6 +252,7 @@ type compilerAttributes struct {
// Not affected by arch variants // Not affected by arch variants
stl *string stl *string
cStd *string
cppStd *string cppStd *string
localIncludes bazel.StringListAttribute localIncludes bazel.StringListAttribute
@@ -278,8 +279,7 @@ func (ca *compilerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversi
localIncludeDirs := props.Local_include_dirs localIncludeDirs := props.Local_include_dirs
if axis == bazel.NoConfigAxis { if axis == bazel.NoConfigAxis {
ca.cppStd = bp2buildResolveCppStdValue(props.Cpp_std, props.Gnu_extensions) ca.cStd, ca.cppStd = bp2buildResolveCppStdValue(props.C_std, props.Cpp_std, props.Gnu_extensions)
if includeBuildDirectory(props.Include_build_directory) { if includeBuildDirectory(props.Include_build_directory) {
localIncludeDirs = append(localIncludeDirs, ".") localIncludeDirs = append(localIncludeDirs, ".")
} }
@@ -371,24 +371,24 @@ func parseSrcs(ctx android.BazelConversionPathContext, props *BaseCompilerProper
return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedSrcsLabelList), anySrcs return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedSrcsLabelList), anySrcs
} }
func bp2buildResolveCppStdValue(cpp_std *string, gnu_extensions *bool) *string { func bp2buildResolveCppStdValue(c_std *string, cpp_std *string, gnu_extensions *bool) (*string, *string) {
var cppStd *string var cStdVal, cppStdVal string
// If cpp_std is not specified, don't generate it in the // If c{,pp}std properties are not specified, don't generate them in the BUILD file.
// BUILD file. For readability purposes, cpp_std and gnu_extensions are // Defaults are handled by the toolchain definition.
// combined into a single -std=<version> copt, except in the // However, if gnu_extensions is false, then the default gnu-to-c version must be specified.
// 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 cpp_std != nil { if cpp_std != nil {
// TODO(b/202491296): Handle C_std. cppStdVal = parseCppStd(cpp_std)
// These transformations are shared with compiler.go.
cppStdVal := parseCppStd(cpp_std)
_, cppStdVal = maybeReplaceGnuToC(gnu_extensions, "", cppStdVal)
cppStd = &cppStdVal
} else if gnu_extensions != nil && !*gnu_extensions { } else if gnu_extensions != nil && !*gnu_extensions {
cppStdVal := "c++17" cppStdVal = "c++17"
cppStd = &cppStdVal
} }
return cppStd if c_std != nil {
cStdVal = parseCStd(c_std)
} else if gnu_extensions != nil && !*gnu_extensions {
cStdVal = "c99"
}
cStdVal, cppStdVal = maybeReplaceGnuToC(gnu_extensions, cStdVal, cppStdVal)
return &cStdVal, &cppStdVal
} }
// bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes. // bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes.

View File

@@ -304,12 +304,25 @@ func parseCppStd(cppStdPtr *string) string {
cppStd := String(cppStdPtr) cppStd := String(cppStdPtr)
switch cppStd { switch cppStd {
case "": case "":
cppStd = config.CppStdVersion return config.CppStdVersion
case "experimental": case "experimental":
cppStd = config.ExperimentalCppStdVersion return config.ExperimentalCppStdVersion
} default:
return cppStd return cppStd
} }
}
func parseCStd(cStdPtr *string) string {
cStd := String(cStdPtr)
switch cStd {
case "":
return config.CStdVersion
case "experimental":
return config.ExperimentalCStdVersion
default:
return cStd
}
}
// Create a Flags struct that collects the compile flags from global values, // Create a Flags struct that collects the compile flags from global values,
// per-target values, module type values, and per-module Blueprints properties // per-target values, module type values, and per-module Blueprints properties
@@ -479,13 +492,7 @@ func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags, deps
flags.Global.CommonFlags = append(flags.Global.CommonFlags, tc.ToolchainCflags()) flags.Global.CommonFlags = append(flags.Global.CommonFlags, tc.ToolchainCflags())
cStd := config.CStdVersion cStd := parseCStd(compiler.Properties.C_std)
if String(compiler.Properties.C_std) == "experimental" {
cStd = config.ExperimentalCStdVersion
} else if String(compiler.Properties.C_std) != "" {
cStd = String(compiler.Properties.C_std)
}
cppStd := parseCppStd(compiler.Properties.Cpp_std) cppStd := parseCppStd(compiler.Properties.Cpp_std)
cStd, cppStd = maybeReplaceGnuToC(compiler.Properties.Gnu_extensions, cStd, cppStd) cStd, cppStd = maybeReplaceGnuToC(compiler.Properties.Gnu_extensions, cStd, cppStd)

View File

@@ -253,6 +253,7 @@ type bazelCcLibraryAttributes struct {
Stl *string Stl *string
Cpp_std *string Cpp_std *string
C_std *string
// This is shared only. // This is shared only.
Link_crt bazel.BoolAttribute Link_crt bazel.BoolAttribute
@@ -335,6 +336,7 @@ func CcLibraryBp2Build(ctx android.TopDownMutatorContext) {
Rtti: compilerAttrs.rtti, Rtti: compilerAttrs.rtti,
Stl: compilerAttrs.stl, Stl: compilerAttrs.stl,
Cpp_std: compilerAttrs.cppStd, Cpp_std: compilerAttrs.cppStd,
C_std: compilerAttrs.cStd,
Additional_linker_inputs: linkerAttrs.additionalLinkerInputs, Additional_linker_inputs: linkerAttrs.additionalLinkerInputs,
@@ -2420,6 +2422,7 @@ func ccSharedOrStaticBp2BuildMutatorInternal(ctx android.TopDownMutatorContext,
Rtti: compilerAttrs.rtti, Rtti: compilerAttrs.rtti,
Stl: compilerAttrs.stl, Stl: compilerAttrs.stl,
Cpp_std: compilerAttrs.cppStd, Cpp_std: compilerAttrs.cppStd,
C_std: compilerAttrs.cStd,
Export_includes: exportedIncludes.Includes, Export_includes: exportedIncludes.Includes,
Export_system_includes: exportedIncludes.SystemIncludes, Export_system_includes: exportedIncludes.SystemIncludes,
Local_includes: compilerAttrs.localIncludes, Local_includes: compilerAttrs.localIncludes,
@@ -2445,6 +2448,7 @@ func ccSharedOrStaticBp2BuildMutatorInternal(ctx android.TopDownMutatorContext,
Rtti: compilerAttrs.rtti, Rtti: compilerAttrs.rtti,
Stl: compilerAttrs.stl, Stl: compilerAttrs.stl,
Cpp_std: compilerAttrs.cppStd, Cpp_std: compilerAttrs.cppStd,
C_std: compilerAttrs.cStd,
Export_includes: exportedIncludes.Includes, Export_includes: exportedIncludes.Includes,
Export_system_includes: exportedIncludes.SystemIncludes, Export_system_includes: exportedIncludes.SystemIncludes,
@@ -2480,6 +2484,7 @@ type bazelCcLibraryStaticAttributes struct {
Rtti bazel.BoolAttribute Rtti bazel.BoolAttribute
Stl *string Stl *string
Cpp_std *string Cpp_std *string
C_std *string
Export_includes bazel.StringListAttribute Export_includes bazel.StringListAttribute
Export_system_includes bazel.StringListAttribute Export_system_includes bazel.StringListAttribute
@@ -2516,6 +2521,7 @@ type bazelCcLibrarySharedAttributes struct {
Rtti bazel.BoolAttribute Rtti bazel.BoolAttribute
Stl *string Stl *string
Cpp_std *string Cpp_std *string
C_std *string
Export_includes bazel.StringListAttribute Export_includes bazel.StringListAttribute
Export_system_includes bazel.StringListAttribute Export_system_includes bazel.StringListAttribute