summaryrefslogtreecommitdiff
path: root/rld-cc.cpp
blob: f69b7c9f24cd98576d25eef7752a7a95c095da60 (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
/*
 * 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>

namespace rld
{
  namespace cc
  {
    std::string cc;
    std::string cc_name = "gcc";
    std::string exec_prefix;
    std::string cppflags;
    std::string cflags;
    std::string cxxflags;
    std::string ldflags;
    std::string warning_cflags;
    std::string include_cflags;
    std::string machine_cflags;
    std::string spec_cflags;
    std::string install_path;
    std::string programs_path;
    std::string 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";

    void
    make_cc_command (rld::process::arg_container& args)
    {
      /*
       * Use the absolute path to CC if provided.
       */
      if (!cc.empty ())
        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
    add_cppflags (rld::process::arg_container& args)
    {
      if (!cppflags.empty ())
        args.push_back (cppflags);
    }

    void
    add_cflags (rld::process::arg_container& args)
    {
      if (!cflags.empty ())
        args.push_back (cflags);
    }

    void
    add_cxxflags (rld::process::arg_container& args)
    {
      if (!cxxflags.empty ())
        args.push_back (cxxflags);
    }

    void
    add_ldflags (rld::process::arg_container& args)
    {
      if (!ldflags.empty ())
        args.push_back (ldflags);
    }

    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);
      }
    }

    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);
      add_cppflags (args);
      add_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);
      add_cflags (args);
      add_ldflags (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 (libraries_path, libpaths, RLD_PATHSTR_SEPARATOR);
    }

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

      rld::split (std_lib_c, libnames, 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);
      }
    }
  }
}