blob: 13e5de622d8da8a3c642e388570b214edb6c9e5a [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."""
Diana Picusb4307602017-04-05 19:48:39 +020015 print(message)
Diana Picus3b2ef822016-10-13 16:53:18 +030016
17 if config_to_dump is not None:
18 dump_config(config_to_dump)
19
20 exit(1)
21
Diana Picus3d1a3012017-03-14 17:38:32 +010022
23def get_llvm_root():
24 """Get the path to the LLVM root, which contains the repos as well as all
25 the work environments."""
26 return os.environ["LLVM_ROOT"]
27
28
29def get_worktree_root(env):
30 """
31 Get the path to the LLVM worktree corresponding to env. The value will be
32 based on the LLVM root.
33 """
34 return os.path.join(get_llvm_root(), env, "llvm")
35
Diana Picus3b2ef822016-10-13 16:53:18 +030036# Figure out the path to the LLVM repos
37if "LLVM_ROOT" not in os.environ:
38 die("Please, define $LLVM_ROOT to point to the root\n"
39 "path where the worktree setup should be performed")
Diana Picus3d1a3012017-03-14 17:38:32 +010040llvm_repos_root = os.path.join(get_llvm_root(), "repos")
Diana Picus3b2ef822016-10-13 16:53:18 +030041
42
43def dump_config(config):
44 """Dump the list of projects that are enabled in the given config."""
45
Diana Picusb4307602017-04-05 19:48:39 +020046 print("Projects linked:")
Diana Picus3b2ef822016-10-13 16:53:18 +030047 enabled = config.get_enabled_subprojects()
48 if not enabled:
Diana Picusb4307602017-04-05 19:48:39 +020049 print("none")
Diana Picus3b2ef822016-10-13 16:53:18 +030050 else:
51 for subproj in sorted(enabled):
Diana Picusb4307602017-04-05 19:48:39 +020052 print(" + {}".format(subproj))
Diana Picus3b2ef822016-10-13 16:53:18 +030053
54
55def projects(args):
56 """Add/remove subprojects based on the values in args."""
57
58 proj = Proj()
Diana Picus3d1a3012017-03-14 17:38:32 +010059
60 llvm_worktree_root = get_worktree_root(args.env)
Diana Picus3b2ef822016-10-13 16:53:18 +030061 config = LLVMSourceConfig(proj, llvm_worktree_root)
62
63 if not args.add and not args.remove:
64 # Nothing to change, just print the current configuration
65 dump_config(config)
66 exit(0)
67
68 to_add = {}
69 if args.add:
70 for subproj in args.add:
71 repo = Clone(proj, os.path.join(llvm_repos_root, subproj))
72 to_add[subproj] = repo
73
74 try:
75 config.update(to_add, args.remove)
76 except (EnvironmentError, ValueError) as exc:
77 die("Failed to update subprojects because:\n{}".format(str(exc)))
78
79 dump_config(config)
80
81##########################################################################
82# Command line parsing #
83##########################################################################
84
85# If we decide we want shorthands for the subprojects, we can append to this
86# list
Diana Picusb4307602017-04-05 19:48:39 +020087valid_subprojects = list(LLVMSubproject.get_all_subprojects().keys())
Diana Picus3b2ef822016-10-13 16:53:18 +030088
89options = ArgumentParser(formatter_class=RawTextHelpFormatter)
Diana Picus3d1a3012017-03-14 17:38:32 +010090options.add_argument('env', help="The environment to update.")
91
Diana Picus3b2ef822016-10-13 16:53:18 +030092subcommands = options.add_subparsers()
93
94# Subcommand for adding / removing subprojects
Diana Picus3d1a3012017-03-14 17:38:32 +010095projs = subcommands.add_parser("projects", help="Add/remove LLVM subprojects.")
Diana Picus3b2ef822016-10-13 16:53:18 +030096projs.set_defaults(run_command=projects)
97
98# TODO: Overwriting previous values is not necessarily what users expect (so for
99# instance --add S1 S2 --remove S3 --add S4 would lead to adding only S4). We
100# can do better by using action='append', which would create a list (of lists?
101# or of lists and scalars?) that we can flatten to obtain all the values passed
102# by the user.
103projs.add_argument(
104 '-a', '--add',
105 nargs='+',
106 choices=valid_subprojects,
107 metavar='subproject',
108 help="Link given subprojects. Valid values are:\n\t{}\n".format(
109 "\n\t".join(valid_subprojects)))
110projs.add_argument(
111 '-r', '--remove',
112 nargs='+',
113 choices=valid_subprojects,
114 metavar='subproject',
115 help="Unlink given subprojects.")
116
117args = options.parse_args()
118args.run_command(args)