versioner: autodetect paths when no specified.

Search for the header/dependency/platform directories in a hard-coded
path relative to $ANDROID_BUILD_TOP when they're not specified.

Change-Id: I476385cfc0247e3b2009348ec37c1810a0e9a7f7
diff --git a/tools/versioner/src/versioner.cpp b/tools/versioner/src/versioner.cpp
index 5068a4e..49cbc9f 100644
--- a/tools/versioner/src/versioner.cpp
+++ b/tools/versioner/src/versioner.cpp
@@ -477,6 +477,7 @@
     exit(1);
   } else {
     fprintf(stderr, "Version headers at HEADER_PATH, with DEPS_PATH/ARCH/* on the include path\n");
+    fprintf(stderr, "Autodetects paths if HEADER_PATH and DEPS_PATH are not specified\n");
     fprintf(stderr, "\n");
     fprintf(stderr, "Target specification (defaults to all):\n");
     fprintf(stderr, "  -a API_LEVEL\tbuild with specified API level (can be repeated)\n");
@@ -559,10 +560,35 @@
     }
   }
 
-  if (argc - optind > 2 || optind >= argc) {
+  if (argc - optind > 2 || optind > argc) {
     usage();
   }
 
+  std::string header_dir;
+  std::string dependency_dir;
+
+  if (optind == argc) {
+    // Neither HEADER_PATH nor DEPS_PATH were specified, so try to figure them out.
+    const char* top = getenv("ANDROID_BUILD_TOP");
+    if (!top) {
+      fprintf(stderr, "versioner: failed to autodetect bionic paths. Is ANDROID_BUILD_TOP set?\n");
+      usage();
+    }
+
+    std::string versioner_dir = std::to_string(top) + "/bionic/tools/versioner";
+    header_dir = versioner_dir + "/current";
+    dependency_dir = versioner_dir + "/dependencies";
+    if (platform_dir.empty()) {
+      platform_dir = versioner_dir + "/platforms";
+    }
+  } else {
+    header_dir = argv[optind];
+
+    if (argc - optind == 2) {
+      dependency_dir = argv[optind + 1];
+    }
+  }
+
   if (selected_levels.empty()) {
     selected_levels = supported_levels;
   }
@@ -571,14 +597,12 @@
     selected_architectures = supported_archs;
   }
 
-  std::string dependencies = (argc - optind == 2) ? argv[optind + 1] : "";
-  const char* header_dir = argv[optind];
 
   struct stat st;
-  if (stat(header_dir, &st) != 0) {
-    err(1, "failed to stat '%s'", header_dir);
+  if (stat(header_dir.c_str(), &st) != 0) {
+    err(1, "failed to stat '%s'", header_dir.c_str());
   } else if (!S_ISDIR(st.st_mode)) {
-    errx(1, "'%s' is not a directory", header_dir);
+    errx(1, "'%s' is not a directory", header_dir.c_str());
   }
 
   std::set<CompilationType> compilation_types;
@@ -594,7 +618,7 @@
   }
 
   bool failed = false;
-  declaration_database = compileHeaders(compilation_types, header_dir, dependencies, &failed);
+  declaration_database = compileHeaders(compilation_types, header_dir, dependency_dir, &failed);
 
   if (!sanityCheck(compilation_types, declaration_database)) {
     printf("versioner: sanity check failed\n");