aboutsummaryrefslogtreecommitdiff
path: root/scripts/llvm.py
blob: 762f9e76be600fec3f3a3b14f6a3ffb3abf5f74c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""This is the main tool for handling llvm builds, bisects etc."""

import os
from sys import exit

from modules.llvm import LLVMSubproject, LLVMSourceConfig
from linaropy.git.clone import Clone
from linaropy.proj import Proj

from argparse import Action, ArgumentParser, RawTextHelpFormatter


def die(message, config_to_dump=None):
    """Print an error message and exit."""
    print message

    if config_to_dump is not None:
        dump_config(config_to_dump)

    exit(1)

# Figure out the path to the LLVM repos
if "LLVM_ROOT" not in os.environ:
    die("Please, define $LLVM_ROOT to point to the root\n"
        "path where the worktree setup should be performed")
llvm_repos_root = os.path.join(os.environ["LLVM_ROOT"], "repos")

# Figure out the path to the current LLVM tree
if "LLVM_SRC" not in os.environ:
    die("Please, define $LLVM_SRC to point to the current LLVM\n"
        "worktree directory, or run llvm-env to set it for you")
llvm_worktree_root = os.environ["LLVM_SRC"]


def dump_config(config):
    """Dump the list of projects that are enabled in the given config."""

    print "Projects linked:"
    enabled = config.get_enabled_subprojects()
    if not enabled:
        print "none"
    else:
        for subproj in sorted(enabled):
            print "  + {}".format(subproj)


def projects(args):
    """Add/remove subprojects based on the values in args."""

    proj = Proj()
    config = LLVMSourceConfig(proj, llvm_worktree_root)

    if not args.add and not args.remove:
        # Nothing to change, just print the current configuration
        dump_config(config)
        exit(0)

    to_add = {}
    if args.add:
        for subproj in args.add:
            repo = Clone(proj, os.path.join(llvm_repos_root, subproj))
            to_add[subproj] = repo

    try:
        config.update(to_add, args.remove)
    except (EnvironmentError, ValueError) as exc:
        die("Failed to update subprojects because:\n{}".format(str(exc)))

    dump_config(config)

##########################################################################
# Command line parsing                                                   #
##########################################################################

# If we decide we want shorthands for the subprojects, we can append to this
# list
valid_subprojects = LLVMSubproject.get_all_subprojects().keys()

options = ArgumentParser(formatter_class=RawTextHelpFormatter)
subcommands = options.add_subparsers()

# Subcommand for adding / removing subprojects
projs = subcommands.add_parser("projects", help="Add/remove LLVM subprojects")
projs.set_defaults(run_command=projects)

# TODO: Overwriting previous values is not necessarily what users expect (so for
# instance --add S1 S2 --remove S3 --add S4 would lead to adding only S4). We
# can do better by using action='append', which would create a list (of lists?
# or of lists and scalars?) that we can flatten to obtain all the values passed
# by the user.
projs.add_argument(
    '-a', '--add',
    nargs='+',
    choices=valid_subprojects,
    metavar='subproject',
    help="Link given subprojects. Valid values are:\n\t{}\n".format(
         "\n\t".join(valid_subprojects)))
projs.add_argument(
    '-r', '--remove',
    nargs='+',
    choices=valid_subprojects,
    metavar='subproject',
    help="Unlink given subprojects.")

args = options.parse_args()
args.run_command(args)