#!/usr/bin/python

# Copyright (c) 2010 The ANGLE Project 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 ANGLE that adds some support for how GYP
# is invoked by ANGLE beyond what can be done in the gclient hooks.

import os
import sys

script_dir = os.path.dirname(__file__)
angle_dir = os.path.normpath(os.path.join(script_dir, os.pardir))

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

def appendCommonArgs(args):
    # Set the depth to get the top-level Makefile generated into the
    # correct directory. This only has an effect on Linux.
    args.append('--depth');
    args.append('.');
    # Add standalone.gypi to the include path to set required
    # variables that are otherwise picked up from Chromium's gyp
    # files.
    args.append('-I' + os.path.join(script_dir, 'standalone.gypi'))
    # Add common.gypi to the include path.
    args.append('-I' + os.path.join(script_dir, 'common.gypi'))
    args.append('--no-parallel')

def generateProjects(generate_args):
    args = generate_args
    appendCommonArgs(args)
    # Add all.gyp as the main gyp file to be generated.
    args.append(os.path.join(script_dir, 'ANGLE.gyp'))

    gyp.main(args)

def generateWinRTProjects(generate_args, generation_dir, msvs_version, app_type_rev,
                          target_platform_ver):
    args = generate_args
    appendCommonArgs(args)

    args.append('--ignore-environment')
    args.append('--generator-output=' + generation_dir)
    args.append('--format=msvs')
    args.append('-Gmsvs_version=' + msvs_version)
    args.append('-Dangle_use_commit_id=1')
    args.append('-Dangle_build_winrt=1')
    args.append('-Dangle_build_winrt_app_type_revision=' + app_type_rev)
    args.append('-Dangle_build_winrt_target_platform_ver=' + target_platform_ver)
    args.append('-Dangle_enable_d3d9=0')
    args.append('-Dangle_enable_gl=0')
    args.append('-Dangle_enable_vulkan=0')
    args.append('-Dangle_enable_null=0')
    args.append('-Dangle_enable_essl=0')
    args.append('-Dangle_enable_glsl=0')
    # Add all.gyp as the main gyp file to be generated.
    args.append(os.path.join(script_dir, 'ANGLE.gyp'))

    print 'Generating WinRT projects to ' + generation_dir + ' from gyp files...'
    sys.stdout.flush()

    gyp.main(args)

if __name__ == '__main__':
    print
    print '* * * WARNING: GYP IS DEPRECATED * * *'
    print 'See https://chromium.googlesource.com/angle/angle/+/master/doc/DevSetup.md for new build instructions.'
    print
    print 'Updating projects from gyp files...'
    sys.stdout.flush()

    # Generate projects
    args = sys.argv[1:]
    generateProjects(args)

    # Generate WinRT projects only if configured
    if 'GYP_GENERATE_WINRT' in os.environ:
      # Generate Windows 10 projects
      args = sys.argv[1:]
      generateWinRTProjects(args, "winrt/10", "2015", "10.0", "10.0.10240.0");
