Move dylib version stuff to proc-macro-srv

This commit is contained in:
Lukas Wirth 2024-06-30 15:05:35 +02:00
parent 5374ebbf36
commit db15273d4d
8 changed files with 36 additions and 33 deletions

4
Cargo.lock generated
View file

@ -1329,13 +1329,10 @@ dependencies = [
"base-db", "base-db",
"indexmap", "indexmap",
"la-arena 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "la-arena 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"memmap2",
"object 0.33.0",
"paths", "paths",
"rustc-hash", "rustc-hash",
"serde", "serde",
"serde_json", "serde_json",
"snap",
"span", "span",
"stdx", "stdx",
"text-size", "text-size",
@ -1358,6 +1355,7 @@ dependencies = [
"proc-macro-api", "proc-macro-api",
"proc-macro-test", "proc-macro-test",
"ra-ap-rustc_lexer", "ra-ap-rustc_lexer",
"snap",
"span", "span",
"stdx", "stdx",
"tt", "tt",

View file

@ -120,6 +120,8 @@ hashbrown = { version = "0.14", features = [
indexmap = "2.1.0" indexmap = "2.1.0"
itertools = "0.12.0" itertools = "0.12.0"
libc = "0.2.150" libc = "0.2.150"
libloading = "0.8.0"
memmap2 = "0.5.4"
nohash-hasher = "0.2.0" nohash-hasher = "0.2.0"
oorandom = "11.1.3" oorandom = "11.1.3"
object = { version = "0.33.0", default-features = false, features = [ object = { version = "0.33.0", default-features = false, features = [
@ -143,6 +145,7 @@ smallvec = { version = "1.10.0", features = [
"const_generics", "const_generics",
] } ] }
smol_str = "0.2.1" smol_str = "0.2.1"
snap = "1.1.0"
text-size = "1.1.1" text-size = "1.1.1"
tracing = "0.1.40" tracing = "0.1.40"
tracing-tree = "0.3.0" tracing-tree = "0.3.0"
@ -156,6 +159,7 @@ url = "2.3.1"
xshell = "0.2.5" xshell = "0.2.5"
# We need to freeze the version of the crate, as the raw-api feature is considered unstable # We need to freeze the version of the crate, as the raw-api feature is considered unstable
dashmap = { version = "=5.5.3", features = ["raw-api"] } dashmap = { version = "=5.5.3", features = ["raw-api"] }

View file

@ -12,15 +12,12 @@ rust-version.workspace = true
doctest = false doctest = false
[dependencies] [dependencies]
object.workspace = true
serde.workspace = true serde.workspace = true
serde_json = { workspace = true, features = ["unbounded_depth"] } serde_json = { workspace = true, features = ["unbounded_depth"] }
tracing.workspace = true tracing.workspace = true
triomphe.workspace = true triomphe.workspace = true
rustc-hash.workspace = true rustc-hash.workspace = true
memmap2 = "0.5.4" indexmap.workspace = true
snap = "1.1.0"
indexmap = "2.1.0"
# local deps # local deps
paths = { workspace = true, features = ["serde1"] } paths = { workspace = true, features = ["serde1"] }

View file

@ -9,7 +9,6 @@
pub mod msg; pub mod msg;
mod process; mod process;
mod version;
use base_db::Env; use base_db::Env;
use indexmap::IndexSet; use indexmap::IndexSet;
@ -31,8 +30,6 @@ use crate::{
process::ProcMacroProcessSrv, process::ProcMacroProcessSrv,
}; };
pub use version::{read_dylib_info, read_version, RustCInfo};
#[derive(Copy, Clone, Eq, PartialEq, Debug, Serialize, Deserialize)] #[derive(Copy, Clone, Eq, PartialEq, Debug, Serialize, Deserialize)]
pub enum ProcMacroKind { pub enum ProcMacroKind {
CustomDerive, CustomDerive,

View file

@ -13,8 +13,9 @@ doctest = false
[dependencies] [dependencies]
object.workspace = true object.workspace = true
libloading = "0.8.0" libloading.workspace = true
memmap2 = "0.5.4" memmap2.workspace = true
snap.workspace = true
stdx.workspace = true stdx.workspace = true
tt.workspace = true tt.workspace = true

View file

@ -1,5 +1,7 @@
//! Handles dynamic library loading for proc macro //! Handles dynamic library loading for proc macro
mod version;
use std::{fmt, fs::File, io}; use std::{fmt, fs::File, io};
use libloading::Library; use libloading::Library;
@ -7,7 +9,7 @@ use memmap2::Mmap;
use object::Object; use object::Object;
use paths::{AbsPath, Utf8Path, Utf8PathBuf}; use paths::{AbsPath, Utf8Path, Utf8PathBuf};
use proc_macro::bridge; use proc_macro::bridge;
use proc_macro_api::{read_dylib_info, ProcMacroKind}; use proc_macro_api::ProcMacroKind;
use crate::ProcMacroSrvSpan; use crate::ProcMacroSrvSpan;
@ -119,11 +121,14 @@ impl ProcMacroLibraryLibloading {
let abs_file: &AbsPath = file let abs_file: &AbsPath = file
.try_into() .try_into()
.map_err(|_| invalid_data_err(format!("expected an absolute path, got {file}")))?; .map_err(|_| invalid_data_err(format!("expected an absolute path, got {file}")))?;
let version_info = read_dylib_info(abs_file)?; let version_info = version::read_dylib_info(abs_file)?;
let lib = load_library(file).map_err(invalid_data_err)?; let lib = load_library(file).map_err(invalid_data_err)?;
let proc_macros = let proc_macros = crate::proc_macros::ProcMacros::from_lib(
crate::proc_macros::ProcMacros::from_lib(&lib, symbol_name, version_info)?; &lib,
symbol_name,
&version_info.version_string,
)?;
Ok(ProcMacroLibraryLibloading { _lib: lib, proc_macros }) Ok(ProcMacroLibraryLibloading { _lib: lib, proc_macros })
} }
} }

View file

@ -11,6 +11,7 @@ use paths::AbsPath;
use snap::read::FrameDecoder as SnapDecoder; use snap::read::FrameDecoder as SnapDecoder;
#[derive(Debug)] #[derive(Debug)]
#[allow(dead_code)]
pub struct RustCInfo { pub struct RustCInfo {
pub version: (usize, usize, usize), pub version: (usize, usize, usize),
pub channel: String, pub channel: String,
@ -164,3 +165,16 @@ pub fn read_version(dylib_path: &AbsPath) -> io::Result<String> {
let version_string = String::from_utf8(version_string_utf8); let version_string = String::from_utf8(version_string_utf8);
version_string.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) version_string.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
} }
#[test]
fn test_version_check() {
let path = paths::AbsPathBuf::assert(crate::proc_macro_test_dylib_path());
let info = read_dylib_info(&path).unwrap();
assert_eq!(
info.version_string,
crate::RUSTC_VERSION_STRING,
"sysroot ABI mismatch: dylib rustc version (read from .rustc section): {:?} != proc-macro-srv version (read from 'rustc --version'): {:?}",
info.version_string,
crate::RUSTC_VERSION_STRING,
);
}

View file

@ -2,7 +2,7 @@
use libloading::Library; use libloading::Library;
use proc_macro::bridge; use proc_macro::bridge;
use proc_macro_api::{ProcMacroKind, RustCInfo}; use proc_macro_api::ProcMacroKind;
use crate::{dylib::LoadProcMacroDylibError, ProcMacroSrvSpan}; use crate::{dylib::LoadProcMacroDylibError, ProcMacroSrvSpan};
@ -29,15 +29,15 @@ impl ProcMacros {
pub(crate) fn from_lib( pub(crate) fn from_lib(
lib: &Library, lib: &Library,
symbol_name: String, symbol_name: String,
info: RustCInfo, version_string: &str,
) -> Result<ProcMacros, LoadProcMacroDylibError> { ) -> Result<ProcMacros, LoadProcMacroDylibError> {
if info.version_string == crate::RUSTC_VERSION_STRING { if version_string == crate::RUSTC_VERSION_STRING {
let macros = let macros =
unsafe { lib.get::<&&[bridge::client::ProcMacro]>(symbol_name.as_bytes()) }?; unsafe { lib.get::<&&[bridge::client::ProcMacro]>(symbol_name.as_bytes()) }?;
return Ok(Self { exported_macros: macros.to_vec() }); return Ok(Self { exported_macros: macros.to_vec() });
} }
Err(LoadProcMacroDylibError::AbiMismatch(info.version_string)) Err(LoadProcMacroDylibError::AbiMismatch(version_string.to_owned()))
} }
pub(crate) fn expand<S: ProcMacroSrvSpan>( pub(crate) fn expand<S: ProcMacroSrvSpan>(
@ -117,16 +117,3 @@ impl ProcMacros {
.collect() .collect()
} }
} }
#[test]
fn test_version_check() {
let path = paths::AbsPathBuf::assert(crate::proc_macro_test_dylib_path());
let info = proc_macro_api::read_dylib_info(&path).unwrap();
assert_eq!(
info.version_string,
crate::RUSTC_VERSION_STRING,
"sysroot ABI mismatch: dylib rustc version (read from .rustc section): {:?} != proc-macro-srv version (read from 'rustc --version'): {:?}",
info.version_string,
crate::RUSTC_VERSION_STRING,
);
}