Commit 74121d62 authored by Daniel Bovensiepen's avatar Daniel Bovensiepen

Clean GEM Generator a bit more

parent d1efcb4c
...@@ -30,7 +30,7 @@ directory_exists(char path[4096]) { ...@@ -30,7 +30,7 @@ directory_exists(char path[4096]) {
} }
/* /*
* Template generator for each GEM * Template generator for each active GEM
* *
* Arguments: * Arguments:
* before: * before:
...@@ -50,17 +50,18 @@ for_each_gem (char before[1024], char after[1024], ...@@ -50,17 +50,18 @@ for_each_gem (char before[1024], char after[1024],
char start[1024], char end[1024], char start[1024], char end[1024],
char dir_to_skip[1024]) char dir_to_skip[1024])
{ {
FILE *fp; /* active GEM check */
char *active_gems = NULL; FILE *active_gem_file;
char ch; char gem_char;
char gem_name[1024] = { 0 }; char gem_name[1024] = { 0 };
int char_index; int char_index;
char gem_list[1024][1024] = { { 0 }, { 0 } }; char gem_list[1024][1024] = { { 0 }, { 0 } };
int gem_index; int gem_index;
int i; int i;
int b; int gem_active;
int cnt; int cnt;
/* folder check */
struct dirent **eps; struct dirent **eps;
int n; int n;
char gemname[1024] = { 0 }; char gemname[1024] = { 0 };
...@@ -68,39 +69,47 @@ for_each_gem (char before[1024], char after[1024], ...@@ -68,39 +69,47 @@ for_each_gem (char before[1024], char after[1024],
char src_path[4096] = { 0 }; char src_path[4096] = { 0 };
struct stat attribut; struct stat attribut;
fp = fopen("GEMS.active", "r+"); /* return value */
if (fp != NULL) { char* complete_line = malloc(4096 + sizeof(char));
strcpy(complete_line, "");
strcat(complete_line, start);
/* Read out the active GEMs */
active_gem_file = fopen("GEMS.active", "r+");
if (active_gem_file != NULL) {
char_index = 0; char_index = 0;
gem_index = 0; gem_index = 0;
while((ch = fgetc(fp)) != EOF) { while((gem_char = fgetc(active_gem_file)) != EOF) {
if (ch == '\n') { if (gem_char == '\n') {
/* Every line contains one active GEM */
gem_name[char_index++] = '\0'; gem_name[char_index++] = '\0';
strcpy(gem_list[gem_index++], gem_name); strcpy(gem_list[gem_index++], gem_name);
gem_name[0] = '\0'; gem_name[0] = '\0';
char_index = 0; char_index = 0;
} }
else { else
gem_name[char_index++] = ch; gem_name[char_index++] = gem_char;
} }
if (gem_index > 0) {
/* clean close of the last GEM name */
gem_name[char_index++] = '\0';
strcpy(gem_list[gem_index++], gem_name);
} }
fclose(fp); fclose(active_gem_file);
} }
else { /* Error: Active GEM list couldn't be loaded */ } else { /* Error: Active GEM list couldn't be loaded */ }
/* return value */
char* complete_line = malloc(4096 + sizeof(char));
strcpy(complete_line, "");
strcat(complete_line, start);
n = scandir("./g", &eps, one, alphasort); n = scandir("./g", &eps, one, alphasort);
if (n >= 0) { if (n >= 0) {
/* iterate over each file and figure out what is a GEM and what not */
for (cnt = 0; cnt < n; ++cnt) { for (cnt = 0; cnt < n; ++cnt) {
strcpy(gemname, eps[cnt]->d_name); strcpy(gemname, eps[cnt]->d_name);
strcpy(gemname_path, "./g/"); strcpy(gemname_path, "./g/");
strcat(gemname_path, gemname); strcat(gemname_path, gemname);
/* we ignore all the default files */
if (strcmp(gemname, ".") == 0) if (strcmp(gemname, ".") == 0)
continue; continue;
if (strcmp(gemname, "..") == 0) if (strcmp(gemname, "..") == 0)
...@@ -108,26 +117,30 @@ for_each_gem (char before[1024], char after[1024], ...@@ -108,26 +117,30 @@ for_each_gem (char before[1024], char after[1024],
if (strcmp(gemname, ".gitignore") == 0) if (strcmp(gemname, ".gitignore") == 0)
continue; continue;
/* In case the current location isn't a folder we skip it */
stat(gemname_path, &attribut); stat(gemname_path, &attribut);
if (S_ISDIR(attribut.st_mode) == 0) { if (S_ISDIR(attribut.st_mode) == 0) {
continue; continue;
} }
b = 0; /* Check if user has activated this GEM */
gem_active = 0;
for(i = 0; i <= gem_index; i++) { for(i = 0; i <= gem_index; i++) {
if (strcmp(gem_list[i], gemname) != 0) if (strcmp(gem_list[i], gemname) != 0)
b = 0; gem_active = FALSE;
else { else {
/* Current GEM is active */ /* Current GEM is active */
b = 1; gem_active = TRUE;
break; break;
} }
} }
/* In case the current GEM isn't active we skip it */ /* In case the current GEM isn't active we skip it */
if (b == 0) if (gem_active == FALSE)
continue; continue;
/* sometimes we are only interested in GEMs
with a specific folder.
*/
if (strcmp(dir_to_skip, "") != 0) { if (strcmp(dir_to_skip, "") != 0) {
strcpy(src_path, gemname_path); strcpy(src_path, gemname_path);
strcat(src_path, "/"); strcat(src_path, "/");
...@@ -153,6 +166,9 @@ for_each_gem (char before[1024], char after[1024], ...@@ -153,6 +166,9 @@ for_each_gem (char before[1024], char after[1024],
/* /*
* Gem Makefile Generator * Gem Makefile Generator
* *
* Global Makefile which starts the build process
* for every active GEM.
*
*/ */
void void
make_gem_makefile() make_gem_makefile()
...@@ -174,21 +190,24 @@ make_gem_makefile() ...@@ -174,21 +190,24 @@ make_gem_makefile()
else else
gem_empty = FALSE; gem_empty = FALSE;
/* Makefile Rules to build every single GEM */
printf(".PHONY : all\n"); printf(".PHONY : all\n");
if (gem_empty) if (gem_empty)
printf("all :\n\n"); printf("all :\n\n");
else { else {
printf("all : all_gems\n"); printf("all : all_gems\n\n");
printf("\n"); /* Call make for every GEM */
/* Rule to make every GEM */
printf("all_gems :\n%s\n", printf("all_gems :\n%s\n",
for_each_gem("\t@$(MAKE) -C ", " $(MAKE_FLAGS)\n", "", "", "") for_each_gem("\t@$(MAKE) -C ", " $(MAKE_FLAGS)\n", "", "", "")
); );
printf("\n");
} }
printf("\n.PHONY : prepare-test\n" /* Makefile Rules to Test GEMs */
printf(".PHONY : prepare-test\n"
"prepare-test :\n" "prepare-test :\n"
); );
if (!gem_empty) if (!gem_empty)
...@@ -197,8 +216,11 @@ make_gem_makefile() ...@@ -197,8 +216,11 @@ make_gem_makefile()
); );
else else
printf("\t../generator rbtmp > mrbgemtest.rbtmp"); printf("\t../generator rbtmp > mrbgemtest.rbtmp");
printf("\n\t../../bin/mrbc -Bmrbgemtest_irep -omrbgemtest.ctmp mrbgemtest.rbtmp\n\n"); printf("\n\t../../bin/mrbc -Bmrbgemtest_irep -omrbgemtest.ctmp mrbgemtest.rbtmp\n\n");
/* Makefile Rules to Clean GEMs */
printf(".PHONY : clean\n" printf(".PHONY : clean\n"
"clean :\n" "clean :\n"
"\t$(RM) *.c *.d *.rbtmp *.ctmp *.o mrbtest\n"); "\t$(RM) *.c *.d *.rbtmp *.ctmp *.o mrbtest\n");
...@@ -211,6 +233,9 @@ make_gem_makefile() ...@@ -211,6 +233,9 @@ make_gem_makefile()
/* /*
* Gem Makefile List Generator * Gem Makefile List Generator
* *
* Creates a Makefile which will be included by other Makefiles
* which need to know which GEMs are active.
*
*/ */
void void
make_gem_makefile_list() make_gem_makefile_list()
...@@ -225,11 +250,11 @@ make_gem_makefile_list() ...@@ -225,11 +250,11 @@ make_gem_makefile_list()
} }
/* /*
* init_gems.c Generator * gem_init.c Generator
* *
*/ */
void void
make_init_gems() make_gem_init()
{ {
printf("/*\n" printf("/*\n"
" * This file contains a list of all\n" " * This file contains a list of all\n"
...@@ -246,9 +271,10 @@ make_init_gems() ...@@ -246,9 +271,10 @@ make_init_gems()
printf("\n%s", printf("\n%s",
for_each_gem("void GENERATED_TMP_mrb_", "_gem_init(mrb_state*);\n", "", "", "") for_each_gem("void GENERATED_TMP_mrb_", "_gem_init(mrb_state*);\n", "", "", "")
); );
printf("\n");
/* mrb_init_mrbgems(mrb) method for initialization of all GEMs */ /* mrb_init_mrbgems(mrb) method for initialization of all GEMs */
printf("\nvoid\n" printf("void\n"
"mrb_init_mrbgems(mrb_state *mrb) {\n"); "mrb_init_mrbgems(mrb_state *mrb) {\n");
printf( "%s", printf( "%s",
for_each_gem(" GENERATED_TMP_mrb_", "_gem_init(mrb);\n", "", "", "") for_each_gem(" GENERATED_TMP_mrb_", "_gem_init(mrb);\n", "", "", "")
...@@ -256,12 +282,24 @@ make_init_gems() ...@@ -256,12 +282,24 @@ make_init_gems()
printf("}"); printf("}");
} }
/*
* Empty Generator
*
* Generates a clean file.
*
*/
void void
make_rbtmp() make_rbtmp()
{ {
printf("\n"); printf("\n");
} }
/*
* Header Generator
*
* Head of the C Code for loading the GEMs into the interpreter.
*
*/
void void
make_gem_mrblib_header() make_gem_mrblib_header()
{ {
...@@ -280,6 +318,13 @@ make_gem_mrblib_header() ...@@ -280,6 +318,13 @@ make_gem_mrblib_header()
"#include \"mruby/proc.h\"\n\n"); "#include \"mruby/proc.h\"\n\n");
} }
/*
* mrblib Generator
*
* Generates the C Code for loading
* the pure Ruby GEMs into the interpreter.
*
*/
void void
make_gem_mrblib(char argv[1024]) make_gem_mrblib(char argv[1024])
{ {
...@@ -295,6 +340,13 @@ make_gem_mrblib(char argv[1024]) ...@@ -295,6 +340,13 @@ make_gem_mrblib(char argv[1024])
"}", argv, argv); "}", argv, argv);
} }
/*
* srclib Generator
*
* Generates the C Code for loading
* the pure C GEMs into the interpreter.
*
*/
void void
make_gem_srclib(char argv[1024]) make_gem_srclib(char argv[1024])
{ {
...@@ -318,6 +370,14 @@ make_gem_srclib(char argv[1024]) ...@@ -318,6 +370,14 @@ make_gem_srclib(char argv[1024])
"}", argv, argv); "}", argv, argv);
} }
/*
* mixlib Generator
*
* Generates the C Code for loading
* the mixed Ruby and C GEMs
* into the interpreter.
*
*/
void void
make_gem_mixlib(char argv[1024]) make_gem_mixlib(char argv[1024])
{ {
...@@ -337,22 +397,27 @@ make_gem_mixlib(char argv[1024]) ...@@ -337,22 +397,27 @@ make_gem_mixlib(char argv[1024])
"}", argv, argv, argv); "}", argv, argv, argv);
} }
/*
* Start the generator and decide what to generate.
*
*/
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
const char * argument_info = "Wrong argument! Options: 'makefile', 'gem_init', 'rbtmp', 'gem_mrblib', gem_srclib\n";
if (argc == 2) { if (argc == 2) {
if (strcmp(argv[1], "makefile") == 0) if (strcmp(argv[1], "makefile") == 0)
make_gem_makefile(); make_gem_makefile();
else if (strcmp(argv[1], "makefile_list") == 0) else if (strcmp(argv[1], "makefile_list") == 0)
make_gem_makefile_list(); make_gem_makefile_list();
else if (strcmp(argv[1], "init_gems") == 0) else if (strcmp(argv[1], "gem_init") == 0)
make_init_gems(); make_gem_init();
else if (strcmp(argv[1], "rbtmp") == 0) else if (strcmp(argv[1], "rbtmp") == 0)
make_rbtmp(); make_rbtmp();
else if (strcmp(argv[1], "gem_mrblib") == 0) else if (strcmp(argv[1], "gem_mrblib") == 0)
make_gem_mrblib_header(); make_gem_mrblib_header();
else { else {
printf("Wrong argument! Options: 'makefile', 'init_gems', 'rbtmp', 'gem_mrblib', gem_srclib\n"); printf("%s", argument_info);
return 1; return 1;
} }
} }
...@@ -364,12 +429,12 @@ main (int argc, char *argv[]) ...@@ -364,12 +429,12 @@ main (int argc, char *argv[])
else if (strcmp(argv[1], "gem_mixlib") == 0) else if (strcmp(argv[1], "gem_mixlib") == 0)
make_gem_mixlib(argv[2]); make_gem_mixlib(argv[2]);
else { else {
printf("Wrong argument! Options: 'makefile', 'init_gems', 'rbtmp', 'gem_mrblib', gem_srclib\n"); printf("%s", argument_info);
return 1; return 1;
} }
} }
else { else {
printf("Argument missing! Options: 'makefile', 'init_gems', 'rbtmp', 'gem_mrblib, gem_srclib'\n"); printf("%s", argument_info);
return 1; return 1;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment