summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/generator.c
blob: 430dcb354388cd4e27deaa65a34b0603b2a7d8b4 (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
#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <mrbconf.h>
#include <stdlib.h>
 
static int
one (const struct dirent *unused)
{
  return 1;
}

/*
 * Does a directory exist?
 *   yes => TRUE
 *   no => FALSE
 *   fs error => FALSE
 *
 */
static int
directory_exists(char path[4096]) {
  DIR* dir = opendir(path);
  if (dir)
    return TRUE;
  else
    return FALSE;
}

/*
 * Template generator for each GEM
 *
 * Arguments:
 *   before:
 *     String before each GEM template
 *   after:
 *     String after each GEM template
 *   start: 
 *     String at the start of the template
 *   end:
 *     String at the end of the template
 *   skip_if_src_not_exist:
 *     TRUE => skip template for GEMs with SRC directory
 *     FALSE => template for all GEMs
 *
 */
static char*
for_each_gem (char before[1024], char after[1024],
               char start[1024], char end[1024],
               char dir_to_skip[1024])
{
  struct dirent **eps;
  int n;
  char gemname[1024] = "";
  char gemname_path[4096] = "";
  char src_path[4096] = "";
  struct stat attribut;

  // return value
  char* complete_line = malloc(4096 + sizeof(char));

  strcat(complete_line, start);

  n = scandir("./g", &eps, one, alphasort);
  if (n >= 0) {
    int cnt;
    for (cnt = 0; cnt < n; ++cnt) {
      strcpy(gemname, eps[cnt]->d_name);
      strcpy(gemname_path, "./g/");
      strcat(gemname_path, gemname);
      strcpy(src_path, gemname_path);
      strcat(src_path, "/src");

      if (strcmp(gemname, ".") == 0)
        continue;
      if (strcmp(gemname, "..") == 0)
        continue;
      if (strcmp(gemname, ".gitignore") == 0)
        continue;

      stat(gemname_path, &attribut);
      if (S_ISDIR(attribut.st_mode) == 0) {
        continue;
      }

      if (strcmp(dir_to_skip, "") != 0) {
        strcpy(src_path, gemname_path);
        strcat(src_path, "/");
        strcat(src_path, dir_to_skip);

        if (directory_exists(src_path) != TRUE)
          continue;
      }

      strcat(complete_line, before);
      strcat(complete_line, gemname);
      strcat(complete_line, after);
    }
  }
  else {
    perror("Error while scanning the directory.");
  }

  strcat(complete_line, end);
  return complete_line;
}

/*
 * Gem Makefile Generator
 *
 */
void
make_gem_makefile()
{
  char *gem_check = "";
  int gem_empty;
  int gem_c_empty;
  int gem_ruby_empty;

  printf("CFLAGS := -I. -I../../include -I../../src\n\n"
         "ifeq ($(OS),Windows_NT)\n"
         "MAKE_FLAGS = --no-print-directory CC=$(CC) LL=$(LL) ALL_CFLAGS='$(ALL_CFLAGS)'\n"
         "else\n"
         "MAKE_FLAGS = --no-print-directory CC='$(CC)' LL='$(LL)' ALL_CFLAGS='$(ALL_CFLAGS)'\n"
         "endif\n\n");

  /* is there any GEM available? */
  gem_check = for_each_gem("", "", "", "", "");
  if (strcmp(gem_check, "") == 0)
    gem_empty = TRUE;
  else
    gem_empty = FALSE;

  /* is there a C extension available? */
  gem_check = for_each_gem("", "", "", "", "src");
  if (strcmp(gem_check, "") == 0)
    gem_c_empty = TRUE;
  else
    gem_c_empty = FALSE;

  /* is there a Ruby extension available */
  gem_check = for_each_gem("", "", "", "", "mrblib");
  if (strcmp(gem_check, "") == 0)
    gem_ruby_empty = TRUE;
  else
    gem_ruby_empty = FALSE;

  printf(".PHONY : all\n");
  if (gem_empty) {
    printf("all :\n\n");
  }
  else {
    if (gem_c_empty) {
      printf("all : mrblib_gem.o\n"
             "\t$(AR) rs ../../lib/libmruby.a mrblib_gem.o\n");
    }
    else if (gem_ruby_empty) {
      printf("all : all_gems\n");
    }
    else {
      printf("all : all_gems mrblib_gem.o\n"
             "\t$(AR) rs ../../lib/libmruby.a mrblib_gem.o\n");
    }

    printf("\n");

    // Rule for building all C extensions of each Gem
    if (!gem_c_empty) {
      printf("all_gems :\n%s\n", 
             for_each_gem("\t@$(MAKE) -C ", " $(MAKE_FLAGS)\n", "", "", "")
            );
    }

    // Rule for building all Ruby Extension of each Gem
    if (!gem_ruby_empty) {
      printf("mrblib_gem.o : mrblib_gem.c\n\n"

             "mrblib_gem.c : mrblib_gem.ctmp\n"
             "\tcat $< > $@\n\n"

             "mrblib_gem.ctmp : mrblib_gem.rbtmp\n"
             "\t../../bin/mrbc -Bmrblib_gem_irep -o$@ $<\n\n"

             "mrblib_gem.rbtmp :\n%s\n", 
             for_each_gem(" ", "/mrblib/*.rb", "\tcat", "> mrblib_gem.rbtmp", "mrblib")
            );
    }
  }

  printf("\n.PHONY : prepare-test\n"
         "prepare-test : mrbgemtest.ctmp\n\n"

         "mrbgemtest.ctmp : mrbgemtest.rbtmp\n"
         "\t../../bin/mrbc -Bmrbgemtest_irep -omrbgemtest.ctmp mrbgemtest.rbtmp\n\n"

         "mrbgemtest.rbtmp :\n"
        );

  if (!gem_empty)
    printf("%s",
           for_each_gem(" ", "/test/*.rb ", "\tcat", " > mrbgemtest.rbtmp", "")
          );
  else
    printf("\t../generator rbtmp > mrbgemtest.rbtmp\n");
    
  printf("\n\n.PHONY : clean\n"
         "clean :\n"
         "\t$(RM) *.c *.d *.rbtmp *.ctmp *.o mrbtest\n");

  if (!gem_empty)
    printf("%s",
           for_each_gem("\t@$(MAKE) clean -C ", " $(MAKE_FLAGS)\n", "", "", "")
          );
}

/*
 * init_gems.c Generator
 *
 */
void
make_init_gems()
{
  char *gem_check = "";
  int gem_empty;
  int gem_c_empty;
  int gem_ruby_empty;

  /* is there any GEM available? */
  gem_check = for_each_gem("", "", "", "", "");
  if (strcmp(gem_check, "") == 0)
    gem_empty = TRUE;
  else
    gem_empty = FALSE;

  /* is there a C extension available? */
  gem_check = for_each_gem("", "", "", "", "src");
  if (strcmp(gem_check, "") == 0)
    gem_c_empty = TRUE;
  else
    gem_c_empty = FALSE;

  /* is there a Ruby extension available */
  gem_check = for_each_gem("", "", "", "", "mrblib");
  if (strcmp(gem_check, "") == 0)
    gem_ruby_empty = TRUE;
  else
    gem_ruby_empty = FALSE;

  printf("/*\n"
         " * This file contains a list of all\n"
         " * initializing methods which are\n"
         " * necessary to bootstrap all gems.\n"
         " *\n"
         " * IMPORTANT:\n"
         " *   This file was generated!\n"
         " *   All manual changes will get lost.\n"
         " */\n\n"
         "#include \"mruby.h\"\n"
         "#include \"mruby/irep.h\"\n"
         "#include \"mruby/dump.h\"\n"
         "#include \"mruby/string.h\"\n"
         "#include \"mruby/proc.h\"\n");

  if (!gem_c_empty)
    printf("\n%s",
           for_each_gem("void mrb_", "_gem_init(mrb_state*);\n", "", "", "src")
          );

  if (!gem_ruby_empty)
    printf("\nextern const char mrblib_gem_irep[];\n");

  printf("\nvoid\n"
         "mrb_init_mrbgems(mrb_state *mrb) {\n");

  if (!gem_c_empty)
    printf("%s",
           for_each_gem("  mrb_", "_gem_init(mrb);\n", "", "", "src")
          );

  if (!gem_ruby_empty) {
    printf("  int n = mrb_read_irep(mrb, mrblib_gem_irep);\n"
           "  mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));\n"
           "  if (mrb->exc) {\n"
           "    mrb_p(mrb, mrb_obj_value(mrb->exc));\n"
           "    exit(0);\n"
           "  }\n");
  }

  printf("}");
}

void
make_rbtmp()
{
  printf("\n");
}

int
main (int argc, char *argv[])
{
  if (argc == 2) {
    if (strcmp(argv[1], "makefile") == 0)
      make_gem_makefile();
    else if (strcmp(argv[1], "init_gems") == 0)
      make_init_gems();
    else if (strcmp(argv[1], "rbtmp") == 0)
      make_rbtmp();
    else
      return 1;
  }
  else {
    printf("Argument missing! Options: 'makefile', 'init_gems', 'rbtmp'");
    return 1;
  }

  return 0;
}