aboutsummaryrefslogtreecommitdiff
path: root/tests/basic/driver.c
blob: b14e0cc8b961d5722de2b46bd2cca77c976fa904 (plain)
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
/* file      : tests/basic/driver.c
 * copyright : Copyright (c) 2016-2019 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>  /* NULL */
#include <stdlib.h>  /* free() */
#include <assert.h>
#include <string.h>  /* strcmp() */
#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
print_and_free (pkgconf_list_t* list)
{
  char* buf = pkgconf_fragment_render (list,
                                       true /* escape */,
                                       NULL /* options */);
  printf("%s", buf);
  free (buf);

  pkgconf_fragment_free (list);
}

/*
 * Usage: argv[0] [--cflags] [--libs] (--with-path <dir>)* <name>
 *
 * Print package compiler and linker flags. If the package name has '.pc'
 * extension it is interpreted as a file name. Prints all flags, as pkgconf
 * utility does when --keep-system-libs and --keep-system-cflags are specified.
 *
 * --cflags
 *     Print compiler flags.
 *
 * --libs
 *     Print linker flags.
 *
 * --with-path <dir>
 *     Search through the directory for pc-files. If at least one --with-path
 *     is specified then the default directories are not searched through.
 */
int
main (int argc, const char* argv[])
{
  pkgconf_client_t* c =
    pkgconf_client_new (error_handler,
                        NULL /* error_handler_data */,
                        pkgconf_cross_personality_default ());

  assert (c != NULL);

  bool cflags = false;
  bool libs = false;
  bool default_dirs = true;
  int client_flags = 0;

  int i = 1;
  for (; i < argc; ++i)
  {
    const char* o = argv[i];

    if (strcmp (o, "--cflags") == 0)
      cflags = true;
    else if (strcmp (o, "--libs") == 0)
      libs = true;
    else if (strcmp (o, "--static") == 0)
      client_flags = PKGCONF_PKG_PKGF_SEARCH_PRIVATE |
                     PKGCONF_PKG_PKGF_MERGE_PRIVATE_FRAGMENTS;
    else if (strcmp (o, "--with-path") == 0)
    {
      ++i;
      assert (i < argc);

      pkgconf_path_add (argv[i], &c->dir_list, /* filter_duplicates */ true);
      default_dirs = false;
    }
    else
      break;
  }

  assert (i + 1 == argc);
  const char* name = argv[i];

  int r = 1;
  int max_depth = 2000;

  pkgconf_client_set_flags (c, client_flags);

  /*
   * Bootstrap the package search default paths if not specified explicitly.
   */
  if (default_dirs)
    pkgconf_client_dir_list_build (c, pkgconf_cross_personality_default ());

  pkgconf_pkg_t* p = pkgconf_pkg_find (c, name);

  if (p != NULL)
  {
    int e = PKGCONF_PKG_ERRF_OK;

    /*
     * Print C flags.
     */
    if (cflags)
    {
      pkgconf_client_set_flags (
        c,
        client_flags | 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)
        print_and_free (&list);

      pkgconf_client_set_flags (c, client_flags); /* Restore. */
    }

    /*
     * Print libs.
     */
    if (libs && e == PKGCONF_PKG_ERRF_OK)
    {
      pkgconf_list_t list = PKGCONF_LIST_INITIALIZER;
      e = pkgconf_pkg_libs (c, p, &list, max_depth);

      if (e == PKGCONF_PKG_ERRF_OK)
        print_and_free (&list);
    }

    if (e == PKGCONF_PKG_ERRF_OK)
    {
      r = 0;

      if (cflags || libs)
        printf ("\n");
    }

    pkgconf_pkg_unref (c, p);
  }
  else
    fprintf (stderr, "package '%s' not found\n", name);

  pkgconf_client_free (c);
  return r;
}