aboutsummaryrefslogtreecommitdiff
path: root/build2/cc/pkgconfig.cxx
blob: 00ab54185bb730b17035da36d1d4bfe1d8fd61ad (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// file      : build2/cc/msvc.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <build2/scope>
#include <build2/target>
#include <build2/context>
#include <build2/variable>
#include <build2/filesystem>
#include <build2/diagnostics>

#include <build2/bin/target>

#include <build2/cc/types>
#include <build2/cc/utility>

#include <build2/cc/common>

using namespace std;
using namespace butl;

namespace build2
{
  namespace cc
  {
    using namespace bin;

    // Try to find a .pc file in the pkgconfig/ subdirectory of libd, trying
    // several names derived from stem. If not found, return false. If found,
    // extract poptions, loptions, and libs, set the corresponding *.export.*
    // variables on targets, and return true.
    //
    // System library search paths (those extracted from the compiler) are
    // passed in sys_sp and should already be extracted.
    //
    // Note that scope and link order should be "top-level" from the
    // search_library() POV.
    //
    bool common::
    pkgconfig_extract (action act,
                       const scope& s,
                       lib& lt,
                       liba* at,
                       libs* st,
                       const optional<string>& proj,
                       const string& stem,
                       const dir_path& libd,
                       const dir_paths& sysd) const
    {
      tracer trace (x, "pkgconfig_extract");

      assert (pkgconfig != nullptr);
      assert (at != nullptr || st != nullptr);

      // When it comes to looking for .pc files we have to decide where to
      // search (which directory(ies)) as well as what to search for (which
      // names).
      //
      path f;
      auto search = [&f, &proj, &stem, &libd] (const dir_path& dir) -> bool
      {
        // Check if we have this directory inrelative to this library's
        // directory.
        //
        dir_path pkgd (dir_path (libd) /= dir);

        if (!exists (pkgd))
          return false;

        // See if there is a corresponding .pc file. About half of them called
        // foo.pc and half libfoo.pc (and one of the pkg-config's authors
        // suggests that some of you should call yours foolib.pc, just to keep
        // things interesting, you know).
        //
        // Given the (general) import in the form <proj>%lib{<stem>}, we will
        // first try <stem>.pc, then lib<stem>.pc. Maybe it also makes sense
        // to try <proj>.pc, just in case. Though, according to pkg-config
        // docs, the .pc file should correspond to a library, not project. But
        // then you get something like zlib which calls it zlib.pc. So let's
        // just do it.
        //
        f = pkgd;
        f /= stem;
        f += ".pc";

        if (exists (f))
          return true;

        f = pkgd;
        f /= "lib";
        f += stem;
        f += ".pc";

        if (exists (f))
          return true;

        if (proj)
        {
          f = pkgd;
          f /= *proj;
          f += ".pc";

          if (exists (f))
            return true;
        }

        return false;
      };

      // First always check the pkgconfig/ subdirectory in this library's
      // directory. Even on platforms where this is not the canonical place,
      // .pc files of autotools-based packages installed by the user often
      // still end up there.
      //
      if (!search (dir_path ("pkgconfig")))
      {
        // Platform-specific locations.
        //
        if (tsys == "freebsd")
        {
          // On FreeBSD .pc files go to libdata/pkgconfig/, not lib/pkgconfig/.
          //
          if (!search ((dir_path ("..") /= "libdata") /= "pkgconfig"))
            return false;
        }
        else
         return false;
      }

      // Ok, we are in business. Time to run pkg-config. To keep things
      // simple, we run it multiple times, for --cflag/--libs and --static.
      //
      auto extract = [&f, this] (const char* op, bool impl) -> string
      {
        const char* args[] = {
          pkgconfig->recall_string (),
          op, // --cflags/--libs
          (impl ? "--static" : f.string ().c_str ()),
          (impl ? f.string ().c_str () : nullptr),
          nullptr
        };

        return run<string> (
          *pkgconfig, args, [] (string& s) -> string {return move (s);});
      };

      // On Windows pkg-config (at least the MSYS2 one which we are using)
      // will escape backslahses in paths. In fact, it may escape things
      // even on non-Windows platforms, for example, spaces. So we use a
      // slightly modified version of next_word().
      //
      auto next = [] (const string& s, size_t& b, size_t& e) -> string
      {
        string r;
        size_t n (s.size ());

        if (b != e)
          b = e;

        // Skip leading delimiters.
        //
        for (; b != n && s[b] == ' '; ++b) ;

        if (b == n)
        {
          e = n;
          return r;
        }

        // Find first trailing delimiter while taking care of escapes.
        //
        r = s[b];
        for (e = b + 1; e != n && s[e] != ' '; ++e)
        {
          if (s[e] == '\\')
          {
            if (++e == n)
              fail << "dangling escape in pkg-config output '" << s << "'";
          }

          r += s[e];
        }

        return r;
      };

      // First extract --cflags and set them as lib{}:export.poptions (i.e.,
      // they will be common for both liba{} and libs{}; later when we do
      // split .pc files, we will have to run this twice).
      //
      {
        string cstr (extract ("--cflags", false));
        strings pops;

        string o;
        char arg ('\0');
        for (size_t b (0), e (0); !(o = next (cstr, b, e)).empty (); )
        {
          // Filter out /usr/local/include since most platforms/compilers
          // search in there (at the end, as in other system header
          // directories) by default. And for those that don't (like FreeBSD)
          // we should just fix it ourselves (see config_module::init ()).
          //
          // Failed that /usr/local/include may appear before "more specific"
          // directories which can lead to the installed headers being picked
          // up instead.
          //
          if (arg != '\0')
          {
            if (arg == 'I' && o == "/usr/local/include")
              pops.pop_back ();
            else
              pops.push_back (move (o));

            arg = '\0';
            continue;
          }

          size_t n (o.size ());

          // We only keep -I, -D and -U.
          //
          if (n >= 2 &&
              o[0] == '-' &&
              (o[1] == 'I' || o[1] == 'D' || o[1] == 'U'))
          {
            if (!(n > 2 &&
                  o[1] == 'I' &&
                  o.compare (2, string::npos, "/usr/local/include") == 0))
              pops.push_back (move (o));

            if (n == 2)
              arg = o[1];
            continue;
          }

          l4 ([&]{trace << "ignoring " << f << " --cflags option " << o;});
        }

        if (arg != '\0')
          fail << "argument expected after " << pops.back () <<
            info << "while parsing pkg-config --cflags output of " << f;

        if (!pops.empty ())
        {
          auto p (lt.vars.insert (c_export_poptions));

          // The only way we could already have this value is if this same
          // library was also imported as a project (as opposed to installed).
          // Unlikely but possible. In this case the values were set by the
          // export stub and we shouldn't touch them.
          //
          if (p.second)
            p.first.get () = move (pops);
        }
      }

      // Now parse --libs into loptions/libs (interface and implementation).
      //
      auto parse_libs = [act, &s, &f, sysd, &next, this] (
        const string& lstr, target& t)
      {
        strings lops;
        vector<name> libs;

        // Normally we will have zero or more -L's followed by one or more
        // -l's, with the first one being the library itself. But sometimes
        // we may have other linker options, for example, -Wl,... or
        // -pthread. It's probably a bad idea to ignore them. Also,
        // theoretically, we could have just the library name/path.
        //
        // The tricky part, of course, is to know whether what follows after
        // an option we don't recognize is its argument or another option or
        // library. What we do at the moment is stop recognizing just
        // library names (without -l) after seeing an unknown option.
        //

        bool arg (false), first (true), known (true), have_L;
        string o;
        for (size_t b (0), e (0); !(o = next (lstr, b, e)).empty (); )
        {
          if (arg)
          {
            // Can only be an argument for an loption.
            //
            lops.push_back (move (o));
            arg = false;
            continue;
          }

          size_t n (o.size ());

          // See if this is -L.
          //
          if (n >= 2 && o[0] == '-' && o[1] == 'L')
          {
            have_L = true;
            lops.push_back (move (o));
            arg = (n == 2);
            continue;
          }

          // See if that's -l or just the library name/path.
          //
          if ((known && o[0] != '-') ||
              (n > 2 && o[0] == '-' && o[1] == 'l'))
          {
            // First one is the library itself, which we skip. Note that we
            // don't verify this and theoretically it could be some other
            // library, but we haven't encountered such a beast yet.
            //
            if (first)
            {
              first = false;
              continue;
            }

            libs.push_back (name (move (o)));
            continue;
          }

          // Otherwise we assume it is some other loption.
          //
          known = false;
          lops.push_back (move (o));
        }

        if (arg)
          fail << "argument expected after " << lops.back () <<
            info << "while parsing pkg-config --libs output of " << f;

        if (first)
          fail << "library expected in '" << lstr << "'" <<
            info << "while parsing pkg-config --libs output of " << f;

        // Resolve -lfoo into the library file path using our import installed
        // machinery (i.e., we are going to call search_library() that will
        // probably call us again, and so on).
        //
        // The reason we do it is the link order. For general libraries it
        // shouldn't matter if we imported them via an export stub, direct
        // import installed, or via a .pc file (which we could have generated
        // from the export stub). The exception is "runtime libraries" (which
        // are really the extension of libc) such as -lm, -ldl, -lpthread,
        // etc. Those we will detect and leave as -l*.
        //
        // If we managed to resolve all the -l's (sans runtime), then we can
        // omit -L's for nice and tidy command line.
        //
        bool all (true);
        optional<dir_paths> usrd; // Populate lazily.

        for (name& n: libs)
        {
          string& l (n.value);

          // These ones are common/standard/POSIX.
          //
          if (l[0] != '-'      || // e.g., shell32.lib
              l == "-lm"       ||
              l == "-ldl"      ||
              l == "-lrt"      ||
              l == "-lpthread")
            continue;

          // Note: these list are most likely incomplete.
          //
          if (tclass == "linux")
          {
            // Some extras from libc (see libc6-dev) and other places.
            //
            if (l == "-lanl"     ||
                l == "-lcrypt"   ||
                l == "-lnsl"     ||
                l == "-lresolv"  ||
                l == "-lgcc")
            continue;
          }
          else if (tclass == "macos")
          {
            if (l == "-lSystem")
              continue;
          }

          // Prepare user search paths by entering the -L paths from the .pc
          // file.
          //
          if (have_L && !usrd)
          {
            usrd = dir_paths ();

            for (auto i (lops.begin ()); i != lops.end (); ++i)
            {
              const string& o (*i);

              if (o.size () >= 2 && o[0] == '-' && o[1] == 'L')
              {
                string p;

                if (o.size () == 2)
                  p = *++i; // We've verified it's there.
                else
                  p = string (o, 2);

                dir_path d (move (p));

                if (d.relative ())
                  fail << "relative -L directory in '" << lstr << "'" <<
                    info << "while parsing pkg-config --libs output of " << f;

                usrd->push_back (move (d));
              }
            }
          }

          // @@ OUT: for now we assume out is undetermined, just like in
          // resolve_library().
          //
          dir_path out;
          string name (l, 2); // Sans -l.

          prerequisite_key pk {
            nullopt, {&lib::static_type, &out, &out, &name, nullopt}, &s};

          if (lib* lt = static_cast<lib*> (
                search_library (act, sysd, usrd, pk)))
          {
            // We used to pick a member but that doesn't seem right since the
            // same target could be used with different link orders.
            //
            n.dir = lt->dir;
            n.type = lib::static_type.name;
            n.value = lt->name;
          }
          else
            // If we couldn't find the library, then leave it as -l.
            //
            all = false;
        }

        // If all the -l's resolved and no other options, then drop all the
        // -L's. If we have unknown options, then leave them in to be safe.
        //
        if (all && known)
          lops.clear ();

        if (!lops.empty ())
        {
          if (cid == "msvc")
          {
            // Translate -L to /LIBPATH.
            //
            for (auto i (lops.begin ()); i != lops.end (); )
            {
              string& o (*i);
              size_t n (o.size ());

              if (n >= 2 && o[0] == '-' && o[1] == 'L')
              {
                o.replace (0, 2, "/LIBPATH:");

                if (n == 2)
                {
                  o += *++i; // We've verified it's there.
                  i = lops.erase (i);
                  continue;
                }
              }

              ++i;
            }
          }

          auto p (t.vars.insert (c_export_loptions));

          if (p.second)
            p.first.get () = move (lops);
        }

        // Set even if empty (export override).
        //
        {
          auto p (t.vars.insert (c_export_libs));

          if (p.second)
            p.first.get () = move (libs);
        }
      };

      {
        string lstr_int (extract ("--libs", false));
        string lstr_imp (extract ("--libs", true));

        parse_libs (lstr_int, lt);

        // Currently, these will result in the same values but it will be
        // different once we support split .pc files.
        //
        if (at != nullptr) parse_libs (lstr_imp, *at);
        if (st != nullptr) parse_libs (lstr_imp, *st);
      }

      return true;
    }
  }
}