#!/usr/bin/env python

# Copyright (c) 2012 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.

# This script is wrapper for Chromium that adds some support for how GYP
# is invoked by Chromium beyond what can be done in the gclient hooks.

import glob
import gyp_helper
import os
import pipes
import shlex
import subprocess
import string
import sys

script_dir = os.path.dirname(os.path.realpath(__file__))
chrome_src = os.path.abspath(os.path.join(script_dir, os.pardir))

sys.path.insert(0, os.path.join(chrome_src, 'tools', 'gyp', 'pylib'))
import gyp

# Assume this file is in a one-level-deep subdirectory of the source root.
SRC_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Add paths so that pymod_do_main(...) can import files.
sys.path.insert(1, os.path.join(chrome_src, 'tools', 'generate_shim_headers'))
sys.path.insert(1, os.path.join(chrome_src, 'tools', 'grit'))
sys.path.insert(1, os.path.join(chrome_src, 'chrome', 'tools', 'build'))
sys.path.insert(1, os.path.join(chrome_src, 'native_client', 'build'))
sys.path.insert(1, os.path.join(chrome_src, 'native_client_sdk', 'src',
    'build_tools'))
sys.path.insert(1, os.path.join(chrome_src, 'remoting', 'tools', 'build'))
sys.path.insert(1, os.path.join(chrome_src, 'third_party', 'liblouis'))
sys.path.insert(1, os.path.join(chrome_src, 'third_party', 'WebKit',
    'Source', 'build', 'scripts'))

# On Windows, Psyco shortens warm runs of build/gyp_chromium by about
# 20 seconds on a z600 machine with 12 GB of RAM, from 90 down to 70
# seconds.  Conversely, memory usage of build/gyp_chromium with Psyco
# maxes out at about 158 MB vs. 132 MB without it.
#
# Psyco uses native libraries, so we need to load a different
# installation depending on which OS we are running under. It has not
# been tested whether using Psyco on our Mac and Linux builds is worth
# it (the GYP running time is a lot shorter, so the JIT startup cost
# may not be worth it).
if sys.platform == 'win32':
  try:
    sys.path.insert(0, os.path.join(chrome_src, 'third_party', 'psyco_win32'))
    import psyco
  except:
    psyco = None
else:
  psyco = None


def GetSupplementalFiles():
  """Returns a list of the supplemental files that are included in all GYP
  sources."""
  return glob.glob(os.path.join(chrome_src, '*', 'supplement.gypi'))


def FormatKeyForGN(key):
  """Returns the given GYP key reformatted for GN.

  GYP dictionary keys can be almost anything, but in GN they are identifiers
  and must follow the same rules. This reformats such keys to be valid GN
  identifiers."""
  return ''.join([c if c in string.ascii_letters else '_' for c in key])


def EscapeStringForGN(s):
  """Converts a string to a GN string literal."""
  # Escape $ characters which have special meaning to GN.
  return '"' + s.replace('$', '\\$').replace('"', '\\"') + '"'


def GetGypVarsForGN(supplemental_files):
  """Returns a dictionary of all GYP vars that we will be passing to GN."""
  vars_dict = {}

  for supplement in supplemental_files:
    with open(supplement, 'r') as f:
      try:
        file_data = eval(f.read(), {'__builtins__': None}, None)
      except SyntaxError, e:
        e.filename = os.path.abspath(supplement)
        raise
      variables = file_data.get('variables', [])
      for v in variables:
        vars_dict[FormatKeyForGN(v)] = EscapeStringForGN(str(variables[v]))

  env_string = os.environ.get('GYP_DEFINES', '')
  items = shlex.split(env_string)
  for item in items:
    tokens = item.split('=', 1)
    # Some GYP variables have hyphens, which we don't support.
    key = FormatKeyForGN(tokens[0])
    if len(tokens) == 2:
      vars_dict[key] = tokens[1]
    else:
      # No value supplied, treat it as a boolean and set it.
      vars_dict[key] = 'true'

  return vars_dict


def GetArgsStringForGN(supplemental_files):
  """Returns the args to pass to GN.
  Based on a subset of the GYP variables that have been rewritten a bit."""

  vars_dict = GetGypVarsForGN(supplemental_files)
  gn_args = ''

  # These tuples of (key, value, gn_arg_string) use the gn_arg_string for
  # gn when the key is set to the given value in the GYP arguments.
  remap_cases = [
      ('branding', 'Chrome', 'is_chrome_branded=true'),
      ('buildtype', 'Official', 'is_official_build=true'),
      ('component', 'shared_library', 'is_component_build=true'),
  ]
  for i in remap_cases:
    if i[0] in vars_dict and vars_dict[i[0]] == i[1]:
      gn_args += ' ' + i[2]

  # These string arguments get passed directly.
  for v in ['windows_sdk_path']:
    if v in vars_dict:
      gn_args += ' ' + v + '=' + EscapeStringForGN(vars_dict[v])

  # Set the GYP flag so BUILD files know they're being invoked in GYP mode.
  gn_args += ' is_gyp=true'
  return gn_args.strip()


def additional_include_files(supplemental_files, args=[]):
  """
  Returns a list of additional (.gypi) files to include, without duplicating
  ones that are already specified on the command line. The list of supplemental
  include files is passed in as an argument.
  """
  # Determine the include files specified on the command line.
  # This doesn't cover all the different option formats you can use,
  # but it's mainly intended to avoid duplicating flags on the automatic
  # makefile regeneration which only uses this format.
  specified_includes = set()
  for arg in args:
    if arg.startswith('-I') and len(arg) > 2:
      specified_includes.add(os.path.realpath(arg[2:]))

  result = []
  def AddInclude(path):
    if os.path.realpath(path) not in specified_includes:
      result.append(path)

  # Always include common.gypi.
  AddInclude(os.path.join(script_dir, 'common.gypi'))

  # Optionally add supplemental .gypi files if present.
  for supplement in supplemental_files:
    AddInclude(supplement)

  return result


def RunGN(supplemental_includes):
  """Runs GN, returning True if it succeeded, printing an error and returning
  false if not."""

  # The binaries in platform-specific subdirectories in src/tools/gn/bin.
  gnpath = SRC_DIR + '/tools/gn/bin/'
  if sys.platform in ('cygwin', 'win32'):
    gnpath += 'win/gn.exe'
  elif sys.platform.startswith('linux'):
    # On Linux we have 32-bit and 64-bit versions.
    if subprocess.check_output(["getconf", "LONG_BIT"]).find("64") >= 0:
      gnpath += 'linux/gn'
    else:
      gnpath += 'linux/gn32'
  elif sys.platform == 'darwin':
    gnpath += 'mac/gn'
  else:
    print 'Unknown platform for GN: ', sys.platform
    return False

  print 'Generating gyp files from GN...'

  # Need to pass both the source root (the bots don't run this command from
  # within the source tree) as well as set the is_gyp value so the BUILD files
  # to know they're being run under GYP.
  args = [gnpath, 'gyp', '-q',
          '--root=' + chrome_src,
          '--args=' + GetArgsStringForGN(supplemental_includes)]
  return subprocess.call(args) == 0


if __name__ == '__main__':
  args = sys.argv[1:]

  if int(os.environ.get('GYP_CHROMIUM_NO_ACTION', 0)):
    print 'Skipping gyp_chromium due to GYP_CHROMIUM_NO_ACTION env var.'
    sys.exit(0)

  # Use the Psyco JIT if available.
  if psyco:
    psyco.profile()
    print "Enabled Psyco JIT."

  # Fall back on hermetic python if we happen to get run under cygwin.
  # TODO(bradnelson): take this out once this issue is fixed:
  #    http://code.google.com/p/gyp/issues/detail?id=177
  if sys.platform == 'cygwin':
    python_dir = os.path.join(chrome_src, 'third_party', 'python_26')
    env = os.environ.copy()
    env['PATH'] = python_dir + os.pathsep + env.get('PATH', '')
    p = subprocess.Popen(
       [os.path.join(python_dir, 'python.exe')] + sys.argv,
       env=env, shell=False)
    p.communicate()
    sys.exit(p.returncode)

  gyp_helper.apply_chromium_gyp_env()

  # This could give false positives since it doesn't actually do real option
  # parsing.  Oh well.
  gyp_file_specified = False
  for arg in args:
    if arg.endswith('.gyp'):
      gyp_file_specified = True
      break

  # If we didn't get a file, check an env var, and then fall back to
  # assuming 'all.gyp' from the same directory as the script.
  if not gyp_file_specified:
    gyp_file = os.environ.get('CHROMIUM_GYP_FILE')
    if gyp_file:
      # Note that CHROMIUM_GYP_FILE values can't have backslashes as
      # path separators even on Windows due to the use of shlex.split().
      args.extend(shlex.split(gyp_file))
    else:
      args.append(os.path.join(script_dir, 'all.gyp'))

  supplemental_includes = GetSupplementalFiles()

  if not RunGN(supplemental_includes):
    sys.exit(1)

  args.extend(
      ['-I' + i for i in additional_include_files(supplemental_includes, args)])

  # There shouldn't be a circular dependency relationship between .gyp files,
  # but in Chromium's .gyp files, on non-Mac platforms, circular relationships
  # currently exist.  The check for circular dependencies is currently
  # bypassed on other platforms, but is left enabled on the Mac, where a
  # violation of the rule causes Xcode to misbehave badly.
  # TODO(mark): Find and kill remaining circular dependencies, and remove this
  # option.  http://crbug.com/35878.
  # TODO(tc): Fix circular dependencies in ChromiumOS then add linux2 to the
  # list.
  if sys.platform not in ('darwin',):
    args.append('--no-circular-check')

  # Default to ninja on linux, but only if no generator has explicitly been set.
  # Also default to ninja on mac, but only when not building chrome/ios.
  # . -f / --format has precedence over the env var, no need to check for it
  # . set the env var only if it hasn't been set yet
  # . chromium.gyp_env has been applied to os.environ at this point already
  if sys.platform.startswith('linux') and not os.environ.get('GYP_GENERATORS'):
    os.environ['GYP_GENERATORS'] = 'ninja'
  elif sys.platform == 'darwin' and not os.environ.get('GYP_GENERATORS') and \
      not 'OS=ios' in os.environ.get('GYP_DEFINES', []):
    os.environ['GYP_GENERATORS'] = 'ninja'

  # If using ninja on windows, and not opting out of the the automatic
  # toolchain, then set up variables for the automatic toolchain. Opt-out is
  # on by default, for now.
  if (sys.platform in ('win32', 'cygwin') and
      os.environ.get('GYP_GENERATORS') == 'ninja' and
      os.environ.get('GYP_MSVS_USE_SYSTEM_TOOLCHAIN', '1') != '1'):
    # For now, call the acquisition script here so that there's only one
    # opt-in step required. This will be moved to a separate DEPS step once
    # it's on by default.
    subprocess.check_call([
        sys.executable,
        os.path.normpath(os.path.join(script_dir, '..', 'tools', 'win',
                                      'toolchain',
                                      'get_toolchain_if_necessary.py'))])
    toolchain = os.path.normpath(os.path.join(
        script_dir, '..', 'third_party', 'win_toolchain', 'files'))
    os.environ['GYP_MSVS_OVERRIDE_PATH'] = toolchain
    os.environ['GYP_MSVS_VERSION'] = '2013'
    # We need to make sure windows_sdk_path is set to the automated toolchain
    # values in GYP_DEFINES, but don't want to override any other values there.
    gyp_defines_dict = gyp.NameValueListToDict(gyp.ShlexEnv('GYP_DEFINES'))
    win8sdk = os.path.join(toolchain, 'win8sdk')
    gyp_defines_dict['windows_sdk_path'] = win8sdk
    os.environ['WINDOWSSDKDIR'] = win8sdk
    os.environ['GYP_DEFINES'] = ' '.join('%s=%s' % (k, pipes.quote(str(v)))
        for k, v in gyp_defines_dict.iteritems())
    # Include the VS runtime in the PATH in case it's not machine-installed.
    runtime_path = ';'.join(
        os.path.normpath(os.path.join(
            script_dir, '..', 'third_party', 'win_toolchain', 'files', s))
        for s in ('sys64', 'sys32'))
    os.environ['PATH'] = runtime_path + os.environ['PATH']
    print('Using automatic toolchain in %s.' % toolchain)

  # If CHROMIUM_GYP_SYNTAX_CHECK is set to 1, it will invoke gyp with --check
  # to enfore syntax checking.
  syntax_check = os.environ.get('CHROMIUM_GYP_SYNTAX_CHECK')
  if syntax_check and int(syntax_check):
    args.append('--check')

  print 'Updating projects from gyp files...'
  sys.stdout.flush()

  # Off we go...
  sys.exit(gyp.main(args))