blob: b8362ec9aa1056bfb3ab585e603f2e9e77157688 [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
Diana Picusadb07c42017-11-22 16:12:57 +01004from sys import argv
Diana Picus3b2ef822016-10-13 16:53:18 +03005from sys import exit
6
Diana Picus052b7d32017-11-24 16:19:41 +01007from modules.llvm import LLVMBuildConfig
Diana Picus95226d42017-11-01 13:16:54 +01008from modules.llvm import LLVMSubproject
9from modules.llvm import LLVMSourceConfig
Diana Picusb368cb62018-01-23 16:41:59 +010010from modules.llvm import run_test_suite
Diana Picusf73abbf2018-01-26 07:06:20 +010011from modules.llvm import setup_test_suite
Diana Picus052b7d32017-11-24 16:19:41 +010012from modules.utils import CommandPrinter
13from modules.utils import CommandRunner
Diana Picus5ad55422017-12-14 17:57:10 +010014from modules.utils import get_remote_branch
15from modules.utils import push_branch
Diana Picus95226d42017-11-01 13:16:54 +010016
Diana Picus052b7d32017-11-24 16:19:41 +010017from linaropy.cd import cd
Diana Picus3b2ef822016-10-13 16:53:18 +030018from linaropy.git.clone import Clone
19from linaropy.proj import Proj
20
21from argparse import Action, ArgumentParser, RawTextHelpFormatter
Diana Picusefc7bda2017-06-09 19:14:08 +020022from functools import partial
Diana Picus3b2ef822016-10-13 16:53:18 +030023
24
25def die(message, config_to_dump=None):
26 """Print an error message and exit."""
Diana Picusb4307602017-04-05 19:48:39 +020027 print(message)
Diana Picus3b2ef822016-10-13 16:53:18 +030028
29 if config_to_dump is not None:
30 dump_config(config_to_dump)
31
32 exit(1)
33
Diana Picus3d1a3012017-03-14 17:38:32 +010034
Diana Picus3b2ef822016-10-13 16:53:18 +030035def dump_config(config):
36 """Dump the list of projects that are enabled in the given config."""
37
Diana Picusb4307602017-04-05 19:48:39 +020038 print("Projects linked:")
Diana Picus3b2ef822016-10-13 16:53:18 +030039 enabled = config.get_enabled_subprojects()
40 if not enabled:
Diana Picusb4307602017-04-05 19:48:39 +020041 print("none")
Diana Picus3b2ef822016-10-13 16:53:18 +030042 else:
43 for subproj in sorted(enabled):
Diana Picusb4307602017-04-05 19:48:39 +020044 print(" + {}".format(subproj))
Diana Picus3b2ef822016-10-13 16:53:18 +030045
46
47def projects(args):
48 """Add/remove subprojects based on the values in args."""
49
50 proj = Proj()
Diana Picus3d1a3012017-03-14 17:38:32 +010051
Diana Picus9f756862017-12-20 10:35:08 +010052 llvm_worktree_root = args.sources
Diana Picus81089db2017-05-05 22:26:49 +020053 llvm_repos_root = args.repos
Diana Picusb1dbcba2018-02-07 01:33:17 +010054 config = LLVMSourceConfig(proj, llvm_worktree_root, dry=False)
Diana Picus3b2ef822016-10-13 16:53:18 +030055
56 if not args.add and not args.remove:
57 # Nothing to change, just print the current configuration
58 dump_config(config)
59 exit(0)
60
61 to_add = {}
62 if args.add:
63 for subproj in args.add:
64 repo = Clone(proj, os.path.join(llvm_repos_root, subproj))
65 to_add[subproj] = repo
66
67 try:
68 config.update(to_add, args.remove)
69 except (EnvironmentError, ValueError) as exc:
70 die("Failed to update subprojects because:\n{}".format(str(exc)))
71
72 dump_config(config)
73
Diana Picusefc7bda2017-06-09 19:14:08 +020074
Diana Picus95226d42017-11-01 13:16:54 +010075def push_current_branch(args):
Diana Picusefc7bda2017-06-09 19:14:08 +020076 """Push current branch to origin."""
77
78 proj = Proj()
79
Diana Picus9f756862017-12-20 10:35:08 +010080 llvm_worktree_root = args.sources
Diana Picusb1dbcba2018-02-07 01:33:17 +010081 config = LLVMSourceConfig(proj, llvm_worktree_root, dry=False)
Diana Picusefc7bda2017-06-09 19:14:08 +020082
Diana Picus95226d42017-11-01 13:16:54 +010083 llvm_worktree = Clone(proj, llvm_worktree_root)
84 local_branch = llvm_worktree.getbranch()
Diana Picusefc7bda2017-06-09 19:14:08 +020085
86 try:
Diana Picus95226d42017-11-01 13:16:54 +010087 remote_branch = get_remote_branch(llvm_worktree, local_branch)
88 config.for_each_enabled(partial(push_branch, proj, local_branch,
89 remote_branch))
90 print("Pushed to {}".format(remote_branch))
91 except (EnvironmentError, RuntimeError) as exc:
Diana Picusefc7bda2017-06-09 19:14:08 +020092 die("Failed to push branch because: " + str(exc) + str(exc.__cause__))
93
Diana Picus95226d42017-11-01 13:16:54 +010094
Diana Picus052b7d32017-11-24 16:19:41 +010095def configure_build(args):
96 """Configure a given build directory."""
97
98 proj = Proj()
99
Diana Picus9f756862017-12-20 10:35:08 +0100100 llvm_worktree_root = args.sources
Diana Picusb1dbcba2018-02-07 01:33:17 +0100101 sourceConfig = LLVMSourceConfig(proj, llvm_worktree_root, args.dry)
Diana Picus052b7d32017-11-24 16:19:41 +0100102
Diana Picus052b7d32017-11-24 16:19:41 +0100103 if args.dry:
104 consumer = CommandPrinter()
105 else:
106 if not os.path.exists(args.build):
107 os.makedirs(args.build)
108 consumer = CommandRunner()
109
Diana Picus6b1935f2018-02-07 16:44:11 +0100110 buildConfig = LLVMBuildConfig(sourceConfig, args.build, consumer)
111
112 if args.defs:
113 args.defs = ["-D{}".format(v) for v in args.defs]
114
Diana Picus052b7d32017-11-24 16:19:41 +0100115 try:
Diana Picus6b1935f2018-02-07 16:44:11 +0100116 buildConfig.cmake(args.defs, args.generator)
Diana Picus052b7d32017-11-24 16:19:41 +0100117 except RuntimeError as exc:
118 die("Failed to configure {} because:\n{}".format(args.build, str(exc)))
119
120
Diana Picus37126b82018-01-19 16:14:26 +0100121def run_build(args):
122 """Run a build command in a given directory."""
123 build_dir = args.build
124
125 if args.dry:
126 consumer = CommandPrinter()
127 else:
128 consumer = CommandRunner()
129
130 try:
Diana Picus6b1935f2018-02-07 16:44:11 +0100131 LLVMBuildConfig(None, args.build, consumer).build(args.flags)
Diana Picus37126b82018-01-19 16:14:26 +0100132 except RuntimeError as exc:
133 die("Failed to build {} because:\n{}".format(args.build, str(exc)))
134
135
Diana Picusf73abbf2018-01-26 07:06:20 +0100136def setup_the_test_suite(args):
137 """Setup a sandbox for the test-suite."""
138 if args.dry:
139 consumer = CommandPrinter()
140 else:
141 consumer = CommandRunner()
142
143 try:
144 setup_test_suite(consumer, args.sandbox, args.lnt)
145 except RuntimeError as exc:
146 die("Failed to setup the test-suite because:\n{}".format(str(exc)))
147
148
Diana Picusb368cb62018-01-23 16:41:59 +0100149def run_the_test_suite(args):
150 """Run the test-suite in a given sandbox."""
151 if args.dry:
152 consumer = CommandPrinter()
153 else:
154 consumer = CommandRunner()
155
156 compilers = ["--cc={}".format(args.cc)]
157 if args.cxx:
158 compilers.append("--cxx={}".format(args.cxx))
159
160 try:
161 run_test_suite(consumer, args.sandbox, args.testsuite, args.lit,
162 compilers + args.flags)
163 except RuntimeError as exc:
164 die("Failed to run the test-suite because:\n{}".format(str(exc)))
165
Diana Picusb03e5082018-02-05 12:36:49 +0100166
167def build_and_test(args):
168 """
169 Build a set of LLVM subprojects, in one or two stages, with or without a
170 test-suite run.
171 """
172
173 proj = Proj()
174
175 dryRun = args.dry
176 llvmWorktreeRoot = args.sources
177
178 stage1BuildDir = args.stage1
179 stage2BuildDir = args.stage2
180 testSuiteDir = args.test_suite
181 sandboxDir = args.sandbox
182 lntDir = args.lnt
183
184 if dryRun:
185 consumer = CommandPrinter()
186 else:
187 consumer = CommandRunner()
188
189 try:
190 sourceConfig = LLVMSourceConfig(proj, llvmWorktreeRoot, args.dry)
191
192 if not dryRun and not os.path.exists(stage1BuildDir):
193 os.makedirs(stage1BuildDir)
194
195 buildConfig1 = LLVMBuildConfig(sourceConfig, stage1BuildDir, consumer)
196 buildConfig1.cmake([], "Ninja")
197 buildConfig1.build()
198 testedBuildDir = stage1BuildDir
199
200 if stage2BuildDir is not None:
201 if not dryRun and not os.path.exists(stage2BuildDir):
202 os.makedirs(stage2BuildDir)
203
204 buildConfig2 = LLVMBuildConfig(sourceConfig, stage2BuildDir,
205 consumer)
206
207 # TODO: Make sure clang is actually built in this config (preferably
208 # before reaching this point)
209 buildConfig2.cmake(
210 [
211 "-DCMAKE_C_COMPILER={}/bin/clang".format(stage1BuildDir),
212 "-DCMAKE_CXX_COMPILER={}/bin/clang++".format(stage1BuildDir)],
213 "Ninja")
214 buildConfig2.build()
215 testedBuildDir = stage2BuildDir
216
217 if testSuiteDir is not None:
218 setup_test_suite(consumer, sandboxDir, lntDir)
219
220 # TODO: Make sure clang is actually built in this config (preferably
221 # before reaching this point)
222 lit = os.path.join(testedBuildDir, "bin", "llvm-lit")
223 clang = os.path.join(testedBuildDir, "bin", "clang")
224 run_test_suite(consumer, sandboxDir, testSuiteDir, lit,
225 ["--cc={}".format(clang)])
226
227 except RuntimeError as exc:
228 die("Failed because:\n{}".format(str(exc)))
229
230
Diana Picus3b2ef822016-10-13 16:53:18 +0300231##########################################################################
232# Command line parsing #
233##########################################################################
234
235# If we decide we want shorthands for the subprojects, we can append to this
236# list
Diana Picusb4307602017-04-05 19:48:39 +0200237valid_subprojects = list(LLVMSubproject.get_all_subprojects().keys())
Diana Picus3b2ef822016-10-13 16:53:18 +0300238
239options = ArgumentParser(formatter_class=RawTextHelpFormatter)
Diana Picusadb07c42017-11-22 16:12:57 +0100240subcommands = options.add_subparsers(dest="subcommand")
Diana Picus3b2ef822016-10-13 16:53:18 +0300241
242# Subcommand for adding / removing subprojects
Diana Picus36317e82017-10-31 15:35:24 +0100243projs = subcommands.add_parser(
244 "projects", help="Add/remove LLVM subprojects.\n"
245 "Adding a subproject will create a worktree for it "
246 "somewhere in the LLVM source tree, on the same git "
247 "branch as LLVM itself.\n"
248 "Removing a subproject will remove the worktree, but "
249 "not the underlying git branch.")
Diana Picus3b2ef822016-10-13 16:53:18 +0300250projs.set_defaults(run_command=projects)
251
252# TODO: Overwriting previous values is not necessarily what users expect (so for
253# instance --add S1 S2 --remove S3 --add S4 would lead to adding only S4). We
254# can do better by using action='append', which would create a list (of lists?
255# or of lists and scalars?) that we can flatten to obtain all the values passed
256# by the user.
257projs.add_argument(
258 '-a', '--add',
259 nargs='+',
260 choices=valid_subprojects,
261 metavar='subproject',
Diana Picus36317e82017-10-31 15:35:24 +0100262 help="Enable given subprojects. Valid values are:\n\t{}\n".format(
Diana Picus3b2ef822016-10-13 16:53:18 +0300263 "\n\t".join(valid_subprojects)))
264projs.add_argument(
265 '-r', '--remove',
266 nargs='+',
267 choices=valid_subprojects,
268 metavar='subproject',
Diana Picus36317e82017-10-31 15:35:24 +0100269 help="Disable given subprojects.")
Diana Picusadb07c42017-11-22 16:12:57 +0100270projs.add_argument(
271 '--repos',
272 help="Path to the directory containing the repositories for all LLVM "
273 "subprojects.")
Diana Picus9f756862017-12-20 10:35:08 +0100274projs.add_argument(
275 '--source-dir',
276 dest='sources',
277 required=True,
278 help="Path to the directory containing the LLVM worktree that we're adding "
279 "or removing subprojects from.")
Diana Picus3b2ef822016-10-13 16:53:18 +0300280
Diana Picusefc7bda2017-06-09 19:14:08 +0200281# Subcommand for pushing the current branch to origin
282push = subcommands.add_parser(
283 "push",
Diana Picus36317e82017-10-31 15:35:24 +0100284 help="Push current branch to origin linaro-local/<user>/<branch>, "
285 "for all enabled subprojects.")
Diana Picus95226d42017-11-01 13:16:54 +0100286push.set_defaults(run_command=push_current_branch)
Diana Picus9f756862017-12-20 10:35:08 +0100287push.add_argument(
288 '--source-dir',
289 dest='sources',
290 required=True,
291 help="Path to the directory containing the LLVM worktree.")
Diana Picusefc7bda2017-06-09 19:14:08 +0200292
Diana Picus052b7d32017-11-24 16:19:41 +0100293# Subcommand for configuring a build directory
294configure = subcommands.add_parser(
295 'configure',
296 help="Run CMake in the given build directory.")
297configure.add_argument(
Diana Picus9f756862017-12-20 10:35:08 +0100298 '--source-dir',
299 dest='sources',
300 required=True,
301 help="Path to the sources directory. It should contain an LLVM worktree.")
302configure.add_argument(
Diana Picus052b7d32017-11-24 16:19:41 +0100303 '--build-dir',
304 dest='build',
305 required=True,
306 help="Path to the build directory. It will be created if it does not exist")
307configure.add_argument(
308 '--cmake-generator',
309 dest='generator',
310 default='Ninja',
311 help="CMake generator to use (default is Ninja).")
312configure.add_argument(
313 '--cmake-def',
314 dest='defs',
315 metavar='VAR=VALUE',
316 default=[],
317 action='append',
318 # We add the -D in front of the variable ourselves because the argument
319 # parsing gets confused otherwise (and quoting doesn't help).
320 help="Additional CMake definitions, e.g. CMAKE_BUILD_TYPE=Release."
321 "May be passed several times. The -D is added automatically.")
322configure.add_argument(
323 '-n', '--dry-run',
324 dest='dry',
325 action='store_true',
326 default=False,
327 help="Print the CMake command instead of executing it.")
328configure.set_defaults(run_command=configure_build)
329
Diana Picus37126b82018-01-19 16:14:26 +0100330# Subcommand for building a target
331build = subcommands.add_parser(
332 'build',
333 help="Run a build command in the given directory."
334 "The build command can be either a 'ninja' or a 'make' command, depending "
335 "on what the build directory contains. First, we look for a 'build.ninja' "
336 "file. If that is not found, we look for a 'Makefile'. If that is not "
337 "found either, the script fails.")
338build.add_argument(
339 '--build-dir',
340 dest='build',
341 required=True,
342 help="Path to the build directory. It must have already been configured.")
343build.add_argument(
344 '-n', '--dry-run',
345 dest='dry',
346 action='store_true',
347 default=False,
348 help="Print the build command instead of executing it.")
349build.add_argument(
350 '--build-flag',
351 dest='flags',
352 metavar='FLAG',
353 default=[],
354 action='append',
355 help="Additional flags for the build command (e.g. targets to build). "
356 "May be passed several times. If your flag starts with a '-', use "
357 "'--build-flag=-FLAG' to pass it.")
358build.set_defaults(run_command=run_build)
359
Diana Picusf73abbf2018-01-26 07:06:20 +0100360# Subcommand for setting up the test-suite
361setupTestSuite = subcommands.add_parser(
362 'setup-test-suite',
363 help="Prepare a sandbox for running the test-suite.")
364setupTestSuite.add_argument(
365 '--sandbox',
366 required=True,
367 help="Path where we should setup the sandbox.")
368setupTestSuite.add_argument(
369 '--lnt',
370 required=True,
371 help="Path to the LNT sources.")
372setupTestSuite.add_argument(
373 '-n', '--dry-run',
374 dest='dry',
375 action='store_true',
376 default=False,
377 help="Print the commands instead of executing them.")
378setupTestSuite.set_defaults(run_command=setup_the_test_suite)
379
Diana Picusb368cb62018-01-23 16:41:59 +0100380# Subcommand for running the test-suite
381runTestSuite = subcommands.add_parser(
382 'run-test-suite',
383 help="Run the test-suite in the given sandbox.")
384runTestSuite.add_argument(
385 '--sandbox',
386 required=True,
387 help="Path to the sandbox. It must point to a virtualenv with a LNT setup.")
388runTestSuite.add_argument(
389 '--test-suite',
390 dest="testsuite",
391 required=True,
392 help="Path to the test-suite repo.")
393runTestSuite.add_argument(
394 '--use-lit',
395 dest="lit",
396 required=True,
397 help="Path to llvm-lit.")
398runTestSuite.add_argument(
399 '--lnt-flag',
400 dest='flags',
401 metavar='FLAG',
402 default=[],
403 action='append',
404 help="Additional flags to be passed to LNT when running the test-suite."
405 "May be passed several times. If your flag starts with a '-', use "
406 "'--lnt-flag=-FLAG' to pass it.")
407runTestSuite.add_argument(
408 # We can pass --cc through the --lnt-flag interface, but we generally won't
409 # want to test the system compiler, so force the user to be specific.
410 '--cc',
411 required=True,
412 help="The path to the C compiler that we're testing.")
413runTestSuite.add_argument(
414 # For symmetry, we also provide a --cxx argument, but this one isn't
415 # required since LNT tries to guess it based on the value of --cc.
416 '--cxx',
417 required=False,
418 help="The path to the C++ compiler that we're testing.")
419runTestSuite.add_argument(
420 '-n', '--dry-run',
421 dest='dry',
422 action='store_true',
423 default=False,
424 help="Print the commands instead of executing them.")
425runTestSuite.set_defaults(run_command=run_the_test_suite)
426
Diana Picusb03e5082018-02-05 12:36:49 +0100427buildAndTest = subcommands.add_parser(
428 'build-and-test', # TODO: This really needs a better name...
429 help="Run complex build scenarios with one or two stages of clang and "
430 "optionally a test-suite run. This should be flexible enough to allow "
431 "us to reproduce any buildbot configuration, but it can obviously be "
432 "used for other purposes as well.")
433buildAndTest.set_defaults(run_command=build_and_test)
434buildAndTest.add_argument(
435 '--source-dir',
436 dest='sources',
437 required=True,
438 help="Path to the directory containing the LLVM worktree that we're going "
439 "to build from.")
440buildAndTest.add_argument(
441 '--stage1-build-dir',
442 dest='stage1',
443 required=True,
444 help="Path to the build directory for stage 1.")
445buildAndTest.add_argument(
446 '--stage2-build-dir',
447 dest='stage2',
448 help="Path to the build directory for stage 2.")
449buildAndTest.add_argument(
450 "--test-suite",
451 help="Path to the test-suite repo.")
452buildAndTest.add_argument(
453 "--sandbox",
454 help="Path to the sandbox where the test-suite should be setup.")
455buildAndTest.add_argument(
456 "--lnt",
457 help="Path to the LNT repo.")
458buildAndTest.add_argument(
459 '-n', '--dry-run',
460 dest='dry',
461 action='store_true',
462 default=False,
463 help="Print the commands instead of executing them.")
464
Diana Picus3b2ef822016-10-13 16:53:18 +0300465args = options.parse_args()
Diana Picusadb07c42017-11-22 16:12:57 +0100466if args.subcommand == "projects" and args.add and not args.repos:
467 projs.error(
468 "When adding a subproject you must also pass the --repos argument")
Diana Picus3b2ef822016-10-13 16:53:18 +0300469args.run_command(args)