#!/usr/bin/env python3
#
# Copyright 2020-2024, The Khronos Group Inc.
#
# SPDX-License-Identifier: Apache-2.0

# Build a spec with requested extension sets and options.
#
# Usage: makeSpec script-options make-options
# Script options are parsed by this script before invoking 'make':
#   -genpath path - directory for generated files and outputs
#   -spec core - make a spec with no extensions (default)
#   -spec khr - make a spec with all KHR extensions
#   -spec all - make a spec with all registered extensions
#   -version {1.0 | 1.1} - make a spec with this core version
#   -ext name - add specified extension and its dependencies
#   -clean - clean generated files before building
#   -registry path - API XML to use instead of default
#   -apiname name - API name to use instead of default
#   -v - verbose, print actions before executing  them
#   -n - dry-run, print actions instead of executing them
# make-options - all other options are passed to 'make', including
# requested build targets

import argparse, copy, io, os, re, string, subprocess, sys
import platform
from pathlib import Path


def execute(args, results):
    if results.verbose or results.dryrun:
        print("'" + "' '".join(args) + "'")
    if not results.dryrun:
        subprocess.check_call(args)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()

    parser.add_argument('-clean', action='store_true',
                        help='Clean generated files before building')
    parser.add_argument('-extension', action='append',
                        default=[],
                        help='Specify a required extension or extensions to add to targets')
    parser.add_argument('-genpath', action='store',
                        default='generated',
                        help='Path to directory containing generated files')
    parser.add_argument('-spec', action='store',
                        choices=['core', 'khr', 'all'],
                        default='core',
                        help='Type of spec to generate')
    parser.add_argument('-version', action='store',
                        choices=[ '1.0', '1.1' ],
                        default='1.1',
                        help='Type of spec to generate')

    parser.add_argument('-registry', action='store',
                        default=None,
                        help='Path to API XML registry file specifying version and extension dependencies')
    parser.add_argument('-apiname', action='store',
                        default=None,
                        help='API name to generate')

    parser.add_argument('-n', action='store_true', dest='dryrun',
                        help='Only prints actions, do not execute them')
    parser.add_argument('-v', action='store_true', dest='verbose',
                        help='Print actions before executing them')

    (results, options) = parser.parse_known_args()

    spec_dir = Path(__file__).resolve().parent

    # Ensure genpath is an absolute path, not relative
    genpath = Path(results.genpath).resolve()

    s = platform.system()
    if "Windows" in s or "CYGWIN" in s:
        genpath = genpath.relative_to(spec_dir).as_posix()

    # Look for scripts/extdependency.py
    scripts_dir = spec_dir / 'scripts'
    sys.path.insert(0, str(scripts_dir))
    from scripts.extdependency import ApiDependencies

    deps = ApiDependencies(results.registry, results.apiname)

    # List of versions to build with from the requested -version
    # This should come from the extdependency module as well, eventually
    versionDict = {
        '1.0' : [ 'XR_VERSION_1_0', 'XR_LOADER_VERSION_1_0' ],
        '1.1' : [ 'XR_VERSION_1_1', 'XR_VERSION_1_0', 'XR_LOADER_VERSION_1_0' ],
    }
    versions = 'VERSIONS={}'.format(' '.join(versionDict[results.version]))

    # List of extensions to build with from the requested -spec
    # Also construct a spec title
    # This should respect version dependencies as well
    title = ''
    exts = set()
    if results.spec == 'core':
        # no extra title or exts
        pass
    elif results.spec == 'khr':
        title = 'with all KHR extensions'
        exts = set(deps.khrExtensions())
    elif results.spec == 'all':
        title = 'with all registered extensions'
        exts = set(deps.allExtensions())

    # List of explicitly requested extension and all its dependencies
    extraexts = set()
    for name in results.extension:
        if name in deps.allExtensions():
            extraexts.add(name)
            extraexts.update(deps.children(name))
        else:
            raise Exception(f'ERROR: unknown extension {name}')

    # See if any explicitly requested extensions are not implicitly requested
    # Add any such extensions to the spec title
    extraexts -= exts
    if len(extraexts) > 0:
        exts.update(extraexts)
        if title != '':
            title += ' and ' + ', '.join(sorted(extraexts))
        else:
            title += 'with ' + ', '.join(sorted(extraexts))

    if title != '':
        title = '(' + title + ')'

    # Finally, actually invoke make as needed for the targets
    args = [ 'make', 'GENDIR=' + str(genpath) ]

    if results.clean:
        execute(args + ['clean'], results)

    args.append(versions)

    # The actual target
    if len(exts) > 0:
        args.append('EXTENSIONS={}'.format(' '.join(sorted(exts))))
    args.append('APITITLE={}'.format(title))
    args += options

    try:
        execute(args, results)
    except:
        sys.exit(1)
