Output correct dependency file with only output dir

There are three ways aidl can decide on the path to write
generated java:
  - A literal output path
  - Writing output next to the input (no output directory given)
  - Writing output to an output directory

Fix a bug where we were writing an incorrect dependency file in the
last case, where we are given an output directory, but no output path.

Bug: 28091660
Change-Id: I7fa743c6f32292e44439b7dc3ce7e49421b93d05
Test: added a unittest
diff --git a/aidl.cpp b/aidl.cpp
index c60626f..d7883b9 100644
--- a/aidl.cpp
+++ b/aidl.cpp
@@ -275,7 +275,8 @@
 
 bool write_java_dep_file(const JavaOptions& options,
                          const vector<unique_ptr<AidlImport>>& imports,
-                         const IoDelegate& io_delegate) {
+                         const IoDelegate& io_delegate,
+                         const string& output_file_name) {
   string dep_file_name = options.DependencyFilePath();
   if (dep_file_name.empty()) {
     return true;  // nothing to do
@@ -293,7 +294,7 @@
     }
   }
 
-  write_common_dep_file(options.output_file_name_, source_aidl, writer.get());
+  write_common_dep_file(output_file_name, source_aidl, writer.get());
 
   return true;
 }
@@ -707,7 +708,7 @@
     return 1;
   }
 
-  if (!write_java_dep_file(options, imports, io_delegate)) {
+  if (!write_java_dep_file(options, imports, io_delegate, output_file_name)) {
     return 1;
   }
 
diff --git a/aidl_unittest.cpp b/aidl_unittest.cpp
index 9421dc9..01a16e1 100644
--- a/aidl_unittest.cpp
+++ b/aidl_unittest.cpp
@@ -39,6 +39,16 @@
 
 namespace android {
 namespace aidl {
+namespace {
+
+const char kExpectedDepFileContents[] =
+R"(place/for/output/p/IFoo.java : \
+  p/IFoo.aidl
+
+p/IFoo.aidl :
+)";
+
+}  // namespace
 
 class AidlTest : public ::testing::Test {
  protected:
@@ -273,5 +283,22 @@
   EXPECT_EQ("p.Bar", java_type->InstantiableName());
 }
 
+TEST_F(AidlTest, WritesCorrectDependencyFile) {
+  // While the in tree build system always gives us an output file name,
+  // other android tools take advantage of our ability to infer the intended
+  // file name.  This test makes sure we handle this correctly.
+  JavaOptions options;
+  options.input_file_name_ = "p/IFoo.aidl";
+  options.output_base_folder_ = "place/for/output";
+  options.dep_file_name_ = "dep/file/path";
+  io_delegate_.SetFileContents(options.input_file_name_,
+                               "package p; interface IFoo {}");
+  EXPECT_EQ(0, ::android::aidl::compile_aidl_to_java(options, io_delegate_));
+  string actual_dep_file_contents;
+  EXPECT_TRUE(io_delegate_.GetWrittenContents(options.dep_file_name_,
+                                              &actual_dep_file_contents));
+  EXPECT_EQ(actual_dep_file_contents, kExpectedDepFileContents);
+}
+
 }  // namespace aidl
 }  // namespace android
diff --git a/options.h b/options.h
index cb8c4af..dd64f7e 100644
--- a/options.h
+++ b/options.h
@@ -61,6 +61,7 @@
   FRIEND_TEST(EndToEndTest, IExampleInterface);
   FRIEND_TEST(AidlTest, FailOnParcelable);
   FRIEND_TEST(AidlTest, WritePreprocessedFile);
+  FRIEND_TEST(AidlTest, WritesCorrectDependencyFile);
   DISALLOW_COPY_AND_ASSIGN(JavaOptions);
 };