Merge "droiddoc accepts aidl files as inputs"
diff --git a/java/droiddoc.go b/java/droiddoc.go
index b34c393..cda50f0 100644
--- a/java/droiddoc.go
+++ b/java/droiddoc.go
@@ -110,6 +110,14 @@
 
 	// if not blank, set to the version of the sdk to compile against
 	Sdk_version *string `android:"arch_variant"`
+
+	Aidl struct {
+		// Top level directories to pass to aidl tool
+		Include_dirs []string
+
+		// Directories rooted at the Android.bp file to pass to aidl tool
+		Local_include_dirs []string
+	}
 }
 
 type ApiToCheck struct {
@@ -310,6 +318,60 @@
 	}
 }
 
+func (j *Javadoc) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
+	var flags javaBuilderFlags
+
+	// aidl flags.
+	aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
+	if len(aidlFlags) > 0 {
+		// optimization.
+		ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
+		flags.aidlFlags = "$aidlFlags"
+	}
+
+	return flags
+}
+
+func (j *Javadoc) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
+	aidlIncludeDirs android.Paths) []string {
+
+	aidlIncludes := android.PathsForModuleSrc(ctx, j.properties.Aidl.Local_include_dirs)
+	aidlIncludes = append(aidlIncludes, android.PathsForSource(ctx, j.properties.Aidl.Include_dirs)...)
+
+	var flags []string
+	if aidlPreprocess.Valid() {
+		flags = append(flags, "-p"+aidlPreprocess.String())
+	} else {
+		flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
+	}
+
+	flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
+	flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
+	if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
+		flags = append(flags, "-I"+src.String())
+	}
+
+	return flags
+}
+
+func (j *Javadoc) genSources(ctx android.ModuleContext, srcFiles android.Paths,
+	flags javaBuilderFlags) android.Paths {
+
+	outSrcFiles := make(android.Paths, 0, len(srcFiles))
+
+	for _, srcFile := range srcFiles {
+		switch srcFile.Ext() {
+		case ".aidl":
+			javaFile := genAidl(ctx, srcFile, flags.aidlFlags)
+			outSrcFiles = append(outSrcFiles, javaFile)
+		default:
+			outSrcFiles = append(outSrcFiles, srcFile)
+		}
+	}
+
+	return outSrcFiles
+}
+
 func (j *Javadoc) collectDeps(ctx android.ModuleContext) deps {
 	var deps deps
 
@@ -388,6 +450,8 @@
 	// do not pass exclude_srcs directly when expanding srcFiles since exclude_srcs
 	// may contain filegroup or genrule.
 	srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
+	flags := j.collectBuilderFlags(ctx, deps)
+	srcFiles = j.genSources(ctx, srcFiles, flags)
 
 	// srcs may depend on some genrule output.
 	j.srcJars = srcFiles.FilterByExt(".srcjar")
diff --git a/java/java_test.go b/java/java_test.go
index fe1c3d7..baf4b72 100644
--- a/java/java_test.go
+++ b/java/java_test.go
@@ -200,6 +200,7 @@
 
 		"bar-doc/a.java":                 nil,
 		"bar-doc/b.java":                 nil,
+		"bar-doc/IFoo.aidl":              nil,
 		"bar-doc/known_oj_tags.txt":      nil,
 		"external/doclava/templates-sdk": nil,
 
@@ -921,6 +922,7 @@
 		    name: "bar-doc",
 		    srcs: [
 		        "bar-doc/*.java",
+		        "bar-doc/IFoo.aidl",
 		    ],
 		    exclude_srcs: [
 		        "bar-doc/b.java"
@@ -943,6 +945,14 @@
 	if stubsJar != barDoc.Output.String() {
 		t.Errorf("expected stubs Jar [%q], got %q", stubsJar, barDoc.Output.String())
 	}
+	inputs := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc").Inputs
+	var javaSrcs []string
+	for _, i := range inputs {
+		javaSrcs = append(javaSrcs, i.Base())
+	}
+	if len(javaSrcs) != 2 || javaSrcs[0] != "a.java" || javaSrcs[1] != "IFoo.java" {
+		t.Errorf("inputs of bar-doc must be []string{\"a.java\", \"IFoo.java\", but was %#v.", javaSrcs)
+	}
 }
 
 func TestJarGenrules(t *testing.T) {