summaryrefslogtreecommitdiff
path: root/rld-cc.cpp
blob: a2b1be4027add5ad72a1582ca3f121ca4828c9d6 (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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/*
 * Copyright (c) 2011-2014, Chris Johns <chrisj@rtems.org>
 *
 * 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.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <string.h>

#include <fstream>

#include <rld.h>
#include <rld-cc.h>
#include <rld-process.h>
#include <rld-rtems.h>

namespace rld
{
  namespace cc
  {
    static std::string cc;              //< The CC executable as absolute path.
    static bool        cc_set;          //< True when the CC has been set.
    static std::string cc_name = "gcc"; //< The CC name, ie gcc, clang.

    static std::string ld;              //< The LD executable as absolute path.
    static bool        ld_set;          //< True when the LD has been set.
    static std::string ld_name = "gcc"; //< The LD name, ie gcc, clang.

    static std::string exec_prefix;     //< The CC/LD executable prefix.

    static std::string cppflags;        //< The CPP flags.
    static std::string cflags;          //< The CC flags.
    static std::string cxxflags;        //< The CXX flags.
    static std::string ldflags;         //< The LD flags.

    static std::string warning_cflags;  //< The warning flags in cflags.
    static std::string include_cflags;  //< The include flags in cflags.
    static std::string machine_cflags;  //< The machine flags in cflags.
    static std::string spec_cflags;     //< The spec flags in cflags.

    static std::string install_path;    //< The CC reported install path.
    static std::string programs_path;   //< The CC reported programs path.
    static std::string libraries_path;  //< The CC reported libraries path.

    /**
     * The list of standard libraries.
     */
    #define RPS RLD_PATHSTR_SEPARATOR_STR
    static const char* std_lib_c         = "libgcc.a" RPS "libssp.a" RPS "libc.a";
    static const char* std_lib_cplusplus = "libstdc++.a";

    const std::string
    strip_cflags (const std::string& flags)
    {
      std::string  oflags;
      rld::strings flags_;
      rld::split (flags_, flags);

      for (rld::strings::iterator si = flags_.begin ();
           si != flags_.end ();
           ++si)
      {
        if (!rld::starts_with ((*si), "-O") && !rld::starts_with ((*si), "-g"))
          oflags += ' ' + *si;
      }

      return rld::trim (oflags);
    }

    const std::string
    filter_flags (const std::string& flags,
                  const std::string& ,
                  const std::string& ,
                  flag_type          type,
                  std::string&       warnings,
                  std::string&       includes,
                  std::string&       machines,
                  std::string&       specs)
    {
      /*
       * Defintion of flags to be filtered.
       */
      enum flag_group
      {
        fg_warning,
        fg_include,
        fg_machine,
        fg_specs
      };
      struct flag_def
      {
        flag_group  group;  ///< The group this flag belong to.
        const char* opt;    ///< Option start.
        int         count;  ///< Number of arguments with the option.
        bool        path;   ///< Is this a path ?
        int         out;    ///< If the flag type is set drop the opt..
      };
      const flag_def flag_defs[] =
        {
          { fg_warning, "-W",       1, false, ft_cppflags | ft_cflags | ft_ldflags },
          { fg_include, "-I",       2, true,  0 },
          { fg_include, "-isystem", 2, true,  0 },
          { fg_include, "-sysroot", 2, true,  0 },
          { fg_machine, "-O",       1, false, 0 },
          { fg_machine, "-m",       1, false, 0 },
          { fg_machine, "-f",       1, false, 0 },
          { fg_specs,   "-q",       1, false, 0 },
          { fg_specs,   "-B",       2, true,  0 },
          { fg_specs,   "--specs",  2, false, 0 }
        };
      const int flag_def_size = sizeof (flag_defs) / sizeof (flag_def);

      std::string  oflags;
      rld::strings flags_;

      rld::split (flags_, strip_cflags (flags));

      warnings.clear ();
      includes.clear ();
      machines.clear ();
      specs.clear ();

      for (rld::strings::iterator si = flags_.begin ();
           si != flags_.end ();
           ++si)
      {
        std::string  opts;
        std::string& opt = *(si);
        bool         in = true;

        for (int fd = 0; fd < flag_def_size; ++fd)
        {
          if (rld::starts_with (opt, flag_defs[fd].opt))
          {
            int opt_count = flag_defs[fd].count;
            if (opt_count > 1)
            {
              /*
               * See if the flag is just the option. If is not take one less
               * because the option's argument is joined to the option.
               */
              if (opt != flag_defs[fd].opt)
              {
                opt_count -= 1;
                /*
                 * @todo Path processing here. Not sure what it is needed for.
                 */
              }
            }
            opts += ' ' + opt;
            while (opt_count > 1)
            {
              ++si;
              /*
               * @todo Path processing here. Not sure what it is needed for.
               */
              opts += ' ' + (*si);
              --opt_count;
            }
            switch (flag_defs[fd].group)
            {
              case fg_warning:
                warnings += ' ' + opts;
                break;
              case fg_include:
                includes += ' ' + opts;
                break;
              case fg_machine:
                machines += ' ' + opts;
                break;
              case fg_specs:
                specs += ' ' + opts;
                break;
              default:
                throw rld::error ("Invalid group", "flag group");
            }
            if ((flag_defs[fd].out & type) != 0)
              in = false;
            break;
          }
        }

        if (in)
          oflags += ' ' + opts;
      }

      rld::trim (warnings);
      rld::trim (includes);
      rld::trim (machines);
      rld::trim (specs);

      return rld::trim (oflags);
    }

    const std::string
    filter_flags (const std::string& flags,
                  const std::string& arch,
                  const std::string& path,
                  flag_type          type)
    {
      if (type != ft_cflags)
      {
        std::string warnings;
        std::string includes;
        std::string machines;
        std::string specs;
        return filter_flags (flags,
                             arch,
                             path,
                             type,
                             warnings,
                             includes,
                             machines,
                             specs);
      }
      else
      {
        return filter_flags (flags,
                             arch,
                             path,
                             type,
                             warning_cflags,
                             include_cflags,
                             machine_cflags,
                             spec_cflags);
      }
    }

    void
    set_cc (const std::string& cc_)
    {
      cc = cc_;
      cc_set = true;
    }

    const std::string
    get_cc ()
    {
      return cc;
    }

    bool
    is_cc_set ()
    {
      return cc_set;
    }

    void
    set_ld (const std::string& ld_)
    {
      ld = ld_;
      ld_set = true;
    }

    const std::string
    get_ld ()
    {
      return ld;
    }

    bool
    is_ld_set ()
    {
      return ld_set;
    }

    void
    set_exec_prefix (const std::string& exec_prefix_)
    {
      exec_prefix = exec_prefix_;
    }

    const std::string
    get_exec_prefix ()
    {
      return exec_prefix;
    }

    bool is_exec_prefix_set ()
    {
      return !exec_prefix.empty ();
    }

    void
    set_flags (const std::string& flags,
               const std::string& arch,
               const std::string& path,
               flag_type          type)
    {
      std::string* oflags;
      switch (type)
      {
        case ft_cppflags:
          oflags = &cppflags;
          break;
        case ft_cflags:
          oflags = &cflags;
          break;
        case ft_cxxflags:
          oflags = &cxxflags;
          break;
        case ft_ldflags:
          oflags = &ldflags;
          break;
        default:
          throw rld::error ("Invalid flag type", "CC set flags");
      }
      (*oflags) = filter_flags (flags, arch, path, type);
    }

    void
    set_flags (const std::string& flags, flag_type type)
    {
      std::string arch;
      std::string path;
      set_flags (flags, arch, path, type);
    }

    void
    append_flags (const std::string& flags,
                  const std::string& arch,
                  const std::string& path,
                  flag_type          type)
    {
      std::string* oflags;
      switch (type)
      {
        case ft_cppflags:
          oflags = &cppflags;
          break;
        case ft_cflags:
          oflags = &cflags;
          break;
        case ft_cxxflags:
          oflags = &cxxflags;
          break;
        case ft_ldflags:
          oflags = &ldflags;
          break;
        default:
          throw rld::error ("Invalid flag type", "CC set flags");
      }
      if (oflags->empty ())
        *oflags += filter_flags (flags, arch, path, type);
      else
        *oflags += ' ' + filter_flags (flags, arch, path, type);
    }

    void
    append_flags (const std::string& flags, flag_type type)
    {
      std::string arch;
      std::string path;
      append_flags (flags, arch, path, type);
    }

    const std::string
    get_flags (flag_type type)
    {
      std::string* flags;
      switch (type)
      {
        case ft_cppflags:
          flags = &cppflags;
          break;
        case ft_cflags:
          flags = &cflags;
          break;
        case ft_cxxflags:
          flags = &cxxflags;
          break;
        case ft_ldflags:
          flags = &ldflags;
          break;
        default:
          throw rld::error ("Invalid flag type", "CC get flags");
      }
      return *flags;
    }

    const std::string
    get_flags (flag_group group)
    {
      std::string* flags;
      switch (group)
      {
        case fg_warning_flags:
          flags = &warning_cflags;
          break;
        case fg_include_flags:
          flags = &include_cflags;
          break;
        case fg_machine_flags:
          flags = &machine_cflags;
          break;
        case fg_spec_flags:
          flags = &spec_cflags;
          break;
        default:
          throw rld::error ("Invalid flag group", "CC get flags");
      }
      return *flags;
    }

    void
    append_flags (flag_type type, rld::process::arg_container& args)
    {
      const std::string* flags = 0;
      switch (type)
      {
        case ft_cppflags:
          flags = &cppflags;
          break;
        case ft_cflags:
          flags = &cflags;
          break;
        case ft_cxxflags:
          flags = &cxxflags;
          break;
        case ft_ldflags:
          flags = &ldflags;
          break;
        default:
          throw rld::error ("Invalid flag type", "CC append flags");
      }
      if (!flags->empty ())
        rld::process::args_append (args, *flags);
    }

    void
    make_cc_command (rld::process::arg_container& args)
    {
      /*
       * Use the absolute path to CC if provided.
       */
      if (is_cc_set ())
      {
        args.push_back (cc);
      }
      else
      {
        std::string cmd = cc_name;
        if (!exec_prefix.empty ())
          cmd = exec_prefix + "-rtems" + rld::rtems::version () + '-' + cmd;
        args.push_back (cmd);
      }
    }

    void
    make_ld_command (rld::process::arg_container& args)
    {
      /*
       * Use the absolute path to LD if provided.
       */
      if (is_ld_set ())
      {
        args.push_back (get_ld ());
      }
      else
      {
        std::string cmd = ld_name;
        if (!exec_prefix.empty ())
          cmd = exec_prefix + "-rtems" + rld::rtems::version () + '-' + cmd;
        args.push_back (cmd);
      }
    }

    static bool
    match_and_trim (const char* prefix, std::string& line, std::string& result)
    {
      std::string::size_type pos = ::strlen (prefix);
      if (line.substr (0, pos) == prefix)
      {
        if (line[pos] == '=')
          ++pos;
        result = line.substr (pos, line.size () - pos - 1);
        return true;
      }
      return false;
    }

    static void
    search_dirs ()
    {
      rld::process::arg_container args;

      make_cc_command (args);
      append_flags (ft_cppflags, args);
      append_flags (ft_cflags, args);
      args.push_back ("-print-search-dirs");

      rld::process::tempfile out;
      rld::process::tempfile err;
      rld::process::status   status;

      status = rld::process::execute (cc_name, args, out.name (), err.name ());

      if ((status.type == rld::process::status::normal) &&
          (status.code == 0))
      {
        if (rld::verbose () >= RLD_VERBOSE_DETAILS)
          out.output (cc_name, std::cout, true);
        out.open ();
        while (true)
        {
          std::string line;
          out.read_line (line);
          if (line.size () == 0)
            break;
          if (match_and_trim ("install: ", line, install_path))
            continue;
          if (match_and_trim ("programs: ", line, programs_path))
            continue;
          if (match_and_trim ("libraries: ", line, libraries_path))
            continue;
        }
        out.close ();
        if (rld::verbose () >= RLD_VERBOSE_DETAILS)
        {
          std::cout << "cc::install: " << install_path << std::endl
                    << "cc::programs: " << programs_path << std::endl
                    << "cc::libraries: " << libraries_path << std::endl;
        }
      }
      else
      {
        err.output (cc_name, std::cout);
      }
    }

    void
    get_library_path (std::string& name, std::string& path)
    {
      rld::process::arg_container args;

      make_cc_command (args);
      append_flags (ft_cppflags, args);
      append_flags (ft_cflags, args);
      args.push_back ("-print-file-name=" + name);

      rld::process::tempfile out;
      rld::process::tempfile err;
      rld::process::status   status;

      status = rld::process::execute (cc_name, args, out.name (), err.name ());

      if ((status.type == rld::process::status::normal) &&
          (status.code == 0))
      {
        if (rld::verbose () >= RLD_VERBOSE_DETAILS)
          out.output ("cc", std::cout, true);
        out.open ();
        out.read (path);
        out.close ();
        if (rld::verbose () >= RLD_VERBOSE_DETAILS)
          std::cout << "cc::libpath: " << name << " -> " << path << std::endl;
      }
      else
      {
        err.output ("cc", std::cout);
      }
    }

    void
    get_standard_libpaths (rld::path::paths& libpaths)
    {
      search_dirs ();
      rld::split (libpaths, libraries_path, RLD_PATHSTR_SEPARATOR);
    }

    void
    get_standard_libs (rld::path::paths& libs,
                       rld::path::paths& libpaths,
                       bool              cplusplus)
    {
      strings libnames;

      rld::split (libnames, std_lib_c, RLD_PATHSTR_SEPARATOR);
      if (cplusplus)
        rld::path::path_split (std_lib_cplusplus, libnames);

      for (strings::iterator lni = libnames.begin ();
           lni != libnames.end ();
           ++lni)
      {
        if (rld::verbose () >= RLD_VERBOSE_INFO)
          std::cout << "cc::stdlib: " << *lni << std::endl;

        std::string path;

        rld::path::find_file (path, *lni, libpaths);
        if (path.empty ())
          throw rld::error ("Library not found: " + *lni, "getting standard libs");

        libs.push_back (path);
      }
    }
  }
}