Skip to content

Commit 92efd26

Browse files
committed
fix(loader): allow parallel compilation on windows
1 parent 24c8feb commit 92efd26

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,7 @@ jobs:
7474
run: |
7575
printf 'EMSCRIPTEN_VERSION=%s\n' "$(<crates/loader/emscripten-version)" >> $GITHUB_ENV
7676
77-
if [[ '${{ matrix.platform }}' =~ ^windows ]]; then
78-
# Prevent race condition (see #2041)
79-
printf 'RUST_TEST_THREADS=1\n' >> $GITHUB_ENV
80-
elif [[ '${{ matrix.cross }}' == true ]]; then
77+
if [[ '${{ matrix.cross }}' == true ]]; then
8178
for target in armv7-unknown-linux-gnueabihf i686-unknown-linux-gnu powerpc64-unknown-linux-gnu; do
8279
camel_target=${target//-/_}; target_cc=${target/-unknown/}
8380
printf 'CC_%s=%s\n' "$camel_target" "${target_cc/v7/}-gcc"

crates/loader/src/loader.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -904,12 +904,27 @@ impl Loader {
904904

905905
let output_path = config.output_path.as_ref().unwrap();
906906

907-
if compiler.is_like_msvc() {
907+
let temp_dir = if compiler.is_like_msvc() {
908908
let out = format!("-out:{}", output_path.to_str().unwrap());
909909
command.arg(if self.debug_build { "-LDd" } else { "-LD" });
910910
command.arg("-utf-8");
911+
912+
// Windows creates intermediate files when compiling (.exp, .lib, .obj), which causes
913+
// issues when multiple processes are compiling in the same directory. This creates a
914+
// temporary directory for those files to go into, which is deleted after compilation.
915+
let temp_dir = output_path.parent().unwrap().join(format!(
916+
"tmp_{}_{:?}",
917+
std::process::id(),
918+
std::thread::current().id()
919+
));
920+
std::fs::create_dir_all(&temp_dir).unwrap();
921+
922+
command.arg(format!("/Fo{}\\", temp_dir.display()));
911923
command.args(cc_config.get_files());
912924
command.arg("-link").arg(out);
925+
command.arg(format!("/IMPLIB:{}.lib", temp_dir.join("temp").display()));
926+
927+
Some(temp_dir)
913928
} else {
914929
command.arg("-Werror=implicit-function-declaration");
915930
if cfg!(any(target_os = "macos", target_os = "ios")) {
@@ -921,12 +936,18 @@ impl Loader {
921936
}
922937
command.args(cc_config.get_files());
923938
command.arg("-o").arg(output_path);
924-
}
939+
940+
None
941+
};
925942

926943
let output = command.output().with_context(|| {
927944
format!("Failed to execute the C compiler with the following command:\n{command:?}")
928945
})?;
929946

947+
if let Some(temp_dir) = temp_dir {
948+
let _ = fs::remove_dir_all(temp_dir);
949+
}
950+
930951
FileExt::unlock(lock_file)?;
931952
fs::remove_file(lock_path)?;
932953

crates/xtask/src/test.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ pub fn run(args: &Test) -> Result<()> {
7373
.arg("--no-run")
7474
.arg("--message-format=json");
7575

76-
#[cfg(target_os = "windows")]
77-
cargo_cmd.arg("--").arg("--test-threads=1");
78-
7976
let cargo_cmd = cargo_cmd.stdout(Stdio::piped()).spawn()?;
8077

8178
let jq_cmd = Command::new("jq")
@@ -103,9 +100,6 @@ pub fn run(args: &Test) -> Result<()> {
103100
}
104101
cargo_cmd.args(&args.args);
105102

106-
#[cfg(target_os = "windows")]
107-
cargo_cmd.arg("--").arg("--test-threads=1");
108-
109103
if args.nocapture {
110104
#[cfg(not(target_os = "windows"))]
111105
cargo_cmd.arg("--");

0 commit comments

Comments
 (0)