1From 267a57022699453e8d8f517519df25ac6bf6ac4e Mon Sep 17 00:00:00 2001
2From: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
3Date: Sun, 16 Dec 2018 11:52:18 +0100
4Subject: [PATCH] Only prefix with the sysroot a subset of variables
5
6The standard logic of pkg-config is to prefix all absolute paths by
7the sysroot defined in PKG_CONFIG_SYSROOT_DIR. However, while some
8paths (like includedir, libdir, and paths used in -L and -I options)
9indeed need to be prefixed by the sysroot, it is not necessarily the
10case for paths that are used on the target. If they get prefixed by
11the sysroot, the runtime path on the target is incorrect.
12
13Unfortunately, pkg-config doesn't have a sense of which path needs to
14be prefixed by the sysroot, and which path should not be prefixed by
15the sysroot.
16
17So, let's simply have a whitelist of paths that should be prefixed:
18g_ir_scanner, g_ir_compiler, g_ir_generate, includedir, libdir, mapdir,
19pkgdatadir and sdkdir. This list of variables was collected over years of
20Buildroot development. All other paths are not prefixed by the sysroot.
21
22Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
23[Updated to include gobject-introspection paths]
24Signed-off-by: Adam Duskett <aduskett@gmail.com>
25---
26 libpkgconf/tuple.c | 60 ++++++++++++++++++++++++++++++++--------------
27 1 file changed, 42 insertions(+), 18 deletions(-)
28
29diff --git a/libpkgconf/tuple.c b/libpkgconf/tuple.c
30index 8523709..7cd2fff 100644
31--- a/libpkgconf/tuple.c
32+++ b/libpkgconf/tuple.c
33@@ -161,6 +161,21 @@ dequote(const char *value)
34 	return buf;
35 }
36
37+static char *
38+pkgconf_tuple_parse_sysroot(const pkgconf_client_t *client, pkgconf_list_t *vars, const char *value, bool add_sysroot);
39+
40+const char *sysrooted_keys[] = {
41+	"g_ir_scanner",
42+	"g_ir_compiler",
43+	"g_ir_generate",
44+	"includedir",
45+	"libdir",
46+	"mapdir",
47+	"pkgdatadir",
48+	"sdkdir",
49+	NULL,
50+};
51+
52 /*
53  * !doc
54  *
55@@ -181,6 +193,8 @@ pkgconf_tuple_add(const pkgconf_client_t *client, pkgconf_list_t *list, const ch
56 {
57 	char *dequote_value;
58 	pkgconf_tuple_t *tuple = calloc(sizeof(pkgconf_tuple_t), 1);
59+	bool add_sysroot = false;
60+	int i;
61
62 	pkgconf_tuple_find_delete(list, key);
63
64@@ -188,9 +202,13 @@ pkgconf_tuple_add(const pkgconf_client_t *client, pkgconf_list_t *list, const ch
65
66 	PKGCONF_TRACE(client, "adding tuple to @%p: %s => %s (parsed? %d)", list, key, dequote_value, parse);
67
68+	for (i = 0; sysrooted_keys[i] != NULL; i++)
69+		if (!strcmp(key, sysrooted_keys[i]))
70+			add_sysroot = true;
71+
72 	tuple->key = strdup(key);
73 	if (parse)
74-		tuple->value = pkgconf_tuple_parse(client, list, dequote_value);
75+		tuple->value = pkgconf_tuple_parse_sysroot(client, list, dequote_value, add_sysroot);
76 	else
77 		tuple->value = strdup(dequote_value);
78
79@@ -234,27 +252,14 @@ pkgconf_tuple_find(const pkgconf_client_t *client, pkgconf_list_t *list, const c
80 	return NULL;
81 }
82
83-/*
84- * !doc
85- *
86- * .. c:function:: char *pkgconf_tuple_parse(const pkgconf_client_t *client, pkgconf_list_t *vars, const char *value)
87- *
88- *    Parse an expression for variable substitution.
89- *
90- *    :param pkgconf_client_t* client: The pkgconf client object to access.
91- *    :param pkgconf_list_t* list: The variable list to search for variables (along side the global variable list).
92- *    :param char* value: The ``key=value`` string to parse.
93- *    :return: the variable data with any variables substituted
94- *    :rtype: char *
95- */
96-char *
97-pkgconf_tuple_parse(const pkgconf_client_t *client, pkgconf_list_t *vars, const char *value)
98+static char *
99+pkgconf_tuple_parse_sysroot(const pkgconf_client_t *client, pkgconf_list_t *vars, const char *value, bool add_sysroot)
100 {
101 	char buf[PKGCONF_BUFSIZE];
102 	const char *ptr;
103 	char *bptr = buf;
104
105-	if (*value == '/' && client->sysroot_dir != NULL && strncmp(value, client->sysroot_dir, strlen(client->sysroot_dir)))
106+	if (add_sysroot && *value == '/' && client->sysroot_dir != NULL && strncmp(value, client->sysroot_dir, strlen(client->sysroot_dir)))
107 		bptr += pkgconf_strlcpy(buf, client->sysroot_dir, sizeof buf);
108
109 	for (ptr = value; *ptr != '\0' && bptr - buf < PKGCONF_BUFSIZE; ptr++)
110@@ -294,7 +299,7 @@ pkgconf_tuple_parse(const pkgconf_client_t *client, pkgconf_list_t *vars, const
111
112 				if (kv != NULL)
113 				{
114-					parsekv = pkgconf_tuple_parse(client, vars, kv);
115+					parsekv = pkgconf_tuple_parse_sysroot(client, vars, kv, add_sysroot);
116
117 					strncpy(bptr, parsekv, PKGCONF_BUFSIZE - (bptr - buf));
118 					bptr += strlen(parsekv);
119@@ -339,6 +344,25 @@ pkgconf_tuple_parse(const pkgconf_client_t *client, pkgconf_list_t *vars, const
120 	return strdup(buf);
121 }
122
123+/*
124+ * !doc
125+ *
126+ * .. c:function:: char *pkgconf_tuple_parse(const pkgconf_client_t *client, pkgconf_list_t *vars, const char *value)
127+ *
128+ *    Parse an expression for variable substitution.
129+ *
130+ *    :param pkgconf_client_t* client: The pkgconf client object to access.
131+ *    :param pkgconf_list_t* list: The variable list to search for variables (along side the global variable list).
132+ *    :param char* value: The ``key=value`` string to parse.
133+ *    :return: the variable data with any variables substituted
134+ *    :rtype: char *
135+ */
136+char *
137+pkgconf_tuple_parse(const pkgconf_client_t *client, pkgconf_list_t *vars, const char *value)
138+{
139+	return pkgconf_tuple_parse_sysroot(client, vars, value, true);
140+}
141+
142 /*
143  * !doc
144  *
145--
1462.19.2
147
148