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 | |
| 23 | def 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 | |
| 29 | def 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 Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 36 | # Figure out the path to the LLVM repos |
| 37 | if "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 Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame^] | 40 | llvm_repos_root = os.path.join(get_llvm_root(), "repos") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 41 | |
| 42 | |
| 43 | def dump_config(config): |
| 44 | """Dump the list of projects that are enabled in the given config.""" |
| 45 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 46 | print("Projects linked:") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 47 | enabled = config.get_enabled_subprojects() |
| 48 | if not enabled: |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 49 | print("none") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 50 | else: |
| 51 | for subproj in sorted(enabled): |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 52 | print(" + {}".format(subproj)) |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 53 | |
| 54 | |
| 55 | def projects(args): |
| 56 | """Add/remove subprojects based on the values in args.""" |
| 57 | |
| 58 | proj = Proj() |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame^] | 59 | |
| 60 | llvm_worktree_root = get_worktree_root(args.env) |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 61 | 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 Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 87 | valid_subprojects = list(LLVMSubproject.get_all_subprojects().keys()) |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 88 | |
| 89 | options = ArgumentParser(formatter_class=RawTextHelpFormatter) |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame^] | 90 | options.add_argument('env', help="The environment to update.") |
| 91 | |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 92 | subcommands = options.add_subparsers() |
| 93 | |
| 94 | # Subcommand for adding / removing subprojects |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame^] | 95 | projs = subcommands.add_parser("projects", help="Add/remove LLVM subprojects.") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 96 | projs.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. |
| 103 | projs.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))) |
| 110 | projs.add_argument( |
| 111 | '-r', '--remove', |
| 112 | nargs='+', |
| 113 | choices=valid_subprojects, |
| 114 | metavar='subproject', |
| 115 | help="Unlink given subprojects.") |
| 116 | |
| 117 | args = options.parse_args() |
| 118 | args.run_command(args) |