2 * Copyright (C) 2001, 2002 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
39 static int want_debug_spew = 0;
40 static int want_verbose_errors = 0;
41 static int want_stdout_errors = 0;
42 char *pcsysrootdir = NULL;
45 debug_spew (const char *format, ...)
51 g_return_if_fail (format != NULL);
56 va_start (args, format);
57 str = g_strdup_vprintf (format, args);
60 if (want_stdout_errors)
72 verbose_error (const char *format, ...)
78 g_return_if_fail (format != NULL);
80 if (!want_verbose_errors)
83 va_start (args, format);
84 str = g_strdup_vprintf (format, args);
87 if (want_stdout_errors)
98 #define DEFINE_VARIABLE 1
101 popt_callback (poptContext con,
102 enum poptCallbackReason reason,
103 const struct poptOption * opt,
104 const char * arg, void * data)
106 debug_spew ("Option --%s seen\n", opt->longName);
108 if (opt->val == DEFINE_VARIABLE)
114 tmp = g_strdup (arg);
117 while (*varname && isspace ((guchar)*varname))
121 while (*varval && *varval != '=' && *varval != ' ')
124 while (*varval && (*varval == '=' || *varval == ' '))
132 fprintf (stderr, "--define-variable argument does not have a value for the variable\n");
137 define_global_variable (varname, varval);
144 pkg_uninstalled (Package *pkg)
146 /* See if > 0 pkgs were uninstalled */
149 if (pkg->uninstalled)
155 Package *pkg = tmp->data;
157 if (pkg_uninstalled (pkg))
160 tmp = g_slist_next (tmp);
167 main (int argc, char **argv)
169 static int want_my_version = 0;
170 static int want_version = 0;
171 static int want_libs = 0;
172 static int want_cflags = 0;
173 static int want_l_libs = 0;
174 static int want_L_libs = 0;
175 static int want_other_libs = 0;
176 static int want_I_cflags = 0;
177 static int want_other_cflags = 0;
178 static int want_list = 0;
179 static int want_static_lib_list = ENABLE_INDIRECT_DEPS;
180 static int want_short_errors = 0;
181 static int want_uninstalled = 0;
182 static char *variable_name = NULL;
183 static int want_exists = 0;
184 static char *required_atleast_version = NULL;
185 static char *required_exact_version = NULL;
186 static char *required_max_version = NULL;
187 static char *required_pkgconfig_version = NULL;
188 static int want_silence_errors = 0;
191 GSList *packages = NULL;
194 const char *pkglibdir;
197 gboolean need_newline;
202 poptContext opt_context;
204 struct poptOption options_table[] = {
205 { NULL, 0, POPT_ARG_CALLBACK, popt_callback, 0, NULL, NULL },
206 { "version", 0, POPT_ARG_NONE, &want_my_version, 0,
207 "output version of pkg-config" },
208 { "modversion", 0, POPT_ARG_NONE, &want_version, 0,
209 "output version for package" },
210 { "atleast-pkgconfig-version", 0, POPT_ARG_STRING, &required_pkgconfig_version, 0,
211 "require given version of pkg-config", "VERSION" },
212 { "libs", 0, POPT_ARG_NONE, &want_libs, 0,
213 "output all linker flags" },
214 { "static", 0, POPT_ARG_NONE, &want_static_lib_list, 0,
215 "output linker flags for static linking" },
216 { "short-errors", 0, POPT_ARG_NONE, &want_short_errors, 0,
217 "print short errors" },
218 { "libs-only-l", 0, POPT_ARG_NONE, &want_l_libs, 0,
220 { "libs-only-other", 0, POPT_ARG_NONE, &want_other_libs, 0,
221 "output other libs (e.g. -pthread)" },
222 { "libs-only-L", 0, POPT_ARG_NONE, &want_L_libs, 0,
224 { "cflags", 0, POPT_ARG_NONE, &want_cflags, 0,
225 "output all pre-processor and compiler flags" },
226 { "cflags-only-I", 0, POPT_ARG_NONE, &want_I_cflags, 0,
228 { "cflags-only-other", 0, POPT_ARG_NONE, &want_other_cflags, 0,
229 "output cflags not covered by the cflags-only-I option"},
230 { "variable", 0, POPT_ARG_STRING, &variable_name, 0,
231 "get the value of variable named NAME", "NAME" },
232 { "define-variable", 0, POPT_ARG_STRING, NULL, DEFINE_VARIABLE,
233 "set variable NAME to VALUE", "NAME=VALUE" },
234 { "exists", 0, POPT_ARG_NONE, &want_exists, 0,
235 "return 0 if the module(s) exist" },
236 { "uninstalled", 0, POPT_ARG_NONE, &want_uninstalled, 0,
237 "return 0 if the uninstalled version of one or more module(s) or their dependencies will be used" },
238 { "atleast-version", 0, POPT_ARG_STRING, &required_atleast_version, 0,
239 "return 0 if the module is at least version VERSION", "VERSION" },
240 { "exact-version", 0, POPT_ARG_STRING, &required_exact_version, 0,
241 "return 0 if the module is at exactly version VERSION", "VERSION" },
242 { "max-version", 0, POPT_ARG_STRING, &required_max_version, 0,
243 "return 0 if the module is at no newer than version VERSION", "VERSION" },
244 { "list-all", 0, POPT_ARG_NONE, &want_list, 0,
245 "list all known packages" },
246 { "debug", 0, POPT_ARG_NONE, &want_debug_spew, 0,
247 "show verbose debug information" },
248 { "print-errors", 0, POPT_ARG_NONE, &want_verbose_errors, 0,
249 "show verbose information about missing or conflicting packages,"
250 "default if --cflags or --libs given on the command line" },
251 { "silence-errors", 0, POPT_ARG_NONE, &want_silence_errors, 0,
252 "be silent about errors (default unless --cflags or --libs"
253 "given on the command line)" },
254 { "errors-to-stdout", 0, POPT_ARG_NONE, &want_stdout_errors, 0,
255 "print errors from --print-errors to stdout not stderr" },
257 { "dont-define-prefix", 0, POPT_ARG_NONE, &dont_define_prefix, 0,
258 "don't try to override the value of prefix for each .pc file found with "
259 "a guesstimated value based on the location of the .pc file" },
260 { "prefix-variable", 0, POPT_ARG_STRING, &prefix_variable, 0,
261 "set the name of the variable that pkg-config automatically sets", "PREFIX" },
262 { "msvc-syntax", 0, POPT_ARG_NONE, &msvc_syntax, 0,
263 "output -l and -L flags for the Microsoft compiler (cl)" },
266 { NULL, 0, 0, NULL, 0 }
269 /* This is here so that we get debug spew from the start,
272 if (getenv ("PKG_CONFIG_DEBUG_SPEW"))
274 want_debug_spew = TRUE;
275 want_verbose_errors = TRUE;
276 want_silence_errors = FALSE;
277 debug_spew ("PKG_CONFIG_DEBUG_SPEW variable enabling debug spew\n");
280 search_path = getenv ("PKG_CONFIG_PATH");
283 add_search_dirs(search_path, G_SEARCHPATH_SEPARATOR_S);
285 if (getenv("PKG_CONFIG_LIBDIR") != NULL)
287 add_search_dirs(getenv("PKG_CONFIG_LIBDIR"), G_SEARCHPATH_SEPARATOR_S);
291 add_search_dirs(PKG_CONFIG_PC_PATH, G_SEARCHPATH_SEPARATOR_S);
294 pcsysrootdir = getenv ("PKG_CONFIG_SYSROOT_DIR");
297 define_global_variable ("pc_sysrootdir", pcsysrootdir);
301 define_global_variable ("pc_sysrootdir", "/");
304 pcbuilddir = getenv ("PKG_CONFIG_TOP_BUILD_DIR");
307 define_global_variable ("pc_top_builddir", pcbuilddir);
311 /* Default appropriate for automake */
312 define_global_variable ("pc_top_builddir", "$(top_builddir)");
315 if (getenv ("PKG_CONFIG_DISABLE_UNINSTALLED"))
317 debug_spew ("disabling auto-preference for uninstalled packages\n");
318 disable_uninstalled = TRUE;
321 opt_context = poptGetContext (NULL, argc, argv,
324 result = poptGetNextOpt (opt_context);
327 fprintf(stderr, "%s: %s\n",
328 poptBadOption(opt_context, POPT_BADOPTION_NOALIAS),
329 poptStrerror(result));
334 /* Error printing is determined as follows:
335 * - for --cflags, --libs, etc. it's on by default
336 * and --silence-errors can turn it off
337 * - for --exists, --max-version, etc. and no options
338 * at all, it's off by default and --print-errors
342 if (want_my_version ||
353 debug_spew ("Error printing enabled by default due to use of --version, --libs, --cflags, --libs-only-l, --libs-only-L, --libs-only-other, --cflags-only-I, --cflags-only-other or --list. Value of --silence-errors: %d\n", want_silence_errors);
355 if (want_silence_errors && getenv ("PKG_CONFIG_DEBUG_SPEW") == NULL)
356 want_verbose_errors = FALSE;
358 want_verbose_errors = TRUE;
362 debug_spew ("Error printing disabled by default, value of --print-errors: %d\n",
363 want_verbose_errors);
365 /* Leave want_verbose_errors unchanged, reflecting --print-errors */
368 if (want_verbose_errors)
369 debug_spew ("Error printing enabled\n");
371 debug_spew ("Error printing disabled\n");
373 if (want_static_lib_list)
374 enable_private_libs();
376 disable_private_libs();
378 /* honor Requires.private if any Cflags are requested or any static
379 * libs are requested */
381 if (want_I_cflags || want_other_cflags || want_cflags ||
382 (want_static_lib_list && (want_libs || want_l_libs || want_L_libs)))
383 enable_requires_private();
385 /* ignore Requires if no Cflags or Libs are requested */
387 if (!want_I_cflags && !want_other_cflags && !want_cflags &&
388 !want_libs && !want_l_libs && !want_L_libs)
393 printf ("%s\n", VERSION);
397 if (required_pkgconfig_version)
399 if (compare_versions (VERSION, required_pkgconfig_version) >= 0)
409 print_package_list ();
413 str = g_string_new ("");
416 pkgname = poptGetArg (opt_context);
420 g_string_append (str, pkgname);
421 g_string_append (str, " ");
424 poptFreeContext (opt_context);
426 g_strstrip (str->str);
428 if (getenv("PKG_CONFIG_LOG") != NULL)
430 log = fopen (getenv ("PKG_CONFIG_LOG"), "a");
433 fprintf (stderr, "Cannot open log file: %s\n",
434 getenv ("PKG_CONFIG_LOG"));
440 gboolean failed = FALSE;
444 reqs = parse_module_list (NULL, str->str,
445 "(command line arguments)");
452 RequiredVersion *ver = iter->data;
454 if (want_short_errors)
455 req = get_package_quiet (ver->name);
457 req = get_package (ver->name);
462 fprintf (log, "%s NOT-FOUND", ver->name);
464 fprintf (log, "%s %s %s", ver->name,
465 comparison_to_str (ver->comparison),
466 (ver->version == NULL) ? "(null)" : ver->version);
473 verbose_error ("No package '%s' found\n", ver->name);
477 if (!version_test (ver->comparison, req->version, ver->version))
480 verbose_error ("Requested '%s %s %s' but version of %s is %s\n",
482 comparison_to_str (ver->comparison),
488 verbose_error ("You may find new versions of %s at %s\n",
489 req->name, req->url);
494 packages = g_slist_prepend (packages, req);
497 iter = g_slist_next (iter);
511 g_string_free (str, TRUE);
513 packages = g_slist_reverse (packages);
515 if (packages == NULL)
517 fprintf (stderr, "Must specify package names on the command line\n");
523 return 0; /* if we got here, all the packages existed. */
525 if (want_uninstalled)
527 /* See if > 0 pkgs (including dependencies recursively) were uninstalled */
532 Package *pkg = tmp->data;
534 if (pkg_uninstalled (pkg))
537 tmp = g_slist_next (tmp);
549 Package *pkg = tmp->data;
551 printf ("%s\n", pkg->version);
553 tmp = g_slist_next (tmp);
557 if (required_exact_version)
559 Package *pkg = packages->data;
561 if (compare_versions (pkg->version, required_exact_version) == 0)
566 else if (required_atleast_version)
568 Package *pkg = packages->data;
570 if (compare_versions (pkg->version, required_atleast_version) >= 0)
575 else if (required_max_version)
577 Package *pkg = packages->data;
579 if (compare_versions (pkg->version, required_max_version) <= 0)
585 /* Print all flags; then print a newline at the end. */
586 need_newline = FALSE;
590 char *str = packages_get_var (packages, variable_name);
598 char *str = packages_get_I_cflags (packages);
603 else if (want_other_cflags)
605 char *str = packages_get_other_cflags (packages);
610 else if (want_cflags)
612 char *str = packages_get_all_cflags (packages);
620 char *str = packages_get_l_libs (packages);
625 else if (want_L_libs)
627 char *str = packages_get_L_libs (packages);
632 else if (want_other_libs)
634 char *str = packages_get_other_libs (packages);
641 char *str = packages_get_all_libs (packages);