Avoid linking programs with plugin libraries
[anjuta.git] / libanjuta / anjuta-pkg-config.c
blob2dd17a472b14444702e10974409ddf2384d93bd8
1 /*
2 * anjuta-pkg-config.c
4 * Copyright (C) 2010 - Johannes Schmid
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU 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, see <http://www.gnu.org/licenses/>.
20 #include <glib.h>
21 #include <string.h>
23 #include <libanjuta/anjuta-pkg-config.h>
24 #include <libanjuta/anjuta-utils.h>
25 #include <libanjuta/anjuta-debug.h>
27 /* Blacklist for packages that shouldn't be parsed. Those packages
28 * usually contain the same include paths as their top-level package
29 * Checked with g_str_has_prefix() so partial names are ok!
31 const gchar* ignore_packages[] =
33 "gdk-x11-",
34 NULL
37 static GList*
38 remove_includes (GList* includes, GList* dependencies)
40 GList* dir;
41 for (dir = dependencies; dir != NULL; dir = g_list_next (dir))
43 GList* find = g_list_find_custom (includes, dir->data, (GCompareFunc)strcmp);
44 if (find)
46 g_free (find->data);
47 includes = g_list_delete_link (includes, find);
50 return includes;
54 GList*
55 anjuta_pkg_config_list_dependencies (const gchar* package, GError** error)
57 GList* deps = NULL;
58 gchar* cmd;
59 gchar *err;
60 gchar *out;
61 gint status;
63 cmd = g_strdup_printf ("pkg-config --print-requires --print-requires-private %s",
64 package);
65 if (g_spawn_command_line_sync (cmd, &out, &err, &status, error))
67 gchar** depends = g_strsplit (out, "\n", -1);
68 if (depends != NULL)
70 gchar** depend;
71 for (depend = depends; *depend != NULL; depend++)
73 if (strlen (*depend) && !anjuta_pkg_config_ignore_package (*depend))
75 deps = g_list_append (deps, g_strdup (*depend));
78 g_strfreev (depends);
80 g_free (out);
82 g_free (cmd);
84 return deps;
87 GList*
88 anjuta_pkg_config_get_directories (const gchar* pkg_name, gboolean no_deps, GError** error)
90 gchar *cmd;
91 gchar *err;
92 gchar *out;
93 gint status;
94 GList *dirs = NULL;
96 cmd = g_strdup_printf ("pkg-config --cflags-only-I %s", pkg_name);
98 if (g_spawn_command_line_sync (cmd, &out, &err, &status, error))
100 gchar **flags;
102 flags = g_strsplit (out, " ", -1);
104 if (flags != NULL)
106 gchar **flag;
108 for (flag = flags; *flag != NULL; flag++)
110 if (g_regex_match_simple ("\\.*/include/\\w+", *flag, 0, 0) == TRUE)
112 dirs = g_list_prepend (dirs, g_strdup (*flag + 2));
115 g_strfreev (flags);
117 g_free (out);
119 g_free (cmd);
121 if (dirs && no_deps)
123 GList* pkgs = anjuta_pkg_config_list_dependencies (pkg_name, error);
124 GList* pkg;
125 for (pkg = pkgs; pkg != NULL; pkg = g_list_next (pkg))
127 GList* dep_dirs = anjuta_pkg_config_get_directories (pkg->data, FALSE, NULL);
128 dirs = remove_includes (dirs, dep_dirs);
129 anjuta_util_glist_strings_free (dep_dirs);
131 anjuta_util_glist_strings_free (pkgs);
134 return dirs;
137 gboolean
138 anjuta_pkg_config_ignore_package (const gchar* name)
140 const gchar** pkg;
141 for (pkg = ignore_packages; *pkg != NULL; pkg++)
143 if (g_str_has_prefix (name, *pkg))
144 return TRUE;
146 return FALSE;
150 * anjuta_pkg_config_get_version:
151 * @package: Name of the package
153 * This does sync io, call from a thread if necessary
155 * Returns: (transfer full) the version of the package or %NULL
157 gchar* anjuta_pkg_config_get_version (const gchar* package)
159 gchar *cmd;
160 gchar *err;
161 gchar *out;
162 gint status;
163 GError* error = NULL;
165 cmd = g_strdup_printf ("pkg-config --modversion %s", package);
167 if (g_spawn_command_line_sync (cmd, &out, &err, &status, &error))
169 g_free (err);
170 g_free (cmd);
171 return out;
173 else
175 g_free (out);
176 g_free (err);
177 g_free (cmd);
178 if (error)
180 DEBUG_PRINT ("Could query package version: %s", error->message);
181 g_error_free (error);
183 return NULL;