Merge "interceptor_analysis supports --relative option."
diff --git a/analysis.cc b/analysis.cc
index 6420f7b..4e1d790 100644
--- a/analysis.cc
+++ b/analysis.cc
@@ -41,9 +41,9 @@
   fs::path command_log;
   OutputFormat output_format = OutputFormat::TEXT;
   fs::path output;
-
   // options for compdb
   bool compdb_use_commands = false;
+  bool compdb_relative = false;
 };
 
 static Options parse_arguments(int argc, char* argv[]) {
@@ -53,18 +53,23 @@
       {"command-log", required_argument, nullptr, 'l'},
       {"output-type", required_argument, nullptr, 't'},
       {"output", required_argument, nullptr, 'o'},
+      {"relative", no_argument, nullptr, 'r'},
       {nullptr, 0, nullptr, 0},
   };
   const auto usage = [&]() {
     std::cerr << "usage: " << argv[0] << '\n'
               << "  -l|--command-log filename\n"
               << "  -o|--output filename\n"
-              << " [-t|--output-type (text|compdb|compdb_commands)]\n";
+              << " [-t|--output-type (text|compdb|compdb_commands)]\n"
+              << " [-r|--relative]\n"
+              << "\n"
+              << "-r is used with -t compdb. If -r is specified, directories\n"
+              << "are relative to the root_directory.";
     exit(EX_USAGE);
   };
   while (true) {
     int ix;
-    int c = getopt_long(argc, argv, "-l:t:o:", opts, &ix);
+    int c = getopt_long(argc, argv, "-l:t:o:r", opts, &ix);
     if (c == -1) {
       break;
     }
@@ -87,6 +92,9 @@
       case 'o':
         result.output = fs::absolute(optarg);
         break;
+      case 'r':
+        result.compdb_relative = true;
+        break;
       default:
         usage();
     }
@@ -129,7 +137,8 @@
   }
 }
 
-void compdb_to_file(const interceptor::Log& log, const fs::path& output, bool use_commands) {
+void compdb_to_file(const interceptor::Log& log, const fs::path& output,
+                    const Options& options) {
   static const std::unordered_set<std::string_view> kCompileExtensions = {
       ".c", ".cc", ".cpp", ".cxx", ".S",
   };
@@ -181,12 +190,18 @@
       // ok, now we have a new command
       auto& compile_command = *compdb.add_commands();
 
-      compile_command.set_directory(fs::path(log.root_directory()) / command.current_directory());
+      fs::path directory;
+      if (options.compdb_relative) {
+        directory = command.current_directory();
+      } else {
+        directory = fs::path(log.root_directory()) / command.current_directory();
+      }
+      compile_command.set_directory(directory);
       compile_command.set_file(input);
       if (!single_output.empty()) {
         compile_command.set_output(single_output);
       }
-      if (use_commands) {
+      if (options.compdb_use_commands) {
         compile_command.set_command(interceptor::command_line(command));
       } else {
         *compile_command.mutable_arguments() = {command.arguments().cbegin(),
@@ -203,9 +218,9 @@
   }
 
   std::string out_str;
-  auto options = google::protobuf::util::JsonPrintOptions{};
-  options.add_whitespace = true;
-  google::protobuf::util::MessageToJsonString(compdb, &out_str, options);
+  auto json_options = google::protobuf::util::JsonPrintOptions{};
+  json_options.add_whitespace = true;
+  google::protobuf::util::MessageToJsonString(compdb, &out_str, json_options);
 
   // this would emit {"command":[yadayada]}, but we want only [yadayada]
   // the additional characters come from options.add_whitespace
@@ -229,7 +244,7 @@
       text_to_file(log, options.output);
       break;
     case OutputFormat::COMPDB:
-      compdb_to_file(log, options.output, options.compdb_use_commands);
+      compdb_to_file(log, options.output, options);
       break;
   }
 }