2012-05-03 20:22:19 +00:00
|
|
|
/*
|
|
|
|
* fragment.c
|
|
|
|
* Management of fragment lists.
|
|
|
|
*
|
2013-12-27 11:41:23 +00:00
|
|
|
* Copyright (c) 2012, 2013, 2014 pkgconf authors (see AUTHORS).
|
2012-05-03 20:22:19 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2012-07-20 19:29:58 +00:00
|
|
|
* 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.
|
2012-05-03 20:22:19 +00:00
|
|
|
*/
|
|
|
|
|
2015-09-06 14:35:08 +00:00
|
|
|
#include <libpkgconf/libpkgconf.h>
|
2012-05-03 20:22:19 +00:00
|
|
|
|
2016-05-19 22:05:04 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-08-24 23:39:42 +00:00
|
|
|
static inline bool
|
|
|
|
pkgconf_fragment_should_munge(const char *string, const char *sysroot_dir)
|
|
|
|
{
|
|
|
|
static struct pkgconf_fragment_check check_fragments[] = {
|
|
|
|
{"-isystem", 8},
|
|
|
|
};
|
|
|
|
|
|
|
|
if (*string != '/')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (sysroot_dir != NULL && strncmp(sysroot_dir, string, strlen(sysroot_dir)))
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-05-19 22:05:04 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-08-24 23:51:02 +00:00
|
|
|
static inline void
|
|
|
|
pkgconf_fragment_munge(char *buf, size_t buflen, const char *source, const char *sysroot_dir)
|
|
|
|
{
|
|
|
|
*buf = '\0';
|
|
|
|
|
|
|
|
if (sysroot_dir == NULL)
|
|
|
|
sysroot_dir = pkgconf_tuple_find_global("pc_sysrootdir");
|
|
|
|
|
|
|
|
if (pkgconf_fragment_should_munge(source, sysroot_dir))
|
|
|
|
strlcat(buf, sysroot_dir, buflen);
|
|
|
|
|
|
|
|
strlcat(buf, source, buflen);
|
|
|
|
}
|
|
|
|
|
2014-07-09 06:23:51 +00:00
|
|
|
static inline char *
|
2015-09-06 15:44:10 +00:00
|
|
|
pkgconf_fragment_copy_munged(const char *source, unsigned int flags)
|
2014-07-09 06:23:51 +00:00
|
|
|
{
|
2015-09-06 16:29:56 +00:00
|
|
|
char mungebuf[PKGCONF_BUFSIZE];
|
2014-07-09 06:23:51 +00:00
|
|
|
char *sysroot_dir;
|
|
|
|
|
2015-09-06 16:45:00 +00:00
|
|
|
if (!(flags & PKGCONF_PKG_PKGF_MUNGE_SYSROOT_PREFIX))
|
2014-07-09 06:23:51 +00:00
|
|
|
return strdup(source);
|
|
|
|
|
2015-09-06 15:41:40 +00:00
|
|
|
sysroot_dir = pkgconf_tuple_find_global("pc_sysrootdir");
|
2014-07-09 06:23:51 +00:00
|
|
|
|
2016-08-24 23:51:02 +00:00
|
|
|
pkgconf_fragment_munge(mungebuf, sizeof mungebuf, source, sysroot_dir);
|
2014-07-09 06:23:51 +00:00
|
|
|
|
|
|
|
return strdup(mungebuf);
|
|
|
|
}
|
|
|
|
|
2013-03-01 17:10:43 +00:00
|
|
|
void
|
2015-09-06 15:44:10 +00:00
|
|
|
pkgconf_fragment_add(pkgconf_list_t *list, const char *string, unsigned int flags)
|
2012-05-03 20:22:19 +00:00
|
|
|
{
|
2015-09-06 15:44:10 +00:00
|
|
|
pkgconf_fragment_t *frag;
|
2012-05-03 20:22:19 +00:00
|
|
|
|
2016-05-19 22:05:04 +00:00
|
|
|
if (!pkgconf_fragment_is_special(string))
|
2012-05-03 20:22:19 +00:00
|
|
|
{
|
2015-09-06 15:44:10 +00:00
|
|
|
frag = calloc(sizeof(pkgconf_fragment_t), 1);
|
2013-12-27 11:37:24 +00:00
|
|
|
|
2012-05-03 20:22:19 +00:00
|
|
|
frag->type = *(string + 1);
|
2015-09-06 15:44:10 +00:00
|
|
|
frag->data = pkgconf_fragment_copy_munged(string + 2, flags);
|
2012-05-03 20:22:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-08-24 23:51:02 +00:00
|
|
|
char mungebuf[PKGCONF_BUFSIZE];
|
|
|
|
|
2013-12-27 11:37:24 +00:00
|
|
|
if (list->tail != NULL && list->tail->data != NULL)
|
|
|
|
{
|
2015-09-06 15:44:10 +00:00
|
|
|
pkgconf_fragment_t *parent = list->tail->data;
|
2013-12-27 11:37:24 +00:00
|
|
|
|
2016-05-19 22:05:04 +00:00
|
|
|
if (pkgconf_fragment_is_unmergeable(parent->data))
|
2013-12-27 11:37:24 +00:00
|
|
|
{
|
2016-08-24 23:51:02 +00:00
|
|
|
size_t len;
|
2013-12-27 11:37:24 +00:00
|
|
|
char *newdata;
|
|
|
|
|
2016-08-24 23:51:02 +00:00
|
|
|
pkgconf_fragment_munge(mungebuf, sizeof mungebuf, string, NULL);
|
|
|
|
|
|
|
|
len = strlen(parent->data) + strlen(mungebuf) + 2;
|
2013-12-27 11:37:24 +00:00
|
|
|
newdata = malloc(len);
|
2016-08-24 23:51:02 +00:00
|
|
|
|
2013-12-27 11:37:24 +00:00
|
|
|
strlcpy(newdata, parent->data, len);
|
|
|
|
strlcat(newdata, " ", len);
|
2016-08-24 23:51:02 +00:00
|
|
|
strlcat(newdata, mungebuf, len);
|
2013-12-27 11:37:24 +00:00
|
|
|
|
|
|
|
free(parent->data);
|
|
|
|
parent->data = newdata;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-06 15:44:10 +00:00
|
|
|
frag = calloc(sizeof(pkgconf_fragment_t), 1);
|
2013-12-27 11:37:24 +00:00
|
|
|
|
2012-05-03 20:22:19 +00:00
|
|
|
frag->type = 0;
|
|
|
|
frag->data = strdup(string);
|
|
|
|
}
|
|
|
|
|
2015-09-06 15:31:21 +00:00
|
|
|
pkgconf_node_insert_tail(&frag->iter, frag, list);
|
2012-05-03 20:22:19 +00:00
|
|
|
}
|
2012-05-03 20:27:19 +00:00
|
|
|
|
2015-09-06 15:44:10 +00:00
|
|
|
static inline pkgconf_fragment_t *
|
|
|
|
pkgconf_fragment_lookup(pkgconf_list_t *list, pkgconf_fragment_t *base)
|
2012-05-03 20:43:09 +00:00
|
|
|
{
|
2015-09-06 15:31:21 +00:00
|
|
|
pkgconf_node_t *node;
|
2012-05-03 20:43:09 +00:00
|
|
|
|
2015-09-06 15:31:21 +00:00
|
|
|
PKGCONF_FOREACH_LIST_ENTRY_REVERSE(list->tail, node)
|
2012-05-03 20:43:09 +00:00
|
|
|
{
|
2015-09-06 15:44:10 +00:00
|
|
|
pkgconf_fragment_t *frag = node->data;
|
2013-03-01 17:10:43 +00:00
|
|
|
|
|
|
|
if (base->type != frag->type)
|
2012-05-03 20:43:09 +00:00
|
|
|
continue;
|
|
|
|
|
2013-03-01 17:10:43 +00:00
|
|
|
if (!strcmp(base->data, frag->data))
|
|
|
|
return frag;
|
2012-05-03 20:43:09 +00:00
|
|
|
}
|
|
|
|
|
2012-08-09 22:06:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-15 22:29:07 +00:00
|
|
|
static inline bool
|
2015-09-06 15:44:10 +00:00
|
|
|
pkgconf_fragment_can_merge_back(pkgconf_fragment_t *base, unsigned int flags, bool is_private)
|
2013-03-15 22:29:07 +00:00
|
|
|
{
|
2015-05-22 04:06:38 +00:00
|
|
|
(void) flags;
|
|
|
|
|
|
|
|
if (base->type == 'l')
|
|
|
|
{
|
|
|
|
if (is_private)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2013-08-23 03:44:33 +00:00
|
|
|
|
2013-03-15 22:29:07 +00:00
|
|
|
if (base->type == 'F')
|
|
|
|
return false;
|
2015-07-19 20:49:36 +00:00
|
|
|
if (base->type == 'L')
|
|
|
|
return false;
|
2015-07-19 21:00:35 +00:00
|
|
|
if (base->type == 'I')
|
|
|
|
return false;
|
2013-03-15 22:29:07 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
2015-09-06 15:44:10 +00:00
|
|
|
pkgconf_fragment_can_merge(pkgconf_fragment_t *base, unsigned int flags, bool is_private)
|
2013-03-15 22:29:07 +00:00
|
|
|
{
|
2013-08-23 03:44:33 +00:00
|
|
|
(void) flags;
|
|
|
|
|
2015-05-22 04:06:38 +00:00
|
|
|
if (is_private)
|
|
|
|
return false;
|
|
|
|
|
2016-05-19 22:05:04 +00:00
|
|
|
return pkgconf_fragment_is_unmergeable(base->data);
|
2013-03-15 22:29:07 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 15:44:10 +00:00
|
|
|
static inline pkgconf_fragment_t *
|
|
|
|
pkgconf_fragment_exists(pkgconf_list_t *list, pkgconf_fragment_t *base, unsigned int flags, bool is_private)
|
2012-08-09 22:06:09 +00:00
|
|
|
{
|
2015-09-06 15:44:10 +00:00
|
|
|
if (!pkgconf_fragment_can_merge_back(base, flags, is_private))
|
2013-03-15 22:29:07 +00:00
|
|
|
return NULL;
|
|
|
|
|
2015-09-06 15:44:10 +00:00
|
|
|
if (!pkgconf_fragment_can_merge(base, flags, is_private))
|
2013-03-15 22:29:07 +00:00
|
|
|
return NULL;
|
|
|
|
|
2015-09-06 15:44:10 +00:00
|
|
|
return pkgconf_fragment_lookup(list, base);
|
2012-05-03 20:43:09 +00:00
|
|
|
}
|
|
|
|
|
2013-03-01 17:10:43 +00:00
|
|
|
void
|
2015-09-06 15:44:10 +00:00
|
|
|
pkgconf_fragment_copy(pkgconf_list_t *list, pkgconf_fragment_t *base, unsigned int flags, bool is_private)
|
2012-05-03 20:31:33 +00:00
|
|
|
{
|
2015-09-06 15:44:10 +00:00
|
|
|
pkgconf_fragment_t *frag;
|
2012-05-03 20:31:33 +00:00
|
|
|
|
2015-09-06 15:44:10 +00:00
|
|
|
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))
|
2013-03-15 22:29:07 +00:00
|
|
|
return;
|
2012-05-03 20:43:09 +00:00
|
|
|
|
2015-09-06 15:44:10 +00:00
|
|
|
frag = calloc(sizeof(pkgconf_fragment_t), 1);
|
2012-05-03 20:31:33 +00:00
|
|
|
|
|
|
|
frag->type = base->type;
|
|
|
|
frag->data = strdup(base->data);
|
|
|
|
|
2015-09-06 15:31:21 +00:00
|
|
|
pkgconf_node_insert_tail(&frag->iter, frag, list);
|
2012-05-03 20:31:33 +00:00
|
|
|
}
|
|
|
|
|
2012-05-03 20:27:19 +00:00
|
|
|
void
|
2015-09-06 15:44:10 +00:00
|
|
|
pkgconf_fragment_delete(pkgconf_list_t *list, pkgconf_fragment_t *node)
|
2012-05-03 20:27:19 +00:00
|
|
|
{
|
2015-09-06 15:31:21 +00:00
|
|
|
pkgconf_node_delete(&node->iter, list);
|
2012-05-03 20:27:19 +00:00
|
|
|
|
|
|
|
free(node->data);
|
|
|
|
free(node);
|
|
|
|
}
|
2012-05-07 01:26:09 +00:00
|
|
|
|
|
|
|
void
|
2015-09-06 15:44:10 +00:00
|
|
|
pkgconf_fragment_free(pkgconf_list_t *list)
|
2012-05-07 01:26:09 +00:00
|
|
|
{
|
2015-09-06 15:31:21 +00:00
|
|
|
pkgconf_node_t *node, *next;
|
2012-05-07 01:26:09 +00:00
|
|
|
|
2015-09-06 15:31:21 +00:00
|
|
|
PKGCONF_FOREACH_LIST_ENTRY_SAFE(list->head, next, node)
|
2013-03-01 17:10:43 +00:00
|
|
|
{
|
2015-09-06 15:44:10 +00:00
|
|
|
pkgconf_fragment_t *frag = node->data;
|
2013-03-01 17:10:43 +00:00
|
|
|
|
|
|
|
free(frag->data);
|
|
|
|
free(frag);
|
|
|
|
}
|
2012-05-07 01:26:09 +00:00
|
|
|
}
|
2012-05-07 03:29:59 +00:00
|
|
|
|
2013-03-01 17:10:43 +00:00
|
|
|
void
|
2015-09-06 15:44:10 +00:00
|
|
|
pkgconf_fragment_parse(pkgconf_list_t *list, pkgconf_list_t *vars, const char *value, unsigned int flags)
|
2012-05-07 03:29:59 +00:00
|
|
|
{
|
|
|
|
int i, argc;
|
|
|
|
char **argv;
|
2015-09-06 15:41:40 +00:00
|
|
|
char *repstr = pkgconf_tuple_parse(vars, value);
|
2012-05-07 03:29:59 +00:00
|
|
|
|
2015-09-06 15:48:24 +00:00
|
|
|
pkgconf_argv_split(repstr, &argc, &argv);
|
2012-05-07 03:29:59 +00:00
|
|
|
|
|
|
|
for (i = 0; i < argc; i++)
|
2015-09-06 15:44:10 +00:00
|
|
|
pkgconf_fragment_add(list, argv[i], flags);
|
2012-05-07 03:29:59 +00:00
|
|
|
|
2015-09-06 15:48:24 +00:00
|
|
|
pkgconf_argv_free(argv);
|
2012-05-07 03:29:59 +00:00
|
|
|
free(repstr);
|
|
|
|
}
|