Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 93c3cb69 authored by Christopher Parsons's avatar Christopher Parsons Committed by Gerrit Code Review
Browse files

Merge "bp2build implementation for c_std"

parents 8a498161 79bd2b77
Loading
Loading
Loading
Loading
+0 −4
Original line number Diff line number Diff line
@@ -364,10 +364,6 @@ var (
		"platform_tools_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.
		"libbionic_tests_headers_posix", // http://b/186024507, cc_library_static, sched.h, time.h not found
		"libjemalloc5_integrationtest",
+42 −21
Original line number Diff line number Diff line
@@ -1691,8 +1691,10 @@ cc_library {
func TestCcLibraryCppStdWithGnuExtensions_ConvertsToFeatureAttr(t *testing.T) {
	type testCase struct {
		cpp_std        string
		c_std          string
		gnu_extensions string
		bazel_cpp_std  string
		bazel_c_std    string
	}

	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
		// is set in the toolchain.)
		{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: ""},
		// experimental defaults to 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"},
		// Explicitly setting a c++ std does not use replace gnu++ std even if
		// gnu_extensions is true.
		// "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"},
		// "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"},
		// "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"},
		// "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"},
		// gnu++ is replaced with c++ if gnu_extensions is explicitly false.
		// "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"},
		// "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"},

		// 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 := ""
		if 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 := ""
		if 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 != "" {
			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{
			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",
			moduleTypeUnderTestFactory:         cc.LibraryFactory,
			moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
			blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
cc_library {
	name: "a",
	name: "%s_full",
%s // cpp_std: *string
%s // c_std: *string
%s // gnu_extensions: *bool
	include_build_directory: false,
}
`, cppStdProp, gnuExtensionsProp),
`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
			expectedBazelTargets: []string{
				makeBazelTarget("cc_library", "a", attrs),
				makeBazelTarget("cc_library", name_prefix+"_full", attrs),
			},
		})

		runCcLibraryStaticTestCase(t, bp2buildTestCase{
			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",
			moduleTypeUnderTestFactory:         cc.LibraryStaticFactory,
			moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
			blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
cc_library_static {
	name: "a",
	name: "%s_static",
%s // cpp_std: *string
%s // c_std: *string
%s // gnu_extensions: *bool
	include_build_directory: false,
}
`, cppStdProp, gnuExtensionsProp),
`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
			expectedBazelTargets: []string{
				makeBazelTarget("cc_library_static", "a", attrs),
				makeBazelTarget("cc_library_static", name_prefix+"_static", attrs),
			},
		})

		runCcLibrarySharedTestCase(t, bp2buildTestCase{
			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",
			moduleTypeUnderTestFactory:         cc.LibrarySharedFactory,
			moduleTypeUnderTestBp2BuildMutator: cc.CcLibrarySharedBp2Build,
			blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
cc_library_shared {
	name: "a",
	name: "%s_shared",
%s // cpp_std: *string
%s // c_std: *string
%s // gnu_extensions: *bool
	include_build_directory: false,
}
`, cppStdProp, gnuExtensionsProp),
`, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
			expectedBazelTargets: []string{
				makeBazelTarget("cc_library_shared", "a", attrs),
				makeBazelTarget("cc_library_shared", name_prefix+"_shared", attrs),
			},
		})
	}
+17 −17
Original line number Diff line number Diff line
@@ -252,6 +252,7 @@ type compilerAttributes struct {

	// Not affected by arch variants
	stl    *string
	cStd   *string
	cppStd *string

	localIncludes    bazel.StringListAttribute
@@ -278,8 +279,7 @@ func (ca *compilerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversi

	localIncludeDirs := props.Local_include_dirs
	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) {
			localIncludeDirs = append(localIncludeDirs, ".")
		}
@@ -371,24 +371,24 @@ func parseSrcs(ctx android.BazelConversionPathContext, props *BaseCompilerProper
	return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedSrcsLabelList), anySrcs
}

func bp2buildResolveCppStdValue(cpp_std *string, gnu_extensions *bool) *string {
	var cppStd *string
	// 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.
func bp2buildResolveCppStdValue(c_std *string, cpp_std *string, gnu_extensions *bool) (*string, *string) {
	var cStdVal, cppStdVal string
	// If c{,pp}std properties are not specified, don't generate them in the BUILD file.
	// Defaults are handled by the toolchain definition.
	// However, if gnu_extensions is false, then the default gnu-to-c version must be specified.
	if cpp_std != nil {
		// TODO(b/202491296): Handle C_std.
		// These transformations are shared with compiler.go.
		cppStdVal := parseCppStd(cpp_std)
		_, cppStdVal = maybeReplaceGnuToC(gnu_extensions, "", cppStdVal)
		cppStd = &cppStdVal
		cppStdVal = parseCppStd(cpp_std)
	} else if gnu_extensions != nil && !*gnu_extensions {
		cppStdVal = "c++17"
	}
	if c_std != nil {
		cStdVal = parseCStd(c_std)
	} else if gnu_extensions != nil && !*gnu_extensions {
		cppStdVal := "c++17"
		cppStd = &cppStdVal
		cStdVal = "c99"
	}
	return cppStd

	cStdVal, cppStdVal = maybeReplaceGnuToC(gnu_extensions, cStdVal, cppStdVal)
	return &cStdVal, &cppStdVal
}

// bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes.
+17 −10
Original line number Diff line number Diff line
@@ -304,12 +304,25 @@ func parseCppStd(cppStdPtr *string) string {
	cppStd := String(cppStdPtr)
	switch cppStd {
	case "":
		cppStd = config.CppStdVersion
		return config.CppStdVersion
	case "experimental":
		cppStd = config.ExperimentalCppStdVersion
	}
		return config.ExperimentalCppStdVersion
	default:
		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,
// 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())

	cStd := config.CStdVersion
	if String(compiler.Properties.C_std) == "experimental" {
		cStd = config.ExperimentalCStdVersion
	} else if String(compiler.Properties.C_std) != "" {
		cStd = String(compiler.Properties.C_std)
	}

	cStd := parseCStd(compiler.Properties.C_std)
	cppStd := parseCppStd(compiler.Properties.Cpp_std)

	cStd, cppStd = maybeReplaceGnuToC(compiler.Properties.Gnu_extensions, cStd, cppStd)
+6 −0
Original line number Diff line number Diff line
@@ -253,6 +253,7 @@ type bazelCcLibraryAttributes struct {

	Stl     *string
	Cpp_std *string
	C_std   *string

	// This is shared only.
	Link_crt                 bazel.BoolAttribute
@@ -335,6 +336,7 @@ func CcLibraryBp2Build(ctx android.TopDownMutatorContext) {
		Rtti:                        compilerAttrs.rtti,
		Stl:                         compilerAttrs.stl,
		Cpp_std:                     compilerAttrs.cppStd,
		C_std:                       compilerAttrs.cStd,

		Additional_linker_inputs: linkerAttrs.additionalLinkerInputs,

@@ -2420,6 +2422,7 @@ func ccSharedOrStaticBp2BuildMutatorInternal(ctx android.TopDownMutatorContext,
			Rtti:                   compilerAttrs.rtti,
			Stl:                    compilerAttrs.stl,
			Cpp_std:                compilerAttrs.cppStd,
			C_std:                  compilerAttrs.cStd,
			Export_includes:        exportedIncludes.Includes,
			Export_system_includes: exportedIncludes.SystemIncludes,
			Local_includes:         compilerAttrs.localIncludes,
@@ -2445,6 +2448,7 @@ func ccSharedOrStaticBp2BuildMutatorInternal(ctx android.TopDownMutatorContext,
			Rtti:       compilerAttrs.rtti,
			Stl:        compilerAttrs.stl,
			Cpp_std:    compilerAttrs.cppStd,
			C_std:      compilerAttrs.cStd,

			Export_includes:          exportedIncludes.Includes,
			Export_system_includes:   exportedIncludes.SystemIncludes,
@@ -2480,6 +2484,7 @@ type bazelCcLibraryStaticAttributes struct {
	Rtti       bazel.BoolAttribute
	Stl        *string
	Cpp_std    *string
	C_std      *string

	Export_includes        bazel.StringListAttribute
	Export_system_includes bazel.StringListAttribute
@@ -2516,6 +2521,7 @@ type bazelCcLibrarySharedAttributes struct {
	Rtti       bazel.BoolAttribute
	Stl        *string
	Cpp_std    *string
	C_std      *string

	Export_includes        bazel.StringListAttribute
	Export_system_includes bazel.StringListAttribute