Add suggestion to the "missing native library" error

If we fail to locate a native library that we are linking with, it could
be the case the user entered a complete file name like `foo.lib` or
`libfoo.a` when we expect them to simply provide `foo`.

In this situation, we now detect that case and suggest the user only
provide the library name itself.
This commit is contained in:
Wesley Wiser 2022-10-12 22:52:31 -04:00
parent 0938e1680d
commit 097b6d3baf
7 changed files with 68 additions and 2 deletions

View File

@ -165,6 +165,8 @@ metadata_failed_write_error =
metadata_missing_native_library =
could not find native static library `{$libname}`, perhaps an -L flag is missing?
metadata_only_provide_library_name = only provide the library name `{$suggested_name}`, not the full filename
metadata_failed_create_tempdir =
couldn't create a temp dir: {$err}

View File

@ -372,7 +372,41 @@ pub struct FailedWriteError {
#[derive(Diagnostic)]
#[diag(metadata::missing_native_library)]
pub struct MissingNativeLibrary<'a> {
pub libname: &'a str,
libname: &'a str,
#[subdiagnostic]
suggest_name: Option<SuggestLibraryName<'a>>,
}
impl<'a> MissingNativeLibrary<'a> {
pub fn new(libname: &'a str, verbatim: bool) -> Self {
// if it looks like the user has provided a complete filename rather just the bare lib name,
// then provide a note that they might want to try trimming the name
let suggested_name = if !verbatim {
if let Some(libname) = libname.strip_prefix("lib") && let Some(libname) = libname.strip_suffix(".a") {
// this is a unix style filename so trim prefix & suffix
Some(libname)
} else if let Some(libname) = libname.strip_suffix(".lib") {
// this is a Windows style filename so just trim the suffix
Some(libname)
} else {
None
}
} else {
None
};
Self {
libname,
suggest_name: suggested_name
.map(|suggested_name| SuggestLibraryName { suggested_name }),
}
}
}
#[derive(Subdiagnostic)]
#[help(metadata::only_provide_library_name)]
pub struct SuggestLibraryName<'a> {
suggested_name: &'a str,
}
#[derive(Diagnostic)]

View File

@ -52,7 +52,7 @@ pub fn find_native_static_library(
}
}
sess.emit_fatal(MissingNativeLibrary { libname: name });
sess.emit_fatal(MissingNativeLibrary::new(name, verbatim.unwrap_or(false)));
}
fn find_bundled_library(

View File

@ -0,0 +1,9 @@
// build-fail
// compile-flags: --crate-type rlib
// error-pattern: could not find native static library `libfoo.a`
// error-pattern: only provide the library name `foo`, not the full filename
#[link(name = "libfoo.a", kind = "static")]
extern { }
pub fn main() { }

View File

@ -0,0 +1,6 @@
error: could not find native static library `libfoo.a`, perhaps an -L flag is missing?
|
= help: only provide the library name `foo`, not the full filename
error: aborting due to previous error

View File

@ -0,0 +1,9 @@
// build-fail
// compile-flags: --crate-type rlib
// error-pattern: could not find native static library `bar.lib`
// error-pattern: only provide the library name `bar`, not the full filename
#[link(name = "bar.lib", kind = "static")]
extern { }
pub fn main() { }

View File

@ -0,0 +1,6 @@
error: could not find native static library `bar.lib`, perhaps an -L flag is missing?
|
= help: only provide the library name `bar`, not the full filename
error: aborting due to previous error