Make soong create sourcepath dirs

Sourcepath is set to dir of the Android.bp, but that dir does not
necessarily have any inputs for the metalava invocation defined in that
Android.bp. This leads to problems when metalava tries to read a
non-existent dir, so create it first.

Bug: 153703940
Test: m sdk dist
Change-Id: I8c2ca45c8f066cabf3247b9652a7094cefc047a9
Merged-In: I8c2ca45c8f066cabf3247b9652a7094cefc047a9
(cherry picked from commit 7eff3a8fc3956e6af9f0a630a87db4892f95ba29)
diff --git a/java/droidstubs.go b/java/droidstubs.go
index 17c7a7b..3607082 100644
--- a/java/droidstubs.go
+++ b/java/droidstubs.go
@@ -397,7 +397,17 @@
 	rule.Command().Text("rm -rf").Flag(homeDir.String())
 	rule.Command().Text("mkdir -p").Flag(homeDir.String())
 
-	cmd := rule.Command()
+	var cmd *android.RuleBuilderCommand
+	if len(sourcepaths) > 0 {
+		// We are passing the sourcepaths as an argument to metalava below, but the directories may
+		// not exist already (if they do not contain any listed inputs for metalava). Note that this
+		// is in a rule.SboxInputs()rule, so we are not modifying the actual source tree by creating
+		// these directories.
+		cmd = rule.Command()
+		cmd.Text("mkdir -p").Flags(cmd.PathsForInputs(sourcepaths))
+	}
+
+	cmd = rule.Command()
 	cmd.FlagWithArg("ANDROID_PREFS_ROOT=", homeDir.String())
 
 	if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_METALAVA") {
@@ -431,6 +441,7 @@
 	}
 
 	if len(sourcepaths) > 0 {
+		// TODO(b/153703940): Pass .html files to metalava and remove this argument.
 		cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
 	} else {
 		cmd.FlagWithArg("-sourcepath ", `""`)