Skip to content

Commit 4a5358e

Browse files
committed
Help users fix missing pkg-config command
1 parent 35067fb commit 4a5358e

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/lib.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,36 @@ impl fmt::Display for Error {
156156
Error::Command {
157157
ref command,
158158
ref cause,
159-
} => 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+
}
160189
Error::Failure {
161190
ref command,
162191
ref output,

0 commit comments

Comments
 (0)