Skip to content

Commit ccebac6

Browse files
authored
Merge pull request #123 from kornelski/installhelp
Help users install pkg-config package
2 parents f864772 + 4a5358e commit ccebac6

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

src/lib.rs

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ pub struct Library {
104104
}
105105

106106
/// Represents all reasons `pkg-config` might not succeed or be run at all.
107-
#[derive(Debug)]
108107
pub enum Error {
109108
/// Aborted because of `*_NO_PKG_CONFIG` environment variable.
110109
///
@@ -135,22 +134,58 @@ pub enum Error {
135134

136135
impl error::Error for Error {}
137136

137+
impl fmt::Debug for Error {
138+
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
139+
// Failed `unwrap()` prints Debug representation, but the default debug format lacks helpful instructions for the end users
140+
<Error as fmt::Display>::fmt(self, f)
141+
}
142+
}
143+
138144
impl fmt::Display for Error {
139145
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
140146
match *self {
141147
Error::EnvNoPkgConfig(ref name) => write!(f, "Aborted because {} is set", name),
142148
Error::CrossCompilation => f.write_str(
143-
"pkg-config has not been configured to support cross-compilation.
144-
145-
Install a sysroot for the target platform and configure it via
146-
PKG_CONFIG_SYSROOT_DIR and PKG_CONFIG_PATH, or install a
147-
cross-compiling wrapper for pkg-config and set it via
149+
"pkg-config has not been configured to support cross-compilation.\n\
150+
\n\
151+
Install a sysroot for the target platform and configure it via\n\
152+
PKG_CONFIG_SYSROOT_DIR and PKG_CONFIG_PATH, or install a\n\
153+
cross-compiling wrapper for pkg-config and set it via\n\
148154
PKG_CONFIG environment variable.",
149155
),
150156
Error::Command {
151157
ref command,
152158
ref cause,
153-
} => write!(f, "Failed to run `{}`: {}", command, cause),
159+
} => {
160+
match cause.kind() {
161+
io::ErrorKind::NotFound => {
162+
let crate_name = std::env::var("CARGO_PKG_NAME");
163+
let crate_name = crate_name.as_deref().unwrap_or("sys");
164+
let instructions = if cfg!(target_os = "macos") || cfg!(target_os = "ios") {
165+
"Try `brew install pkg-config` if you have Homebrew.\n"
166+
} else if cfg!(unix) {
167+
"Try `apt install pkg-config`, or `yum install pkg-config`,\n\
168+
or `pkg install pkg-config` depending on your distribution.\n"
169+
} else {
170+
"" // There's no easy fix for Windows users
171+
};
172+
write!(f, "Could not run `{command}`\n\
173+
The pkg-config command could not be found.\n\
174+
\n\
175+
Most likely, you need to install a pkg-config package for your OS.\n\
176+
{instructions}\
177+
\n\
178+
If you've already installed it, ensure the pkg-config command is one of the\n\
179+
directories in the PATH environment variable.\n\
180+
\n\
181+
If you did not expect this build to link to a pre-installed system library,\n\
182+
then check documentation of the {crate_name} crate for an option to\n\
183+
build the library from source, or disable features or dependencies\n\
184+
that require pkg-config.", command = command, instructions = instructions, crate_name = crate_name)
185+
}
186+
_ => write!(f, "Failed to run command `{}`, because: {}", command, cause),
187+
}
188+
}
154189
Error::Failure {
155190
ref command,
156191
ref output,

0 commit comments

Comments
 (0)