summaryrefslogtreecommitdiff
path: root/src/bin/eolian/main.c
blob: 114f989ac4018c0a777b74b4136d7daad96d12b4 (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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
#include <stdlib.h>
#include <unistd.h>

#include "main.h"
#include "types.h"
#include "headers.h"
#include "sources.h"

int _eolian_gen_log_dom = -1;

enum
{
   GEN_H        = 1 << 0,
   GEN_H_STUB   = 1 << 1,
   GEN_C        = 1 << 2,
   GEN_C_IMPL   = 1 << 3,
   GEN_D        = 1 << 4,
   GEN_D_FULL   = 1 << 5
};

static const char *_dexts[6] =
{
  ".h", ".stub.h", ".c", ".c", ".d", ".d"
};

static int
_get_bit_pos(int flag)
{
   int pos = 0;
   for (; !(flag & 1); flag >>= 1)
     ++pos;
   return pos;
}

static void
_print_usage(const char *progn, FILE *outf)
{
   fprintf(outf, "Usage: %s [options] [input]\n", progn);
   fprintf(outf, "Options:\n"
                 "  -I inc        include path \"inc\"\n"
                 "  -S            do not scan system dir for eo files\n"
                 "  -g type       generate file of type \"type\"\n"
                 "  -o name       specify the base name for output\n"
                 "  -o type:name  specify a particular output filename\n"
                 "  -h            print this message and exit\n"
                 "  -v            print version and exit\n"
                 "\n"
                 "Available types:\n"
                 "  h: C header file (.eo.h/.eot.h)\n"
                 "  s: Stub C header file (.eo.stub.h/.eot.stub.h)\n"
                 "  c: C source file (.eo.c)\n"
                 "  i: Implementation file (.c, merged with existing)\n"
                 "  d: Make-style dependencies, only for headers (.d)\n"
                 "  D: Like 'd' but for all generated files (.d)\n"
                 "\n"
                 "By default, the 'hc' set is used ('h' for .eot files).\n\n"
                 "The system-wide Eolian directory is scanned for eo files\n"
                 "by default, together with all specified '-I' flags.\n\n"
                 "Output filenames are determined from input .eo filename.\n"
                 "Default output path is where the input file is.\n\n"
                 "Also, specifying a type-dependent input file automatically\n"
                 "adds it to generated files, so if you specify those, you\n"
                 "don't need to explicitly specify -g for those types anymore.\n\n"
                 "Explicit output base name is without extension. The extension\n"
                 "is determined from the input file name. If that is not possible\n"
                 "for some reason, it defaults to \".eo\". Obviously, this does not\n"
                 "affect specific filenames (-o x:y) as these are full names.\n"
                 "Implementation files are a special case (no \".eo\" added).\n");
}

static void
_print_version(FILE *outf)
{
   fprintf(outf, "Eolian C generator version: " PACKAGE_VERSION "\n");
}

static Eina_Bool
_try_set_out(char t, char **outs, const char *val, int *what)
{
   int pos = -1;
   switch (t)
     {
      case 'h':
        pos = _get_bit_pos(GEN_H);
        *what |= GEN_H;
        break;
      case 's':
        pos = _get_bit_pos(GEN_H_STUB);
        *what |= GEN_H_STUB;
        break;
      case 'c':
        pos = _get_bit_pos(GEN_C);
        *what |= GEN_C;
        break;
      case 'i':
        pos = _get_bit_pos(GEN_C_IMPL);
        *what |= GEN_C_IMPL;
        break;
      case 'd':
        pos = _get_bit_pos(GEN_D);
        *what |= GEN_D;
        break;
      case 'D':
        pos = _get_bit_pos(GEN_D_FULL);
        *what |= GEN_D_FULL;
        break;
     }
   if (pos < 0)
     return EINA_FALSE;
   if (outs[pos])
     free(outs[pos]);
   outs[pos] = strdup(val);
   return EINA_TRUE;
}

static void _fill_all_outs(char **outs, const char *val, char *base)
{
   const char *ext = strrchr(val, '.');
   if (!ext)
     ext = ".eo";

   char *basen = base;
   if (!basen)
     {
        basen = strdup(val);
        char *p = strrchr(basen, '.');
        if (p) *p = '\0';
     }

   size_t blen = strlen(basen),
          elen = strlen(ext);

   for (size_t i = 0; i < (sizeof(_dexts) / sizeof(char *)); ++i)
     {
        if (outs[i])
          continue;
        size_t dlen = strlen(_dexts[i]);
        char *str = malloc(blen + elen + dlen + 1);
        char *p = str;
        memcpy(p, basen, blen);
        p += blen;
        if ((1 << i) != GEN_C_IMPL)
          {
             memcpy(p, ext, elen);
             p += elen;
          }
        memcpy(p, _dexts[i], dlen);
        p[dlen] = '\0';
        outs[i] = str;
     }

   if (!base)
     free(basen);
}

static Eina_Strbuf *
_include_guard(const char *fname, const char *gname, Eina_Strbuf *buf)
{
   if (!buf)
     return NULL;

   if (!gname)
     gname = "";

   char iname[256] = {0};
   strncpy(iname, fname, sizeof(iname) - 1);
   char *inamep = iname;
   eina_str_toupper(&inamep);

   Eina_Strbuf *g = eina_strbuf_new();
   eina_strbuf_append_printf(g, "#ifndef _%s_%s\n", iname, gname);
   eina_strbuf_append_printf(g, "#define _%s_%s\n\n", iname, gname);

   eina_strbuf_replace_all(g, ".", "_");
   eina_strbuf_append(g, eina_strbuf_string_get(buf));
   eina_strbuf_append(g, "\n#endif\n");
   eina_strbuf_free(buf);
   return g;
}

static const char *
_get_filename(const char *path)
{
   if (!path)
     return NULL;
   const char *ret1 = strrchr(path, '/');
   const char *ret2 = strrchr(path, '\\');
   if (!ret1 && !ret2)
     return path;
   if (ret1 && ret2)
     {
        if (ret1 > ret2)
          return ret1 + 1;
        else
          return ret2 + 1;
     }
   if (ret1)
     return ret1 + 1;
   return ret2 + 1;
}

static Eina_Bool
_write_file(const char *fname, const Eina_Strbuf *buf)
{
   FILE *f = fopen(fname, "wb");
   if (!f)
     {
        fprintf(stderr, "eolian: could not open '%s' (%s)\n",
                fname, strerror(errno));
        return EINA_FALSE;
     }

   Eina_Bool fret = EINA_TRUE;

   size_t bl = eina_strbuf_length_get(buf);
   if (!bl)
     goto end;

   if (fwrite(eina_strbuf_string_get(buf), 1, bl, f) != bl)
     {
        fprintf(stderr, "eolian: could not write '%s' (%s)\n",
                fname, strerror(errno));
        fret = EINA_FALSE;
     }

end:
   fclose(f);
   return fret;
}

static Eina_Bool
_read_file(const char *fname, Eina_Strbuf **buf)
{
   FILE *f = fopen(fname, "rb");
   if (!f)
     {
        *buf = eina_strbuf_new();
        return EINA_TRUE;
     }

   fseek(f, 0, SEEK_END);
   long fs = ftell(f);
   if (fs < 0)
     {
        fprintf(stderr, "eolian: could not get length of '%s'\n", fname);
        fclose(f);
        return EINA_FALSE;
     }
   fseek(f, 0, SEEK_SET);

   char *cont = malloc(fs + 1);
   if (!cont)
     {
        fprintf(stderr, "eolian: could not allocate memory for '%s'\n", fname);
        fclose(f);
        return EINA_FALSE;
     }

   long as = fread(cont, 1, fs, f);
   if (as != fs)
     {
        fprintf(stderr, "eolian: could not read %ld bytes from '%s' (got %ld)\n",
                fs, fname, as);
        free(cont);
        fclose(f);
        return EINA_FALSE;
     }

   cont[fs] = '\0';
   fclose(f);
   *buf = eina_strbuf_manage_new_length(cont, fs);
   return EINA_TRUE;
}

char *eo_gen_c_full_name_get(const char *nm)
{
   if (!nm)
     return NULL;
   char *buf = strdup(nm);
   if (!buf)
     abort();
   for (char *p = strchr(buf, '.'); p; p = strchr(p, '.'))
     *p = '_';
   return buf;
}

void eo_gen_class_names_get(const Eolian_Class *cl, char **cname,
                            char **cnameu, char **cnamel)
{
   char *cn = NULL, *cnu = NULL, *cnl = NULL;
   cn = eo_gen_c_full_name_get(eolian_class_name_get(cl));
   if (!cn)
     abort();
   if (cname)
     *cname = cn;

   if (cnameu)
     {
        cnu = strdup(cn);
        if (!cnu)
          {
             free(cn);
             abort();
          }
        eina_str_toupper(&cnu);
        *cnameu = cnu;
     }

   if (cnamel)
     {
        cnl = strdup(cn);
        if (!cnl)
          {
             free(cn);
             free(cnu);
             abort();
          }
        eina_str_tolower(&cnl);
        *cnamel = cnl;
     }

   if (!cname)
     free(cn);
}

static Eina_Bool
_write_header(const Eolian_State *eos, const Eolian_State *state, const char *ofname,
              const char *ifname)
{
   INF("generating header: %s", ofname);
   Eina_Strbuf *buf = eina_strbuf_new();

   eo_gen_types_header_gen(state, eolian_state_objects_by_file_get(eos, ifname),
                           buf, EINA_TRUE);
   buf = _include_guard(ifname, "TYPES", buf);

   Eina_Strbuf *cltd = eo_gen_class_typedef_gen(eos, ifname);
   if (cltd)
     {
        cltd = _include_guard(ifname, "CLASS_TYPE", cltd);
        eina_strbuf_prepend_char(buf, '\n');
        eina_strbuf_prepend(buf, eina_strbuf_string_get(cltd));
        eina_strbuf_free(cltd);
     }

   eo_gen_header_gen(state, eolian_state_class_by_file_get(eos, ifname), buf);

   buf = _include_guard(_get_filename(ofname), NULL, buf);
   if (_write_file(ofname, buf))
     {
        eina_strbuf_free(buf);
        return EINA_TRUE;
     }

   eina_strbuf_free(buf);
   return EINA_FALSE;
}

static Eina_Bool
_write_stub_header(const Eolian_State *eos, const Eolian_State *state, const char *ofname,
                   const char *ifname)
{
   INF("generating stub header: %s", ofname);
   Eina_Strbuf *buf = eina_strbuf_new();

   eo_gen_types_header_gen(state, eolian_state_objects_by_file_get(eos, ifname),
                           buf, EINA_FALSE);

   Eina_Strbuf *cltd = eo_gen_class_typedef_gen(eos, ifname);
   if (cltd)
     {
        eina_strbuf_prepend_char(buf, '\n');
        eina_strbuf_prepend(buf, eina_strbuf_string_get(cltd));
        eina_strbuf_free(cltd);
     }

   buf = _include_guard(_get_filename(ofname), "STUBS", buf);

   Eina_Bool ret = _write_file(ofname, buf);
   eina_strbuf_free(buf);
   return ret;
}

static Eina_Bool
_write_source(const Eolian_State *eos, const char *ofname,
              const char *ifname, Eina_Bool eot)
{
   INF("generating source: %s", ofname);
   Eina_Strbuf *buf = eina_strbuf_new();
   Eina_Bool ret = EINA_FALSE;

   const Eolian_Class *cl = eolian_state_class_by_file_get(eos, ifname);
   eo_gen_types_source_gen(eolian_state_objects_by_file_get(eos, ifname), buf);
   eo_gen_source_gen(cl, buf);
   if (cl || eot)
     {
        /* always have at least a stub in order to allow unconditional generation */
        if (!eina_strbuf_length_get(buf))
          eina_strbuf_append(buf, "/* Nothing to implement. */\n");
        if (!_write_file(ofname, buf))
          goto done;
        ret = EINA_TRUE;
     }

done:
   eina_strbuf_free(buf);
   return ret;
}

static Eina_Bool
_write_impl(const Eolian_State *eos, const char *ofname, const char *ifname)
{
   INF("generating impl: %s", ofname);

   const Eolian_Class *cl = eolian_state_class_by_file_get(eos, ifname);
   if (!cl)
     return EINA_FALSE;

   Eina_Strbuf *buf;
   if (!_read_file(ofname, &buf))
     return EINA_FALSE;

   eo_gen_impl_gen(cl, buf);
   Eina_Bool ret = _write_file(ofname, buf);
   eina_strbuf_free(buf);
   return ret;
}

static void
_append_dep_line(Eina_Strbuf *buf, Eina_Strbuf *dbuf, char **outs, int gen_what, int what)
{
   if (!(gen_what & what))
     return;
   eina_strbuf_append(buf, outs[_get_bit_pos(what)]);
   eina_strbuf_append_buffer(buf, dbuf);
}

static Eina_Bool
_write_deps(const Eolian_State *eos, const char *ofname, const char *ifname,
            char **outs, int gen_what)
{
   INF("generating deps: %s", ofname);

   Eina_Bool ret = EINA_TRUE;
   Eina_Strbuf *buf = eina_strbuf_new();
   Eina_Strbuf *dbuf = eina_strbuf_new();

   const Eolian_Unit *un = eolian_state_unit_by_file_get(eos, ifname);
   if (!un)
     {
        ret = EINA_FALSE;
        goto result;
     }

   eina_strbuf_append(dbuf, ": ");
   /* every generated file depends on its .eo/.eot file */
   eina_strbuf_append(dbuf, eolian_unit_file_path_get(un));

   const Eolian_Unit *dun;
   Eina_Iterator *deps = eolian_unit_children_get(un);
   EINA_ITERATOR_FOREACH(deps, dun)
     {
        const char *dpath = eolian_unit_file_path_get(dun);
        if (!dpath)
          continue;
        eina_strbuf_append_char(dbuf, ' ');
        eina_strbuf_append(dbuf, dpath);
     }
   eina_iterator_free(deps);
   eina_strbuf_append_char(dbuf, '\n');

   _append_dep_line(buf, dbuf, outs, gen_what, GEN_H);
   _append_dep_line(buf, dbuf, outs, gen_what, GEN_H_STUB);

   if (gen_what & GEN_D_FULL)
     {
        _append_dep_line(buf, dbuf, outs, gen_what, GEN_C);
        _append_dep_line(buf, dbuf, outs, gen_what, GEN_C_IMPL);
     }

   ret = _write_file(ofname, buf);
result:
   eina_strbuf_free(dbuf);
   eina_strbuf_free(buf);
   return ret;
}

int
main(int argc, char **argv)
{
   int pret = 1;

   char *outs[sizeof(_dexts) / sizeof(void *)] = {
     NULL, NULL, NULL, NULL, NULL, NULL
   };
   char *basen = NULL;
   Eina_List *includes = NULL;

   eina_init();
   eolian_init();

   Eolian_State *eos = eolian_state_new();

   const char *dom = "eolian_gen";
   _eolian_gen_log_dom = eina_log_domain_register(dom, EINA_COLOR_GREEN);
   if (_eolian_gen_log_dom < 0)
     {
        EINA_LOG_ERR("Could not register log domain: %s", dom);
        goto end;
     }

   eina_log_timing(_eolian_gen_log_dom, EINA_LOG_STATE_STOP, EINA_LOG_STATE_INIT);

   int gen_what = 0;
   Eina_Bool scan_system = EINA_TRUE;

   for (int opt; (opt = getopt(argc, argv, "SI:g:o:hv")) != -1;)
     switch (opt)
       {
        case 0:
          break;
        case 'S':
          scan_system = EINA_FALSE;
          break;
        case 'I':
          /* just a pointer to argv contents, so it persists */
          includes = eina_list_append(includes, optarg);
          break;
        case 'g':
          for (const char *wstr = optarg; *wstr; ++wstr)
            switch (*wstr)
              {
               case 'h':
                 gen_what |= GEN_H;
                 break;
               case 's':
                 gen_what |= GEN_H_STUB;
                 break;
               case 'c':
                 gen_what |= GEN_C;
                 break;
               case 'i':
                 gen_what |= GEN_C_IMPL;
                 break;
               case 'd':
                 gen_what |= GEN_D;
                 break;
               case 'D':
                 gen_what |= GEN_D_FULL;
                 break;
               default:
                 fprintf(stderr, "unknown type: '%c'\n", *wstr);
                 goto end;
              }
          break;
        case 'o':
          if (strchr(optarg, ':'))
            {
               const char *abeg = optarg;
               const char *cpos = strchr(abeg, ':');
               if (((cpos - abeg) != 1) ||
                   !_try_set_out(*abeg, outs, cpos + 1, &gen_what))
                 {
                    char *oa = strdup(abeg);
                    oa[cpos - abeg] = '\0';
                    fprintf(stderr, "unknown type: '%s'\n", oa);
                    free(oa);
                    goto end;
                 }
            }
          else
            {
               if (basen)
                 free(basen);
               basen = strdup(optarg);
            }
          break;
        case 'h':
          _print_usage(argv[0], stdout);
          pret = 0;
          goto end;
        case 'v':
          _print_version(stdout);
          pret = 0;
          goto end;
        default:
          _print_usage(argv[0], stderr);
          goto end;
       }

   const char *input = argv[optind];
   if (!input)
     {
        fprintf(stderr, "eolian: no input file\n");
        goto end;
     }

   const char *ext = strrchr(input, '.');
   if (!ext || (strcmp(ext, ".eo") && strcmp(ext, ".eot")))
     {
        fprintf(stderr, "eolian: invalid input file '%s'\n", input);
        goto end;
     }

   if (scan_system)
     {
        if (!eolian_state_system_directory_add(eos))
          {
             fprintf(stderr, "eolian: could not scan system directory\n");
             goto end;
          }
     }

   const char *inc;
   EINA_LIST_FREE(includes, inc)
     {
        if (!eolian_state_directory_add(eos, inc))
          {
             fprintf(stderr, "eolian: could not scan '%s'\n", inc);
             goto end;
          }
     }

   if (!eolian_state_file_path_parse(eos, input))
     {
        fprintf(stderr, "eolian: could not parse file '%s'\n", input);
        goto end;
     }

   _fill_all_outs(outs, input, basen);

   const char *eobn = _get_filename(input);

   if (!gen_what)
     gen_what = GEN_H | GEN_C;

   Eina_Bool succ = EINA_TRUE;
   if (gen_what & GEN_H)
     succ = _write_header(eos, eos, outs[_get_bit_pos(GEN_H)], eobn);
   if (succ && (gen_what & GEN_H_STUB))
     succ = _write_stub_header(eos, eos, outs[_get_bit_pos(GEN_H_STUB)], eobn);
   if (succ && (gen_what & GEN_C))
     succ = _write_source(eos, outs[_get_bit_pos(GEN_C)], eobn, !strcmp(ext, ".eot"));
   if (succ && (gen_what & GEN_C_IMPL))
     succ = _write_impl(eos, outs[_get_bit_pos(GEN_C_IMPL)], eobn);

   if (succ && (gen_what & GEN_D_FULL))
     succ = _write_deps(eos, outs[_get_bit_pos(GEN_D_FULL)], eobn, outs, gen_what);
   else if (succ && (gen_what & GEN_D))
     succ = _write_deps(eos, outs[_get_bit_pos(GEN_D)], eobn, outs, gen_what);

   if (!succ)
     goto end;

   pret = 0;
end:
   if (_eolian_gen_log_dom >= 0)
     {
        eina_log_timing(_eolian_gen_log_dom, EINA_LOG_STATE_START, EINA_LOG_STATE_SHUTDOWN);
        eina_log_domain_unregister(_eolian_gen_log_dom);
     }

   eina_list_free(includes);
   for (size_t i = 0; i < (sizeof(_dexts) / sizeof(char *)); ++i)
     free(outs[i]);
   free(basen);

   eolian_state_free(eos);
   eolian_shutdown();
   eina_shutdown();

   return pret;
}