1# DExTer : Debugging Experience Tester
2# ~~~~~~   ~         ~~         ~   ~~
3#
4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5# See https://llvm.org/LICENSE.txt for license information.
6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7"""Command line options for subtools that use the builder component."""
8
9import os
10
11from dex.tools import Context
12from dex.utils import is_native_windows
13
14
15def _find_build_scripts():
16    """Finds build scripts in the 'scripts' subdirectory.
17
18    Returns:
19        { script_name (str): directory (str) }
20    """
21    try:
22        return _find_build_scripts.cached
23    except AttributeError:
24        scripts_directory = os.path.join(os.path.dirname(__file__), 'scripts')
25        if is_native_windows():
26            scripts_directory = os.path.join(scripts_directory, 'windows')
27        else:
28            scripts_directory = os.path.join(scripts_directory, 'posix')
29        assert os.path.isdir(scripts_directory), scripts_directory
30        results = {}
31
32        for f in os.listdir(scripts_directory):
33            results[os.path.splitext(f)[0]] = os.path.abspath(
34                os.path.join(scripts_directory, f))
35
36        _find_build_scripts.cached = results
37        return results
38
39
40def add_builder_tool_arguments(parser):
41    build_group = parser.add_mutually_exclusive_group(required=True)
42    build_group.add_argument('--binary',
43                             metavar="<file>",
44                             help='provide binary file instead of --builder')
45
46    build_group.add_argument(
47        '--builder',
48        type=str,
49        choices=sorted(_find_build_scripts().keys()),
50        help='test builder to use')
51    build_group.add_argument('--vs-solution', metavar="<file>",
52        help='provide a path to an already existing visual studio solution.')
53    parser.add_argument(
54        '--cflags', type=str, default='', help='compiler flags')
55    parser.add_argument('--ldflags', type=str, default='', help='linker flags')
56
57
58def handle_builder_tool_options(context: Context) -> str:
59    return _find_build_scripts()[context.options.builder]
60