blob: 762f9e76be600fec3f3a3b14f6a3ffb3abf5f74c [file] [log] [blame]
Diana Picus3b2ef822016-10-13 16:53:18 +03001"""This is the main tool for handling llvm builds, bisects etc."""
2
3import os
4from sys import exit
5
6from modules.llvm import LLVMSubproject, LLVMSourceConfig
7from linaropy.git.clone import Clone
8from linaropy.proj import Proj
9
10from argparse import Action, ArgumentParser, RawTextHelpFormatter
11
12
13def die(message, config_to_dump=None):
14 """Print an error message and exit."""
15 print message
16
17 if config_to_dump is not None:
18 dump_config(config_to_dump)
19
20 exit(1)
21
22# Figure out the path to the LLVM repos
23if "LLVM_ROOT" not in os.environ:
24 die("Please, define $LLVM_ROOT to point to the root\n"
25 "path where the worktree setup should be performed")
26llvm_repos_root = os.path.join(os.environ["LLVM_ROOT"], "repos")
27
28# Figure out the path to the current LLVM tree
29if "LLVM_SRC" not in os.environ:
30 die("Please, define $LLVM_SRC to point to the current LLVM\n"
31 "worktree directory, or run llvm-env to set it for you")
32llvm_worktree_root = os.environ["LLVM_SRC"]
33
34
35def dump_config(config):
36 """Dump the list of projects that are enabled in the given config."""
37
38 print "Projects linked:"
39 enabled = config.get_enabled_subprojects()
40 if not enabled:
41 print "none"
42 else:
43 for subproj in sorted(enabled):
44 print " + {}".format(subproj)
45
46
47def projects(args):
48 """Add/remove subprojects based on the values in args."""
49
50 proj = Proj()
51 config = LLVMSourceConfig(proj, llvm_worktree_root)
52
53 if not args.add and not args.remove:
54 # Nothing to change, just print the current configuration
55 dump_config(config)
56 exit(0)
57
58 to_add = {}
59 if args.add:
60 for subproj in args.add:
61 repo = Clone(proj, os.path.join(llvm_repos_root, subproj))
62 to_add[subproj] = repo
63
64 try:
65 config.update(to_add, args.remove)
66 except (EnvironmentError, ValueError) as exc:
67 die("Failed to update subprojects because:\n{}".format(str(exc)))
68
69 dump_config(config)
70
71##########################################################################
72# Command line parsing #
73##########################################################################
74
75# If we decide we want shorthands for the subprojects, we can append to this
76# list
77valid_subprojects = LLVMSubproject.get_all_subprojects().keys()
78
79options = ArgumentParser(formatter_class=RawTextHelpFormatter)
80subcommands = options.add_subparsers()
81
82# Subcommand for adding / removing subprojects
83projs = subcommands.add_parser("projects", help="Add/remove LLVM subprojects")
84projs.set_defaults(run_command=projects)
85
86# TODO: Overwriting previous values is not necessarily what users expect (so for
87# instance --add S1 S2 --remove S3 --add S4 would lead to adding only S4). We
88# can do better by using action='append', which would create a list (of lists?
89# or of lists and scalars?) that we can flatten to obtain all the values passed
90# by the user.
91projs.add_argument(
92 '-a', '--add',
93 nargs='+',
94 choices=valid_subprojects,
95 metavar='subproject',
96 help="Link given subprojects. Valid values are:\n\t{}\n".format(
97 "\n\t".join(valid_subprojects)))
98projs.add_argument(
99 '-r', '--remove',
100 nargs='+',
101 choices=valid_subprojects,
102 metavar='subproject',
103 help="Unlink given subprojects.")
104
105args = options.parse_args()
106args.run_command(args)