Skip to content

Commit 043c634

Browse files
committed
assert that should_fix_bins_and_dylibs has been run
1 parent b925031 commit 043c634

File tree

3 files changed

+56
-43
lines changed

3 files changed

+56
-43
lines changed

src/bootstrap/bootstrap.py

+50-39
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,10 @@ def __init__(self):
402402
self.rust_root = ''
403403
self.use_locked_deps = False
404404
self.use_vendored_sources = False
405-
self.verbose = 0
405+
self.verbose = False
406406
self.git_version = None
407407
self.nix_deps_dir = None
408+
self._should_fix_bins_and_dylibs = None
408409

409410
def download_toolchain(self):
410411
"""Fetch the build system for Rust, written in Rust
@@ -466,46 +467,54 @@ def _download_component_helper(
466467
"dist/{}/{}".format(key, filename),
467468
tarball,
468469
self.checksums_sha256,
469-
verbose=self.verbose != 0,
470+
verbose=self.verbose,
470471
)
471-
unpack(tarball, tarball_suffix, self.bin_root(), match=pattern, verbose=self.verbose != 0)
472+
unpack(tarball, tarball_suffix, self.bin_root(), match=pattern, verbose=self.verbose)
472473

473474
def should_fix_bins_and_dylibs(self):
474475
"""Whether or not `fix_bin_or_dylib` needs to be run; can only be True
475476
on NixOS.
476477
"""
477-
default_encoding = sys.getdefaultencoding()
478-
try:
479-
ostype = subprocess.check_output(
480-
['uname', '-s']).strip().decode(default_encoding)
481-
except subprocess.CalledProcessError:
482-
return False
483-
except OSError as reason:
484-
if getattr(reason, 'winerror', None) is not None:
478+
if self._should_fix_bins_and_dylibs is not None:
479+
return self._should_fix_bins_and_dylibs
480+
481+
def get_answer():
482+
default_encoding = sys.getdefaultencoding()
483+
try:
484+
ostype = subprocess.check_output(
485+
['uname', '-s']).strip().decode(default_encoding)
486+
except subprocess.CalledProcessError:
485487
return False
486-
raise reason
488+
except OSError as reason:
489+
if getattr(reason, 'winerror', None) is not None:
490+
return False
491+
raise reason
487492

488-
if ostype != "Linux":
489-
return False
493+
if ostype != "Linux":
494+
return False
490495

491-
# If the user has asked binaries to be patched for Nix, then
492-
# don't check for NixOS or `/lib`.
493-
if self.get_toml("patch-binaries-for-nix", "build") == "true":
494-
return True
496+
# If the user has asked binaries to be patched for Nix, then
497+
# don't check for NixOS or `/lib`.
498+
if self.get_toml("patch-binaries-for-nix", "build") == "true":
499+
return True
495500

496-
# Use `/etc/os-release` instead of `/etc/NIXOS`.
497-
# The latter one does not exist on NixOS when using tmpfs as root.
498-
try:
499-
with open("/etc/os-release", "r") as f:
500-
if not any(l.strip() in ("ID=nixos", "ID='nixos'", 'ID="nixos"') for l in f):
501-
return False
502-
except FileNotFoundError:
503-
return False
504-
if os.path.exists("/lib"):
505-
return False
501+
# Use `/etc/os-release` instead of `/etc/NIXOS`.
502+
# The latter one does not exist on NixOS when using tmpfs as root.
503+
try:
504+
with open("/etc/os-release", "r") as f:
505+
if not any(l.strip() in ("ID=nixos", "ID='nixos'", 'ID="nixos"') for l in f):
506+
return False
507+
except FileNotFoundError:
508+
return False
509+
if os.path.exists("/lib"):
510+
return False
511+
512+
return True
506513

507-
print("info: You seem to be using Nix.")
508-
return True
514+
answer = self._should_fix_bins_and_dylibs = get_answer()
515+
if answer:
516+
print("info: You seem to be using Nix.")
517+
return answer
509518

510519
def fix_bin_or_dylib(self, fname):
511520
"""Modifies the interpreter section of 'fname' to fix the dynamic linker,
@@ -516,6 +525,7 @@ def fix_bin_or_dylib(self, fname):
516525
517526
Please see https://nixos.org/patchelf.html for more information
518527
"""
528+
assert self._should_fix_bins_and_dylibs is True
519529
print("attempting to patch", fname)
520530

521531
# Only build `.nix-deps` once.
@@ -707,7 +717,7 @@ def bootstrap_binary(self):
707717
"""
708718
return os.path.join(self.build_dir, "bootstrap", "debug", "bootstrap")
709719

710-
def build_bootstrap(self, color):
720+
def build_bootstrap(self, color, verbose_count):
711721
"""Build bootstrap"""
712722
print("Building bootstrap")
713723
build_dir = os.path.join(self.build_dir, "bootstrap")
@@ -764,7 +774,7 @@ def build_bootstrap(self, color):
764774
self.cargo()))
765775
args = [self.cargo(), "build", "--manifest-path",
766776
os.path.join(self.rust_root, "src/bootstrap/Cargo.toml")]
767-
args.extend("--verbose" for _ in range(self.verbose))
777+
args.extend("--verbose" for _ in range(verbose_count))
768778
if self.use_locked_deps:
769779
args.append("--locked")
770780
if self.use_vendored_sources:
@@ -778,7 +788,7 @@ def build_bootstrap(self, color):
778788
args.append("--color=never")
779789

780790
# Run this from the source directory so cargo finds .cargo/config
781-
run(args, env=env, verbose=self.verbose != 0, cwd=self.rust_root)
791+
run(args, env=env, verbose=self.verbose, cwd=self.rust_root)
782792

783793
def build_triple(self):
784794
"""Build triple as in LLVM
@@ -787,7 +797,7 @@ def build_triple(self):
787797
so use `self.build` where possible.
788798
"""
789799
config = self.get_toml('build')
790-
return config or default_build_triple(self.verbose != 0)
800+
return config or default_build_triple(self.verbose)
791801

792802
def check_vendored_status(self):
793803
"""Check that vendoring is configured properly"""
@@ -838,7 +848,7 @@ def bootstrap(args):
838848
# Configure initial bootstrap
839849
build = RustBuild()
840850
build.rust_root = os.path.abspath(os.path.join(__file__, '../../..'))
841-
build.verbose = args.verbose
851+
build.verbose = args.verbose != 0
842852
build.clean = args.clean
843853

844854
# Read from `--config`, then `RUST_BOOTSTRAP_CONFIG`, then `./config.toml`,
@@ -866,9 +876,10 @@ def bootstrap(args):
866876
with open(include_path) as included_toml:
867877
build.config_toml += os.linesep + included_toml.read()
868878

869-
config_verbose = build.get_toml('verbose', 'build')
870-
if config_verbose is not None:
871-
build.verbose = max(build.verbose, int(config_verbose))
879+
verbose_count = args.verbose
880+
config_verbose_count = build.get_toml('verbose', 'build')
881+
if config_verbose_count is not None:
882+
verbose_count = max(args.verbose, int(config_verbose_count))
872883

873884
build.use_vendored_sources = build.get_toml('vendor', 'build') == 'true'
874885
build.use_locked_deps = build.get_toml('locked-deps', 'build') == 'true'
@@ -892,7 +903,7 @@ def bootstrap(args):
892903
# Fetch/build the bootstrap
893904
build.download_toolchain()
894905
sys.stdout.flush()
895-
build.build_bootstrap(args.color)
906+
build.build_bootstrap(args.color, verbose_count)
896907
sys.stdout.flush()
897908

898909
# Run the bootstrap

src/bootstrap/download.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use crate::{
1818
Config,
1919
};
2020

21+
static SHOULD_FIX_BINS_AND_DYLIBS: OnceCell<bool> = OnceCell::new();
22+
2123
/// Generic helpers that are useful anywhere in bootstrap.
2224
impl Config {
2325
pub fn is_verbose(&self) -> bool {
@@ -73,9 +75,7 @@ impl Config {
7375
/// Whether or not `fix_bin_or_dylib` needs to be run; can only be true
7476
/// on NixOS
7577
fn should_fix_bins_and_dylibs(&self) -> bool {
76-
static CACHED: OnceCell<bool> = OnceCell::new();
77-
78-
let val = *CACHED.get_or_init(|| {
78+
let val = *SHOULD_FIX_BINS_AND_DYLIBS.get_or_init(|| {
7979
match Command::new("uname").arg("-s").stderr(Stdio::inherit()).output() {
8080
Err(_) => return false,
8181
Ok(output) if !output.status.success() => return false,
@@ -125,6 +125,7 @@ impl Config {
125125
///
126126
/// Please see https://nixos.org/patchelf.html for more information
127127
fn fix_bin_or_dylib(&self, fname: &Path) {
128+
assert_eq!(SHOULD_FIX_BINS_AND_DYLIBS.get(), Some(&true));
128129
println!("attempting to patch {}", fname.display());
129130

130131
// Only build `.nix-deps` once.

x.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
pass
2323

2424
rust_dir = os.path.dirname(os.path.abspath(__file__))
25-
sys.path[:0] = [os.path.join(rust_dir, "src", "bootstrap")]
25+
# For the import below, have Python search in src/bootstrap first.
26+
sys.path.insert(0, os.path.join(rust_dir, "src", "bootstrap"))
2627

2728
import bootstrap
2829
bootstrap.main()

0 commit comments

Comments
 (0)