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 |
Diana Picus | adb07c4 | 2017-11-22 16:12:57 +0100 | [diff] [blame] | 4 | from sys import argv |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 5 | from sys import exit |
| 6 | |
Diana Picus | 052b7d3 | 2017-11-24 16:19:41 +0100 | [diff] [blame^] | 7 | from modules.llvm import LLVMBuildConfig |
Diana Picus | 95226d4 | 2017-11-01 13:16:54 +0100 | [diff] [blame] | 8 | from modules.llvm import LLVMSubproject |
| 9 | from modules.llvm import LLVMSourceConfig |
| 10 | from modules.llvm import get_remote_branch |
| 11 | from modules.llvm import push_branch |
Diana Picus | 052b7d3 | 2017-11-24 16:19:41 +0100 | [diff] [blame^] | 12 | from modules.utils import CommandPrinter |
| 13 | from modules.utils import CommandRunner |
Diana Picus | 95226d4 | 2017-11-01 13:16:54 +0100 | [diff] [blame] | 14 | |
Diana Picus | 052b7d3 | 2017-11-24 16:19:41 +0100 | [diff] [blame^] | 15 | from linaropy.cd import cd |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 16 | from linaropy.git.clone import Clone |
| 17 | from linaropy.proj import Proj |
| 18 | |
| 19 | from argparse import Action, ArgumentParser, RawTextHelpFormatter |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 20 | from functools import partial |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 21 | |
| 22 | |
| 23 | def die(message, config_to_dump=None): |
| 24 | """Print an error message and exit.""" |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 25 | print(message) |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 26 | |
| 27 | if config_to_dump is not None: |
| 28 | dump_config(config_to_dump) |
| 29 | |
| 30 | exit(1) |
| 31 | |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame] | 32 | |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame] | 33 | def get_worktree_root(env): |
Diana Picus | 81089db | 2017-05-05 22:26:49 +0200 | [diff] [blame] | 34 | """Get the path to the LLVM worktree corresponding to env.""" |
| 35 | return os.path.join(env, "llvm") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 36 | |
| 37 | |
| 38 | def dump_config(config): |
| 39 | """Dump the list of projects that are enabled in the given config.""" |
| 40 | |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 41 | print("Projects linked:") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 42 | enabled = config.get_enabled_subprojects() |
| 43 | if not enabled: |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 44 | print("none") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 45 | else: |
| 46 | for subproj in sorted(enabled): |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 47 | print(" + {}".format(subproj)) |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 48 | |
| 49 | |
| 50 | def projects(args): |
| 51 | """Add/remove subprojects based on the values in args.""" |
| 52 | |
| 53 | proj = Proj() |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame] | 54 | |
| 55 | llvm_worktree_root = get_worktree_root(args.env) |
Diana Picus | 81089db | 2017-05-05 22:26:49 +0200 | [diff] [blame] | 56 | llvm_repos_root = args.repos |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 57 | config = LLVMSourceConfig(proj, llvm_worktree_root) |
| 58 | |
| 59 | if not args.add and not args.remove: |
| 60 | # Nothing to change, just print the current configuration |
| 61 | dump_config(config) |
| 62 | exit(0) |
| 63 | |
| 64 | to_add = {} |
| 65 | if args.add: |
| 66 | for subproj in args.add: |
| 67 | repo = Clone(proj, os.path.join(llvm_repos_root, subproj)) |
| 68 | to_add[subproj] = repo |
| 69 | |
| 70 | try: |
| 71 | config.update(to_add, args.remove) |
| 72 | except (EnvironmentError, ValueError) as exc: |
| 73 | die("Failed to update subprojects because:\n{}".format(str(exc))) |
| 74 | |
| 75 | dump_config(config) |
| 76 | |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 77 | |
Diana Picus | 95226d4 | 2017-11-01 13:16:54 +0100 | [diff] [blame] | 78 | def push_current_branch(args): |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 79 | """Push current branch to origin.""" |
| 80 | |
| 81 | proj = Proj() |
| 82 | |
| 83 | llvm_worktree_root = get_worktree_root(args.env) |
| 84 | config = LLVMSourceConfig(proj, llvm_worktree_root) |
| 85 | |
Diana Picus | 95226d4 | 2017-11-01 13:16:54 +0100 | [diff] [blame] | 86 | llvm_worktree = Clone(proj, llvm_worktree_root) |
| 87 | local_branch = llvm_worktree.getbranch() |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 88 | |
| 89 | try: |
Diana Picus | 95226d4 | 2017-11-01 13:16:54 +0100 | [diff] [blame] | 90 | remote_branch = get_remote_branch(llvm_worktree, local_branch) |
| 91 | config.for_each_enabled(partial(push_branch, proj, local_branch, |
| 92 | remote_branch)) |
| 93 | print("Pushed to {}".format(remote_branch)) |
| 94 | except (EnvironmentError, RuntimeError) as exc: |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 95 | die("Failed to push branch because: " + str(exc) + str(exc.__cause__)) |
| 96 | |
Diana Picus | 95226d4 | 2017-11-01 13:16:54 +0100 | [diff] [blame] | 97 | |
Diana Picus | 052b7d3 | 2017-11-24 16:19:41 +0100 | [diff] [blame^] | 98 | def configure_build(args): |
| 99 | """Configure a given build directory.""" |
| 100 | |
| 101 | proj = Proj() |
| 102 | |
| 103 | llvm_worktree_root = get_worktree_root(args.env) |
| 104 | sourceConfig = LLVMSourceConfig(proj, llvm_worktree_root) |
| 105 | |
| 106 | buildConfig = LLVMBuildConfig(sourceConfig, args.build) |
| 107 | |
| 108 | if args.defs: |
| 109 | args.defs = ["-D{}".format(v) for v in args.defs] |
| 110 | |
| 111 | if args.dry: |
| 112 | consumer = CommandPrinter() |
| 113 | else: |
| 114 | if not os.path.exists(args.build): |
| 115 | os.makedirs(args.build) |
| 116 | consumer = CommandRunner() |
| 117 | |
| 118 | try: |
| 119 | buildConfig.cmake(consumer, args.defs, args.generator) |
| 120 | except RuntimeError as exc: |
| 121 | die("Failed to configure {} because:\n{}".format(args.build, str(exc))) |
| 122 | |
| 123 | |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 124 | ########################################################################## |
| 125 | # Command line parsing # |
| 126 | ########################################################################## |
| 127 | |
| 128 | # If we decide we want shorthands for the subprojects, we can append to this |
| 129 | # list |
Diana Picus | b430760 | 2017-04-05 19:48:39 +0200 | [diff] [blame] | 130 | valid_subprojects = list(LLVMSubproject.get_all_subprojects().keys()) |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 131 | |
| 132 | options = ArgumentParser(formatter_class=RawTextHelpFormatter) |
Diana Picus | 81089db | 2017-05-05 22:26:49 +0200 | [diff] [blame] | 133 | options.add_argument( |
| 134 | '--env', |
| 135 | required=True, |
| 136 | help="Path to the environment to update.") |
Diana Picus | 3d1a301 | 2017-03-14 17:38:32 +0100 | [diff] [blame] | 137 | |
Diana Picus | adb07c4 | 2017-11-22 16:12:57 +0100 | [diff] [blame] | 138 | subcommands = options.add_subparsers(dest="subcommand") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 139 | |
| 140 | # Subcommand for adding / removing subprojects |
Diana Picus | 36317e8 | 2017-10-31 15:35:24 +0100 | [diff] [blame] | 141 | projs = subcommands.add_parser( |
| 142 | "projects", help="Add/remove LLVM subprojects.\n" |
| 143 | "Adding a subproject will create a worktree for it " |
| 144 | "somewhere in the LLVM source tree, on the same git " |
| 145 | "branch as LLVM itself.\n" |
| 146 | "Removing a subproject will remove the worktree, but " |
| 147 | "not the underlying git branch.") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 148 | projs.set_defaults(run_command=projects) |
| 149 | |
| 150 | # TODO: Overwriting previous values is not necessarily what users expect (so for |
| 151 | # instance --add S1 S2 --remove S3 --add S4 would lead to adding only S4). We |
| 152 | # can do better by using action='append', which would create a list (of lists? |
| 153 | # or of lists and scalars?) that we can flatten to obtain all the values passed |
| 154 | # by the user. |
| 155 | projs.add_argument( |
| 156 | '-a', '--add', |
| 157 | nargs='+', |
| 158 | choices=valid_subprojects, |
| 159 | metavar='subproject', |
Diana Picus | 36317e8 | 2017-10-31 15:35:24 +0100 | [diff] [blame] | 160 | help="Enable given subprojects. Valid values are:\n\t{}\n".format( |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 161 | "\n\t".join(valid_subprojects))) |
| 162 | projs.add_argument( |
| 163 | '-r', '--remove', |
| 164 | nargs='+', |
| 165 | choices=valid_subprojects, |
| 166 | metavar='subproject', |
Diana Picus | 36317e8 | 2017-10-31 15:35:24 +0100 | [diff] [blame] | 167 | help="Disable given subprojects.") |
Diana Picus | adb07c4 | 2017-11-22 16:12:57 +0100 | [diff] [blame] | 168 | projs.add_argument( |
| 169 | '--repos', |
| 170 | help="Path to the directory containing the repositories for all LLVM " |
| 171 | "subprojects.") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 172 | |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 173 | # Subcommand for pushing the current branch to origin |
| 174 | push = subcommands.add_parser( |
| 175 | "push", |
Diana Picus | 36317e8 | 2017-10-31 15:35:24 +0100 | [diff] [blame] | 176 | help="Push current branch to origin linaro-local/<user>/<branch>, " |
| 177 | "for all enabled subprojects.") |
Diana Picus | 95226d4 | 2017-11-01 13:16:54 +0100 | [diff] [blame] | 178 | push.set_defaults(run_command=push_current_branch) |
Diana Picus | efc7bda | 2017-06-09 19:14:08 +0200 | [diff] [blame] | 179 | |
Diana Picus | 052b7d3 | 2017-11-24 16:19:41 +0100 | [diff] [blame^] | 180 | # Subcommand for configuring a build directory |
| 181 | configure = subcommands.add_parser( |
| 182 | 'configure', |
| 183 | help="Run CMake in the given build directory.") |
| 184 | configure.add_argument( |
| 185 | '--build-dir', |
| 186 | dest='build', |
| 187 | required=True, |
| 188 | help="Path to the build directory. It will be created if it does not exist") |
| 189 | configure.add_argument( |
| 190 | '--cmake-generator', |
| 191 | dest='generator', |
| 192 | default='Ninja', |
| 193 | help="CMake generator to use (default is Ninja).") |
| 194 | configure.add_argument( |
| 195 | '--cmake-def', |
| 196 | dest='defs', |
| 197 | metavar='VAR=VALUE', |
| 198 | default=[], |
| 199 | action='append', |
| 200 | # We add the -D in front of the variable ourselves because the argument |
| 201 | # parsing gets confused otherwise (and quoting doesn't help). |
| 202 | help="Additional CMake definitions, e.g. CMAKE_BUILD_TYPE=Release." |
| 203 | "May be passed several times. The -D is added automatically.") |
| 204 | configure.add_argument( |
| 205 | '-n', '--dry-run', |
| 206 | dest='dry', |
| 207 | action='store_true', |
| 208 | default=False, |
| 209 | help="Print the CMake command instead of executing it.") |
| 210 | configure.set_defaults(run_command=configure_build) |
| 211 | |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 212 | args = options.parse_args() |
Diana Picus | adb07c4 | 2017-11-22 16:12:57 +0100 | [diff] [blame] | 213 | if args.subcommand == "projects" and args.add and not args.repos: |
| 214 | projs.error( |
| 215 | "When adding a subproject you must also pass the --repos argument") |
Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame] | 216 | args.run_command(args) |