aboutsummaryrefslogtreecommitdiff
path: root/odb/odb.cxx
blob: 79c6b826eefdbb0af4c0a89ccdfd4d09cb991d68 (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
// file      : odb/odb.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
// license   : GNU GPL v3; see accompanying LICENSE file

#include <errno.h>
#include <stdlib.h>    // getenv
#include <string.h>    // strerror
#include <unistd.h>    // stat, execvp
#include <sys/types.h> // stat
#include <sys/stat.h>  // stat

#ifdef _WIN32
#  include <process.h> // _spawnvp
#endif

#include <string>
#include <vector>
#include <cstddef>     // size_t
#include <iostream>

#include <cutl/fs/path.hxx>

#include <odb/version.hxx>
#include <odb/options.hxx>

#ifdef HAVE_CONFIG_H
#  include <odb/config.h>
#endif

using namespace std;
using cutl::fs::path;

static path
driver_path (path const& driver);

static path
plugin_path (path const& driver);

static char const* const db_macro[] =
{
  "-DODB_MYSQL",
  "-DODB_TRACER"
};

int
main (int argc, char* argv[])
{
  typedef vector<string> strings;

  ostream& e (cerr);

  // Find the plugin. It should be in the same directory as the
  // driver.
  //
#ifndef STATIC_PLUGIN
  path plugin (plugin_path (path (argv[0])));
#else
  // Use a dummy name if the plugin is linked into the compiler.
  //
  path plugin ("odb");
#endif

  if (plugin.empty ())
  {
    e << argv[0] << ": error: unable to locate ODB GCC plugin" << endl;
    e << argv[0] << ": info: make sure '" << argv[0] << ".so' is in "
      << "the same directory as '" << argv[0] << "'" << endl;
    return 1;
  }

  strings args, plugin_args;
  bool v (false);

  // The first argument points to the program name, which is
  // g++ by default.
  //
#ifdef GXX_NAME
  path gxx (GXX_NAME);

  // If the g++ name is a relative path (starts with '.'), then use
  // our own path as base.
  //
  if (!gxx.empty () && gxx.string ()[0] == '.')
  {
    path dp (driver_path (path (argv[0])));
    path d (dp.directory ());

    if (!d.empty ())
      gxx = d / gxx;
  }

  args.push_back (gxx.string ());
#else
  args.push_back ("g++");
#endif

  // Default options.
  //
  args.push_back ("-x");
  args.push_back ("c++");
  args.push_back ("-S");
  args.push_back ("-fplugin=" + plugin.string ());

  // Parse driver options.
  //
  for (int i = 1; i < argc; ++i)
  {
    string a (argv[i]);
    size_t n (a.size ());

    // -v
    //
    if (a == "-v")
    {
      v = true;
      args.push_back (a);
    }
    // -x
    //
    else if (a == "-x")
    {
      if (++i == argc || argv[i][0] == '\0')
      {
        e << argv[0] << ": error: expected argument for the -x option" << endl;
        return 1;
      }

      a = argv[i];

      if (a[0] == '-')
        args.push_back (a);
      else
      {
        // This must be the g++ executable name. Update the first
        // argument with it.
        //
        args[0] = a;
      }
    }
    // -I
    //
    else if (n > 1 && a[0] == '-' && a[1] == 'I')
    {
      args.push_back (a);

      if (n == 2) // -I /path
      {
        if (++i == argc || argv[i][0] == '\0')
        {
          e << argv[0] << ": error: expected argument for the -I option"
            << endl;
          return 1;
        }

        args.push_back (argv[i]);
      }
    }
    // -D
    //
    else if (n > 1 && a[0] == '-' && a[1] == 'D')
    {
      args.push_back (a);

      if (n == 2) // -D macro
      {
        if (++i == argc || argv[i][0] == '\0')
        {
          e << argv[0] << ": error: expected argument for the -D option"
            << endl;
          return 1;
        }

        args.push_back (argv[i]);
      }
    }
    // -U
    //
    else if (n > 1 && a[0] == '-' && a[1] == 'U')
    {
      args.push_back (a);

      if (n == 2) // -U macro
      {
        if (++i == argc || argv[i][0] == '\0')
        {
          e << argv[0] << ": error: expected argument for the -U option"
            << endl;
          return 1;
        }

        args.push_back (argv[i]);
      }
    }
    // Store everything else in a list so that we can parse it with the
    // cli parser. This is the only reliable way to find out where the
    // options end.
    //
    else
      plugin_args.push_back (a);
  }

  // Parse plugin options.
  //
  try
  {
    vector<char*> av;
    av.push_back (argv[0]);

    for (strings::iterator i (plugin_args.begin ()), end (plugin_args.end ());
         i != end; ++i)
    {
      av.push_back (const_cast<char*> (i->c_str ()));
    }

    int ac (static_cast<int> (av.size ()));
    cli::argv_file_scanner scan (ac, &av[0], "--options-file");

    options ops (scan);

    // Handle --version.
    //
    if (ops.version ())
    {
      e << "ODB object-relational mapping (ORM) compiler for C++ "
        ODB_COMPILER_VERSION_STR << endl
        << "Copyright (C) 2009-2010 Code Synthesis Tools CC" << endl;

      e << "This is free software; see the source for copying conditions. "
        << "There is NO\nwarranty; not even for MERCHANTABILITY or FITNESS "
        << "FOR A PARTICULAR PURPOSE." << endl;

      return 0;
    }

    // Handle --help.
    //
    if (ops.help ())
    {
      e << "Usage: " << argv[0] << " [options] file [file ...]"
        << endl
        << "Options:" << endl;

      options::print_usage (e);
      return 0;
    }

    // Check that required options were specifed.
    //
    if (!ops.database_specified ())
    {
      e << argv[0] << ": error: no database specified with the --database "
        << "option" << endl;
      return 1;
    }

    size_t end (scan.end () - 1); // We have one less in plugin_args.

    if (end == plugin_args.size ())
    {
      e << argv[0] << ": error: input file expected" << endl;
      return 1;
    }

    // Add ODB macros.
    //
    args.push_back ("-DODB_COMPILER");
    args.push_back (db_macro[ops.database ()]);

    // Encode plugin options.
    //
    for (size_t i (0); i < end; ++i)
    {
      string k, v;
      string a (plugin_args[i]);

      if (a == "--")
      {
        // Ignore the option seperator since GCC doesn't understand it.
        //
        continue;
      }

      if (a.size () > 2)
        k = string (a, 2); // long format
      else
        k = string (a, 1); // short format

      // If there are more arguments then we may have a value.
      //
      if (i + 1 < end)
      {
        a = plugin_args[i + 1];
        if (a.size () > 1 && a[0] != '-')
        {
          v = a;
          ++i;
        }
      }

      string o ("-fplugin-arg-odb-");
      o += k;

      if (!v.empty ())
      {
        o += '=';

        // On Windows we need to protect values with spaces using quotes.
        // Since there could be actual quotes in the value, we need to
        // escape them.
        //
#ifdef _WIN32
        {
          string t ("\"");
          for (size_t i (0); i < v.size (); ++i)
          {
            if (v[i] == '"')
              t += "\\\"";
            else
              t += v[i];
          }
          t += '"';
          v = t;
        }
#endif
        o += v;
      }

      args.push_back (o);
    }

    // Copy over arguments.
    //
    args.insert (args.end (), plugin_args.begin () + end, plugin_args.end ());
  }
  catch (cli::exception const& ex)
  {
    e << ex << endl;
    return 1;
  }

  // Create an execvp-compatible argument array.
  //
  vector<char const*> exec_args;

  for (strings::const_iterator i (args.begin ()), end (args.end ());
       i != end; ++i)
  {
    exec_args.push_back (i->c_str ());

    if (v)
      e << *i << ' ';
  }

  if (v)
    e << endl;

  exec_args.push_back (0);

#ifdef _WIN32
  intptr_t r (_spawnvp (_P_WAIT, exec_args[0], &exec_args[0]));

  if (r == (intptr_t)(-1))
  {
    e << exec_args[0] << ": error: " << strerror (errno) << endl;
    return 1;
  }

  return r == 0 ? 0 : 1;
#else
  if (execvp (exec_args[0], const_cast<char**> (&exec_args[0])) < 0)
  {
    e << exec_args[0] << ": error: " << strerror (errno) << endl;
    return 1;
  }
#endif
}

//
// Path manipulation.
//

static path
driver_path (path const& drv)
{
  typedef path::traits traits;

  if (!drv.directory ().empty ())
    return drv;

  // Search the PATH environment variable.
  //
  string paths;

  // If there is no PATH in environment then the default search
  // path is the current directory.
  //
  if (char const* s = getenv ("PATH"))
    paths = s;
  else
    paths = traits::path_separator;

  // On Windows also check the current directory.
  //
#ifdef _WIN32
  paths += traits::path_separator;
#endif

  struct stat info;

  for (size_t b (0), e (paths.find (traits::path_separator));
       b != string::npos;)
  {
    path p (string (paths, b, e != string::npos ? e - b : e));

    // Empty path (i.e., a double colon or a colon at the beginning
    // or end of PATH) means search in the current dirrectory.
    //
    if (p.empty ())
      p = path (".");

    path dp (p / drv);

    // Just check that the file exist without checking for
    // permissions, etc.
    //
    if (stat (dp.string ().c_str (), &info) == 0 && S_ISREG (info.st_mode))
      return dp;

    // On Windows also try the path with the .exe extension.
    //
#ifdef _WIN32
    dp += ".exe";

    if (stat (dp.string ().c_str (), &info) == 0 && S_ISREG (info.st_mode))
      return dp;
#endif

    if (e == string::npos)
      b = e;
    else
    {
      b = e + 1;
      e = paths.find (traits::path_separator, b);
    }
  }

  return path ();
}

static path
plugin_path (path const& drv)
{
  path dp (driver_path (drv));

  if (!dp.empty ())
  {
    // If the driver name starts with 'lt-', then we are running through
    // the libtool script. Strip this prefix -- the shared object should
    // be in the same directory.
    //
    {
      string n (dp.leaf ().string ());

      if (n.size () > 3 && n[0] == 'l' && n[1] == 't' && n[2] == '-')
        dp = dp.directory () / path (string (n, 3, string::npos));
    }

    struct stat info;

    path so (dp + ".so");
    if (stat (so.string ().c_str (), &info) == 0)
      return so;

    path la (dp + ".la");
    if (stat (la.string ().c_str (), &info) == 0)
    {
      so = la.directory () / path (".libs") / dp.leaf () + ".so";

      if (stat (so.string ().c_str (), &info) == 0)
        return so;
    }
  }

  return path ();
}