#!/usr/bin/env python
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Produces a dot file showing dependency relationships between modules.

The dot file contains a text-based representation of a directed graph that
explains why given module names were included in a trace_viewer config.

Example usage:
$ ./why_imported ui.analysis.analysis_view > ~/analysis_view.dot

This can then be converted to a graphical representation with the dot tool:
$ dot -Grankdir=LR -Tpng ~/analysis_view.dot -o ~/analysis_view.png
"""

import sys
import optparse

from tracing import tracing_project

def Main(args):
  project = tracing_project.TracingProject()

  parser = optparse.OptionParser(
      usage='%prog <options> moduleNames', epilog=__doc__)
  parser.add_option(
      '--config', type='choice', choices=project.GetConfigNames())
  options, args = parser.parse_args(args)

  if options.config:
    names = ['tracing',
             project.GetModuleNameForConfigName(options.config)]
    load_sequence = project.CalcLoadSequenceForModuleNames(names)
  else:
    parser.parse_err('Unsupported')
  print project.GetDominatorGraphForModulesNamed(args, load_sequence)


if __name__ == '__main__':
  sys.exit(Main(sys.argv[1:]))