summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/gem_helper.c
blob: 01daef3641853cef70f16b25914de797b42798ad (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
#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
     
static int
one (const struct dirent *unused)
{
  return 1;
}

void
dir_list (char before[1024], char after[1024], char start[1024], char end[1024])
{
  struct dirent **eps;
  int n;
  char gemname[1024] = "";
  char gemname_path[4096] = "";
  char complete_line[4096] = "";
  struct stat attribut;

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

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

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

      strcat(complete_line, before);
      strcat(complete_line, gemname);
      strcat(complete_line, after);

    }
  }
  else {
    perror("Error while scanning the directory.");
  }

  strcat(complete_line, end);
  puts(complete_line);
}

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

  puts(".PHONY : all");
  puts("all :");
  dir_list("\t@$(MAKE) -C ", " $(MAKE_FLAGS)\n", "", "");

  puts(".PHONY : test");
  puts("test :");
  dir_list("", "/test/*.rb ", "\tcat ../../test/assert.rb ", "> mrbtest.rbtmp");
  puts("\t../../bin/mrbc -Bmrbtest_irep -omrbtest.ctmp mrbtest.rbtmp");
  puts("\tcat ../../test/init_mrbtest.c mrbtest.ctmp > mrbtest.c");
  puts("\t$(CC) -c ../../test/driver.c -o ./driver.o $(CFLAGS)");
  puts("\t$(CC) -c ./mrbtest.c -o ./mrbtest.o $(CFLAGS)");
  puts("\t$(CC) -o ./mrbtest ./mrbtest.o ../../lib/libmruby.a ./driver.o $(CFLAGS) -lm");
  puts("\t./mrbtest");
  puts("");

  puts(".PHONY : clean");
  puts("clean :");
  puts("\t$(RM) *.c *.d *.rbtmp *.ctmp *.o mrbtest");
  dir_list("\t@$(MAKE) clean -C ", " $(MAKE_FLAGS)\n", "", "");
}

void
make_init_gems()
{
  puts("/*");
  puts(" * This file contains a list of all");
  puts(" * initializing methods which are");
  puts(" * necessary to bootstrap all gems.");
  puts(" *");
  puts(" * IMPORTANT:");
  puts(" *   This file was generated!");
  puts(" *   All manual changes will get lost.");
  puts(" */");

  puts("");
  puts("#include \"mruby.h\"");
  puts("");

  dir_list("void mrb_", "_gem_init(mrb_state*);\n", "", "");

  puts("void");
  puts("mrb_init_mrbgems(mrb_state *mrb)");
  puts("{");
  dir_list("  mrb_", "_gem_init(mrb);\n", "", "");
  puts("}");
}

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
      return 1;
  }
  else {
    puts("Argument missing! Options: 'makefile', 'init_gems'");
    return 1;
  }

  return 0;
}