Skip to content

Commit 6860654

Browse files
committed
allow bypassing the build directory lock
As bootstrap locks its entire build directory, parallel bootstrapping for anything becomes impossible. This change enables developers to bypass the locking mechanism when it is unnecessary for their specific use case. Signed-off-by: onur-ozkan <work@onurozkan.dev>
1 parent ee5ef3a commit 6860654

7 files changed

Lines changed: 128 additions & 68 deletions

File tree

‎src/bootstrap/src/bin/main.rs‎

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,40 @@ fn main() {
2323
letmut build_lock;
2424
#[cfg(all(any(unix, windows), not(target_os = "solaris")))]
2525
let _build_lock_guard;
26-
#[cfg(all(any(unix, windows), not(target_os = "solaris")))]
27-
// Display PID of process holding the lock
28-
// PID will be stored in a lock file
29-
{
30-
let path = config.out.join("lock");
31-
let pid = match fs::read_to_string(&path){
32-
Ok(contents) => contents,
33-
Err(_) => String::new(),
34-
};
35-
36-
build_lock =
37-
fd_lock::RwLock::new(t!(fs::OpenOptions::new().write(true).create(true).open(&path)));
38-
_build_lock_guard = match build_lock.try_write(){
39-
Ok(mut lock) => {
40-
t!(lock.write(&process::id().to_string().as_ref()));
41-
lock
42-
}
43-
err => {
44-
drop(err);
45-
println!("WARNING: build directory locked by process {pid}, waiting for lock");
46-
letmut lock = t!(build_lock.write());
47-
t!(lock.write(&process::id().to_string().as_ref()));
48-
lock
49-
}
50-
};
51-
}
5226

53-
#[cfg(any(not(any(unix, windows)), target_os = "solaris"))]
54-
println!("WARNING: file locking not supported for target, not locking build directory");
27+
if !config.bypass_bootstrap_lock{
28+
// Display PID of process holding the lock
29+
// PID will be stored in a lock file
30+
#[cfg(all(any(unix, windows), not(target_os = "solaris")))]
31+
{
32+
let path = config.out.join("lock");
33+
let pid = match fs::read_to_string(&path){
34+
Ok(contents) => contents,
35+
Err(_) => String::new(),
36+
};
37+
38+
build_lock = fd_lock::RwLock::new(t!(fs::OpenOptions::new()
39+
.write(true)
40+
.create(true)
41+
.open(&path)));
42+
_build_lock_guard = match build_lock.try_write(){
43+
Ok(mut lock) => {
44+
t!(lock.write(&process::id().to_string().as_ref()));
45+
lock
46+
}
47+
err => {
48+
drop(err);
49+
println!("WARNING: build directory locked by process {pid}, waiting for lock");
50+
letmut lock = t!(build_lock.write());
51+
t!(lock.write(&process::id().to_string().as_ref()));
52+
lock
53+
}
54+
};
55+
}
56+
57+
#[cfg(any(not(any(unix, windows)), target_os = "solaris"))]
58+
println!("WARNING: file locking not supported for target, not locking build directory");
59+
}
5560

5661
// check_version warnings are not printed during setup
5762
let changelog_suggestion =

‎src/bootstrap/src/core/config/config.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ impl Display for DebuginfoLevel {
117117
pubstructConfig{
118118
pubchangelog_seen:Option<usize>,// FIXME: Deprecated field. Remove it at 2024.
119119
pubchange_id:Option<usize>,
120+
pubbypass_bootstrap_lock:bool,
120121
pubccache:Option<String>,
121122
/// Call Build::ninja() instead of this.
122123
pubninja_in_file:bool,
@@ -1057,6 +1058,7 @@ define_config! {
10571058
implConfig{
10581059
pubfndefault_opts() -> Config{
10591060
letmut config = Config::default();
1061+
config.bypass_bootstrap_lock = false;
10601062
config.llvm_optimize = true;
10611063
config.ninja_in_file = true;
10621064
config.llvm_static_stdcpp = false;
@@ -1135,6 +1137,7 @@ impl Config {
11351137
config.llvm_profile_use = flags.llvm_profile_use;
11361138
config.llvm_profile_generate = flags.llvm_profile_generate;
11371139
config.enable_bolt_settings = flags.enable_bolt_settings;
1140+
config.bypass_bootstrap_lock = flags.bypass_bootstrap_lock;
11381141

11391142
// Infer the rest of the configuration.
11401143

‎src/bootstrap/src/core/config/flags.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ pub struct Flags {
133133
/// whether to use color in cargo and rustc output
134134
pubcolor:Color,
135135

136+
#[arg(global(true), long)]
137+
/// Bootstrap uses this value to decide whether it should bypass locking the build process.
138+
/// This is rarely needed (e.g., compiling the std library for different targets in parallel).
139+
///
140+
/// Unless you know exactly what you are doing, you probably don't need this.
141+
pubbypass_bootstrap_lock:bool,
142+
136143
/// whether rebuilding llvm should be skipped, overriding `skip-rebuld` in config.toml
137144
#[arg(global(true), long, value_name = "VALUE")]
138145
publlvm_skip_rebuild:Option<bool>,

0 commit comments

Comments
 (0)