1 # Copyright 2015-2016 The Rust Project Developers. See the COPYRIGHT
2 # file at the top-level directory of this distribution and at
3 # http://rust-lang.org/COPYRIGHT.
5 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 # option. This file may not be copied, modified, or distributed
9 # except according to those terms.
11 from __future__ import print_function
26 def get(url, path, verbose=False):
27 sha_url = url + ".sha256"
28 with tempfile.NamedTemporaryFile(delete=False) as temp_file:
29 temp_path = temp_file.name
30 with tempfile.NamedTemporaryFile(suffix=".sha256", delete=False) as sha_file:
31 sha_path = sha_file.name
34 download(sha_path, sha_url, False, verbose)
35 if os.path.exists(path):
36 if verify(path, sha_path, False):
38 print("using already-download file " + path)
42 print("ignoring already-download file " + path + " due to failed verification")
44 download(temp_path, url, True, verbose)
45 if not verify(temp_path, sha_path, verbose):
46 raise RuntimeError("failed verification")
48 print("moving {} to {}".format(temp_path, path))
49 shutil.move(temp_path, path)
51 delete_if_present(sha_path, verbose)
52 delete_if_present(temp_path, verbose)
55 def delete_if_present(path, verbose):
56 if os.path.isfile(path):
58 print("removing " + path)
62 def download(path, url, probably_big, verbose):
65 _download(path, url, probably_big, verbose, True)
68 print("\nspurious failure, trying again")
69 _download(path, url, probably_big, verbose, False)
72 def _download(path, url, probably_big, verbose, exception):
73 if probably_big or verbose:
74 print("downloading {}".format(url))
75 # see http://serverfault.com/questions/301128/how-to-download
76 if sys.platform == 'win32':
77 run(["PowerShell.exe", "/nologo", "-Command",
78 "(New-Object System.Net.WebClient)"
79 ".DownloadFile('{}', '{}')".format(url, path)],
83 if probably_big or verbose:
87 run(["curl", option, "--retry", "3", "-Sf", "-o", path, url],
92 def verify(path, sha_path, verbose):
94 print("verifying " + path)
95 with open(path, "rb") as f:
96 found = hashlib.sha256(f.read()).hexdigest()
97 with open(sha_path, "r") as f:
98 expected = f.readline().split()[0]
99 verified = found == expected
101 print("invalid checksum:\n"
103 " expected: {}".format(found, expected))
107 def unpack(tarball, dst, verbose=False, match=None):
108 print("extracting " + tarball)
109 fname = os.path.basename(tarball).replace(".tar.gz", "")
110 with contextlib.closing(tarfile.open(tarball)) as tar:
111 for p in tar.getnames():
114 name = p.replace(fname + "/", "", 1)
115 if match is not None and not name.startswith(match):
117 name = name[len(match) + 1:]
119 fp = os.path.join(dst, name)
121 print(" extracting " + p)
123 tp = os.path.join(dst, p)
124 if os.path.isdir(tp) and os.path.exists(fp):
127 shutil.rmtree(os.path.join(dst, fname))
129 def run(args, verbose=False, exception=False):
131 print("running: " + ' '.join(args))
133 # Use Popen here instead of call() as it apparently allows powershell on
134 # Windows to not lock up waiting for input presumably.
135 ret = subprocess.Popen(args)
138 err = "failed to run: " + ' '.join(args)
139 if verbose or exception:
140 raise RuntimeError(err)
143 def stage0_data(rust_root):
144 nightlies = os.path.join(rust_root, "src/stage0.txt")
146 with open(nightlies, 'r') as nightlies:
147 for line in nightlies:
148 line = line.rstrip() # Strip newline character, '\n'
149 if line.startswith("#") or line == '':
151 a, b = line.split(": ", 1)
155 def format_build_time(duration):
156 return str(datetime.timedelta(seconds=int(duration)))
159 class RustBuild(object):
160 def download_stage0(self):
161 cache_dst = os.path.join(self.build_dir, "cache")
162 rustc_cache = os.path.join(cache_dst, self.stage0_rustc_date())
163 if not os.path.exists(rustc_cache):
164 os.makedirs(rustc_cache)
166 if self.rustc().startswith(self.bin_root()) and \
167 (not os.path.exists(self.rustc()) or self.rustc_out_of_date()):
168 self.print_what_it_means_to_bootstrap()
169 if os.path.exists(self.bin_root()):
170 shutil.rmtree(self.bin_root())
171 channel = self.stage0_rustc_channel()
172 filename = "rust-std-{}-{}.tar.gz".format(channel, self.build)
173 url = "https://static.rust-lang.org/dist/" + self.stage0_rustc_date()
174 tarball = os.path.join(rustc_cache, filename)
175 if not os.path.exists(tarball):
176 get("{}/{}".format(url, filename), tarball, verbose=self.verbose)
177 unpack(tarball, self.bin_root(),
178 match="rust-std-" + self.build,
179 verbose=self.verbose)
181 filename = "rustc-{}-{}.tar.gz".format(channel, self.build)
182 url = "https://static.rust-lang.org/dist/" + self.stage0_rustc_date()
183 tarball = os.path.join(rustc_cache, filename)
184 if not os.path.exists(tarball):
185 get("{}/{}".format(url, filename), tarball, verbose=self.verbose)
186 unpack(tarball, self.bin_root(), match="rustc", verbose=self.verbose)
187 self.fix_executable(self.bin_root() + "/bin/rustc")
188 self.fix_executable(self.bin_root() + "/bin/rustdoc")
189 with open(self.rustc_stamp(), 'w') as f:
190 f.write(self.stage0_rustc_date())
192 if self.cargo().startswith(self.bin_root()) and \
193 (not os.path.exists(self.cargo()) or self.cargo_out_of_date()):
194 self.print_what_it_means_to_bootstrap()
195 filename = "cargo-{}-{}.tar.gz".format(channel, self.build)
196 url = "https://static.rust-lang.org/dist/" + self.stage0_rustc_date()
197 tarball = os.path.join(rustc_cache, filename)
198 if not os.path.exists(tarball):
199 get("{}/{}".format(url, filename), tarball, verbose=self.verbose)
200 unpack(tarball, self.bin_root(), match="cargo", verbose=self.verbose)
201 self.fix_executable(self.bin_root() + "/bin/cargo")
202 with open(self.cargo_stamp(), 'w') as f:
203 f.write(self.stage0_rustc_date())
205 def fix_executable(self, fname):
206 # If we're on NixOS we need to change the path to the dynamic loader
208 default_encoding = sys.getdefaultencoding()
210 ostype = subprocess.check_output(['uname', '-s']).strip().decode(default_encoding)
211 except (subprocess.CalledProcessError, WindowsError):
214 if ostype != "Linux":
217 if not os.path.exists("/etc/NIXOS"):
219 if os.path.exists("/lib"):
222 # At this point we're pretty sure the user is running NixOS
223 print("info: you seem to be running NixOS. Attempting to patch " + fname)
226 interpreter = subprocess.check_output(["patchelf", "--print-interpreter", fname])
227 interpreter = interpreter.strip().decode(default_encoding)
228 except subprocess.CalledProcessError as e:
229 print("warning: failed to call patchelf: %s" % e)
232 loader = interpreter.split("/")[-1]
235 ldd_output = subprocess.check_output(['ldd', '/run/current-system/sw/bin/sh'])
236 ldd_output = ldd_output.strip().decode(default_encoding)
237 except subprocess.CalledProcessError as e:
238 print("warning: unable to call ldd: %s" % e)
241 for line in ldd_output.splitlines():
242 libname = line.split()[0]
243 if libname.endswith(loader):
244 loader_path = libname[:len(libname) - len(loader)]
247 print("warning: unable to find the path to the dynamic linker")
250 correct_interpreter = loader_path + loader
253 subprocess.check_output(["patchelf", "--set-interpreter", correct_interpreter, fname])
254 except subprocess.CalledProcessError as e:
255 print("warning: failed to call patchelf: %s" % e)
258 def stage0_rustc_date(self):
259 return self._rustc_date
261 def stage0_rustc_channel(self):
262 return self._rustc_channel
264 def rustc_stamp(self):
265 return os.path.join(self.bin_root(), '.rustc-stamp')
267 def cargo_stamp(self):
268 return os.path.join(self.bin_root(), '.cargo-stamp')
270 def rustc_out_of_date(self):
271 if not os.path.exists(self.rustc_stamp()) or self.clean:
273 with open(self.rustc_stamp(), 'r') as f:
274 return self.stage0_rustc_date() != f.read()
276 def cargo_out_of_date(self):
277 if not os.path.exists(self.cargo_stamp()) or self.clean:
279 with open(self.cargo_stamp(), 'r') as f:
280 return self.stage0_rustc_date() != f.read()
283 return os.path.join(self.build_dir, self.build, "stage0")
285 def get_toml(self, key):
286 for line in self.config_toml.splitlines():
287 if line.startswith(key + ' ='):
288 return self.get_string(line)
291 def get_mk(self, key):
292 for line in iter(self.config_mk.splitlines()):
293 if line.startswith(key + ' '):
294 var = line[line.find(':=') + 2:].strip()
300 config = self.get_toml('cargo')
303 config = self.get_mk('CFG_LOCAL_RUST_ROOT')
305 return config + '/bin/cargo' + self.exe_suffix()
306 return os.path.join(self.bin_root(), "bin/cargo" + self.exe_suffix())
309 config = self.get_toml('rustc')
312 config = self.get_mk('CFG_LOCAL_RUST_ROOT')
314 return config + '/bin/rustc' + self.exe_suffix()
315 return os.path.join(self.bin_root(), "bin/rustc" + self.exe_suffix())
317 def get_string(self, line):
318 start = line.find('"')
319 end = start + 1 + line[start + 1:].find('"')
320 return line[start + 1:end]
322 def exe_suffix(self):
323 if sys.platform == 'win32':
328 def print_what_it_means_to_bootstrap(self):
329 if hasattr(self, 'printed'):
332 if os.path.exists(self.bootstrap_binary()):
334 if not '--help' in sys.argv or len(sys.argv) == 1:
337 print('info: the build system for Rust is written in Rust, so this')
338 print(' script is now going to download a stage0 rust compiler')
339 print(' and then compile the build system itself')
341 print('info: in the meantime you can read more about rustbuild at')
342 print(' src/bootstrap/README.md before the download finishes')
344 def bootstrap_binary(self):
345 return os.path.join(self.build_dir, "bootstrap/debug/bootstrap")
347 def build_bootstrap(self):
348 self.print_what_it_means_to_bootstrap()
349 build_dir = os.path.join(self.build_dir, "bootstrap")
350 if self.clean and os.path.exists(build_dir):
351 shutil.rmtree(build_dir)
352 env = os.environ.copy()
353 env["CARGO_TARGET_DIR"] = build_dir
354 env["RUSTC"] = self.rustc()
355 env["LD_LIBRARY_PATH"] = os.path.join(self.bin_root(), "lib") + \
356 (os.pathsep + env["LD_LIBRARY_PATH"]) \
357 if "LD_LIBRARY_PATH" in env else ""
358 env["DYLD_LIBRARY_PATH"] = os.path.join(self.bin_root(), "lib") + \
359 (os.pathsep + env["DYLD_LIBRARY_PATH"]) \
360 if "DYLD_LIBRARY_PATH" in env else ""
361 env["PATH"] = os.path.join(self.bin_root(), "bin") + \
362 os.pathsep + env["PATH"]
363 if not os.path.isfile(self.cargo()):
364 raise Exception("no cargo executable found at `%s`" % self.cargo())
365 args = [self.cargo(), "build", "--manifest-path",
366 os.path.join(self.rust_root, "src/bootstrap/Cargo.toml")]
367 if self.use_locked_deps:
368 args.append("--locked")
369 if self.use_vendored_sources:
370 args.append("--frozen")
373 def run(self, args, env):
374 proc = subprocess.Popen(args, env=env)
379 def build_triple(self):
380 default_encoding = sys.getdefaultencoding()
381 config = self.get_toml('build')
384 config = self.get_mk('CFG_BUILD')
388 ostype = subprocess.check_output(['uname', '-s']).strip().decode(default_encoding)
389 cputype = subprocess.check_output(['uname', '-m']).strip().decode(default_encoding)
390 except (subprocess.CalledProcessError, OSError):
391 if sys.platform == 'win32':
392 return 'x86_64-pc-windows-msvc'
393 err = "uname not found"
398 # Darwin's `uname -s` lies and always returns i386. We have to use
400 if ostype == 'Darwin' and cputype == 'i686':
401 args = ['sysctl', 'hw.optional.x86_64']
402 sysctl = subprocess.check_output(args).decode(default_encoding)
406 # The goal here is to come up with the same triple as LLVM would,
407 # at least for the subset of platforms we're willing to target.
408 if ostype == 'Linux':
409 ostype = 'unknown-linux-gnu'
410 elif ostype == 'FreeBSD':
411 ostype = 'unknown-freebsd'
412 elif ostype == 'DragonFly':
413 ostype = 'unknown-dragonfly'
414 elif ostype == 'Bitrig':
415 ostype = 'unknown-bitrig'
416 elif ostype == 'OpenBSD':
417 ostype = 'unknown-openbsd'
418 elif ostype == 'NetBSD':
419 ostype = 'unknown-netbsd'
420 elif ostype == 'SunOS':
421 ostype = 'sun-solaris'
422 # On Solaris, uname -m will return a machine classification instead
423 # of a cpu type, so uname -p is recommended instead. However, the
424 # output from that option is too generic for our purposes (it will
425 # always emit 'i386' on x86/amd64 systems). As such, isainfo -k
426 # must be used instead.
428 cputype = subprocess.check_output(['isainfo',
429 '-k']).strip().decode(default_encoding)
430 except (subprocess.CalledProcessError, OSError):
431 err = "isainfo not found"
435 elif ostype == 'Darwin':
436 ostype = 'apple-darwin'
437 elif ostype == 'Haiku':
438 ostype = 'unknown-haiku'
439 elif ostype.startswith('MINGW'):
440 # msys' `uname` does not print gcc configuration, but prints msys
441 # configuration. so we cannot believe `uname -m`:
442 # msys1 is always i686 and msys2 is always x86_64.
443 # instead, msys defines $MSYSTEM which is MINGW32 on i686 and
445 ostype = 'pc-windows-gnu'
447 if os.environ.get('MSYSTEM') == 'MINGW64':
449 elif ostype.startswith('MSYS'):
450 ostype = 'pc-windows-gnu'
451 elif ostype.startswith('CYGWIN_NT'):
453 if ostype.endswith('WOW64'):
455 ostype = 'pc-windows-gnu'
457 err = "unknown OS type: " + ostype
459 raise ValueError(err)
462 if cputype in {'i386', 'i486', 'i686', 'i786', 'x86'}:
464 elif cputype in {'xscale', 'arm'}:
466 elif cputype in {'armv6l', 'armv7l', 'armv8l'}:
469 elif cputype == 'armv7l':
472 elif cputype == 'aarch64':
474 elif cputype == 'arm64':
476 elif cputype == 'mips':
477 if sys.byteorder == 'big':
479 elif sys.byteorder == 'little':
482 raise ValueError('unknown byteorder: ' + sys.byteorder)
483 elif cputype == 'mips64':
484 if sys.byteorder == 'big':
486 elif sys.byteorder == 'little':
489 raise ValueError('unknown byteorder: ' + sys.byteorder)
490 # only the n64 ABI is supported, indicate it
492 elif cputype in {'powerpc', 'ppc'}:
494 elif cputype in {'powerpc64', 'ppc64'}:
495 cputype = 'powerpc64'
496 elif cputype in {'powerpc64le', 'ppc64le'}:
497 cputype = 'powerpc64le'
498 elif cputype == 'sparcv9':
500 elif cputype in {'amd64', 'x86_64', 'x86-64', 'x64'}:
502 elif cputype == 's390x':
504 elif cputype == 'BePC':
507 err = "unknown cpu type: " + cputype
509 raise ValueError(err)
512 return "{}-{}".format(cputype, ostype)
515 parser = argparse.ArgumentParser(description='Build rust')
516 parser.add_argument('--config')
517 parser.add_argument('--clean', action='store_true')
518 parser.add_argument('-v', '--verbose', action='store_true')
520 args = [a for a in sys.argv if a != '-h' and a != '--help']
521 args, _ = parser.parse_known_args(args)
523 # Configure initial bootstrap
527 rb.rust_root = os.path.abspath(os.path.join(__file__, '../../..'))
528 rb.build_dir = os.path.join(os.getcwd(), "build")
529 rb.verbose = args.verbose
530 rb.clean = args.clean
533 with open(args.config or 'config.toml') as config:
534 rb.config_toml = config.read()
538 rb.config_mk = open('config.mk').read()
542 rb.use_vendored_sources = '\nvendor = true' in rb.config_toml or \
543 'CFG_ENABLE_VENDOR' in rb.config_mk
545 rb.use_locked_deps = '\nlocked-deps = true' in rb.config_toml or \
546 'CFG_ENABLE_LOCKED_DEPS' in rb.config_mk
548 if 'SUDO_USER' in os.environ and not rb.use_vendored_sources:
549 if os.environ.get('USER') != os.environ['SUDO_USER']:
550 rb.use_vendored_sources = True
551 print('info: looks like you are running this command under `sudo`')
552 print(' and so in order to preserve your $HOME this will now')
553 print(' use vendored sources by default. Note that if this')
554 print(' does not work you should run a normal build first')
555 print(' before running a command like `sudo make install`')
557 if rb.use_vendored_sources:
558 if not os.path.exists('.cargo'):
559 os.makedirs('.cargo')
560 with open('.cargo/config','w') as f:
563 replace-with = 'vendored-sources'
564 registry = 'https://example.com'
566 [source.vendored-sources]
567 directory = '{}/src/vendor'
568 """.format(rb.rust_root))
570 if os.path.exists('.cargo'):
571 shutil.rmtree('.cargo')
573 data = stage0_data(rb.rust_root)
574 rb._rustc_channel, rb._rustc_date = data['rustc'].split('-', 1)
576 # Fetch/build the bootstrap
577 rb.build = rb.build_triple()
584 args = [rb.bootstrap_binary()]
585 args.extend(sys.argv[1:])
586 env = os.environ.copy()
587 env["BUILD"] = rb.build
588 env["SRC"] = rb.rust_root
589 env["BOOTSTRAP_PARENT_ID"] = str(os.getpid())
596 print("Build completed successfully in %s" % format_build_time(time() - start_time))
597 except (SystemExit, KeyboardInterrupt) as e:
598 if hasattr(e, 'code') and isinstance(e.code, int):
603 print("Build completed unsuccessfully in %s" % format_build_time(time() - start_time))
606 if __name__ == '__main__':