Add filename_from_src property to prebuilt_etc

Base name of the output file of a prebuilt_etc is by default the module
name. This default behavior can be customized by either via the
'filename' property or the new 'filename_from_src' property. The former
explicitly sets the base name while the latter makes the file name to be
that of the source file.

Test: m (prebuilt_etc_test added)
Change-Id: Ic2900417bda62993f6de2612d993234b82b74479
diff --git a/android/prebuilt_etc.go b/android/prebuilt_etc.go
index 83c38db..f7d08cc 100644
--- a/android/prebuilt_etc.go
+++ b/android/prebuilt_etc.go
@@ -40,6 +40,10 @@
 	// optional name for the installed file. If unspecified, name of the module is used as the file name
 	Filename *string `android:"arch_variant"`
 
+	// when set to true, and filename property is not set, the name for the installed file
+	// is the same as the file name of the source file.
+	Filename_from_src *bool `android:"arch_variant"`
+
 	// Make this module available when building for recovery.
 	Recovery_available *bool
 
@@ -106,8 +110,16 @@
 func (p *PrebuiltEtc) GenerateAndroidBuildActions(ctx ModuleContext) {
 	p.sourceFilePath = ctx.ExpandSource(String(p.properties.Src), "src")
 	filename := String(p.properties.Filename)
+	filename_from_src := Bool(p.properties.Filename_from_src)
 	if filename == "" {
-		filename = ctx.ModuleName()
+		if filename_from_src {
+			filename = p.sourceFilePath.Base()
+		} else {
+			filename = ctx.ModuleName()
+		}
+	} else if filename_from_src {
+		ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
+		return
 	}
 	p.outputFilePath = PathForModuleOut(ctx, filename).OutputPath
 	p.installDirPath = PathForModuleInstall(ctx, "etc", String(p.properties.Sub_dir))
diff --git a/android/prebuilt_etc_test.go b/android/prebuilt_etc_test.go
index 1ecdf79..27736ba 100644
--- a/android/prebuilt_etc_test.go
+++ b/android/prebuilt_etc_test.go
@@ -106,3 +106,27 @@
 		t.Errorf("expected foo.installed.conf, got %q", p.outputFilePath.Base())
 	}
 }
+
+func TestPrebuiltEtcGlob(t *testing.T) {
+	ctx := testPrebuiltEtc(t, `
+		prebuilt_etc {
+			name: "my_foo",
+			src: "foo.*",
+		}
+		prebuilt_etc {
+			name: "my_bar",
+			src: "bar.*",
+			filename_from_src: true,
+		}
+	`)
+
+	p := ctx.ModuleForTests("my_foo", "android_common_core").Module().(*PrebuiltEtc)
+	if p.outputFilePath.Base() != "my_foo" {
+		t.Errorf("expected my_foo, got %q", p.outputFilePath.Base())
+	}
+
+	p = ctx.ModuleForTests("my_bar", "android_common_core").Module().(*PrebuiltEtc)
+	if p.outputFilePath.Base() != "bar.conf" {
+		t.Errorf("expected bar.conf, got %q", p.outputFilePath.Base())
+	}
+}