You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This issue is duplicate of #134596, since it has been a week since I last received a reply, and this issue is extremely important to my crate, I'm sorry to create this issue.
My crate uses hook technology, which requires Rust to be able to link .dylib/.so/.dll. Unlike other projects, dynamic link libraries are also compiled from rust code(I built link-example to show what I'm trying to do), the link can not works since 1.82.0. The CI failed due to link problem.
If there is any other information you need to know, please tell me, thanks a lot for helping me.
I tried this code:
use cargo_metadata::MetadataCommand;use std::env::var;use std::fs::{copy, read_dir};use std::path::PathBuf;fnmain(){// build dyliblet out_dir = PathBuf::from(var("OUT_DIR").expect("OUT_DIR not found"));let target = var("TARGET").expect("env not found");letmut cargo = std::process::Command::new("cargo");letmut cmd = cargo.arg("build").arg("--target").arg(target.clone());ifcfg!(not(debug_assertions)){
cmd = cmd.arg("--release");}letmut hook_toml = PathBuf::from(var("CARGO_MANIFEST_DIR").expect("env not found")).parent().expect("parent not found").join("dep").join("Cargo.toml");let metadata = MetadataCommand::default().no_deps().exec().expect("read cargo metadata failed");let package = if hook_toml.exists(){
metadata
.packages.iter().find(|pkg| pkg.name.eq("mycrate")).expect("read current package failed")}else{
metadata
.packages.first().expect("read current package failed")};let dependency = package
.dependencies.iter().find(|dep| dep.name.eq("dep")).expect("dep not found");if !hook_toml.exists(){// 使用cargo_metadata读到依赖版本,结合CARGO_HOME获取dep的tomllet dep_src_dir = PathBuf::from(var("CARGO_HOME").expect("CARGO_HOME not found")).join("registry").join("src");let crates_parent_dirs = Vec::from_iter(read_dir(dep_src_dir.clone()).expect("Failed to read deps").flatten(),);let crates_parent = if crates_parent_dirs.len() == 1{
crates_parent_dirs.first().expect("host dir not found")}else{let rustup_dist_server =
var("RUSTUP_DIST_SERVER").expect("RUSTUP_DIST_SERVER not found");let host = rustup_dist_server
.split("://").last().expect("host not found");
crates_parent_dirs
.iter().find(|entry| {
entry
.file_name().to_string_lossy().to_string().contains(host)}).unwrap_or_else(|| {
crates_parent_dirs
.iter().find(|entry| {
entry
.file_name().to_string_lossy().to_string().contains("crates.io")}).expect("host dir not found")})}.file_name().to_string_lossy().to_string();let version = &dependency
.req.comparators.first().expect("version not found");
hook_toml = dep_src_dir
.join(crates_parent).join(format!("dep-{}.{}.{}",
version.major,
version.minor.unwrap_or(0),
version.patch.unwrap_or(0))).join("Cargo.toml");}if !dependency.uses_default_features{
cmd = cmd.arg("--no-default-features");}let features:Vec<&str> = Vec::new();ifletErr(e) = cmd
.arg("--features").arg(features.join(",")).arg("--manifest-path").arg(hook_toml).arg("--target-dir").arg(out_dir.clone()).status(){panic!("failed to build dylib {}", e);}// correct dylib pathlet hook_deps = out_dir
.join(target).join(ifcfg!(debug_assertions){"debug"}else{"release"}).join("deps");let deps = out_dir
.parent().expect("can not find deps dir").parent().expect("can not find deps dir").parent().expect("can not find deps dir").join("deps");for entry inread_dir(hook_deps.clone()).expect("can not find deps dir").flatten(){let file_name = entry.file_name().to_string_lossy().to_string();if !file_name.contains("dep"){continue;}ifcfg!(target_os = "linux") && file_name.ends_with(".so"){let from = hook_deps.join(file_name);let to = deps.join("libdep.so");copy(from.clone(), to.clone()).expect("copy to libdep.so failed!");}elseifcfg!(target_os = "macos") && file_name.ends_with(".dylib"){let from = hook_deps.join(file_name);let to = deps.join("libdep.dylib");copy(from.clone(), to.clone()).expect("copy to libdep.dylib failed!");}elseifcfg!(windows){if file_name.ends_with(".dll"){let from = hook_deps.join(file_name);let to = deps.join("dep.dll");copy(from.clone(), to.clone()).expect("copy to dep.dll failed!");}elseif file_name.ends_with(".lib"){let from = hook_deps.join(file_name);let to = deps.join("dep.lib");copy(from.clone(), to.clone()).expect("copy to dep.lib failed!");}}}// link dylibprintln!("cargo:rustc-link-search=native={:?}", deps);println!("cargo:rustc-link-lib=dylib=dep");}
I expected to see this happen: it should be successfully linked
Instead, this happened: cannot find -ldep: No such file or directory
This issue is duplicate of #134596, since it has been a week since I last received a reply, and this issue is extremely important to my crate, I'm sorry to create this issue.
My crate uses hook technology, which requires Rust to be able to link
.dylib
/.so
/.dll
. Unlike other projects, dynamic link libraries are also compiled from rust code(I built link-example to show what I'm trying to do), the link can not works since1.82.0
. The CI failed due to link problem.If there is any other information you need to know, please tell me, thanks a lot for helping me.
I tried this code:
I expected to see this happen: it should be successfully linked
Instead, this happened: cannot find -ldep: No such file or directory
Meta
rustc --version --verbose
:Backtrace
The text was updated successfully, but these errors were encountered: