Skip to content

Commit

Permalink
refactor: simplify the linker code
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Jan 30, 2024
1 parent 7974e66 commit 138b32c
Showing 1 changed file with 4 additions and 27 deletions.
31 changes: 4 additions & 27 deletions kclvm/runner/src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,41 +99,18 @@ impl Command {
pub(crate) fn unix_args(
&self,
libs: &[String],
lib_path: String,
_lib_path: String,
cmd: &mut std::process::Command,
) -> Result<()> {
let path = self.runtime_lib_path(&lib_path)?;
let path = self.get_lib_link_path()?;
cmd.args(libs)
.arg(&format!("-Wl,-rpath,{}", &path))
.arg(&format!("-L{}", &path))
.arg(&format!("-I{}/include", self.executable_root))
.arg("-lkclvm_cli_cdylib");
.arg(&format!("-l{KCLVM_LIB_SHORT_NAME}"));
Ok(())
}

/// Get the runtime library path.
pub(crate) fn runtime_lib_path(&self, lib_path: &str) -> Result<String> {
let path = self.get_lib_link_path()?;
let lib_name = Self::get_lib_name();
let lib_file_path = std::path::Path::new(&path).join(&lib_name);
// Copy runtime library to target path for parallel execute.
Ok(
if let Some(target_path) = std::path::Path::new(&lib_path).parent() {
let target_lib_file_path = std::path::Path::new(target_path).join(&lib_name);

// Locking file for parallel file copy.
let mut file_lock =
fslock::LockFile::open(&format!("{}.lock", target_lib_file_path.display()))?;
file_lock.lock()?;

std::fs::copy(lib_file_path, target_lib_file_path)?;
target_path.to_string_lossy().to_string()
} else {
path
},
)
}

// Add args for cc on windows os.
#[cfg(target_os = "windows")]
pub(crate) fn msvc_win_args(
Expand All @@ -143,7 +120,7 @@ impl Command {
cmd: &mut std::process::Command,
) -> Result<()> {
cmd.args(libs)
.arg("kclvm_cli_cdylib.lib")
.arg(&format!("{KCL_RUNTIME_LIB_SHORT_NAME}.lib"))
.arg("/link")
.arg("/NOENTRY")
.arg("/NOLOGO")
Expand Down

0 comments on commit 138b32c

Please sign in to comment.