2 * Copyright (C) 2006-2011 Tollef Fog Heen <tfheen@err.no>
3 * Copyright (C) 2001, 2002, 2005-2006 Red Hat Inc.
4 * Copyright (C) 2010 Dan Nicholson <dbn.lists@gmail.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
32 #ifdef HAVE_SYS_WAIT_H
35 #include <sys/types.h>
38 int dont_define_prefix = FALSE;
39 char *prefix_variable = "prefix";
40 int msvc_syntax = FALSE;
44 #ifndef G_IS_DIR_SEPARATOR
45 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR || (c) == '/')
50 * Read an entire line from a file into a buffer. Lines may
51 * be delimited with '\n', '\r', '\n\r', or '\r\n'. The delimiter
52 * is not written into the buffer. Text after a '#' character is treated as
53 * a comment and skipped. '\' can be used to escape a # character.
54 * '\' proceding a line delimiter combines adjacent lines. A '\' proceding
55 * any other character is ignored and written into the output buffer
58 * Return value: %FALSE if the stream was already at an EOF character.
61 read_one_line (FILE *stream, GString *str)
63 gboolean quoted = FALSE;
64 gboolean comment = FALSE;
67 g_string_truncate (str, 0);
78 g_string_append_c (str, '\\');
92 g_string_append_c (str, '#');
97 int next_c = getc (stream);
100 (c == '\r' && next_c == '\n') ||
101 (c == '\n' && next_c == '\r')))
102 ungetc (next_c, stream);
107 g_string_append_c (str, '\\');
108 g_string_append_c (str, c);
124 int next_c = getc (stream);
127 (c == '\r' && next_c == '\n') ||
128 (c == '\n' && next_c == '\r')))
129 ungetc (next_c, stream);
135 g_string_append_c (str, c);
146 trim_string (const char *str)
150 g_return_val_if_fail (str != NULL, NULL);
152 while (*str && isspace ((guchar)*str))
156 while (len > 0 && isspace ((guchar)str[len-1]))
159 return g_strndup (str, len);
163 trim_and_sub (Package *pkg, const char *str, const char *path)
169 trimmed = trim_string (str);
171 subst = g_string_new ("");
180 g_string_append_c (subst, '$');
183 else if (p[0] == '$' &&
193 /* Get up to close brace. */
194 while (*p && *p != '}')
197 varname = g_strndup (var_start, p - var_start);
199 ++p; /* past brace */
201 varval = package_get_var (pkg, varname);
205 verbose_error ("Variable '%s' not defined in '%s'\n",
213 g_string_append (subst, varval);
218 g_string_append_c (subst, *p);
226 g_string_free (subst, FALSE);
232 parse_name (Package *pkg, const char *str, const char *path)
236 verbose_error ("Name field occurs twice in '%s'\n", path);
241 pkg->name = trim_and_sub (pkg, str, path);
245 parse_version (Package *pkg, const char *str, const char *path)
249 verbose_error ("Version field occurs twice in '%s'\n", path);
254 pkg->version = trim_and_sub (pkg, str, path);
258 parse_description (Package *pkg, const char *str, const char *path)
260 if (pkg->description)
262 verbose_error ("Description field occurs twice in '%s'\n", path);
267 pkg->description = trim_and_sub (pkg, str, path);
271 #define MODULE_SEPARATOR(c) ((c) == ',' || isspace ((guchar)(c)))
272 #define OPERATOR_CHAR(c) ((c) == '<' || (c) == '>' || (c) == '!' || (c) == '=')
274 /* A module list is a list of modules with optional version specification,
275 * separated by commas and/or spaces. Commas are treated just like whitespace,
276 * in order to allow stuff like: Requires: @FRIBIDI_PC@, glib, gmodule
277 * where @FRIBIDI_PC@ gets substituted to nothing or to 'fribidi'
282 /* put numbers to help interpret lame debug spew ;-) */
288 IN_MODULE_VERSION = 5
294 split_module_list (const char *str, const char *path)
296 GSList *retval = NULL;
299 ModuleSplitState state = OUTSIDE_MODULE;
300 ModuleSplitState last_state = OUTSIDE_MODULE;
302 /* fprintf (stderr, "Parsing: '%s'\n", str); */
310 fprintf (stderr, "p: %c state: %d last_state: %d\n", *p, state, last_state);
316 if (!MODULE_SEPARATOR (*p))
317 state = IN_MODULE_NAME;
321 if (isspace ((guchar)*p))
323 /* Need to look ahead to determine next state */
325 while (*s && isspace ((guchar)*s))
329 state = OUTSIDE_MODULE;
330 else if (MODULE_SEPARATOR (*s))
331 state = OUTSIDE_MODULE;
332 else if (OPERATOR_CHAR (*s))
333 state = BEFORE_OPERATOR;
335 state = OUTSIDE_MODULE;
337 else if (MODULE_SEPARATOR (*p))
338 state = OUTSIDE_MODULE; /* comma precludes any operators */
341 case BEFORE_OPERATOR:
342 /* We know an operator is coming up here due to lookahead from
345 if (isspace ((guchar)*p))
347 else if (OPERATOR_CHAR (*p))
350 g_assert_not_reached ();
354 if (!OPERATOR_CHAR (*p))
355 state = AFTER_OPERATOR;
359 if (!isspace ((guchar)*p))
360 state = IN_MODULE_VERSION;
363 case IN_MODULE_VERSION:
364 if (MODULE_SEPARATOR (*p))
365 state = OUTSIDE_MODULE;
369 g_assert_not_reached ();
372 if (state == OUTSIDE_MODULE &&
373 last_state != OUTSIDE_MODULE)
375 /* We left a module */
376 char *module = g_strndup (start, p - start);
377 retval = g_slist_prepend (retval, module);
380 fprintf (stderr, "found module: '%s'\n", module);
393 /* get the last module */
394 char *module = g_strndup (start, p - start);
395 retval = g_slist_prepend (retval, module);
398 fprintf (stderr, "found module: '%s'\n", module);
403 retval = g_slist_reverse (retval);
409 parse_module_list (Package *pkg, const char *str, const char *path)
413 GSList *retval = NULL;
415 split = split_module_list (str, path);
420 RequiredVersion *ver;
426 ver = g_new0 (RequiredVersion, 1);
427 ver->comparison = ALWAYS_MATCH;
429 retval = g_slist_prepend (retval, ver);
431 while (*p && MODULE_SEPARATOR (*p))
436 while (*p && !isspace ((guchar)*p))
439 while (*p && MODULE_SEPARATOR (*p))
447 verbose_error ("Empty package name in Requires or Conflicts in file '%s'\n", path);
452 ver->name = g_strdup (start);
456 while (*p && !isspace ((guchar)*p))
459 while (*p && isspace ((guchar)*p))
467 if (strcmp (start, "=") == 0)
468 ver->comparison = EQUAL;
469 else if (strcmp (start, ">=") == 0)
470 ver->comparison = GREATER_THAN_EQUAL;
471 else if (strcmp (start, "<=") == 0)
472 ver->comparison = LESS_THAN_EQUAL;
473 else if (strcmp (start, ">") == 0)
474 ver->comparison = GREATER_THAN;
475 else if (strcmp (start, "<") == 0)
476 ver->comparison = LESS_THAN;
477 else if (strcmp (start, "!=") == 0)
478 ver->comparison = NOT_EQUAL;
481 verbose_error ("Unknown version comparison operator '%s' after package name '%s' in file '%s'\n", start, ver->name, path);
489 while (*p && !MODULE_SEPARATOR (*p))
492 while (*p && MODULE_SEPARATOR (*p))
498 if (ver->comparison != ALWAYS_MATCH && *start == '\0')
500 verbose_error ("Comparison operator but no version after package name '%s' in file '%s'\n", ver->name, path);
507 ver->version = g_strdup (start);
510 g_assert (ver->name);
512 iter = g_slist_next (iter);
515 g_slist_foreach (split, (GFunc) g_free, NULL);
516 g_slist_free (split);
518 retval = g_slist_reverse (retval);
524 parse_requires (Package *pkg, const char *str, const char *path)
532 verbose_error ("Requires field occurs twice in '%s'\n", path);
537 trimmed = trim_and_sub (pkg, str, path);
538 parsed = parse_module_list (pkg, trimmed, path);
545 RequiredVersion *ver = iter->data;
547 req = get_package (ver->name);
551 verbose_error ("Package '%s', required by '%s', not found\n",
552 ver->name, pkg->name ? pkg->name : path);
557 if (pkg->required_versions == NULL)
558 pkg->required_versions = g_hash_table_new (g_str_hash, g_str_equal);
560 g_hash_table_insert (pkg->required_versions, ver->name, ver);
562 pkg->requires = g_slist_prepend (pkg->requires, req);
564 iter = g_slist_next (iter);
567 g_slist_free (parsed);
571 parse_requires_private (Package *pkg, const char *str, const char *path)
577 if (pkg->requires_private)
579 verbose_error ("Requires.private field occurs twice in '%s'\n", path);
584 trimmed = trim_and_sub (pkg, str, path);
585 parsed = parse_module_list (pkg, trimmed, path);
592 RequiredVersion *ver = iter->data;
594 req = get_package (ver->name);
598 verbose_error ("Package '%s', required by '%s', not found\n",
599 ver->name, pkg->name ? pkg->name : path);
604 if (pkg->required_versions == NULL)
605 pkg->required_versions = g_hash_table_new (g_str_hash, g_str_equal);
607 g_hash_table_insert (pkg->required_versions, ver->name, ver);
609 pkg->requires_private = g_slist_prepend (pkg->requires_private, req);
611 iter = g_slist_next (iter);
614 g_slist_free (parsed);
618 parse_conflicts (Package *pkg, const char *str, const char *path)
624 verbose_error ("Conflicts field occurs twice in '%s'\n", path);
629 trimmed = trim_and_sub (pkg, str, path);
630 pkg->conflicts = parse_module_list (pkg, trimmed, path);
634 static char *strdup_escape_shell(const char *s)
636 size_t r_s = strlen(s)+10, c = 0;
637 char *r = g_malloc(r_s);
640 (s[0] > '$' && s[0] < '(') ||
641 (s[0] > ')' && s[0] < '+') ||
642 (s[0] > ':' && s[0] < '=') ||
643 (s[0] > '=' && s[0] < '@') ||
644 (s[0] > 'Z' && s[0] < '^') ||
654 r = g_realloc(r, r_s);
662 static void _do_parse_libs (Package *pkg, int argc, char **argv)
666 char *L_flag = (msvc_syntax ? "/libpath:" : "-L");
667 char *l_flag = (msvc_syntax ? "" : "-l");
668 char *lib_suffix = (msvc_syntax ? ".lib" : "");
672 char *lib_suffix = "";
678 char *tmp = trim_string (argv[i]);
679 char *arg = strdup_escape_shell(tmp);
686 /* -lib: is used by the C# compiler for libs; it's not an -l
688 (strncmp(p, "-lib:", 5) != 0))
691 while (*p && isspace ((guchar)*p))
694 pkg->l_libs = g_slist_prepend (pkg->l_libs,
695 g_strconcat (l_flag, p, lib_suffix, NULL));
698 else if (p[0] == '-' &&
702 while (*p && isspace ((guchar)*p))
704 pkg->L_libs = g_slist_prepend (pkg->L_libs,
705 g_strconcat (L_flag, p, NULL));
707 else if (strcmp("-framework",p) == 0 && i+1 < argc)
709 /* Mac OS X has a -framework Foo which is really one option,
710 * so we join those to avoid having -framework Foo
711 * -framework Bar being changed into -framework Foo Bar
714 gchar *framework, *tmp = trim_string (argv[i+1]);
716 framework = strdup_escape_shell(tmp);
717 pkg->other_libs = g_slist_prepend (pkg->other_libs,
718 g_strconcat(arg, " ", framework, NULL));
726 pkg->other_libs = g_slist_prepend (pkg->other_libs,
739 parse_libs (Package *pkg, const char *str, const char *path)
741 /* Strip out -l and -L flags, put them in a separate list. */
746 GError *error = NULL;
748 if (pkg->libs_num > 0)
750 verbose_error ("Libs field occurs twice in '%s'\n", path);
755 trimmed = trim_and_sub (pkg, str, path);
757 if (trimmed && *trimmed &&
758 !g_shell_parse_argv (trimmed, &argc, &argv, &error))
760 verbose_error ("Couldn't parse Libs field into an argument vector: %s\n",
761 error ? error->message : "unknown");
765 _do_parse_libs(pkg, argc, argv);
773 parse_libs_private (Package *pkg, const char *str, const char *path)
776 List of private libraries. Private libraries are libraries which
777 are needed in the case of static linking or on platforms not
778 supporting inter-library dependencies. They are not supposed to
779 be used for libraries which are exposed through the library in
780 question. An example of an exposed library is GTK+ exposing Glib.
781 A common example of a private library is libm.
783 Generally, if include another library's headers in your own, it's
784 a public dependency and not a private one.
790 GError *error = NULL;
792 if (pkg->libs_private_num > 0)
794 verbose_error ("Libs.private field occurs twice in '%s'\n", path);
799 trimmed = trim_and_sub (pkg, str, path);
801 if (trimmed && *trimmed &&
802 !g_shell_parse_argv (trimmed, &argc, &argv, &error))
804 verbose_error ("Couldn't parse Libs.private field into an argument vector: %s\n",
805 error ? error->message : "unknown");
809 _do_parse_libs(pkg, argc, argv);
814 pkg->libs_private_num++;
818 parse_cflags (Package *pkg, const char *str, const char *path)
820 /* Strip out -I flags, put them in a separate list. */
825 GError *error = NULL;
828 if (pkg->I_cflags || pkg->other_cflags)
830 verbose_error ("Cflags field occurs twice in '%s'\n", path);
835 trimmed = trim_and_sub (pkg, str, path);
837 if (trimmed && *trimmed &&
838 !g_shell_parse_argv (trimmed, &argc, &argv, &error))
840 verbose_error ("Couldn't parse Cflags field into an argument vector: %s\n",
841 error ? error->message : "unknown");
848 char *tmp = trim_string (argv[i]);
849 char *arg = strdup_escape_shell(tmp);
857 while (*p && isspace ((guchar)*p))
860 pkg->I_cflags = g_slist_prepend (pkg->I_cflags,
861 g_strconcat ("-I", p, NULL));
865 pkg->other_cflags = g_slist_prepend (pkg->other_cflags,
867 if (strcmp("-idirafter", arg) == 0) {
870 tmp = trim_string(argv[++i]);
871 n = strdup_escape_shell(tmp);
872 pkg->other_cflags = g_slist_prepend(pkg->other_cflags, n);
887 parse_url (Package *pkg, const char *str, const char *path)
889 if (pkg->url != NULL)
891 verbose_error ("URL field occurs twice in '%s'\n", path);
896 pkg->url = trim_and_sub (pkg, str, path);
900 static char *orig_prefix = NULL;
903 pathnamecmp (const char *a,
907 ((G_IS_DIR_SEPARATOR (*a) && G_IS_DIR_SEPARATOR (*b)) ||
908 g_ascii_toupper (*a) == g_ascii_toupper (*b)))
913 return g_ascii_toupper (*a) - g_ascii_toupper (*b);
918 parse_line (Package *pkg, const char *untrimmed, const char *path,
919 gboolean ignore_requires, gboolean ignore_private_libs,
920 gboolean ignore_requires_private)
926 debug_spew (" line>%s\n", untrimmed);
928 str = trim_string (untrimmed);
930 if (*str == '\0') /* empty line */
939 while ((*p >= 'A' && *p <= 'Z') ||
940 (*p >= 'a' && *p <= 'z') ||
941 (*p >= '0' && *p <= '9') ||
942 *p == '_' || *p == '.')
945 tag = g_strndup (str, p - str);
947 while (*p && isspace ((guchar)*p))
954 while (*p && isspace ((guchar)*p))
957 if (strcmp (tag, "Name") == 0)
958 parse_name (pkg, p, path);
959 else if (strcmp (tag, "Description") == 0)
960 parse_description (pkg, p, path);
961 else if (strcmp (tag, "Version") == 0)
962 parse_version (pkg, p, path);
963 else if (strcmp (tag, "Requires.private") == 0)
965 if (!ignore_requires_private)
966 parse_requires_private (pkg, p, path);
968 else if (strcmp (tag, "Requires") == 0)
970 if (ignore_requires == FALSE)
971 parse_requires (pkg, p, path);
975 else if ((strcmp (tag, "Libs.private") == 0) &&
976 ignore_private_libs == FALSE)
977 parse_libs_private (pkg, p, path);
978 else if (strcmp (tag, "Libs") == 0)
979 parse_libs (pkg, p, path);
980 else if (strcmp (tag, "Cflags") == 0 ||
981 strcmp (tag, "CFlags") == 0)
982 parse_cflags (pkg, p, path);
983 else if (strcmp (tag, "Conflicts") == 0)
984 parse_conflicts (pkg, p, path);
985 else if (strcmp (tag, "URL") == 0)
986 parse_url (pkg, p, path);
989 /* we don't error out on unknown keywords because they may
990 * represent additions to the .pc file format from future
991 * versions of pkg-config. We do make a note of them in the
992 * debug spew though, in order to help catch mistakes in .pc
994 debug_spew ("Unknown keyword '%s' in '%s'\n",
1005 while (*p && isspace ((guchar)*p))
1008 if (pkg->vars == NULL)
1009 pkg->vars = g_hash_table_new (g_str_hash, g_str_equal);
1012 if (!dont_define_prefix && strcmp (tag, prefix_variable) == 0)
1014 /* This is the prefix variable. Try to guesstimate a value for it
1015 * for this package from the location of the .pc file.
1018 gchar *prefix = pkg->pcfiledir;
1019 const int prefix_len = strlen (prefix);
1020 const char *const lib_pkgconfig = "\\lib\\pkgconfig";
1021 const char *const share_pkgconfig = "\\share\\pkgconfig";
1022 const int lib_pkgconfig_len = strlen (lib_pkgconfig);
1023 const int share_pkgconfig_len = strlen (share_pkgconfig);
1025 if ((strlen (prefix) > lib_pkgconfig_len &&
1026 pathnamecmp (prefix + prefix_len - lib_pkgconfig_len, lib_pkgconfig) == 0) ||
1027 (strlen (prefix) > share_pkgconfig_len &&
1028 pathnamecmp (prefix + prefix_len - share_pkgconfig_len, share_pkgconfig) == 0))
1030 /* It ends in lib\pkgconfig or share\pkgconfig. Good. */
1034 orig_prefix = g_strdup (p);
1036 prefix = g_strdup (prefix);
1037 if (strlen (prefix) > lib_pkgconfig_len &&
1038 pathnamecmp (prefix + prefix_len - lib_pkgconfig_len, lib_pkgconfig) == 0)
1039 prefix[prefix_len - lib_pkgconfig_len] = '\0';
1041 prefix[prefix_len - share_pkgconfig_len] = '\0';
1043 /* Turn backslashes into slashes or
1044 * g_shell_parse_argv() will eat them when ${prefix}
1045 * has been expanded in parse_libs().
1055 /* Now escape the special characters so that there's no danger
1056 * of arguments that include the prefix getting split.
1059 prefix = strdup_escape_shell (prefix);
1062 varname = g_strdup (tag);
1063 debug_spew (" Variable declaration, '%s' overridden with '%s'\n",
1065 g_hash_table_insert (pkg->vars, varname, prefix);
1069 else if (!dont_define_prefix &&
1070 orig_prefix != NULL &&
1071 strncmp (p, orig_prefix, strlen (orig_prefix)) == 0 &&
1072 G_IS_DIR_SEPARATOR (p[strlen (orig_prefix)]))
1076 p = str = g_strconcat (g_hash_table_lookup (pkg->vars, prefix_variable), p + strlen (orig_prefix), NULL);
1081 if (g_hash_table_lookup (pkg->vars, tag))
1083 verbose_error ("Duplicate definition of variable '%s' in '%s'\n",
1089 varname = g_strdup (tag);
1090 varval = trim_and_sub (pkg, p, path);
1092 debug_spew (" Variable declaration, '%s' has value '%s'\n",
1094 g_hash_table_insert (pkg->vars, varname, varval);
1104 parse_package_file (const char *path, gboolean ignore_requires,
1105 gboolean ignore_private_libs,
1106 gboolean ignore_requires_private)
1111 gboolean one_line = FALSE;
1113 f = fopen (path, "r");
1117 verbose_error ("Failed to open '%s': %s\n",
1118 path, strerror (errno));
1123 debug_spew ("Parsing package file '%s'\n", path);
1125 pkg = g_new0 (Package, 1);
1129 pkg->pcfiledir = g_dirname (path);
1133 debug_spew ("No pcfiledir determined for package\n");
1134 pkg->pcfiledir = g_strdup ("???????");
1137 str = g_string_new ("");
1139 while (read_one_line (f, str))
1143 parse_line (pkg, str->str, path, ignore_requires, ignore_private_libs,
1144 ignore_requires_private);
1146 g_string_truncate (str, 0);
1150 verbose_error ("Package file '%s' appears to be empty\n",
1152 g_string_free (str, TRUE);
1155 /* make ->requires_private include a copy of the public requires too */
1156 pkg->requires_private = g_slist_concat(g_slist_copy (pkg->requires),
1157 pkg->requires_private);
1159 pkg->requires = g_slist_reverse (pkg->requires);
1161 pkg->requires_private = g_slist_reverse (pkg->requires_private);
1163 pkg->I_cflags = g_slist_reverse (pkg->I_cflags);
1164 pkg->other_cflags = g_slist_reverse (pkg->other_cflags);
1166 pkg->l_libs = g_slist_reverse (pkg->l_libs);
1167 pkg->L_libs = g_slist_reverse (pkg->L_libs);
1168 pkg->other_libs = g_slist_reverse (pkg->other_libs);