Skip to content

Commit

Permalink
Fix(kclvm-windows): rm '\\?\' in windows path. (#410)
Browse files Browse the repository at this point in the history
* Fix(kclvm-windows): rm '\\?\' in windows path.
issue #379.

* add test case

* fix test case

* fix test case
  • Loading branch information
zong-zhe committed Feb 13, 2023
1 parent 5329233 commit 972e37f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
1 change: 0 additions & 1 deletion build_and_test_win.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
@echo off
cd %~dp0

call .\\scripts\\build-windows\\build.bat
Expand Down
42 changes: 42 additions & 0 deletions kclvm/runner/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(dead_code)]
use std::env::consts::DLL_SUFFIX;
use std::ffi::CString;
use std::path::Path;
use std::path::PathBuf;

#[derive(Debug)]
Expand Down Expand Up @@ -103,6 +104,12 @@ impl Command {
.host(&target)
.flag("-o")
.flag(&lib_path);

let libs: Vec<String> = libs
.into_iter()
.map(|lib| adjust_canonicalization(lib))
.collect();
let libs = libs.as_slice();
build.files(libs);

// Run command with cc.
Expand Down Expand Up @@ -224,3 +231,38 @@ impl Command {
})
}
}

#[cfg(not(target_os = "windows"))]
fn adjust_canonicalization<P: AsRef<Path>>(p: P) -> String {
p.as_ref().display().to_string()
}

#[cfg(target_os = "windows")]
/// On windows, the "\\?\ ", for the Windows APIs, will cause the obj file to not be found when linking by "cl.exe".
/// Slicing this path directly is not a good solution,
/// we will find a more fluent way to solve this problem in the future. @zongz
/// Note: On windows systems, a file path that is too long may cause "cl.exe" to crash.
fn adjust_canonicalization<P: AsRef<Path>>(p: P) -> String {
const VERBATIM_PREFIX: &str = r#"\\?\"#;
let p = p.as_ref().display().to_string();
if p.starts_with(VERBATIM_PREFIX) {
p[VERBATIM_PREFIX.len()..].to_string()
} else {
p
}
}

#[test]
#[cfg(target_os = "windows")]
fn test_adjust_canonicalization() {
let path = Path::new(".").canonicalize().unwrap().display().to_string();
assert!(path.contains("\\\\?\\"));
assert!(!adjust_canonicalization(path).contains("\\\\?\\"));
}

#[test]
#[cfg(not(target_os = "windows"))]
fn test_adjust_canonicalization1() {
let path = Path::new(".").canonicalize().unwrap().display().to_string();
assert_eq!(adjust_canonicalization(path.to_string()), path);
}
4 changes: 4 additions & 0 deletions scripts/build-windows/clean.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ setlocal
cd %~dp0

rmdir _output
del /s *.obj
del /s *.exp
del /s *.lib
del /s *.dll

del *.zip

0 comments on commit 972e37f

Please sign in to comment.