Fix genrule depending on disabled module with ALLOW_MISSING_DEPENDENCIES=true

If a genrule depends on a module that is disabled, in this case because
it is a device module in a host-only build, it can cause panics when
getPathsFromModuleDep retrieves a nil Path from the disabled module.
Treat disabled modules as missing dependencies.

Test: TestGenruleAllowMissingDependencies
Change-Id: I3c689c6b5505b21eaf7ae7cb93c00f96f438ac17
diff --git a/android/paths.go b/android/paths.go
index f648c55..babf48c 100644
--- a/android/paths.go
+++ b/android/paths.go
@@ -511,6 +511,9 @@
 	if module == nil {
 		return nil, missingDependencyError{[]string{moduleName}}
 	}
+	if aModule, ok := module.(Module); ok && !aModule.Enabled() {
+		return nil, missingDependencyError{[]string{moduleName}}
+	}
 	if outProducer, ok := module.(OutputFileProducer); ok {
 		outputFiles, err := outProducer.OutputFiles(tag)
 		if err != nil {
diff --git a/genrule/genrule_test.go b/genrule/genrule_test.go
index bb17e8e..2ee456d 100644
--- a/genrule/genrule_test.go
+++ b/genrule/genrule_test.go
@@ -36,6 +36,7 @@
 	PrepareForTestWithGenRuleBuildComponents,
 	android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
 		ctx.RegisterModuleType("tool", toolFactory)
+		ctx.RegisterModuleType("output", outputProducerFactory)
 	}),
 	android.FixtureMergeMockFs(android.MockFS{
 		"tool":       nil,
@@ -653,6 +654,35 @@
 	android.AssertDeepEquals(t, "srcs", expectedSrcs, gen.properties.Srcs)
 }
 
+func TestGenruleAllowMissingDependencies(t *testing.T) {
+	bp := `
+		output {
+			name: "disabled",
+			enabled: false,
+		}
+
+		genrule {
+			name: "gen",
+			srcs: [
+				":disabled",
+			],
+			out: ["out"],
+			cmd: "cat $(in) > $(out)",
+		}
+       `
+	result := prepareForGenRuleTest.Extend(
+		android.FixtureModifyConfigAndContext(
+			func(config android.Config, ctx *android.TestContext) {
+				config.TestProductVariables.Allow_missing_dependencies = proptools.BoolPtr(true)
+				ctx.SetAllowMissingDependencies(true)
+			})).RunTestWithBp(t, bp)
+
+	gen := result.ModuleForTests("gen", "").Output("out")
+	if gen.Rule != android.ErrorRule {
+		t.Errorf("Expected missing dependency error rule for gen, got %q", gen.Rule.String())
+	}
+}
+
 func TestGenruleWithBazel(t *testing.T) {
 	bp := `
 		genrule {
@@ -697,3 +727,24 @@
 }
 
 var _ android.HostToolProvider = (*testTool)(nil)
+
+type testOutputProducer struct {
+	android.ModuleBase
+	outputFile android.Path
+}
+
+func outputProducerFactory() android.Module {
+	module := &testOutputProducer{}
+	android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
+	return module
+}
+
+func (t *testOutputProducer) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+	t.outputFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "bin"), ctx.ModuleName(), android.PathForOutput(ctx, ctx.ModuleName()))
+}
+
+func (t *testOutputProducer) OutputFiles(tag string) (android.Paths, error) {
+	return android.Paths{t.outputFile}, nil
+}
+
+var _ android.OutputFileProducer = (*testOutputProducer)(nil)