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
|
/* file : tests/api/driver.c
* copyright : Copyright (c) 2016-2017 Code Synthesis Ltd
* license : ISC; see accompanying COPYING file
*/
/*
* Enable assertions.
*/
#ifdef NDEBUG
# undef NDEBUG
#endif
#include <libpkgconf/libpkgconf.h>
#include <stdio.h> /* printf(), fprintf(), stderr */
#include <stddef.h> /* size_t, NULL */
#include <assert.h>
#include <string.h> /* strcmp(), strlen() */
#include <stdbool.h> /* bool, true, false */
static bool
error_handler (const char* msg, const pkgconf_client_t* c, const void* d)
{
(void) c; /* Unused. */
(void) d; /* Unused. */
/*
* Seems it always have a trailing newline char. Probably it still a good
* idea to check if it is. Let's see if it ever be missed.
*
*/
fprintf (stderr, "%s", msg);
return true;
}
static void
frags_print_and_free (pkgconf_list_t* list)
{
pkgconf_node_t *node;
PKGCONF_FOREACH_LIST_ENTRY(list->head, node)
{
pkgconf_fragment_t* frag = node->data;
printf("%c %s\n", frag->type, frag->data);
}
pkgconf_fragment_free (list);
}
static void
tuples_print (pkgconf_list_t *list)
{
pkgconf_node_t *node;
PKGCONF_FOREACH_LIST_ENTRY(list->head, node)
{
pkgconf_tuple_t *tuple = node->data;
// Skip the automatically added variable.
//
if (strcmp (tuple->key, "pcfiledir") != 0)
printf("%s %s\n", tuple->key, tuple->value);
}
}
/*
* Usage: argv[0] (--cflags|--libs|--vars) <path>
*
* Print package compiler flags, linker flags or variable name/values one per
* line. The specified package file must have .pc extension.
*
* --cflags
* Print compiler flags in the '<name> <value>' format.
*
* --libs
* Print linker flags in the '<name> <value>' format.
*
* --vars
* Print variables in the '<name> <value>' format.
*/
int
main (int argc, const char* argv[])
{
enum
{
dump_none,
dump_cflags,
dump_libs,
dump_vars
} mode = dump_none;
int i = 1;
for (; i < argc; ++i)
{
const char* o = argv[i];
if (strcmp (o, "--cflags") == 0)
{
assert (mode == dump_none);
mode = dump_cflags;
}
else if (strcmp (o, "--libs") == 0)
{
assert (mode == dump_none);
mode = dump_libs;
}
else if (strcmp (o, "--vars") == 0)
{
assert (mode == dump_none);
mode = dump_vars;
}
else
break;
}
assert (mode != dump_none);
assert (i + 1 == argc);
const char* path = argv[i];
// Make sure the file has .pc extension.
//
size_t n = strlen (path);
assert (n > 3 && strcmp (path + n - 3, ".pc") == 0);
pkgconf_client_t* c =
pkgconf_client_new (error_handler, NULL /* error_handler_data */);
assert (c != NULL);
int r = 1;
int max_depth = 2000;
pkgconf_client_set_flags (c, 0);
pkgconf_pkg_t* p = pkgconf_pkg_find (c, path);
if (p != NULL)
{
int e = PKGCONF_PKG_ERRF_OK;
switch (mode)
{
case dump_cflags:
{
pkgconf_client_set_flags (c, PKGCONF_PKG_PKGF_SEARCH_PRIVATE);
pkgconf_list_t list = PKGCONF_LIST_INITIALIZER;
e = pkgconf_pkg_cflags (c, p, &list, max_depth);
if (e == PKGCONF_PKG_ERRF_OK)
frags_print_and_free (&list);
pkgconf_client_set_flags (c, 0); /* Restore. */
break;
}
case dump_libs:
{
pkgconf_list_t list = PKGCONF_LIST_INITIALIZER;
e = pkgconf_pkg_libs (c, p, &list, max_depth);
if (e == PKGCONF_PKG_ERRF_OK)
frags_print_and_free (&list);
break;
}
case dump_vars:
{
tuples_print (&p->vars);
break;
}
default:
{
assert (false);
}
}
if (e == PKGCONF_PKG_ERRF_OK)
r = 0;
pkgconf_pkg_unref (c, p);
}
else
fprintf (stderr, "package file '%s' not found or invalid\n", path);
pkgconf_client_free (c);
return r;
}
|