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