Skip to content

Commit

Permalink
Add suggestion to the "missing native library" error
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wesleywiser committed Oct 13, 2022
1 parent 0938e16 commit 097b6d3
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 2 deletions.
2 changes: 2 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/metadata.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
36 changes: 35 additions & 1 deletion compiler/rustc_metadata/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/native_libs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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() { }
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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() { }
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 097b6d3

Please sign in to comment.