Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 1 | """This is the main tool for handling llvm builds, bisects etc.""" |
| 2 | |
| 3 | import os |
| 4 | from sys import exit |
| 5 | |
| 6 | from modules.llvm import LLVMSubproject, LLVMSourceConfig |
| 7 | from linaropy.git.clone import Clone |
| 8 | from linaropy.proj import Proj |
| 9 | |
| 10 | from argparse import Action, ArgumentParser, RawTextHelpFormatter |
| 11 | |
| 12 | |
| 13 | def die(message, config_to_dump=None): |
| 14 | """Print an error message and exit.""" |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 15 | print(message) |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 16 | |
| 17 | if config_to_dump is not None: |
| 18 | dump_config(config_to_dump) |
| 19 | |
| 20 | exit(1) |
| 21 | |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame] | 22 | |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame] | 23 | def get_worktree_root(env): |
Diana Picus | 81089db | 2017-05-05 22:26:49 +0200 | [diff] [blame^] | 24 | """Get the path to the LLVM worktree corresponding to env.""" |
| 25 | return os.path.join(env, "llvm") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 26 | |
| 27 | |
| 28 | def dump_config(config): |
| 29 | """Dump the list of projects that are enabled in the given config.""" |
| 30 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 31 | print("Projects linked:") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 32 | enabled = config.get_enabled_subprojects() |
| 33 | if not enabled: |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 34 | print("none") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 35 | else: |
| 36 | for subproj in sorted(enabled): |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 37 | print(" + {}".format(subproj)) |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 38 | |
| 39 | |
| 40 | def projects(args): |
| 41 | """Add/remove subprojects based on the values in args.""" |
| 42 | |
| 43 | proj = Proj() |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame] | 44 | |
| 45 | llvm_worktree_root = get_worktree_root(args.env) |
Diana Picus | 81089db | 2017-05-05 22:26:49 +0200 | [diff] [blame^] | 46 | llvm_repos_root = args.repos |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 47 | 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 Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 73 | valid_subprojects = list(LLVMSubproject.get_all_subprojects().keys()) |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 74 | |
| 75 | options = ArgumentParser(formatter_class=RawTextHelpFormatter) |
Diana Picus | 81089db | 2017-05-05 22:26:49 +0200 | [diff] [blame^] | 76 | options.add_argument( |
| 77 | '--env', |
| 78 | required=True, |
| 79 | help="Path to the environment to update.") |
| 80 | options.add_argument( |
| 81 | '--repos', required=True, |
| 82 | help="Path to the directory containing the repositories for all LLVM subprojects.") |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame] | 83 | |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 84 | subcommands = options.add_subparsers() |
| 85 | |
| 86 | # Subcommand for adding / removing subprojects |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame] | 87 | projs = subcommands.add_parser("projects", help="Add/remove LLVM subprojects.") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 88 | projs.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. |
| 95 | projs.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))) |
| 102 | projs.add_argument( |
| 103 | '-r', '--remove', |
| 104 | nargs='+', |
| 105 | choices=valid_subprojects, |
| 106 | metavar='subproject', |
| 107 | help="Unlink given subprojects.") |
| 108 | |
| 109 | args = options.parse_args() |
| 110 | args.run_command(args) |