1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
|
/*
* Copyright (C) 2001, 2002 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "pkg.h"
#include "parse.h"
#ifdef HAVE_ALLOCA_H
# include <alloca.h>
#else
# ifdef _AIX
# pragma alloca
# endif
#endif
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdlib.h>
#include <ctype.h>
static void verify_package (Package *pkg);
static GHashTable *packages = NULL;
static GHashTable *locations = NULL;
static GHashTable *path_positions = NULL;
static GHashTable *globals = NULL;
static GSList *search_dirs = NULL;
static int scanned_dir_count = 0;
gboolean disable_uninstalled = FALSE;
gboolean ignore_requires = FALSE;
gboolean ignore_private_libs = TRUE;
static Package pkg_config_package = {
.key = PACKAGE,
.name = PACKAGE,
.version = VERSION, /* .version */
.description = "returns metainformation about installed libraries",
.url = "http://pkg-config.freedesktop.org",
0 /* keep the rest as null */
};
void
add_search_dir (const char *path)
{
search_dirs = g_slist_append (search_dirs, g_strdup (path));
}
void
add_search_dirs (const char *path, const char *separator)
{
char **search_dirs;
char **iter;
search_dirs = g_strsplit (path, separator, -1);
iter = search_dirs;
while (*iter)
{
debug_spew ("Adding directory '%s' from PKG_CONFIG_PATH\n",
*iter);
add_search_dir (*iter);
++iter;
}
g_strfreev (search_dirs);
}
#ifdef G_OS_WIN32
/* Guard against .pc file being installed with UPPER CASE name */
# define FOLD(x) tolower(x)
# define FOLDCMP(a, b) g_ascii_strcasecmp (a, b)
#else
# define FOLD(x) (x)
# define FOLDCMP(a, b) strcmp (a, b)
#endif
#define EXT_LEN 3
static gboolean
ends_in_dotpc (const char *str)
{
int len = strlen (str);
if (len > EXT_LEN &&
str[len - 3] == '.' &&
FOLD (str[len - 2]) == 'p' &&
FOLD (str[len - 1]) == 'c')
return TRUE;
else
return FALSE;
}
/* strlen ("uninstalled") */
#define UNINSTALLED_LEN 11
gboolean
name_ends_in_uninstalled (const char *str)
{
int len = strlen (str);
if (len > UNINSTALLED_LEN &&
FOLDCMP ((str + len - UNINSTALLED_LEN), "uninstalled") == 0)
return TRUE;
else
return FALSE;
}
/* Look for .pc files in the given directory and add them into
* locations, ignoring duplicates
*/
static void
scan_dir (const char *dirname)
{
DIR *dir;
struct dirent *dent;
int dirnamelen = strlen (dirname);
/* Use a copy of dirname cause Win32 opendir doesn't like
* superfluous trailing (back)slashes in the directory name.
*/
char *dirname_copy = g_strdup (dirname);
if (dirnamelen > 1 && dirname[dirnamelen-1] == G_DIR_SEPARATOR)
{
dirnamelen--;
dirname_copy[dirnamelen] = '\0';
}
#ifdef G_OS_WIN32
{
gchar *p;
/* Turn backslashes into slashes or
* poptParseArgvString() will eat them when ${prefix}
* has been expanded in parse_libs().
*/
p = dirname;
while (*p)
{
if (*p == '\\')
*p = '/';
p++;
}
}
#endif
dir = opendir (dirname_copy);
g_free (dirname_copy);
if (!dir)
{
debug_spew ("Cannot open directory '%s' in package search path: %s\n",
dirname, g_strerror (errno));
return;
}
debug_spew ("Scanning directory '%s'\n", dirname);
scanned_dir_count += 1;
while ((dent = readdir (dir)))
{
int len = strlen (dent->d_name);
if (ends_in_dotpc (dent->d_name))
{
char *pkgname = malloc (len - 2);
debug_spew ("File '%s' appears to be a .pc file\n", dent->d_name);
strncpy (pkgname, dent->d_name, len - EXT_LEN);
pkgname[len-EXT_LEN] = '\0';
if (g_hash_table_lookup (locations, pkgname))
{
debug_spew ("File '%s' ignored, we already know about package '%s'\n", dent->d_name, pkgname);
g_free (pkgname);
}
else
{
char *filename = g_malloc (dirnamelen + 1 + len + 1);
strncpy (filename, dirname, dirnamelen);
filename[dirnamelen] = G_DIR_SEPARATOR;
strcpy (filename + dirnamelen + 1, dent->d_name);
g_hash_table_insert (locations, pkgname, filename);
g_hash_table_insert (path_positions, pkgname,
GINT_TO_POINTER (scanned_dir_count));
debug_spew ("Will find package '%s' in file '%s'\n",
pkgname, filename);
}
}
else
{
debug_spew ("Ignoring file '%s' in search directory; not a .pc file\n",
dent->d_name);
}
}
closedir(dir);
}
static Package *
add_virtual_pkgconfig_package (void)
{
Package *pkg = NULL;
pkg = g_new0 (Package, 1);
pkg->key = g_strdup ("pkg-config");
pkg->version = g_strdup (VERSION);
pkg->name = g_strdup ("pkg-config");
pkg->description = g_strdup ("pkg-config is a system for managing "
"compile/link flags for libraries");
pkg->url = g_strdup ("http://www.freedesktop.org/software/pkgconfig/");
debug_spew ("Adding virtual 'pkg-config' package to list of known packages\n");
g_hash_table_insert (packages, pkg->key, pkg);
return pkg;
}
void
package_init ()
{
static gboolean initted = FALSE;
if (!initted)
{
initted = TRUE;
packages = g_hash_table_new (g_str_hash, g_str_equal);
locations = g_hash_table_new (g_str_hash, g_str_equal);
path_positions = g_hash_table_new (g_str_hash, g_str_equal);
add_virtual_pkgconfig_package ();
g_slist_foreach (search_dirs, (GFunc)scan_dir, NULL);
}
}
static gboolean
file_readable (const char *path)
{
FILE *f = fopen (path, "r");
if (f != NULL)
{
fclose (f);
return TRUE;
}
else
return FALSE;
}
static Package *
internal_get_package (const char *name, gboolean warn, gboolean check_compat)
{
Package *pkg = NULL;
const char *location;
if (strcmp(PACKAGE, name) == 0)
return &pkg_config_package;
pkg = g_hash_table_lookup (packages, name);
if (pkg)
return pkg;
debug_spew ("Looking for package '%s'\n", name);
/* treat "name" as a filename if it ends in .pc and exists */
if ( ends_in_dotpc (name) )
{
debug_spew ("Considering '%s' to be a filename rather than a package name\n", name);
location = name;
}
else
{
/* See if we should auto-prefer the uninstalled version */
if (!disable_uninstalled &&
!name_ends_in_uninstalled (name))
{
char *un;
un = g_strconcat (name, "-uninstalled", NULL);
pkg = internal_get_package (un, FALSE, FALSE);
g_free (un);
if (pkg)
{
debug_spew ("Preferring uninstalled version of package '%s'\n", name);
return pkg;
}
}
location = g_hash_table_lookup (locations, name);
}
if (location == NULL && check_compat)
{
pkg = get_compat_package (name);
if (pkg)
{
debug_spew ("Returning values for '%s' from a legacy -config script\n",
name);
return pkg;
}
}
if (location == NULL)
{
if (warn)
verbose_error ("Package %s was not found in the pkg-config search path.\n"
"Perhaps you should add the directory containing `%s.pc'\n"
"to the PKG_CONFIG_PATH environment variable\n",
name, name);
return NULL;
}
debug_spew ("Reading '%s' from file '%s'\n", name, location);
pkg = parse_package_file (location, ignore_requires, ignore_private_libs);
if (pkg == NULL)
{
debug_spew ("Failed to parse '%s'\n", location);
return NULL;
}
if (strstr (location, "uninstalled.pc"))
pkg->uninstalled = TRUE;
if (location != name)
pkg->key = g_strdup (name);
else
{
/* need to strip package name out of the filename */
int len = strlen (name);
const char *end = name + (len - EXT_LEN);
const char *start = end;
while (start != name && *start != G_DIR_SEPARATOR)
--start;
g_assert (end >= start);
pkg->key = g_strndup (start, end - start);
}
pkg->path_position =
GPOINTER_TO_INT (g_hash_table_lookup (path_positions, pkg->key));
debug_spew ("Path position of '%s' is %d\n",
pkg->name, pkg->path_position);
verify_package (pkg);
debug_spew ("Adding '%s' to list of known packages, returning as package '%s'\n",
pkg->key, name);
g_hash_table_insert (packages, pkg->key, pkg);
return pkg;
}
Package *
get_package (const char *name)
{
return internal_get_package (name, TRUE, TRUE);
}
Package *
get_package_quiet (const char *name)
{
return internal_get_package (name, FALSE, TRUE);
}
static GSList*
string_list_strip_duplicates (GSList *list)
{
GHashTable *table;
GSList *tmp;
GSList *nodups = NULL;
table = g_hash_table_new (g_str_hash, g_str_equal);
tmp = list;
while (tmp != NULL)
{
if (g_hash_table_lookup (table, tmp->data) == NULL)
{
nodups = g_slist_prepend (nodups, tmp->data);
g_hash_table_insert (table, tmp->data, tmp->data);
}
else
{
debug_spew (" removing duplicate \"%s\"\n", tmp->data);
}
tmp = g_slist_next (tmp);
}
nodups = g_slist_reverse (nodups);
g_hash_table_destroy (table);
return nodups;
}
static GSList*
string_list_strip_duplicates_from_back (GSList *list)
{
GHashTable *table;
GSList *tmp;
GSList *nodups = NULL;
GSList *reversed;
table = g_hash_table_new (g_str_hash, g_str_equal);
reversed = g_slist_reverse (g_slist_copy (list));
tmp = reversed;
while (tmp != NULL)
{
if (g_hash_table_lookup (table, tmp->data) == NULL)
{
/* This unreverses the reversed list */
nodups = g_slist_prepend (nodups, tmp->data);
g_hash_table_insert (table, tmp->data, tmp->data);
}
else
{
debug_spew (" removing duplicate (from back) \"%s\"\n", tmp->data);
}
tmp = g_slist_next (tmp);
}
g_slist_free (reversed);
g_hash_table_destroy (table);
return nodups;
}
static char *
string_list_to_string (GSList *list)
{
GSList *tmp;
GString *str = g_string_new ("");
char *retval;
tmp = list;
while (tmp != NULL)
{
g_string_append (str, tmp->data);
g_string_append_c (str, ' ');
tmp = g_slist_next (tmp);
}
retval = str->str;
g_string_free (str, FALSE);
return retval;
}
typedef GSList *(* GetListFunc) (Package *pkg);
static GSList *
get_l_libs (Package *pkg)
{
return pkg->l_libs;
}
static GSList *
get_L_libs (Package *pkg)
{
return pkg->L_libs;
}
static GSList*
get_other_libs (Package *pkg)
{
return pkg->other_libs;
}
static GSList *
get_I_cflags (Package *pkg)
{
return pkg->I_cflags;
}
static GSList *
get_other_cflags (Package *pkg)
{
return pkg->other_cflags;
}
static GSList *
get_conflicts (Package *pkg)
{
return pkg->conflicts;
}
static GSList *
get_requires (Package *pkg)
{
return pkg->requires;
}
static GSList *
get_requires_private (Package *pkg)
{
return pkg->requires_private;
}
static int
pathposcmp (gconstpointer a, gconstpointer b)
{
const Package *pa = a;
const Package *pb = b;
if (pa->path_position < pb->path_position)
return -1;
else if (pa->path_position > pb->path_position)
return 1;
else
return 0;
}
static void
spew_package_list (const char *name,
GSList *list)
{
GSList *tmp;
debug_spew (" %s: ", name);
tmp = list;
while (tmp != NULL)
{
Package *pkg = tmp->data;
debug_spew (" %s ", pkg->name);
tmp = tmp->next;
}
debug_spew ("\n");
}
static void
spew_string_list (const char *name,
GSList *list)
{
GSList *tmp;
debug_spew (" %s: ", name);
tmp = list;
while (tmp != NULL)
{
debug_spew (" %s ", tmp->data);
tmp = tmp->next;
}
debug_spew ("\n");
}
static GSList*
packages_sort_by_path_position (GSList *list)
{
return g_slist_sort (list, pathposcmp);
}
static void
fill_one_level (Package *pkg, GetListFunc func, GSList **listp)
{
GSList *copy;
copy = g_slist_copy ((*func)(pkg));
*listp = g_slist_concat (*listp, copy);
}
static void
recursive_fill_list (Package *pkg, GetListFunc func, GSList **listp)
{
GSList *tmp;
fill_one_level (pkg, func, listp);
tmp = pkg->requires;
while (tmp != NULL)
{
recursive_fill_list (tmp->data, func, listp);
tmp = g_slist_next (tmp);
}
}
static void
fill_list_single_package (Package *pkg, GetListFunc func,
GSList **listp, gboolean in_path_order,
gboolean include_private)
{
/* First we get the list in natural/recursive order, then
* stable sort by path position
*/
GSList *packages;
GSList *tmp;
/* Get list of packages */
packages = NULL;
packages = g_slist_append (packages, pkg);
recursive_fill_list (pkg,
include_private ? get_requires_private : get_requires,
&packages);
if (in_path_order)
{
spew_package_list ("original", packages);
packages = packages_sort_by_path_position (packages);
spew_package_list ("sorted", packages);
}
/* Convert package list to string list */
tmp = packages;
while (tmp != NULL)
{
fill_one_level (tmp->data, func, listp);
tmp = tmp->next;
}
g_slist_free (packages);
}
static void
fill_list (GSList *packages, GetListFunc func,
GSList **listp, gboolean in_path_order, gboolean include_private)
{
GSList *tmp;
GSList *expanded;
expanded = NULL;
tmp = packages;
while (tmp != NULL)
{
expanded = g_slist_append (expanded, tmp->data);
recursive_fill_list (tmp->data,
include_private ? get_requires_private : get_requires,
&expanded);
tmp = tmp->next;
}
if (in_path_order)
{
spew_package_list ("original", expanded);
expanded = packages_sort_by_path_position (expanded);
spew_package_list ("sorted", expanded);
}
tmp = expanded;
while (tmp != NULL)
{
fill_one_level (tmp->data, func, listp);
tmp = tmp->next;
}
g_slist_free (expanded);
}
static gint
compare_req_version_names (gconstpointer a, gconstpointer b)
{
const RequiredVersion *ver_a = a;
const RequiredVersion *ver_b = b;
return strcmp (ver_a->name, ver_b->name);
}
static gint
compare_package_keys (gconstpointer a, gconstpointer b)
{
const Package *pkg_a = a;
const Package *pkg_b = b;
return strcmp (pkg_a->key, pkg_b->key);
}
static GSList *
add_env_variable_to_list (GSList *list, const gchar *env)
{
gchar **values;
gint i;
values = g_strsplit (env, G_SEARCHPATH_SEPARATOR_S, 0);
for (i = 0; values[i] != NULL; i++)
{
list = g_slist_append (list, g_strdup (values[i]));
}
g_strfreev (values);
return list;
}
static void
verify_package (Package *pkg)
{
GSList *requires = NULL;
GSList *conflicts = NULL;
GSList *system_directories = NULL;
GSList *iter;
GSList *requires_iter;
GSList *conflicts_iter;
GSList *system_dir_iter = NULL;
int count;
gchar *c_include_path;
/* Be sure we have the required fields */
if (pkg->key == NULL)
{
fprintf (stderr,
"Internal pkg-config error, package with no key, please file a bug report\n");
exit (1);
}
if (pkg->name == NULL)
{
verbose_error ("Package '%s' has no Name: field\n",
pkg->key);
exit (1);
}
if (pkg->version == NULL)
{
verbose_error ("Package '%s' has no Version: field\n",
pkg->key);
exit (1);
}
if (pkg->description == NULL)
{
verbose_error ("Package '%s' has no Description: field\n",
pkg->key);
exit (1);
}
/* Make sure we have the right version for all requirements */
iter = pkg->requires_private;
while (iter != NULL)
{
Package *req = iter->data;
RequiredVersion *ver = NULL;
if (pkg->required_versions)
ver = g_hash_table_lookup (pkg->required_versions,
req->key);
if (ver)
{
if (!version_test (ver->comparison, req->version, ver->version))
{
verbose_error ("Package '%s' requires '%s %s %s' but version of %s is %s\n",
pkg->name, req->key,
comparison_to_str (ver->comparison),
ver->version,
req->name,
req->version);
if (req->url)
verbose_error ("You may find new versions of %s at %s\n",
req->name, req->url);
exit (1);
}
}
iter = g_slist_next (iter);
}
/* Make sure we didn't drag in any conflicts via Requires
* (inefficient algorithm, who cares)
*/
recursive_fill_list (pkg, get_requires_private, &requires);
recursive_fill_list (pkg, get_conflicts, &conflicts);
requires_iter = requires;
while (requires_iter != NULL)
{
Package *req = requires_iter->data;
conflicts_iter = conflicts;
while (conflicts_iter != NULL)
{
RequiredVersion *ver = conflicts_iter->data;
if (version_test (ver->comparison,
req->version,
ver->version))
{
verbose_error ("Version %s of %s creates a conflict.\n"
"(%s %s %s conflicts with %s %s)\n",
req->version, req->name,
ver->name,
comparison_to_str (ver->comparison),
ver->version,
ver->owner->name,
ver->owner->version);
exit (1);
}
conflicts_iter = g_slist_next (conflicts_iter);
}
requires_iter = g_slist_next (requires_iter);
}
g_slist_free (requires);
g_slist_free (conflicts);
/* We make a list of system directories that gcc expects so we can remove
* them.
*/
#ifndef G_OS_WIN32
system_directories = g_slist_append (NULL, g_strdup ("/usr/include"));
#endif
c_include_path = g_getenv ("C_INCLUDE_PATH");
if (c_include_path != NULL)
{
system_directories = add_env_variable_to_list (system_directories, c_include_path);
}
c_include_path = g_getenv ("CPLUS_INCLUDE_PATH");
if (c_include_path != NULL)
{
system_directories = add_env_variable_to_list (system_directories, c_include_path);
}
count = 0;
iter = pkg->I_cflags;
while (iter != NULL)
{
gint offset = 0;
/* we put things in canonical -I/usr/include (vs. -I /usr/include) format,
* but if someone changes it later we may as well be robust
*/
if (((strncmp (iter->data, "-I", 2) == 0) && (offset = 2))||
((strncmp (iter->data, "-I ", 3) == 0) && (offset = 3)))
{
if (offset == 0)
{
iter = iter->next;
continue;
}
system_dir_iter = system_directories;
while (system_dir_iter != NULL)
{
if (strcmp (system_dir_iter->data,
((char*)iter->data) + offset) == 0)
{
debug_spew ("Package %s has %s in Cflags\n",
pkg->name, (gchar *)iter->data);
if (g_getenv ("PKG_CONFIG_ALLOW_SYSTEM_CFLAGS") == NULL)
{
debug_spew ("Removing %s from cflags for %s\n", iter->data, pkg->key);
++count;
iter->data = NULL;
break;
}
}
system_dir_iter = system_dir_iter->next;
}
}
iter = iter->next;
}
while (count)
{
pkg->I_cflags = g_slist_remove (pkg->I_cflags, NULL);
--count;
}
g_slist_foreach (system_directories, (GFunc) g_free, NULL);
g_slist_free (system_directories);
#ifdef PREFER_LIB64
#define SYSTEM_LIBDIR "/usr/lib64"
#else
#define SYSTEM_LIBDIR "/usr/lib"
#endif
count = 0;
iter = pkg->L_libs;
while (iter != NULL)
{
if (strcmp (iter->data, "-L" SYSTEM_LIBDIR) == 0 ||
strcmp (iter->data, "-L " SYSTEM_LIBDIR) == 0)
{
debug_spew ("Package %s has -L" SYSTEM_LIBDIR " in Libs\n",
pkg->name);
if (g_getenv ("PKG_CONFIG_ALLOW_SYSTEM_LIBS") == NULL)
{
iter->data = NULL;
++count;
debug_spew ("Removing -L" SYSTEM_LIBDIR " from libs for %s\n", pkg->key);
}
}
iter = iter->next;
}
#undef SYSTEM_LIBDIR
while (count)
{
pkg->L_libs = g_slist_remove (pkg->L_libs, NULL);
--count;
}
}
static char*
get_merged (Package *pkg, GetListFunc func, gboolean in_path_order,
gboolean include_private)
{
GSList *list;
GSList *dups_list = NULL;
char *retval;
fill_list_single_package (pkg, func, &dups_list, in_path_order,
include_private);
list = string_list_strip_duplicates (dups_list);
g_slist_free (dups_list);
retval = string_list_to_string (list);
g_slist_free (list);
return retval;
}
static char*
get_merged_from_back (Package *pkg, GetListFunc func, gboolean in_path_order,
gboolean include_private)
{
GSList *list;
GSList *dups_list = NULL;
char *retval;
fill_list_single_package (pkg, func, &dups_list, in_path_order,
include_private);
list = string_list_strip_duplicates_from_back (dups_list);
g_slist_free (dups_list);
retval = string_list_to_string (list);
g_slist_free (list);
return retval;
}
static char*
get_multi_merged (GSList *pkgs, GetListFunc func, gboolean in_path_order,
gboolean include_private)
{
GSList *tmp;
GSList *dups_list = NULL;
GSList *list;
char *retval;
fill_list (pkgs, func, &dups_list, in_path_order, include_private);
list = string_list_strip_duplicates (dups_list);
g_slist_free (dups_list);
retval = string_list_to_string (list);
g_slist_free (list);
return retval;
}
static char*
get_multi_merged_from_back (GSList *pkgs, GetListFunc func,
gboolean in_path_order, gboolean include_private)
{
GSList *tmp;
GSList *dups_list = NULL;
GSList *list;
char *retval;
fill_list (pkgs, func, &dups_list, in_path_order, include_private);
list = string_list_strip_duplicates_from_back (dups_list);
g_slist_free (dups_list);
retval = string_list_to_string (list);
g_slist_free (list);
return retval;
}
char *
package_get_l_libs (Package *pkg)
{
/* We don't want these in search path order, rather in dependency
* order, so static linking works.
*/
if (pkg->l_libs_merged == NULL)
pkg->l_libs_merged = get_merged_from_back (pkg, get_l_libs, FALSE,
!ignore_private_libs);
return pkg->l_libs_merged;
}
char *
packages_get_l_libs (GSList *pkgs)
{
return get_multi_merged_from_back (pkgs, get_l_libs, FALSE,
!ignore_private_libs);
}
char *
package_get_L_libs (Package *pkg)
{
/* We want these in search path order so the -L flags don't override PKG_CONFIG_PATH */
if (pkg->L_libs_merged == NULL)
pkg->L_libs_merged = get_merged (pkg, get_L_libs, TRUE,
!ignore_private_libs);
return pkg->L_libs_merged;
}
char *
packages_get_L_libs (GSList *pkgs)
{
return get_multi_merged (pkgs, get_L_libs, TRUE, !ignore_private_libs);
}
char *
package_get_other_libs (Package *pkg)
{
if (pkg->other_libs_merged == NULL)
pkg->other_libs_merged = get_merged (pkg, get_other_libs, TRUE,
!ignore_private_libs);
return pkg->other_libs_merged;
}
char *
packages_get_other_libs (GSList *pkgs)
{
return get_multi_merged (pkgs, get_other_libs, TRUE, !ignore_private_libs);
}
char *
packages_get_all_libs (GSList *pkgs)
{
char *l_libs;
char *L_libs;
char *other_libs;
GString *str;
char *retval;
str = g_string_new ("");
other_libs = packages_get_other_libs (pkgs);
L_libs = packages_get_L_libs (pkgs);
l_libs = packages_get_l_libs (pkgs);
if (other_libs)
g_string_append (str, other_libs);
if (L_libs)
g_string_append (str, L_libs);
if (l_libs)
g_string_append (str, l_libs);
g_free (l_libs);
g_free (L_libs);
g_free (other_libs);
retval = str->str;
g_string_free (str, FALSE);
return retval;
}
char *
package_get_I_cflags (Package *pkg)
{
/* sort by path position so PKG_CONFIG_PATH affects -I flag order */
if (pkg->I_cflags_merged == NULL)
pkg->I_cflags_merged = get_merged (pkg, get_I_cflags, TRUE, FALSE);
return pkg->I_cflags_merged;
}
char *
packages_get_I_cflags (GSList *pkgs)
{
/* sort by path position so PKG_CONFIG_PATH affects -I flag order */
return get_multi_merged (pkgs, get_I_cflags, TRUE, TRUE);
}
char *
package_get_other_cflags (Package *pkg)
{
if (pkg->other_cflags_merged == NULL)
pkg->other_cflags_merged = get_merged (pkg, get_other_cflags, TRUE, TRUE);
return pkg->other_cflags_merged;
}
char *
packages_get_other_cflags (GSList *pkgs)
{
return get_multi_merged (pkgs, get_other_cflags, TRUE, TRUE);
}
char *
package_get_cflags (Package *pkg)
{
g_assert_not_reached ();
return NULL;
}
char *
packages_get_all_cflags (GSList *pkgs)
{
char *I_cflags;
char *other_cflags;
GString *str;
char *retval;
str = g_string_new ("");
other_cflags = packages_get_other_cflags (pkgs);
I_cflags = packages_get_I_cflags (pkgs);
if (other_cflags)
g_string_append (str, other_cflags);
if (I_cflags)
g_string_append (str, I_cflags);
g_free (I_cflags);
g_free (other_cflags);
retval = str->str;
g_string_free (str, FALSE);
return retval;
}
void
define_global_variable (const char *varname,
const char *varval)
{
if (globals == NULL)
globals = g_hash_table_new (g_str_hash, g_str_equal);
if (g_hash_table_lookup (globals, varname))
{
verbose_error ("Variable '%s' defined twice globally\n", varname);
exit (1);
}
g_hash_table_insert (globals, g_strdup (varname), g_strdup (varval));
debug_spew ("Global variable definition '%s' = '%s'\n",
varname, varval);
}
char *
package_get_var (Package *pkg,
const char *var)
{
char *varval = NULL;
if (globals)
varval = g_strdup (g_hash_table_lookup (globals, var));
if (varval == NULL && pkg->vars)
varval = g_strdup (g_hash_table_lookup (pkg->vars, var));
/* Magic "pcfiledir" variable */
if (varval == NULL && pkg->pcfiledir && strcmp (var, "pcfiledir") == 0)
varval = g_strdup (pkg->pcfiledir);
return varval;
}
char *
packages_get_var (GSList *pkgs,
const char *varname)
{
GSList *tmp;
GString *str;
char *retval;
str = g_string_new ("");
tmp = pkgs;
while (tmp != NULL)
{
Package *pkg = tmp->data;
char *var;
var = package_get_var (pkg, varname);
if (var)
{
g_string_append (str, var);
g_string_append_c (str, ' ');
g_free (var);
}
tmp = g_slist_next (tmp);
}
/* chop last space */
if (str->len > 0)
str->str[str->len - 1] = '\0';
retval = str->str;
g_string_free (str, FALSE);
return retval;
}
/* Stolen verbatim from rpm/lib/misc.c
RPM is Copyright (c) 1998 by Red Hat Software, Inc.,
and may be distributed under the terms of the GPL and LGPL.
*/
/* compare alpha and numeric segments of two versions */
/* return 1: a is newer than b */
/* 0: a and b are the same version */
/* -1: b is newer than a */
static int rpmvercmp(const char * a, const char * b) {
char oldch1, oldch2;
char * str1, * str2;
char * one, * two;
int rc;
int isnum;
/* easy comparison to see if versions are identical */
if (!strcmp(a, b)) return 0;
str1 = alloca(strlen(a) + 1);
str2 = alloca(strlen(b) + 1);
strcpy(str1, a);
strcpy(str2, b);
one = str1;
two = str2;
/* loop through each version segment of str1 and str2 and compare them */
while (*one && *two) {
while (*one && !isalnum((guchar)*one)) one++;
while (*two && !isalnum((guchar)*two)) two++;
str1 = one;
str2 = two;
/* grab first completely alpha or completely numeric segment */
/* leave one and two pointing to the start of the alpha or numeric */
/* segment and walk str1 and str2 to end of segment */
if (isdigit((guchar)*str1)) {
while (*str1 && isdigit((guchar)*str1)) str1++;
while (*str2 && isdigit((guchar)*str2)) str2++;
isnum = 1;
} else {
while (*str1 && isalpha((guchar)*str1)) str1++;
while (*str2 && isalpha((guchar)*str2)) str2++;
isnum = 0;
}
/* save character at the end of the alpha or numeric segment */
/* so that they can be restored after the comparison */
oldch1 = *str1;
*str1 = '\0';
oldch2 = *str2;
*str2 = '\0';
/* take care of the case where the two version segments are */
/* different types: one numeric and one alpha */
if (one == str1) return -1; /* arbitrary */
if (two == str2) return -1;
if (isnum) {
/* this used to be done by converting the digit segments */
/* to ints using atoi() - it's changed because long */
/* digit segments can overflow an int - this should fix that. */
/* throw away any leading zeros - it's a number, right? */
while (*one == '0') one++;
while (*two == '0') two++;
/* whichever number has more digits wins */
if (strlen(one) > strlen(two)) return 1;
if (strlen(two) > strlen(one)) return -1;
}
/* strcmp will return which one is greater - even if the two */
/* segments are alpha or if they are numeric. don't return */
/* if they are equal because there might be more segments to */
/* compare */
rc = strcmp(one, two);
if (rc) return rc;
/* restore character that was replaced by null above */
*str1 = oldch1;
one = str1;
*str2 = oldch2;
two = str2;
}
/* this catches the case where all numeric and alpha segments have */
/* compared identically but the segment sepparating characters were */
/* different */
if ((!*one) && (!*two)) return 0;
/* whichever version still has characters left over wins */
if (!*one) return -1; else return 1;
}
int
compare_versions (const char * a, const char *b)
{
return rpmvercmp (a, b);
}
gboolean
version_test (ComparisonType comparison,
const char *a,
const char *b)
{
switch (comparison)
{
case LESS_THAN:
return compare_versions (a, b) < 0;
break;
case GREATER_THAN:
return compare_versions (a, b) > 0;
break;
case LESS_THAN_EQUAL:
return compare_versions (a, b) <= 0;
break;
case GREATER_THAN_EQUAL:
return compare_versions (a, b) >= 0;
break;
case EQUAL:
return compare_versions (a, b) == 0;
break;
case NOT_EQUAL:
return compare_versions (a, b) != 0;
break;
case ALWAYS_MATCH:
return TRUE;
break;
default:
g_assert_not_reached ();
break;
}
return FALSE;
}
const char *
comparison_to_str (ComparisonType comparison)
{
switch (comparison)
{
case LESS_THAN:
return "<";
break;
case GREATER_THAN:
return ">";
break;
case LESS_THAN_EQUAL:
return "<=";
break;
case GREATER_THAN_EQUAL:
return ">=";
break;
case EQUAL:
return "=";
break;
case NOT_EQUAL:
return "!=";
break;
case ALWAYS_MATCH:
return "(any)";
break;
default:
g_assert_not_reached ();
break;
}
return "???";
}
static void
max_len_foreach (gpointer key, gpointer value, gpointer data)
{
int *mlen = data;
*mlen = MAX (*mlen, strlen (key));
}
static void
packages_foreach (gpointer key, gpointer value, gpointer data)
{
Package *pkg = get_package (key);
if (pkg != NULL)
{
char *pad;
pad = g_strnfill (GPOINTER_TO_INT (data) - strlen (pkg->key), ' ');
printf ("%s%s%s - %s\n",
pkg->key, pad, pkg->name, pkg->description);
g_free (pad);
}
}
void
print_package_list (void)
{
int mlen = 0;
ignore_requires = TRUE;
g_hash_table_foreach (locations, max_len_foreach, &mlen);
g_hash_table_foreach (locations, packages_foreach, GINT_TO_POINTER (mlen + 1));
}
void
enable_private_libs(void)
{
ignore_private_libs = FALSE;
}
void
disable_private_libs(void)
{
ignore_private_libs = TRUE;
}
|