blob: 829c720554457807b7d3fc3dba4514aec5aca66e [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
Diana Picus3d1a3012017-03-14 17:38:32 +010023def get_worktree_root(env):
Diana Picus81089db2017-05-05 22:26:49 +020024 """Get the path to the LLVM worktree corresponding to env."""
25 return os.path.join(env, "llvm")
Diana Picus3b2ef822016-10-13 16:53:18 +030026
27
28def dump_config(config):
29 """Dump the list of projects that are enabled in the given config."""
30
Diana Picusb4307602017-04-05 19:48:39 +020031 print("Projects linked:")
Diana Picus3b2ef822016-10-13 16:53:18 +030032 enabled = config.get_enabled_subprojects()
33 if not enabled:
Diana Picusb4307602017-04-05 19:48:39 +020034 print("none")
Diana Picus3b2ef822016-10-13 16:53:18 +030035 else:
36 for subproj in sorted(enabled):
Diana Picusb4307602017-04-05 19:48:39 +020037 print(" + {}".format(subproj))
Diana Picus3b2ef822016-10-13 16:53:18 +030038
39
40def projects(args):
41 """Add/remove subprojects based on the values in args."""
42
43 proj = Proj()
Diana Picus3d1a3012017-03-14 17:38:32 +010044
45 llvm_worktree_root = get_worktree_root(args.env)
Diana Picus81089db2017-05-05 22:26:49 +020046 llvm_repos_root = args.repos
Diana Picus3b2ef822016-10-13 16:53:18 +030047 config = LLVMSourceConfig(proj, llvm_worktree_root)
48
49 if not args.add and not args.remove:
50 # Nothing to change, just print the current configuration
51 dump_config(config)
52 exit(0)
53
54 to_add = {}
55 if args.add:
56 for subproj in args.add:
57 repo = Clone(proj, os.path.join(llvm_repos_root, subproj))
58 to_add[subproj] = repo
59
60 try:
61 config.update(to_add, args.remove)
62 except (EnvironmentError, ValueError) as exc:
63 die("Failed to update subprojects because:\n{}".format(str(exc)))
64
65 dump_config(config)
66
67##########################################################################
68# Command line parsing #
69##########################################################################
70
71# If we decide we want shorthands for the subprojects, we can append to this
72# list
Diana Picusb4307602017-04-05 19:48:39 +020073valid_subprojects = list(LLVMSubproject.get_all_subprojects().keys())
Diana Picus3b2ef822016-10-13 16:53:18 +030074
75options = ArgumentParser(formatter_class=RawTextHelpFormatter)
Diana Picus81089db2017-05-05 22:26:49 +020076options.add_argument(
77 '--env',
78 required=True,
79 help="Path to the environment to update.")
80options.add_argument(
81 '--repos', required=True,
82 help="Path to the directory containing the repositories for all LLVM subprojects.")
Diana Picus3d1a3012017-03-14 17:38:32 +010083
Diana Picus3b2ef822016-10-13 16:53:18 +030084subcommands = options.add_subparsers()
85
86# Subcommand for adding / removing subprojects
Diana Picus3d1a3012017-03-14 17:38:32 +010087projs = subcommands.add_parser("projects", help="Add/remove LLVM subprojects.")
Diana Picus3b2ef822016-10-13 16:53:18 +030088projs.set_defaults(run_command=projects)
89
90# TODO: Overwriting previous values is not necessarily what users expect (so for
91# instance --add S1 S2 --remove S3 --add S4 would lead to adding only S4). We
92# can do better by using action='append', which would create a list (of lists?
93# or of lists and scalars?) that we can flatten to obtain all the values passed
94# by the user.
95projs.add_argument(
96 '-a', '--add',
97 nargs='+',
98 choices=valid_subprojects,
99 metavar='subproject',
100 help="Link given subprojects. Valid values are:\n\t{}\n".format(
101 "\n\t".join(valid_subprojects)))
102projs.add_argument(
103 '-r', '--remove',
104 nargs='+',
105 choices=valid_subprojects,
106 metavar='subproject',
107 help="Unlink given subprojects.")
108
109args = options.parse_args()
110args.run_command(args)