#!/usr/bin/python

# Copyright (C) 2013 Canonical Ltd.

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

from __future__ import print_function
import glob
import os
import os.path
import sys

sys.dont_write_bytecode = True
os.environ["PYTHONDONTWRITEBYTECODE"] = "1"

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "python"))
from oxide_utils import CHROMIUMSRCDIR, TOPSRCDIR, CheckCall

sys.path.insert(0, os.path.join(CHROMIUMSRCDIR, 'tools', 'gyp', 'pylib'))
try:
    import gyp
except ImportError:
    print('Cannot find gyp module. Do you have a Chromium checkout?', file=sys.stderr)
    sys.exit(1)

sys.path.insert(0, os.path.join(CHROMIUMSRCDIR, 'build'))
sys.path.insert(0, os.path.join(CHROMIUMSRCDIR, 'tools'))
sys.path.insert(0, os.path.join(CHROMIUMSRCDIR, 'tools', 'generate_shim_headers'))
sys.path.insert(0, os.path.join(CHROMIUMSRCDIR, 'tools', 'grit'))
sys.path.insert(0, os.path.join(CHROMIUMSRCDIR, 'chrome', 'tools', 'build'))
sys.path.insert(0, os.path.join(CHROMIUMSRCDIR, 'third_party', 'WebKit', 'Source', 'build', 'scripts'))

## Needed to build chrome driver and as one of its deps pulls remoting.gyp or one of its deps
sys.path.insert(0, os.path.join(CHROMIUMSRCDIR, 'remoting', 'tools', 'build'))

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "scripts"))

def additional_include_files(args=[]):
    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 from Oxide
    AddInclude(os.path.join(TOPSRCDIR, 'build', 'common.gypi'))

    # Always include common.gypi from Chromium
    AddInclude(os.path.join(CHROMIUMSRCDIR, 'build', 'common.gypi'))

    # Include the Oxide Ozone impl
    AddInclude(os.path.join(TOPSRCDIR, 'shared', 'port', 'ozone', 'ozone.gypi'))

    # Optionally add supplemental .gypi files if present.
    supplements = glob.glob(os.path.join(CHROMIUMSRCDIR, '*', 'supplement.gypi'))
    for supplement in supplements:
        AddInclude(supplement)

    return result

def main(args):
    args.append(os.path.join(TOPSRCDIR, 'build', 'all.gyp'))

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

    args.append('--no-circular-check')
    args.extend(['--toplevel-dir', TOPSRCDIR])
    args.extend(['--depth', CHROMIUMSRCDIR])
    args.extend(['--format', 'ninja'])

    os.chdir(TOPSRCDIR)
    sys.exit(gyp.main(args))

if __name__ == "__main__":
  main(sys.argv[1:])
