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

Commit df4c2134 authored by Spandan Das's avatar Spandan Das
Browse files

bp2build converter for cc libraries containing yacc

For cc_libraries containing .y/.yy srcs, we will generate an additional
cc_yacc_static_library target. This target will produce an .a file that
will be statically linked to the parent target.

Test: go test ./bp2build
Bug: 281546029
Change-Id: I094ba56f0a95869e5bc1e1d38b83c777192b7ddd
parent 9241da96
Loading
Loading
Loading
Loading
+56 −0
Original line number Diff line number Diff line
@@ -4523,3 +4523,59 @@ func TestCcLibraryCppFlagsInProductVariables(t *testing.T) {
	},
	)
}

func TestCcLibraryYaccConversion(t *testing.T) {
	runCcLibraryTestCase(t, Bp2buildTestCase{
		Description:                "cc_library is built from .y/.yy files",
		ModuleTypeUnderTest:        "cc_library",
		ModuleTypeUnderTestFactory: cc.LibraryFactory,
		Blueprint: soongCcLibraryPreamble + `cc_library {
    name: "a",
    srcs: [
	"a.cpp",
	"a.yy",
    ],
    shared_libs: ["sharedlib"],
    static_libs: ["staticlib"],
    yacc: {
	    flags: ["someYaccFlag"],
	    gen_location_hh: true,
	    gen_position_hh: true,
	},
}
cc_library_static {
	name: "staticlib",
	bazel_module: { bp2build_available: false },
}
cc_library {
	name: "sharedlib",
	bazel_module: { bp2build_available: false },
}
`,
		ExpectedBazelTargets: []string{
			MakeBazelTarget("cc_yacc_static_library", "a_yacc", AttrNameToString{
				"src":                         `"a.yy"`,
				"implementation_deps":         `[":staticlib"]`,
				"implementation_dynamic_deps": `[":sharedlib"]`,
				"flags":                       `["someYaccFlag"]`,
				"gen_location_hh":             "True",
				"gen_position_hh":             "True",
				"local_includes":              `["."]`,
			}),
			MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
				"srcs":                              `["a.cpp"]`,
				"implementation_deps":               `[":staticlib"]`,
				"implementation_dynamic_deps":       `[":sharedlib"]`,
				"implementation_whole_archive_deps": `[":a_yacc"]`,
				"local_includes":                    `["."]`,
			}),
			MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
				"srcs":                              `["a.cpp"]`,
				"implementation_deps":               `[":staticlib"]`,
				"implementation_dynamic_deps":       `[":sharedlib"]`,
				"implementation_whole_archive_deps": `[":a_yacc"]`,
				"local_includes":                    `["."]`,
			}),
		},
	})
}
+67 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ const (
	protoSrcPartition   = "proto"
	aidlSrcPartition    = "aidl"
	syspropSrcPartition = "sysprop"
	yaccSrcPartition    = "yacc"

	stubsSuffix = "_stub_libs_current"
)
@@ -154,6 +155,7 @@ func groupSrcsByExtension(ctx android.BazelConversionPathContext, srcs bazel.Lab
		// know the language of these sources until the genrule is executed.
		cppSrcPartition:     bazel.LabelPartition{Extensions: []string{".cpp", ".cc", ".cxx", ".mm"}, LabelMapper: addSuffixForFilegroup("_cpp_srcs"), Keep_remainder: true},
		syspropSrcPartition: bazel.LabelPartition{Extensions: []string{".sysprop"}},
		yaccSrcPartition:    bazel.LabelPartition{Extensions: []string{".y", "yy"}},
	}

	return bazel.PartitionLabelListAttribute(ctx, &srcs, labels)
@@ -404,6 +406,12 @@ type compilerAttributes struct {
	// Sysprop sources
	syspropSrcs bazel.LabelListAttribute

	// Yacc sources
	yaccSrc               *bazel.LabelAttribute
	yaccFlags             bazel.StringListAttribute
	yaccGenLocationHeader bazel.BoolAttribute
	yaccGenPositionHeader bazel.BoolAttribute

	hdrs bazel.LabelListAttribute

	rtti bazel.BoolAttribute
@@ -566,6 +574,12 @@ func (ca *compilerAttributes) finalize(ctx android.BazelConversionPathContext, i
	ca.asmSrcs = partitionedSrcs[asmSrcPartition]
	ca.lSrcs = partitionedSrcs[lSrcPartition]
	ca.llSrcs = partitionedSrcs[llSrcPartition]
	if yacc := partitionedSrcs[yaccSrcPartition]; !yacc.IsEmpty() {
		if len(yacc.Value.Includes) > 1 {
			ctx.PropertyErrorf("srcs", "Found multiple yacc (.y/.yy) files in library")
		}
		ca.yaccSrc = bazel.MakeLabelAttribute(yacc.Value.Includes[0].Label)
	}
	ca.syspropSrcs = partitionedSrcs[syspropSrcPartition]

	ca.absoluteIncludes.DeduplicateAxesFromBase()
@@ -742,6 +756,11 @@ func bp2BuildParseBaseProps(ctx android.Bp2buildMutatorContext, module *Module)
				if baseCompilerProps.Lex != nil {
					compilerAttrs.lexopts.SetSelectValue(axis, cfg, baseCompilerProps.Lex.Flags)
				}
				if baseCompilerProps.Yacc != nil {
					compilerAttrs.yaccFlags.SetSelectValue(axis, cfg, baseCompilerProps.Yacc.Flags)
					compilerAttrs.yaccGenLocationHeader.SetSelectValue(axis, cfg, baseCompilerProps.Yacc.Gen_location_hh)
					compilerAttrs.yaccGenPositionHeader.SetSelectValue(axis, cfg, baseCompilerProps.Yacc.Gen_position_hh)
				}
				(&compilerAttrs).bp2buildForAxisAndConfig(ctx, axis, cfg, baseCompilerProps)
			}

@@ -826,6 +845,12 @@ func bp2BuildParseBaseProps(ctx android.Bp2buildMutatorContext, module *Module)
		}
	}

	// Create a cc_yacc_static_library if srcs contains .y/.yy files
	// This internal target will produce an .a file that will be statically linked to the parent library
	if yaccDep := bp2buildCcYaccLibrary(ctx, compilerAttrs, linkerAttrs); yaccDep != nil {
		(&linkerAttrs).implementationWholeArchiveDeps.Add(yaccDep)
	}

	convertedLSrcs := bp2BuildLex(ctx, module.Name(), compilerAttrs)
	(&compilerAttrs).srcs.Add(&convertedLSrcs.srcName)
	(&compilerAttrs).cSrcs.Add(&convertedLSrcs.cSrcName)
@@ -864,6 +889,48 @@ func bp2BuildParseBaseProps(ctx android.Bp2buildMutatorContext, module *Module)
	}
}

type ccYaccLibraryAttributes struct {
	Src                         bazel.LabelAttribute
	Flags                       bazel.StringListAttribute
	Gen_location_hh             bazel.BoolAttribute
	Gen_position_hh             bazel.BoolAttribute
	Local_includes              bazel.StringListAttribute
	Implementation_deps         bazel.LabelListAttribute
	Implementation_dynamic_deps bazel.LabelListAttribute
}

func bp2buildCcYaccLibrary(ctx android.Bp2buildMutatorContext, ca compilerAttributes, la linkerAttributes) *bazel.LabelAttribute {
	if ca.yaccSrc == nil {
		return nil
	}
	yaccLibraryLabel := ctx.Module().Name() + "_yacc"
	ctx.CreateBazelTargetModule(
		bazel.BazelTargetModuleProperties{
			Rule_class:        "cc_yacc_static_library",
			Bzl_load_location: "//build/bazel/rules/cc:cc_yacc_library.bzl",
		},
		android.CommonAttributes{
			Name: yaccLibraryLabel,
		},
		&ccYaccLibraryAttributes{
			Src:                         *ca.yaccSrc,
			Flags:                       ca.yaccFlags,
			Gen_location_hh:             ca.yaccGenLocationHeader,
			Gen_position_hh:             ca.yaccGenPositionHeader,
			Local_includes:              ca.localIncludes,
			Implementation_deps:         la.implementationDeps,
			Implementation_dynamic_deps: la.implementationDynamicDeps,
		},
	)

	yaccLibrary := &bazel.LabelAttribute{
		Value: &bazel.Label{
			Label: ":" + yaccLibraryLabel,
		},
	}
	return yaccLibrary
}

// As a workaround for b/261657184, we are manually adding the default value
// of system_dynamic_deps for the linux_musl os.
// TODO: Solve this properly