Tag
Hash :
f72e1c6d
Author :
Date :
2016-05-19T17:05:04
fragment: genericize the fragment merge determination and add support for -isystem (closes #87)
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
/*
* fragment.c
* Management of fragment lists.
*
* Copyright (c) 2012, 2013, 2014 pkgconf authors (see AUTHORS).
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* This software is provided 'as is' and without any warranty, express or
* implied. In no event shall the authors be liable for any damages arising
* from the use of this software.
*/
#include <libpkgconf/libpkgconf.h>
struct pkgconf_fragment_check {
char *token;
size_t len;
};
static inline bool
pkgconf_fragment_is_unmergeable(const char *string)
{
static struct pkgconf_fragment_check check_fragments[] = {
{"-framework", 10},
{"-isystem", 8},
};
if (*string != '-')
return true;
for (size_t i = 0; i < PKGCONF_ARRAY_SIZE(check_fragments); i++)
if (!strncmp(string, check_fragments[i].token, check_fragments[i].len))
return true;
return false;
}
static inline bool
pkgconf_fragment_is_special(const char *string)
{
if (*string != '-')
return true;
if (!strncmp(string, "-lib:", 5))
return true;
return pkgconf_fragment_is_unmergeable(string);
}
static inline char *
pkgconf_fragment_copy_munged(const char *source, unsigned int flags)
{
char mungebuf[PKGCONF_BUFSIZE];
char *sysroot_dir;
if (!(flags & PKGCONF_PKG_PKGF_MUNGE_SYSROOT_PREFIX))
return strdup(source);
sysroot_dir = pkgconf_tuple_find_global("pc_sysrootdir");
if (*source != '/' ||
(sysroot_dir != NULL && !strncmp(sysroot_dir, source, strlen(sysroot_dir))))
return strdup(source);
strlcpy(mungebuf, sysroot_dir, sizeof mungebuf);
strlcat(mungebuf, source, sizeof mungebuf);
return strdup(mungebuf);
}
void
pkgconf_fragment_add(pkgconf_list_t *list, const char *string, unsigned int flags)
{
pkgconf_fragment_t *frag;
if (!pkgconf_fragment_is_special(string))
{
frag = calloc(sizeof(pkgconf_fragment_t), 1);
frag->type = *(string + 1);
frag->data = pkgconf_fragment_copy_munged(string + 2, flags);
}
else
{
if (list->tail != NULL && list->tail->data != NULL)
{
pkgconf_fragment_t *parent = list->tail->data;
if (pkgconf_fragment_is_unmergeable(parent->data))
{
size_t len = strlen(parent->data) + strlen(string) + 2;
char *newdata;
newdata = malloc(len);
strlcpy(newdata, parent->data, len);
strlcat(newdata, " ", len);
strlcat(newdata, string, len);
free(parent->data);
parent->data = newdata;
return;
}
}
frag = calloc(sizeof(pkgconf_fragment_t), 1);
frag->type = 0;
frag->data = strdup(string);
}
pkgconf_node_insert_tail(&frag->iter, frag, list);
}
static inline pkgconf_fragment_t *
pkgconf_fragment_lookup(pkgconf_list_t *list, pkgconf_fragment_t *base)
{
pkgconf_node_t *node;
PKGCONF_FOREACH_LIST_ENTRY_REVERSE(list->tail, node)
{
pkgconf_fragment_t *frag = node->data;
if (base->type != frag->type)
continue;
if (!strcmp(base->data, frag->data))
return frag;
}
return NULL;
}
static inline bool
pkgconf_fragment_can_merge_back(pkgconf_fragment_t *base, unsigned int flags, bool is_private)
{
(void) flags;
if (base->type == 'l')
{
if (is_private)
return false;
return true;
}
if (base->type == 'F')
return false;
if (base->type == 'L')
return false;
if (base->type == 'I')
return false;
return true;
}
static inline bool
pkgconf_fragment_can_merge(pkgconf_fragment_t *base, unsigned int flags, bool is_private)
{
(void) flags;
if (is_private)
return false;
return pkgconf_fragment_is_unmergeable(base->data);
}
static inline pkgconf_fragment_t *
pkgconf_fragment_exists(pkgconf_list_t *list, pkgconf_fragment_t *base, unsigned int flags, bool is_private)
{
if (!pkgconf_fragment_can_merge_back(base, flags, is_private))
return NULL;
if (!pkgconf_fragment_can_merge(base, flags, is_private))
return NULL;
return pkgconf_fragment_lookup(list, base);
}
void
pkgconf_fragment_copy(pkgconf_list_t *list, pkgconf_fragment_t *base, unsigned int flags, bool is_private)
{
pkgconf_fragment_t *frag;
if ((frag = pkgconf_fragment_exists(list, base, flags, is_private)) != NULL)
pkgconf_fragment_delete(list, frag);
else if (!is_private && !pkgconf_fragment_can_merge_back(base, flags, is_private) && (pkgconf_fragment_lookup(list, base) != NULL))
return;
frag = calloc(sizeof(pkgconf_fragment_t), 1);
frag->type = base->type;
frag->data = strdup(base->data);
pkgconf_node_insert_tail(&frag->iter, frag, list);
}
void
pkgconf_fragment_delete(pkgconf_list_t *list, pkgconf_fragment_t *node)
{
pkgconf_node_delete(&node->iter, list);
free(node->data);
free(node);
}
void
pkgconf_fragment_free(pkgconf_list_t *list)
{
pkgconf_node_t *node, *next;
PKGCONF_FOREACH_LIST_ENTRY_SAFE(list->head, next, node)
{
pkgconf_fragment_t *frag = node->data;
free(frag->data);
free(frag);
}
}
void
pkgconf_fragment_parse(pkgconf_list_t *list, pkgconf_list_t *vars, const char *value, unsigned int flags)
{
int i, argc;
char **argv;
char *repstr = pkgconf_tuple_parse(vars, value);
pkgconf_argv_split(repstr, &argc, &argv);
for (i = 0; i < argc; i++)
pkgconf_fragment_add(list, argv[i], flags);
pkgconf_argv_free(argv);
free(repstr);
}