@@ -104,7 +104,6 @@ pub struct Library {
104
104
}
105
105
106
106
/// Represents all reasons `pkg-config` might not succeed or be run at all.
107
- #[ derive( Debug ) ]
108
107
pub enum Error {
109
108
/// Aborted because of `*_NO_PKG_CONFIG` environment variable.
110
109
///
@@ -135,22 +134,58 @@ pub enum Error {
135
134
136
135
impl error:: Error for Error { }
137
136
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
+
138
144
impl fmt:: Display for Error {
139
145
fn fmt ( & self , f : & mut fmt:: Formatter ) -> Result < ( ) , fmt:: Error > {
140
146
match * self {
141
147
Error :: EnvNoPkgConfig ( ref name) => write ! ( f, "Aborted because {} is set" , name) ,
142
148
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 \
148
154
PKG_CONFIG environment variable.",
149
155
) ,
150
156
Error :: Command {
151
157
ref command,
152
158
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
+ }
154
189
Error :: Failure {
155
190
ref command,
156
191
ref output,
0 commit comments