#!/usr/bin/python
# Copyright 2009 The Native Client 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 NaCl that adds some support for how GYP
is invoked by NaCl beyond what can be done it the gclient hooks."""

import glob
import os
import shlex
import sys

SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
GCLIENT_ROOT = os.path.dirname(os.path.dirname(SCRIPT_DIR))
GYP_DIR = os.path.join(GCLIENT_ROOT, 'tools', 'gyp')
sys.path.insert(0, os.path.join(GYP_DIR, 'pylib'))

import gyp

def main():
  print 'Updating projects from gyp files...'
  os.chdir(GCLIENT_ROOT)

  if not os.environ.get('GYP_GENERATORS'):
    os.environ['GYP_GENERATORS'] = 'ninja'

  args = sys.argv[1:]

  # If we didn't get a file, check an env var, and then fall back to
  # assuming 'native_client/build/all.gyp'
  if len(args) == 0:
    args += shlex.split(os.environ.get('NACL_GYP_FILE',
                                       'native_client/build/all.gyp'))

  # Add settings that are only included in the standalone NaCl Gyp
  # build and won't get included when NaCl is built as part of another
  # project, such as Chrome.  configs.gypi includes compiler
  # configuration that arguably should be built into Gyp; Chrome
  # provides its own conflicting version in its common.gypi.
  args += ['-I', 'native_client/build/configs.gypi']
  # Enable warnings that we don't necessarily want to enable for
  # Chrome source that is built as NaCl untrusted code.
  args += ['-I', 'native_client/build/standalone_flags.gypi']

  # Pick depth explicitly.
  args += ['--depth', '.']

  # Building NaCl as standalone (not as part of Chrome)
  args += ['-D', 'nacl_standalone=1']

  # Off we go...
  return gyp.main(args)

if __name__ == '__main__':
  sys.exit(main())
