Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add new source file to lua bindings code, having possibility to use a 'lua' version...
[simgrid.git] / src / bindings / lua / lua_stub_generator.c
1 /* gras_stub_generator - creates the main() to use a GRAS program           */
2
3 /* Copyright (c) 2005, 2006, 2007, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "lua_stub_generator.h"
10
11 #include <stdio.h>
12 #include "xbt/sysdep.h"
13 #include "xbt/function_types.h"
14 #include "xbt/log.h"
15 #include "surf/surfxml_parse.h"
16 #include "surf/surf.h"
17 #include "portable.h"           /* Needed for the time of the SIMIX convertion */
18 #include <stdarg.h>
19
20 #include <lua.h>
21 #include <lauxlib.h>
22 #include <lualib.h>
23
24 //XBT_LOG_NEW_DEFAULT_SUBCATEGORY(stubgen, gras, "lua Stub generator");
25
26 //to geretae associed files
27 //XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(stubgen);
28
29
30 #define WARN "/***********\n * DO NOT EDIT! THIS FILE HAS BEEN AUTOMATICALLY GENERATED FROM %s BY gras_stub_generator\n ***********/\n"
31 #define SIM_SOURCENAME  "_%s_simulator.c"
32 #define SIM_OBJNAME  "_%s_simulator.o"
33 #define SIM_BINARYNAME  "%s_simulator"
34 #define SIM_SOURCENAME_LDADD  "%s_simulator_LDADD"
35 #define SIM_SOURCENAME_SOURCES  "%s_simulator_SOURCES"
36 #define RL_SOURCENAME  "_%s_%s.c"
37 #define RL_OBJNAME  "_%s_%s.o"
38 #define RL_BINARYNAME  "%s_%s"
39 #define RL_SOURCENAME_LDADD  "%s_%s_LDADD"
40 #define RL_SOURCENAME_SOURCES  "%s_%s_SOURCES"
41 #define MAKEFILE_FILENAME_AM  "%s.Makefile.am"
42 #define MAKEFILE_FILENAME_LOCAL  "%s.mk"
43 #define MAKEFILE_FILENAME_REMOTE  "%s.Makefile.remote"
44 #define DEPLOYMENT  "%s.deploy.sh"
45
46 char *warning = NULL;
47
48 /**********************************************/
49 /**** Generate the file for the simulator *****/
50 /**********************************************/
51
52 const char *SIM_PREEMBULE =
53   "/* specific to Borland Compiler */\n"
54   "#ifdef __BORLANDC__\n"
55   "#pragma hdrstop\n"
56   "#endif\n\n"
57   "#include <stdlib.h>\n"
58   "#include <stdio.h>\n"
59   "#include \"msg/msg.h\"\n"
60   "#include <gras.h>\n" "\n" "char *gras_log=NULL;\n";
61
62
63 #define SIM_LAUNCH_FUNC  \
64 "int launch_%s(int argc, char **argv) {\n" \
65 "  char **myargv=argv;\n" \
66 "  int myargc=argc;\n" \
67 "  int i;\n" \
68 "  int retcode;\n"\
69 "    \n"\
70 "  if (gras_log) {\n"\
71 "    myargv=malloc((argc+1) * sizeof(char**));\n" \
72 "    for (i=0; i<argc; i++)\n" \
73 "      myargv[i] = argv[i];\n" \
74 "    myargv[myargc++] = gras_log;\n" \
75 "  }\n" \
76 "  retcode = %s(myargc,myargv);\n" \
77 "  if (myargv != argv)\n" \
78 "    free(myargv);\n" \
79 "  return retcode;\n" \
80 "}\n"
81
82 const char *SIM_MAIN_POSTEMBULE = "\n"
83   "\n"
84   "  gras_load_platform_script(argv[1]);\n"
85   "\n"
86   "  /*  Run the simulation */\n"
87   "  gras_main();\n"
88   "\n"
89   "  /* cleanup the place */\n"
90   "  gras_clean();\n"
91   "  if (gras_log)\n" "    free(gras_log);\n" "  return 0;\n" "}\n";
92
93
94 /***************************************
95  * generator functions
96  ***************************************/
97
98
99
100 void generate_sim(const char *project)
101 {
102   xbt_dict_cursor_t cursor = NULL;
103   char *key = NULL;
104   void *data = NULL;
105   char *filename = NULL;
106   FILE *OUT = NULL;
107
108   /* Output file: <projet>_simulator.c */
109   filename = xbt_new(char, strlen(project) + strlen(SIM_SOURCENAME));
110   sprintf(filename, SIM_SOURCENAME, project);
111
112   OUT = fopen(filename, "w");
113
114   xbt_assert1(OUT, "Unable to open %s for writing", filename);
115
116   fprintf(OUT, "%s", SIM_PREEMBULE);
117
118   xbt_dict_foreach(process_function_set, cursor, key, data) {
119     fprintf(OUT, "int %s(int argc,char *argv[]);\n", key);
120   }
121
122   fprintf(OUT, "\n");
123
124   xbt_dict_foreach(process_function_set, cursor, key, data) {
125     fprintf(OUT, "int launch_%s(int argc,char *argv[]);\n", key);
126   }
127
128
129   xbt_dict_foreach(process_function_set, cursor, key, data) {
130     fprintf(OUT, SIM_LAUNCH_FUNC, key, key);
131   }
132   fprintf(OUT, "\n%s\n", warning);
133
134   fprintf(OUT, "%s", "/* specific to Borland Compiler */\n"
135           "#ifdef __BORLANDDC__\n" "#pragma argsused\n" "#endif\n\n");
136
137   fprintf(OUT, "%s", "int main (int argc,char *argv[]) {\n"
138           "\n"
139           "  /*  Simulation setup */\n"
140           "  gras_global_init(&argc,argv);\n"
141           "  if (argc != 2) {\n"
142           "    fprintf(stderr, \"Usage: lua platform_script.lua [--log=...]\\n\");\n"
143           "    exit(1);\n" "  }\n" "\n");
144   fprintf(OUT,
145           "  gras_load_platform_script(argv[1]);\n"
146           "\n" "  /*  Application deployment */\n");
147   xbt_dict_foreach(process_function_set, cursor, key, data) {
148     fprintf(OUT, "  gras_function_register(\"%s\", launch_%s);\n", key, key);
149   }
150   fprintf(OUT, "%s", SIM_MAIN_POSTEMBULE);
151   fclose(OUT);
152   free(filename);
153 }
154
155 /**********************************************/
156 /**** Generate the file for the real life *****/
157 /**********************************************/
158 void generate_rl(const char *project)
159 {
160   xbt_dict_cursor_t cursor = NULL;
161   char *key = NULL;
162   void *data = NULL;
163   char *filename = NULL;
164   FILE *OUT = NULL;
165
166   xbt_dict_foreach(process_function_set, cursor, key, data) {
167     filename =
168       xbt_new(char, strlen(project) + strlen(RL_SOURCENAME) + strlen(key));
169
170     sprintf(filename, RL_SOURCENAME, project, key);
171
172     OUT = fopen(filename, "w");
173     xbt_assert1(OUT, "Unable to open %s for writing", filename);
174
175     fprintf(OUT, "\n%s\n", warning);
176     fprintf(OUT, "/* specific to Borland Compiler */\n"
177             "#ifdef __BORLANDC__\n"
178             "#pragma hdrstop\n"
179             "#endif\n\n"
180             "#include <stdio.h>\n"
181             "#include <signal.h>\n"
182             "#include <gras.h>\n"
183             "\n"
184             "XBT_PUBLIC_DATA(const char *) _gras_procname;\n"
185             "/* user code */\n"
186             "int %s(int argc, char *argv[]);\n"
187             "\n"
188             "/* specific to Borland Compiler */\n"
189             "#ifdef __BORLANDC__\n"
190             "#pragma argsused\n"
191             "#endif\n\n"
192             "int main(int argc, char *argv[]){\n"
193             "  int errcode;\n"
194             "\n"
195             "  _gras_procname = \"%s\";\n"
196             "  errcode=%s(argc,argv);\n"
197             " \n" "  return errcode;\n" "}\n", key, key, key);
198     fprintf(OUT, "\n%s\n", warning);
199     fclose(OUT);
200     free(filename);
201   }
202 }
203
204 void generate_makefile_am(const char *project)
205 {
206   xbt_dict_cursor_t cursor = NULL;
207   char *key = NULL;
208   void *data = NULL;
209   char *filename = NULL;
210   FILE *OUT = NULL;
211
212   filename = xbt_new(char, strlen(project) + strlen(MAKEFILE_FILENAME_AM));
213   sprintf(filename, MAKEFILE_FILENAME_AM, project);
214
215   OUT = fopen(filename, "w");
216   xbt_assert1(OUT, "Unable to open %s for writing", filename);
217
218   fprintf(OUT, "# AUTOMAKE variable definition\n");
219   fprintf(OUT, "INCLUDES= @CFLAGS_SimGrid@\n\n");
220   fprintf(OUT, "PROGRAMS=");
221   fprintf(OUT, SIM_BINARYNAME, project);
222
223   xbt_dict_foreach(process_function_set, cursor, key, data) {
224     fprintf(OUT, " ");
225     fprintf(OUT, RL_BINARYNAME, project, key);
226   }
227
228   fprintf(OUT, "\n\n");
229   fprintf(OUT, SIM_SOURCENAME_SOURCES, project);
230   fprintf(OUT, "=\t");
231   fprintf(OUT, SIM_SOURCENAME, project);
232   fprintf(OUT, " %s.c\n", project);
233   fprintf(OUT, SIM_SOURCENAME_LDADD, project);
234   fprintf(OUT, "=\tpath/to/libsimgrid.a\n\n");
235
236   xbt_dict_foreach(process_function_set, cursor, key, data) {
237     fprintf(OUT, RL_SOURCENAME_SOURCES, project, key);
238     fprintf(OUT, "=\t");
239     fprintf(OUT, RL_SOURCENAME, project, key);
240     fprintf(OUT, " %s.c\n", project);
241     fprintf(OUT, RL_SOURCENAME_LDADD, project, key);
242     fprintf(OUT, "=\tpath/to/libgras.a\n\n");
243   }
244
245   fprintf(OUT,
246           "\n# cleanup temps (allowing the user to add extra clean files)\n");
247   fprintf(OUT, "CLEANFILES?= \n");
248   fprintf(OUT, "CLEANFILES+= ");
249   fprintf(OUT, SIM_SOURCENAME, project);
250
251   xbt_dict_foreach(process_function_set, cursor, key, data) {
252     fprintf(OUT, " ");
253     fprintf(OUT, RL_SOURCENAME, project, key);
254   }
255   fprintf(OUT, "\n");
256
257   fprintf(OUT, "\n# generate temps\n");
258   fprintf(OUT,
259           "\n# A rule to generate the source file each time the deployment file changes\n");
260
261   xbt_dict_foreach(process_function_set, cursor, key, data) {
262     fprintf(OUT, RL_SOURCENAME, project, key);
263     fprintf(OUT, " ");
264   }
265   fprintf(OUT, SIM_SOURCENAME, project);
266   /*fprintf(OUT, ": %s\n", deployment);
267   fprintf(OUT, "\tgras_stub_generator %s %s >/dev/null\n", project,
268           deployment);*/
269   fclose(OUT);
270 }
271
272 void generate_makefile_local(const char *project)
273 {
274   xbt_dict_cursor_t cursor = NULL;
275   char *key = NULL;
276   void *data = NULL;
277   char *filename = NULL;
278   FILE *OUT = NULL;
279
280   filename = xbt_new(char, strlen(project) + strlen(MAKEFILE_FILENAME_LOCAL));
281   sprintf(filename, MAKEFILE_FILENAME_LOCAL, project);
282
283   OUT = fopen(filename, "w");
284   xbt_assert1(OUT, "Unable to open %s for writing", filename);
285   free(filename);
286
287   fprintf(OUT,
288           "\n"
289           "####\n"
290           "#### THIS FILE WAS GENERATED, DO NOT EDIT BEFORE RENAMING IT\n"
291           "####\n\n\n");
292
293   fprintf(OUT, "## Variable declarations\n"
294           "PROJECT_NAME=%s\n" "DISTDIR=gras-$(PROJECT_NAME)\n\n", project);
295
296   fprintf(OUT,
297           "# Set the GRAS_ROOT environment variable to the path under which you installed SimGrid\n"
298           "# Compilation will fail if you don't do so\n"
299           "GRAS_ROOT?= $(shell if [ -e /usr/local/lib/libgras.so ] ; then echo /usr/local ; else echo \"\\\"<<<< GRAS_ROOT undefined !!! >>>>\\\"\"; fi)\n\n"
300           "# You can fiddle the following to make it fit your taste\n"
301           "INCLUDES = -I$(GRAS_ROOT)/include\n"
302           "CFLAGS ?= -O3 -w -g -Wall\n"
303           "LIBS_SIM = -lm  -L$(GRAS_ROOT)/lib/ -lsimgrid\n"
304           "LIBS_RL = -lm  -L$(GRAS_ROOT)/lib/ -lgras\n" "LIBS = \n" "\n");
305
306   fprintf(OUT, "PRECIOUS_C_FILES ?= %s.c\n", project);
307
308   fprintf(OUT, "GENERATED_C_FILES = ");
309   fprintf(OUT, SIM_SOURCENAME " ", project);
310   xbt_dict_foreach(process_function_set, cursor, key, data) {
311     fprintf(OUT, RL_SOURCENAME " ", project, key);
312   }
313   fprintf(OUT, "\n");
314
315   fprintf(OUT, "OBJ_FILES = $(patsubst %%.c,%%.o,$(PRECIOUS_C_FILES))\n");
316
317   fprintf(OUT, "BIN_FILES = ");
318
319   fprintf(OUT, SIM_BINARYNAME " ", project);
320   xbt_dict_foreach(process_function_set, cursor, key, data) {
321     fprintf(OUT, RL_BINARYNAME " ", project, key);
322   }
323   fprintf(OUT, "\n");
324
325   fprintf(OUT,
326           "\n"
327           "## By default, build all the binaries\n"
328           "all: $(BIN_FILES)\n" "\n");
329
330   //No Need to generate source files, already done.
331
332   fprintf(OUT, "\n## Generate the binaries\n");
333   fprintf(OUT, SIM_BINARYNAME ": " SIM_OBJNAME " $(OBJ_FILES)\n", project,
334           project);
335   fprintf(OUT,
336           "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS_SIM) $(LIBS) $(LDADD) -o $@ \n");
337   xbt_dict_foreach(process_function_set, cursor, key, data) {
338     fprintf(OUT, RL_BINARYNAME " : " RL_OBJNAME " $(OBJ_FILES)\n", project,
339             key, project, key);
340     fprintf(OUT,
341             "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS_RL) $(LIBS) $(LDADD) -o $@ \n");
342   }
343   fprintf(OUT,
344           "\n"
345           "%%: %%.o\n"
346           "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS) $(LDADD) -o $@ \n"
347           "\n"
348           "%%.o: %%.c\n"
349           "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) -c -o $@ $<\n" "\n");
350
351   fprintf(OUT,
352           "##┬áRules for tarballs and cleaning\n"
353           "DIST_FILES= $(EXTRA_DIST) $(GENERATED_C_FILES) $(PRECIOUS_C_FILES) "
354           MAKEFILE_FILENAME_LOCAL " " /*MAKEFILE_FILENAME_REMOTE */ "\n"
355           "distdir: $(DIST_FILES)\n" "\trm -rf $(DISTDIR)\n"
356           "\tmkdir -p $(DISTDIR)\n" "\tcp $^ $(DISTDIR)\n" "\n"
357           "dist: clean distdir\n"
358           "\ttar c $(DISTDIR) | gzip -c9 > $(DISTDIR).tar.gz\n" "\n",
359           project /*, project */ );
360
361   fprintf(OUT,
362           "clean:\n"
363           "\trm -f $(CLEANFILES) $(BIN_FILES) $(OBJ_FILES) *~ %s.o "
364           SIM_OBJNAME, project, project);
365   xbt_dict_foreach(process_function_set, cursor, key, data) {
366     fprintf(OUT, " " RL_OBJNAME, project, key);
367   }
368   fprintf(OUT,
369           "\n"
370           "\trm -rf $(DISTDIR)\n"
371           "\n" ".SUFFIXES:\n" ".PHONY : clean\n" "\n");
372   fclose(OUT);
373 }
374
375 static void print(void *p)
376 {
377   printf("%p", p);
378 }