2018-05-09 21:54:21 +00:00
|
|
|
/*
|
|
|
|
* personality.c
|
|
|
|
* libpkgconf cross-compile personality database
|
|
|
|
*
|
|
|
|
* Copyright (c) 2018 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.
|
|
|
|
*/
|
|
|
|
|
2019-06-07 16:19:28 +00:00
|
|
|
#include <libpkgconf/config.h>
|
2018-05-09 21:54:21 +00:00
|
|
|
#include <libpkgconf/stdinc.h>
|
|
|
|
#include <libpkgconf/libpkgconf.h>
|
|
|
|
|
2023-01-22 10:49:54 +00:00
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
|
|
|
* libpkgconf `personality` module
|
|
|
|
* =========================
|
|
|
|
*/
|
|
|
|
|
2018-09-17 13:19:18 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
# define strcasecmp _stricmp
|
|
|
|
#endif
|
|
|
|
|
2022-02-11 03:46:19 +00:00
|
|
|
/*
|
|
|
|
* Increment each time the default personality is inited, decrement each time
|
|
|
|
* it's deinited. Whenever it is 0, then the deinit frees the personality. In
|
|
|
|
* that case an additional call to init will create it anew.
|
|
|
|
*/
|
|
|
|
static unsigned default_personality_init = 0;
|
|
|
|
|
2018-05-09 21:54:21 +00:00
|
|
|
static pkgconf_cross_personality_t default_personality = {
|
|
|
|
.name = "default",
|
2021-03-18 12:03:32 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
.want_default_static = true,
|
|
|
|
.want_default_pure = true,
|
|
|
|
#endif
|
2018-05-09 21:54:21 +00:00
|
|
|
};
|
|
|
|
|
2018-05-09 22:07:26 +00:00
|
|
|
static inline void
|
|
|
|
build_default_search_path(pkgconf_list_t* dirlist)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
char namebuf[MAX_PATH];
|
|
|
|
char outbuf[MAX_PATH];
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
int sizepath = GetModuleFileName(NULL, namebuf, sizeof namebuf);
|
|
|
|
char * winslash;
|
|
|
|
namebuf[sizepath] = '\0';
|
|
|
|
while ((winslash = strchr (namebuf, '\\')) != NULL)
|
|
|
|
{
|
|
|
|
*winslash = '/';
|
|
|
|
}
|
|
|
|
p = strrchr(namebuf, '/');
|
|
|
|
if (p == NULL)
|
|
|
|
pkgconf_path_split(PKG_DEFAULT_PATH, dirlist, true);
|
|
|
|
|
|
|
|
*p = '\0';
|
|
|
|
pkgconf_strlcpy(outbuf, namebuf, sizeof outbuf);
|
|
|
|
pkgconf_strlcat(outbuf, "/", sizeof outbuf);
|
|
|
|
pkgconf_strlcat(outbuf, "../lib/pkgconfig", sizeof outbuf);
|
|
|
|
pkgconf_path_add(outbuf, dirlist, true);
|
|
|
|
pkgconf_strlcpy(outbuf, namebuf, sizeof outbuf);
|
|
|
|
pkgconf_strlcat(outbuf, "/", sizeof outbuf);
|
|
|
|
pkgconf_strlcat(outbuf, "../share/pkgconfig", sizeof outbuf);
|
|
|
|
pkgconf_path_add(outbuf, dirlist, true);
|
|
|
|
#elif __HAIKU__
|
|
|
|
char **paths;
|
|
|
|
size_t count;
|
|
|
|
if (find_paths(B_FIND_PATH_DEVELOP_LIB_DIRECTORY, "pkgconfig", &paths, &count) == B_OK) {
|
|
|
|
for (size_t i = 0; i < count; i++)
|
|
|
|
pkgconf_path_add(paths[i], dirlist, true);
|
|
|
|
free(paths);
|
|
|
|
paths = NULL;
|
|
|
|
}
|
|
|
|
if (find_paths(B_FIND_PATH_DATA_DIRECTORY, "pkgconfig", &paths, &count) == B_OK) {
|
|
|
|
for (size_t i = 0; i < count; i++)
|
|
|
|
pkgconf_path_add(paths[i], dirlist, true);
|
|
|
|
free(paths);
|
|
|
|
paths = NULL;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
pkgconf_path_split(PKG_DEFAULT_PATH, dirlist, true);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-05-09 21:54:21 +00:00
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
|
|
|
* .. c:function:: const pkgconf_cross_personality_t *pkgconf_cross_personality_default(void)
|
|
|
|
*
|
|
|
|
* Returns the default cross-compile personality.
|
|
|
|
*
|
2022-02-11 03:46:19 +00:00
|
|
|
* Not thread safe.
|
|
|
|
*
|
2018-05-09 21:54:21 +00:00
|
|
|
* :rtype: pkgconf_cross_personality_t*
|
|
|
|
* :return: the default cross-compile personality
|
|
|
|
*/
|
2018-05-10 03:52:27 +00:00
|
|
|
pkgconf_cross_personality_t *
|
2018-05-09 21:54:21 +00:00
|
|
|
pkgconf_cross_personality_default(void)
|
|
|
|
{
|
2022-02-11 03:46:19 +00:00
|
|
|
if (default_personality_init) {
|
|
|
|
++default_personality_init;
|
2018-05-09 21:54:21 +00:00
|
|
|
return &default_personality;
|
2022-02-11 03:46:19 +00:00
|
|
|
}
|
2018-05-09 21:54:21 +00:00
|
|
|
|
2018-05-09 22:07:26 +00:00
|
|
|
build_default_search_path(&default_personality.dir_list);
|
|
|
|
|
2021-07-25 02:06:55 +00:00
|
|
|
pkgconf_path_split(SYSTEM_LIBDIR, &default_personality.filter_libdirs, false);
|
|
|
|
pkgconf_path_split(SYSTEM_INCLUDEDIR, &default_personality.filter_includedirs, false);
|
2018-05-09 21:54:21 +00:00
|
|
|
|
2022-02-11 03:46:19 +00:00
|
|
|
++default_personality_init;
|
2018-05-09 21:54:21 +00:00
|
|
|
return &default_personality;
|
|
|
|
}
|
2018-05-10 00:27:53 +00:00
|
|
|
|
2022-02-11 03:46:19 +00:00
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
|
|
|
* .. c:function:: void pkgconf_cross_personality_deinit(pkgconf_cross_personality_t *)
|
|
|
|
*
|
|
|
|
* Decrements the count of default cross personality instances.
|
|
|
|
*
|
|
|
|
* Not thread safe.
|
|
|
|
*
|
|
|
|
* :rtype: void
|
|
|
|
*/
|
2021-06-11 06:00:47 +00:00
|
|
|
void
|
|
|
|
pkgconf_cross_personality_deinit(pkgconf_cross_personality_t *personality)
|
|
|
|
{
|
2022-02-11 03:46:19 +00:00
|
|
|
if (--default_personality_init == 0) {
|
|
|
|
pkgconf_path_free(&personality->dir_list);
|
|
|
|
pkgconf_path_free(&personality->filter_libdirs);
|
|
|
|
pkgconf_path_free(&personality->filter_includedirs);
|
|
|
|
}
|
2021-06-11 06:00:47 +00:00
|
|
|
}
|
|
|
|
|
2019-05-06 20:13:17 +00:00
|
|
|
#ifndef PKGCONF_LITE
|
2018-05-10 00:27:53 +00:00
|
|
|
static bool
|
|
|
|
valid_triplet(const char *triplet)
|
|
|
|
{
|
|
|
|
const char *c = triplet;
|
|
|
|
|
2018-05-10 18:36:22 +00:00
|
|
|
for (; *c; c++)
|
Avoid undefined behaviour with the ctype(3) functions.
fix https://github.com/pkgconf/pkgconf/issues/291
As defined in the C standard:
In all cases the argument is an int, the value of which shall
be representable as an unsigned char or shall equal the value
of the macro EOF. If the argument has any other value, the
behavior is undefined.
This is because they're designed to work with the int values returned
by getc or fgetc; they need extra work to handle a char value.
If EOF is -1 (as it almost always is), with 8-bit bytes, the allowed
inputs to the ctype(3) functions are:
{-1, 0, 1, 2, 3, ..., 255}.
However, on platforms where char is signed, such as x86 with the
usual ABI, code like
char *ptr = ...;
... isspace(*ptr) ...
may pass in values in the range:
{-128, -127, -126, ..., -2, -1, 0, 1, ..., 127}.
This has two problems:
1. Inputs in the set {-128, -127, -126, ..., -2} are forbidden.
2. The non-EOF byte 0xff is conflated with the value EOF = -1, so
even though the input is not forbidden, it may give the wrong
answer.
Casting char to unsigned int first before passing the result to
ctype(3) doesn't help: inputs like -128 are unchanged by this cast,
because (on a two's-complement machine with 32-bit int and unsigned
int), converting the signed char with integer value -128 to unsigned
int gives integer value 2^32 - 128 = 0xffffff80, which is out of
range, and which is converted in int back to -128, which is also out
of range.
It is necessary to cast char inputs to unsigned char first; you can
then cast to unsigned int if you like but there's no need because the
functions will always convert the argument to int by definition. So
the above fragment needs to be:
char *ptr = ...;
... isspace((unsigned char)*ptr) ...
This patch changes unsigned int casts to unsigned char casts, and
adds unsigned char casts where they are missing.
2023-03-17 19:32:58 +00:00
|
|
|
if (!isalnum((unsigned char)*c) && *c != '-' && *c != '_')
|
2018-05-10 00:27:53 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-10 03:08:28 +00:00
|
|
|
typedef void (*personality_keyword_func_t)(pkgconf_cross_personality_t *p, const char *keyword, const size_t lineno, const ptrdiff_t offset, char *value);
|
|
|
|
typedef struct {
|
|
|
|
const char *keyword;
|
|
|
|
const personality_keyword_func_t func;
|
|
|
|
const ptrdiff_t offset;
|
|
|
|
} personality_keyword_pair_t;
|
|
|
|
|
2019-10-19 05:56:17 +00:00
|
|
|
static void
|
|
|
|
personality_bool_func(pkgconf_cross_personality_t *p, const char *keyword, const size_t lineno, const ptrdiff_t offset, char *value)
|
|
|
|
{
|
|
|
|
(void) keyword;
|
|
|
|
(void) lineno;
|
|
|
|
|
|
|
|
bool *dest = (bool *)((char *) p + offset);
|
|
|
|
*dest = strcasecmp(value, "true") || strcasecmp(value, "yes") || *value == '1';
|
|
|
|
}
|
|
|
|
|
2018-05-10 03:08:28 +00:00
|
|
|
static void
|
|
|
|
personality_copy_func(pkgconf_cross_personality_t *p, const char *keyword, const size_t lineno, const ptrdiff_t offset, char *value)
|
|
|
|
{
|
|
|
|
(void) keyword;
|
|
|
|
(void) lineno;
|
|
|
|
|
|
|
|
char **dest = (char **)((char *) p + offset);
|
|
|
|
*dest = strdup(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
personality_fragment_func(pkgconf_cross_personality_t *p, const char *keyword, const size_t lineno, const ptrdiff_t offset, char *value)
|
|
|
|
{
|
|
|
|
(void) keyword;
|
|
|
|
(void) lineno;
|
|
|
|
|
|
|
|
pkgconf_list_t *dest = (pkgconf_list_t *)((char *) p + offset);
|
2018-05-10 03:39:39 +00:00
|
|
|
pkgconf_path_split(value, dest, false);
|
2018-05-10 03:08:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* keep in alphabetical order! */
|
|
|
|
static const personality_keyword_pair_t personality_keyword_pairs[] = {
|
|
|
|
{"DefaultSearchPaths", personality_fragment_func, offsetof(pkgconf_cross_personality_t, dir_list)},
|
|
|
|
{"SysrootDir", personality_copy_func, offsetof(pkgconf_cross_personality_t, sysroot_dir)},
|
|
|
|
{"SystemIncludePaths", personality_fragment_func, offsetof(pkgconf_cross_personality_t, filter_includedirs)},
|
|
|
|
{"SystemLibraryPaths", personality_fragment_func, offsetof(pkgconf_cross_personality_t, filter_libdirs)},
|
|
|
|
{"Triplet", personality_copy_func, offsetof(pkgconf_cross_personality_t, name)},
|
2021-03-18 11:59:54 +00:00
|
|
|
{"WantDefaultPure", personality_bool_func, offsetof(pkgconf_cross_personality_t, want_default_pure)},
|
2019-10-19 05:56:17 +00:00
|
|
|
{"WantDefaultStatic", personality_bool_func, offsetof(pkgconf_cross_personality_t, want_default_static)},
|
2018-05-10 03:08:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
personality_keyword_pair_cmp(const void *key, const void *ptr)
|
|
|
|
{
|
|
|
|
const personality_keyword_pair_t *pair = ptr;
|
|
|
|
return strcasecmp(key, pair->keyword);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
personality_keyword_set(pkgconf_cross_personality_t *p, const size_t lineno, const char *keyword, char *value)
|
|
|
|
{
|
|
|
|
const personality_keyword_pair_t *pair = bsearch(keyword,
|
|
|
|
personality_keyword_pairs, PKGCONF_ARRAY_SIZE(personality_keyword_pairs),
|
|
|
|
sizeof(personality_keyword_pair_t), personality_keyword_pair_cmp);
|
|
|
|
|
|
|
|
if (pair == NULL || pair->func == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pair->func(p, keyword, lineno, pair->offset, value);
|
|
|
|
}
|
|
|
|
|
2020-05-24 21:13:19 +00:00
|
|
|
static const pkgconf_parser_operand_func_t personality_parser_ops[256] = {
|
2018-05-10 03:08:28 +00:00
|
|
|
[':'] = (pkgconf_parser_operand_func_t) personality_keyword_set
|
|
|
|
};
|
|
|
|
|
|
|
|
static void personality_warn_func(void *p, const char *fmt, ...) PRINTFLIKE(2, 3);
|
|
|
|
|
|
|
|
static void
|
|
|
|
personality_warn_func(void *p, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
(void) p;
|
|
|
|
|
|
|
|
va_start(va, fmt);
|
|
|
|
vfprintf(stderr, fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
2018-05-10 00:27:53 +00:00
|
|
|
static pkgconf_cross_personality_t *
|
2023-10-31 13:09:55 +00:00
|
|
|
load_personality_with_path(const char *path, const char *triplet, bool datadir)
|
2018-05-10 00:27:53 +00:00
|
|
|
{
|
2018-05-10 03:08:28 +00:00
|
|
|
char pathbuf[PKGCONF_ITEM_SIZE];
|
|
|
|
FILE *f;
|
|
|
|
pkgconf_cross_personality_t *p;
|
|
|
|
|
|
|
|
/* if triplet is null, assume that path is a direct path to the personality file */
|
2023-10-31 13:09:55 +00:00
|
|
|
if (triplet == NULL)
|
2018-05-29 21:36:04 +00:00
|
|
|
pkgconf_strlcpy(pathbuf, path, sizeof pathbuf);
|
2023-10-31 13:09:55 +00:00
|
|
|
else if (datadir)
|
|
|
|
snprintf(pathbuf, sizeof pathbuf, "%s/pkgconfig/personality.d/%s.personality", path, triplet);
|
|
|
|
else
|
|
|
|
snprintf(pathbuf, sizeof pathbuf, "%s/%s.personality", path, triplet);
|
2018-05-10 03:08:28 +00:00
|
|
|
|
|
|
|
f = fopen(pathbuf, "r");
|
|
|
|
if (f == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2023-11-05 22:17:02 +00:00
|
|
|
p = calloc(1, sizeof(pkgconf_cross_personality_t));
|
2018-05-10 03:39:39 +00:00
|
|
|
if (triplet != NULL)
|
|
|
|
p->name = strdup(triplet);
|
2018-05-10 03:08:28 +00:00
|
|
|
pkgconf_parser_parse(f, p, personality_parser_ops, personality_warn_func, pathbuf);
|
|
|
|
|
|
|
|
return p;
|
2018-05-10 00:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
|
|
|
* .. c:function:: pkgconf_cross_personality_t *pkgconf_cross_personality_find(const char *triplet)
|
|
|
|
*
|
|
|
|
* Attempts to find a cross-compile personality given a triplet.
|
|
|
|
*
|
|
|
|
* :rtype: pkgconf_cross_personality_t*
|
|
|
|
* :return: the default cross-compile personality
|
|
|
|
*/
|
|
|
|
pkgconf_cross_personality_t *
|
|
|
|
pkgconf_cross_personality_find(const char *triplet)
|
|
|
|
{
|
2018-05-10 18:36:22 +00:00
|
|
|
pkgconf_list_t plist = PKGCONF_LIST_INITIALIZER;
|
2018-05-10 00:27:53 +00:00
|
|
|
pkgconf_node_t *n;
|
|
|
|
pkgconf_cross_personality_t *out = NULL;
|
2023-10-31 13:09:55 +00:00
|
|
|
#if ! defined(_WIN32) && ! defined(__HAIKU__)
|
|
|
|
char pathbuf[PKGCONF_ITEM_SIZE];
|
|
|
|
const char *envvar;
|
|
|
|
#endif
|
2018-05-10 00:27:53 +00:00
|
|
|
|
2023-10-31 13:09:55 +00:00
|
|
|
out = load_personality_with_path(triplet, NULL, false);
|
2018-05-10 03:08:28 +00:00
|
|
|
if (out != NULL)
|
|
|
|
return out;
|
|
|
|
|
2018-05-10 03:39:39 +00:00
|
|
|
if (!valid_triplet(triplet))
|
|
|
|
return NULL;
|
|
|
|
|
2023-10-31 13:09:55 +00:00
|
|
|
#if ! defined(_WIN32) && ! defined(__HAIKU__)
|
|
|
|
envvar = getenv("XDG_DATA_HOME");
|
|
|
|
if (envvar != NULL)
|
|
|
|
pkgconf_path_add(envvar, &plist, true);
|
|
|
|
else {
|
|
|
|
envvar = getenv("HOME");
|
|
|
|
if (envvar != NULL) {
|
|
|
|
pkgconf_strlcpy(pathbuf, envvar, sizeof pathbuf);
|
|
|
|
pkgconf_strlcat(pathbuf, "/.local/share", sizeof pathbuf);
|
|
|
|
pkgconf_path_add(pathbuf, &plist, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pkgconf_path_build_from_environ("XDG_DATA_DIRS", "/usr/local/share" PKG_CONFIG_PATH_SEP_S "/usr/share", &plist, true);
|
|
|
|
|
|
|
|
PKGCONF_FOREACH_LIST_ENTRY(plist.head, n)
|
|
|
|
{
|
|
|
|
pkgconf_path_t *pn = n->data;
|
|
|
|
|
|
|
|
out = load_personality_with_path(pn->path, triplet, true);
|
|
|
|
if (out != NULL)
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
pkgconf_path_free(&plist);
|
|
|
|
#endif
|
|
|
|
|
2018-05-10 00:27:53 +00:00
|
|
|
pkgconf_path_split(PERSONALITY_PATH, &plist, true);
|
|
|
|
|
|
|
|
PKGCONF_FOREACH_LIST_ENTRY(plist.head, n)
|
|
|
|
{
|
|
|
|
pkgconf_path_t *pn = n->data;
|
|
|
|
|
2023-10-31 13:09:55 +00:00
|
|
|
out = load_personality_with_path(pn->path, triplet, false);
|
2018-05-10 00:27:53 +00:00
|
|
|
if (out != NULL)
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
finish:
|
|
|
|
pkgconf_path_free(&plist);
|
2019-08-23 17:47:22 +00:00
|
|
|
return out != NULL ? out : pkgconf_cross_personality_default();
|
2018-05-10 00:27:53 +00:00
|
|
|
}
|
2019-05-06 20:13:17 +00:00
|
|
|
#endif
|