Generate genlex rules from bp2build for cc targets

This change will cause bp2build to generate genlex targets any
time a .l or .ll file is present in the srcs for a cc target and
add those genlex targets to the srcs attribute of the original
target.

Bug: 207408632
Test: unit tests
Change-Id: I1bce82c9d3c3d458eae1cef547ffae3d6e975134
diff --git a/cc/gen.go b/cc/gen.go
index 8f62363..08b49c9 100644
--- a/cc/gen.go
+++ b/cc/gen.go
@@ -18,6 +18,7 @@
 	"path/filepath"
 	"strings"
 
+	"android/soong/bazel"
 	"github.com/google/blueprint"
 
 	"android/soong/android"
@@ -169,6 +170,41 @@
 	})
 }
 
+type LexAttrs struct {
+	Srcs    bazel.LabelListAttribute
+	Lexopts bazel.StringListAttribute
+}
+
+type LexNames struct {
+	cSrcName bazel.LabelAttribute
+	srcName  bazel.LabelAttribute
+}
+
+func bp2BuildLex(ctx android.Bp2buildMutatorContext, moduleName string, ca compilerAttributes) LexNames {
+	names := LexNames{}
+	if !ca.lSrcs.IsEmpty() {
+		names.cSrcName = createLexTargetModule(ctx, moduleName+"_genlex_l", ca.lSrcs, ca.lexopts)
+	}
+	if !ca.llSrcs.IsEmpty() {
+		names.srcName = createLexTargetModule(ctx, moduleName+"_genlex_ll", ca.llSrcs, ca.lexopts)
+	}
+	return names
+}
+
+func createLexTargetModule(ctx android.Bp2buildMutatorContext, name string, srcs bazel.LabelListAttribute, opts bazel.StringListAttribute) bazel.LabelAttribute {
+	ctx.CreateBazelTargetModule(
+		bazel.BazelTargetModuleProperties{
+			Rule_class:        "genlex",
+			Bzl_load_location: "//build/bazel/rules/cc:flex.bzl",
+		},
+		android.CommonAttributes{Name: name},
+		&LexAttrs{
+			Srcs:    srcs,
+			Lexopts: opts,
+		})
+	return bazel.LabelAttribute{Value: &bazel.Label{Label: ":" + name}}
+}
+
 func genSysprop(ctx android.ModuleContext, syspropFile android.Path) (android.Path, android.Paths) {
 	headerFile := android.PathForModuleGen(ctx, "sysprop", "include", syspropFile.Rel()+".h")
 	publicHeaderFile := android.PathForModuleGen(ctx, "sysprop/public", "include", syspropFile.Rel()+".h")