2 ##===- utils/llvmbuild - Build the LLVM project ----------------*-python-*-===##
4 # The LLVM Compiler Infrastructure
6 # This file is distributed under the University of Illinois Open Source
7 # License. See LICENSE.TXT for details.
9 ##===----------------------------------------------------------------------===##
11 # This script builds many different flavors of the LLVM ecosystem. It
12 # will build LLVM, Clang, llvm-gcc, and dragonegg as well as run tests
13 # on them. This script is convenient to use to check builds and tests
14 # before committing changes to the upstream repository
16 # A typical source setup uses three trees and looks like this:
70 # "gcc" above is the upstream FSF gcc and "gcc/trunk" refers to the
71 # 4.5 branch as discussed in the dragonegg build guide.
73 # In a typical workflow, the "official" tree always contains unchanged
74 # sources from the main LLVM project repositories. The "staging" tree
75 # is where local work is done. A set of changes resides there waiting
76 # to be moved upstream. The "commit" tree is where changes from
77 # "staging" make their way upstream. Individual incremental changes
78 # from "staging" are applied to "commit" and committed upstream after
79 # a successful build and test run. A successful build is one in which
80 # testing results in no more failures than seen in the testing of the
83 # A build may be invoked as such:
85 # llvmbuild --src=~/llvm/commit --src=~/llvm/staging
86 # --src=~/llvm/official --branch=trunk --branch=tags/RELEASE_28
87 # --build=debug --build=release --build=paranoid
88 # --prefix=/home/greened/install --builddir=/home/greened/build
90 # This will build the LLVM ecosystem, including LLVM, Clang, llvm-gcc,
91 # gcc 4.5 and dragonegg, putting build results in ~/build and
92 # installing tools in ~/install. llvmbuild creates separate build and
93 # install directories for each source/branch/build flavor. In the
94 # above example, llvmbuild will build debug, release and paranoid
95 # (debug+checks) flavors of the trunk and RELEASE_28 branches from
96 # each source tree (official, staging and commit) for a total of
97 # eighteen builds. All builds will be run in parallel.
99 # The user may control parallelism via the --jobs and --threads
100 # switches. --jobs tells llvmbuild the maximum total number of builds
101 # to activate in parallel. The user may think of it as equivalent to
102 # the GNU make -j switch. --threads tells llvmbuild how many worker
103 # threads to use to accomplish those builds. If --threads is less
104 # than --jobs, --threads workers will be launched and each one will
105 # pick a source/branch/flavor combination to build. Then llvmbuild
106 # will invoke GNU make with -j (--jobs / --threads) to use up the
107 # remaining job capacity. Once a worker is finished with a build, it
108 # will pick another combination off the list and start building it.
110 ##===----------------------------------------------------------------------===##
122 # TODO: Use shutil.which when it is available (3.2 or later)
123 def find_executable(executable, path=None):
124 """Try to find 'executable' in the directories listed in 'path' (a
125 string listing directories separated by 'os.pathsep'; defaults to
126 os.environ['PATH']). Returns the complete filename or None if not
130 path = os.environ['PATH']
131 paths = path.split(os.pathsep)
134 (base, ext) = os.path.splitext(executable)
135 # executable files on OS/2 can have an arbitrary extension, but
136 # .exe is automatically appended if no dot is present in the name
138 executable = executable + ".exe"
139 elif sys.platform == 'win32':
140 pathext = os.environ['PATHEXT'].lower().split(os.pathsep)
141 (base, ext) = os.path.splitext(executable)
142 if ext.lower() not in pathext:
145 execname = executable + ext
146 if os.path.isfile(execname):
150 f = os.path.join(p, execname)
151 if os.path.isfile(f):
156 def is_executable(fpath):
157 return os.path.exists(fpath) and os.access(fpath, os.X_OK)
159 def add_options(parser):
160 parser.add_option("-v", "--verbose", action="store_true",
162 help=("Output informational messages"
163 " [default: %default]"))
164 parser.add_option("--src", action="append",
165 help=("Top-level source directory [default: %default]"))
166 parser.add_option("--build", action="append",
167 help=("Build types to run [default: %default]"))
168 parser.add_option("--branch", action="append",
169 help=("Source branch to build [default: %default]"))
170 parser.add_option("--cc", default=find_executable("cc"),
171 help=("The C compiler to use [default: %default]"))
172 parser.add_option("--cxx", default=find_executable("c++"),
173 help=("The C++ compiler to use [default: %default]"))
174 parser.add_option("--threads", default=4, type="int",
175 help=("The number of worker threads to use "
176 "[default: %default]"))
177 parser.add_option("--jobs", "-j", default=8, type="int",
178 help=("The number of simultaneous build jobs "
179 "[default: %default]"))
180 parser.add_option("--prefix",
181 help=("Root install directory [default: %default]"))
182 parser.add_option("--builddir",
183 help=("Root build directory [default: %default]"))
184 parser.add_option("--extra-llvm-config-flags", default="",
185 help=("Extra flags to pass to llvm configure [default: %default]"))
186 parser.add_option("--extra-llvm-gcc-config-flags", default="",
187 help=("Extra flags to pass to llvm-gcc configure [default: %default]"))
188 parser.add_option("--extra-gcc-config-flags", default="",
189 help=("Extra flags to pass to gcc configure [default: %default]"))
190 parser.add_option("--force-configure", default=False, action="store_true",
191 help=("Force reconfigure of all components"))
194 def check_options(parser, options, valid_builds):
195 # See if we're building valid flavors.
196 for build in options.build:
197 if (build not in valid_builds):
198 parser.error("'" + build + "' is not a valid build flavor "
201 # See if we can find source directories.
202 for src in options.src:
203 for component in components:
204 component = component.rstrip("2")
205 compsrc = src + "/" + component
206 if (not os.path.isdir(compsrc)):
207 parser.error("'" + compsrc + "' does not exist")
208 if (options.branch is not None):
209 for branch in options.branch:
210 if (not os.path.isdir(os.path.join(compsrc, branch))):
211 parser.error("'" + os.path.join(compsrc, branch)
212 + "' does not exist")
214 # See if we can find the compilers
215 options.cc = find_executable(options.cc)
216 options.cxx = find_executable(options.cxx)
220 # Find a unique short name for the given set of paths. This searches
221 # back through path components until it finds unique component names
222 # among all given paths.
223 def get_path_abbrevs(paths):
224 # Find the number of common starting characters in the last component
226 unique_paths = list(paths)
228 class NotFoundException(Exception): pass
230 # Find a unique component of each path.
231 unique_bases = unique_paths[:]
233 while len(unique_paths) > 0:
234 bases = [os.path.basename(src) for src in unique_paths]
235 components = { c for c in bases }
236 # Account for single entry in paths.
237 if len(components) > 1 or len(components) == len(bases):
238 # We found something unique.
240 if bases.count(c) == 1:
241 index = bases.index(c)
242 unique_bases[index] = c
243 # Remove the corresponding path from the set under
245 unique_paths[index] = None
246 unique_paths = [ p for p in unique_paths if p is not None ]
247 unique_paths = [os.path.dirname(src) for src in unique_paths]
249 if len(unique_paths) > 0:
250 raise NotFoundException()
252 abbrevs = dict(zip(paths, [base for base in unique_bases]))
256 # Given a set of unique names, find a short character sequence that
257 # uniquely identifies them.
258 def get_short_abbrevs(unique_bases):
259 # Find a unique start character for each path base.
260 my_unique_bases = unique_bases[:]
261 unique_char_starts = unique_bases[:]
262 while len(my_unique_bases) > 0:
263 for start, char_tuple in enumerate(zip(*[base
264 for base in my_unique_bases])):
265 chars = { c for c in char_tuple }
266 # Account for single path.
267 if len(chars) > 1 or len(chars) == len(char_tuple):
268 # We found something unique.
270 if char_tuple.count(c) == 1:
271 index = char_tuple.index(c)
272 unique_char_starts[index] = start
273 # Remove the corresponding path from the set under
275 my_unique_bases[index] = None
276 my_unique_bases = [ b for b in my_unique_bases
280 if len(my_unique_bases) > 0:
281 raise NotFoundException()
283 abbrevs = [abbrev[start_index:start_index+3]
284 for abbrev, start_index
285 in zip([base for base in unique_bases],
286 [index for index in unique_char_starts])]
288 abbrevs = dict(zip(unique_bases, abbrevs))
292 class Builder(threading.Thread):
293 class ExecutableNotFound(Exception): pass
294 class FileNotExecutable(Exception): pass
296 def __init__(self, work_queue, jobs,
297 build_abbrev, source_abbrev, branch_abbrev,
300 self.work_queue = work_queue
303 self.cxx = options.cxx
304 self.build_abbrev = build_abbrev
305 self.source_abbrev = source_abbrev
306 self.branch_abbrev = branch_abbrev
307 self.build_prefix = options.builddir
308 self.install_prefix = options.prefix
309 self.options = options
310 self.component_abbrev = dict(
319 source, branch, build = self.work_queue.get()
320 self.dobuild(source, branch, build)
322 traceback.print_exc()
324 self.work_queue.task_done()
326 def execute(self, command, execdir, env, component):
327 prefix = self.component_abbrev[component.replace("-", "_")]
329 if not os.path.exists(execdir):
332 execenv = os.environ.copy()
334 for key, value in env.items():
337 self.logger.debug("[" + prefix + "] " + "env " + str(env) + " "
338 + " ".join(command));
341 proc = subprocess.Popen(command,
344 stdout=subprocess.PIPE,
345 stderr=subprocess.STDOUT)
347 line = proc.stdout.readline()
349 self.logger.info("[" + prefix + "] "
350 + str(line, "utf-8").rstrip())
351 line = proc.stdout.readline()
354 traceback.print_exc()
356 # Get a list of C++ include directories to pass to clang.
357 def get_includes(self):
358 # Assume we're building with g++ for now.
360 command += ["-v", "-x", "c++", "/dev/null", "-fsyntax-only"]
362 self.logger.debug(command)
364 proc = subprocess.Popen(command,
365 stdout=subprocess.PIPE,
366 stderr=subprocess.STDOUT)
369 line = proc.stdout.readline()
371 self.logger.debug(line)
372 if re.search("End of search list", str(line)) is not None:
373 self.logger.debug("Stop Gather")
376 includes.append(str(line, "utf-8").strip())
377 if re.search("#include <...> search starts", str(line)) is not None:
378 self.logger.debug("Start Gather")
380 line = proc.stdout.readline()
382 traceback.print_exc()
383 self.logger.debug(includes)
386 def dobuild(self, source, branch, build):
389 ssabbrev = get_short_abbrevs([ab for ab in self.source_abbrev.values()])
391 if branch is not None:
392 sbabbrev = get_short_abbrevs([ab for ab in self.branch_abbrev.values()])
394 prefix = "[" + ssabbrev[self.source_abbrev[source]] + "-" + sbabbrev[self.branch_abbrev[branch]] + "-" + self.build_abbrev[build] + "]"
395 self.install_prefix += "/" + self.source_abbrev[source] + "/" + branch + "/" + build
396 build_suffix += self.source_abbrev[source] + "/" + branch + "/" + build
398 prefix = "[" + ssabbrev[self.source_abbrev[source]] + "-" + self.build_abbrev[build] + "]"
399 self.install_prefix += "/" + self.source_abbrev[source] + "/" + build
400 build_suffix += "/" + self.source_abbrev[source] + "/" + build
402 self.logger = logging.getLogger(prefix)
404 self.logger.debug(self.install_prefix)
406 # Assume we're building with gcc for now.
407 cxxincludes = self.get_includes()
408 cxxroot = cxxincludes[0]
409 cxxarch = os.path.basename(cxxincludes[1])
411 configure_flags = dict(
412 llvm=dict(debug=["--prefix=" + self.install_prefix,
413 "--with-extra-options=-Werror",
414 "--enable-assertions",
415 "--disable-optimized",
416 "--with-cxx-include-root=" + cxxroot,
417 "--with-cxx-include-arch=" + cxxarch],
418 release=["--prefix=" + self.install_prefix,
419 "--with-extra-options=-Werror",
420 "--enable-optimized",
421 "--with-cxx-include-root=" + cxxroot,
422 "--with-cxx-include-arch=" + cxxarch],
423 paranoid=["--prefix=" + self.install_prefix,
424 "--with-extra-options=-Werror",
425 "--enable-assertions",
426 "--enable-expensive-checks",
427 "--disable-optimized",
428 "--with-cxx-include-root=" + cxxroot,
429 "--with-cxx-include-arch=" + cxxarch]),
430 llvm_gcc=dict(debug=["--prefix=" + self.install_prefix,
432 "--program-prefix=llvm-",
433 "--enable-llvm=" + self.build_prefix + "/llvm/" + build_suffix,
434 # Fortran install seems to be broken.
435 # "--enable-languages=c,c++,fortran"],
436 "--enable-languages=c,c++"],
437 release=["--prefix=" + self.install_prefix,
438 "--program-prefix=llvm-",
439 "--enable-llvm=" + self.build_prefix + "/llvm/" + build_suffix,
440 # Fortran install seems to be broken.
441 # "--enable-languages=c,c++,fortran"],
442 "--enable-languages=c,c++"],
443 paranoid=["--prefix=" + self.install_prefix,
445 "--program-prefix=llvm-",
446 "--enable-llvm=" + self.build_prefix + "/llvm/" + build_suffix,
447 # Fortran install seems to be broken.
448 # "--enable-languages=c,c++,fortran"]),
449 "--enable-languages=c,c++"]),
450 llvm2=dict(debug=["--prefix=" + self.install_prefix,
451 "--with-extra-options=-Werror",
452 "--enable-assertions",
453 "--disable-optimized",
454 "--with-llvmgccdir=" + self.install_prefix + "/bin",
455 "--with-cxx-include-root=" + cxxroot,
456 "--with-cxx-include-arch=" + cxxarch],
457 release=["--prefix=" + self.install_prefix,
458 "--with-extra-options=-Werror",
459 "--enable-optimized",
460 "--with-llvmgccdir=" + self.install_prefix + "/bin",
461 "--with-cxx-include-root=" + cxxroot,
462 "--with-cxx-include-arch=" + cxxarch],
463 paranoid=["--prefix=" + self.install_prefix,
464 "--with-extra-options=-Werror",
465 "--enable-assertions",
466 "--enable-expensive-checks",
467 "--disable-optimized",
468 "--with-llvmgccdir=" + self.install_prefix + "/bin",
469 "--with-cxx-include-root=" + cxxroot,
470 "--with-cxx-include-arch=" + cxxarch]),
471 gcc=dict(debug=["--prefix=" + self.install_prefix,
472 "--enable-checking"],
473 release=["--prefix=" + self.install_prefix],
474 paranoid=["--prefix=" + self.install_prefix,
475 "--enable-checking"]),
476 dragonegg=dict(debug=[],
480 configure_env = dict(
481 llvm=dict(debug=dict(CC=self.cc,
483 release=dict(CC=self.cc,
485 paranoid=dict(CC=self.cc,
487 llvm_gcc=dict(debug=dict(CC=self.cc,
489 release=dict(CC=self.cc,
491 paranoid=dict(CC=self.cc,
493 llvm2=dict(debug=dict(CC=self.cc,
495 release=dict(CC=self.cc,
497 paranoid=dict(CC=self.cc,
499 gcc=dict(debug=dict(CC=self.cc,
501 release=dict(CC=self.cc,
503 paranoid=dict(CC=self.cc,
505 dragonegg=dict(debug=dict(CC=self.cc,
507 release=dict(CC=self.cc,
509 paranoid=dict(CC=self.cc,
513 llvm=dict(debug=["-j" + str(self.jobs)],
514 release=["-j" + str(self.jobs)],
515 paranoid=["-j" + str(self.jobs)]),
516 llvm_gcc=dict(debug=["-j" + str(self.jobs),
518 release=["-j" + str(self.jobs),
520 paranoid=["-j" + str(self.jobs),
522 llvm2=dict(debug=["-j" + str(self.jobs)],
523 release=["-j" + str(self.jobs)],
524 paranoid=["-j" + str(self.jobs)]),
525 gcc=dict(debug=["-j" + str(self.jobs),
527 release=["-j" + str(self.jobs),
529 paranoid=["-j" + str(self.jobs),
531 dragonegg=dict(debug=["-j" + str(self.jobs)],
532 release=["-j" + str(self.jobs)],
533 paranoid=["-j" + str(self.jobs)]))
536 llvm=dict(debug=dict(),
539 llvm_gcc=dict(debug=dict(),
542 llvm2=dict(debug=dict(),
545 gcc=dict(debug=dict(),
548 dragonegg=dict(debug=dict(GCC=self.install_prefix + "/bin/gcc",
549 LLVM_CONFIG=self.install_prefix + "/bin/llvm-config"),
550 release=dict(GCC=self.install_prefix + "/bin/gcc",
551 LLVM_CONFIG=self.install_prefix + "/bin/llvm-config"),
552 paranoid=dict(GCC=self.install_prefix + "/bin/gcc",
553 LLVM_CONFIG=self.install_prefix + "/bin/llvm-config")))
555 make_install_flags = dict(
556 llvm=dict(debug=["install"],
558 paranoid=["install"]),
559 llvm_gcc=dict(debug=["install"],
561 paranoid=["install"]),
562 llvm2=dict(debug=["install"],
564 paranoid=["install"]),
565 gcc=dict(debug=["install"],
567 paranoid=["install"]),
568 dragonegg=dict(debug=["install"],
570 paranoid=["install"]))
572 make_install_env = dict(
573 llvm=dict(debug=dict(),
576 llvm_gcc=dict(debug=dict(),
579 llvm2=dict(debug=dict(),
582 gcc=dict(debug=dict(),
585 dragonegg=dict(debug=dict(),
589 make_check_flags = dict(
590 llvm=dict(debug=["check"],
593 llvm_gcc=dict(debug=["check"],
596 llvm2=dict(debug=["check"],
599 gcc=dict(debug=["check"],
602 dragonegg=dict(debug=["check"],
606 make_check_env = dict(
607 llvm=dict(debug=dict(),
610 llvm_gcc=dict(debug=dict(),
613 llvm2=dict(debug=dict(),
616 gcc=dict(debug=dict(),
619 dragonegg=dict(debug=dict(),
623 for component in components:
626 srcdir = source + "/" + comp.rstrip("2")
627 builddir = self.build_prefix + "/" + comp + "/" + build_suffix
628 installdir = self.install_prefix
630 if (branch is not None):
631 srcdir += "/" + branch
633 comp_key = comp.replace("-", "_")
635 config_args = configure_flags[comp_key][build][:]
636 config_args.extend(getattr(self.options,
637 "extra_" + comp_key.rstrip("2")
638 + "_config_flags").split())
640 self.logger.info("Configuring " + component + " in " + builddir)
641 self.configure(component, srcdir, builddir,
643 configure_env[comp_key][build])
645 self.logger.info("Building " + component + " in " + builddir)
646 self.make(component, srcdir, builddir,
647 make_flags[comp_key][build],
648 make_env[comp_key][build])
650 self.logger.info("Installing " + component + " in " + installdir)
651 self.make(component, srcdir, builddir,
652 make_install_flags[comp_key][build],
653 make_install_env[comp_key][build])
655 self.logger.info("Testing " + component + " in " + builddir)
656 self.make(component, srcdir, builddir,
657 make_check_flags[comp_key][build],
658 make_check_env[comp_key][build])
661 def configure(self, component, srcdir, builddir, flags, env):
662 self.logger.debug("Configure " + str(flags) + " " + str(srcdir) + " -> "
665 configure_files = dict(
666 llvm=[(srcdir + "/configure", builddir + "/Makefile")],
667 llvm_gcc=[(srcdir + "/configure", builddir + "/Makefile"),
668 (srcdir + "/gcc/configure", builddir + "/gcc/Makefile")],
669 llvm2=[(srcdir + "/configure", builddir + "/Makefile")],
670 gcc=[(srcdir + "/configure", builddir + "/Makefile"),
671 (srcdir + "/gcc/configure", builddir + "/gcc/Makefile")],
676 for conf, mf in configure_files[component.replace("-", "_")]:
677 if not os.path.exists(conf):
679 if os.path.exists(conf) and os.path.exists(mf):
680 confstat = os.stat(conf)
681 makestat = os.stat(mf)
682 if confstat.st_mtime > makestat.st_mtime:
689 if not doconfig and not self.options.force_configure:
692 program = srcdir + "/configure"
693 if not is_executable(program):
697 args += ["--verbose"]
699 self.execute(args, builddir, env, component)
701 def make(self, component, srcdir, builddir, flags, env):
702 program = find_executable("make")
704 raise ExecutableNotFound
706 if not is_executable(program):
707 raise FileNotExecutable
711 self.execute(args, builddir, env, component)
714 build_abbrev = dict(debug="dbg", release="opt", paranoid="par")
715 #components = ["llvm", "llvm-gcc", "llvm2", "gcc", "dragonegg"]
716 components = ["llvm", "llvm2", "gcc", "dragonegg"]
719 parser = optparse.OptionParser(version="%prog 1.0")
721 (options, args) = parser.parse_args()
722 check_options(parser, options, build_abbrev.keys());
725 logging.basicConfig(level=logging.DEBUG,
726 format='%(name)-13s: %(message)s')
728 logging.basicConfig(level=logging.INFO,
729 format='%(name)-13s: %(message)s')
731 source_abbrev = get_path_abbrevs(set(options.src))
734 if options.branch is not None:
735 branch_abbrev = get_path_abbrevs(set(options.branch))
737 work_queue = queue.Queue()
739 jobs = options.jobs // options.threads
743 numthreads = options.threads
744 if jobs < numthreads:
748 for t in range(numthreads):
749 builder = Builder(work_queue, jobs,
750 build_abbrev, source_abbrev, branch_abbrev,
752 builder.daemon = True
755 for build in set(options.build):
756 for source in set(options.src):
757 if options.branch is not None:
758 for branch in set(options.branch):
759 work_queue.put((source, branch, build))
761 work_queue.put((source, None, build))