#!/usr/bin/python

# Copyright (c) 2011 Google Inc. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import collections
import gyp
import gyp.common
import json

generator_wants_static_library_dependencies_adjusted = False

generator_default_variables = {
  'OS': 'linux',
}
for dirname in ['INTERMEDIATE_DIR', 'SHARED_INTERMEDIATE_DIR', 'PRODUCT_DIR',
                'LIB_DIR', 'SHARED_LIB_DIR']:
  # Some gyp steps fail if these are empty(!).
  generator_default_variables[dirname] = 'dir'
for unused in ['RULE_INPUT_PATH', 'RULE_INPUT_ROOT', 'RULE_INPUT_NAME',
               'RULE_INPUT_EXT',
               'EXECUTABLE_PREFIX', 'EXECUTABLE_SUFFIX',
               'STATIC_LIB_PREFIX', 'STATIC_LIB_SUFFIX',
               'SHARED_LIB_PREFIX', 'SHARED_LIB_SUFFIX',
               'LINKER_SUPPORTS_ICF']:
  generator_default_variables[unused] = ''


def CalculateVariables(default_variables, params):
  generator_flags = params.get('generator_flags', {})
  default_variables['OS'] = generator_flags.get('os', 'linux')


def CalculateGeneratorInputInfo(params):
  """Calculate the generator specific info that gets fed to input (called by
  gyp)."""
  generator_flags = params.get('generator_flags', {})
  if generator_flags.get('adjust_static_libraries', False):
    global generator_wants_static_library_dependencies_adjusted
    generator_wants_static_library_dependencies_adjusted = True


def GenerateOutput(target_list, target_dicts, data, params):
  # Map of target -> list of targets it depends on.
  edges = {}

  # Queue of targets to visit.
  targets_to_visit = target_list[:]

  while len(targets_to_visit) > 0:
    target = targets_to_visit.pop()
    if target in edges:
      continue
    edges[target] = []

    for dep in target_dicts[target].get('dependencies', []):
      edges[target].append(dep)
      targets_to_visit.append(dep)

  filename = 'dump.json'
  f = open(filename, 'w')
  json.dump(edges, f)
  f.close()
  print 'Wrote json to %s.' % filename

@@ -228,16 +228,27 @@
 ort json
+%0Aimport sys
 %0A%0Agenera
@@ -339,25 +339,8 @@
 = %7B%0A
-  'OS': 'linux',%0A
 %7D%0Afo
@@ -641,16 +641,38 @@
         
+ 'RULE_INPUT_DIRNAME',
  'RULE_I
@@ -937,16 +937,322 @@
  = ''%0A%0A%0A
+def GetFlavor(params):%0A  %22%22%22Returns %7Cparams.flavor%7C if it's set, the system's default flavor else.%22%22%22%0A  flavors = %7B%0A    'darwin': 'mac',%0A    'sunos5': 'solaris',%0A    'freebsd7': 'freebsd',%0A    'freebsd8': 'freebsd',%0A  %7D%0A  flavor = flavors.get(sys.platform, 'linux')%0A  return params.get('flavor', flavor)%0A%0A%0A
 def Calc
@@ -1402,23 +1402,33 @@
 t('os', 
-'linux'
+GetFlavor(params)
 )%0A%0A%0Adef 

Make dump_dependency_json work on Mac and possibly other OSs. Review URL: http://codereview.chromium.org/8634012