Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove duplicated line
[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           "\n" "  /*  Application deployment */\n");
146   xbt_dict_foreach(process_function_set, cursor, key, data) {
147     fprintf(OUT, "  gras_function_register(\"%s\", launch_%s);\n", key, key);
148   }
149   fprintf(OUT, "%s", SIM_MAIN_POSTEMBULE);
150   fclose(OUT);
151   free(filename);
152 }
153
154 /**********************************************/
155 /**** Generate the file for the real life *****/
156 /**********************************************/
157 void generate_rl(const char *project)
158 {
159   xbt_dict_cursor_t cursor = NULL;
160   char *key = NULL;
161   void *data = NULL;
162   char *filename = NULL;
163   FILE *OUT = NULL;
164
165   xbt_dict_foreach(process_function_set, cursor, key, data) {
166     filename =
167       xbt_new(char, strlen(project) + strlen(RL_SOURCENAME) + strlen(key));
168
169     sprintf(filename, RL_SOURCENAME, project, key);
170
171     OUT = fopen(filename, "w");
172     xbt_assert1(OUT, "Unable to open %s for writing", filename);
173
174     fprintf(OUT, "\n%s\n", warning);
175     fprintf(OUT, "/* specific to Borland Compiler */\n"
176             "#ifdef __BORLANDC__\n"
177             "#pragma hdrstop\n"
178             "#endif\n\n"
179             "#include <stdio.h>\n"
180             "#include <signal.h>\n"
181             "#include <gras.h>\n"
182             "\n"
183             "XBT_PUBLIC_DATA(const char *) _gras_procname;\n"
184             "/* user code */\n"
185             "int %s(int argc, char *argv[]);\n"
186             "\n"
187             "/* specific to Borland Compiler */\n"
188             "#ifdef __BORLANDC__\n"
189             "#pragma argsused\n"
190             "#endif\n\n"
191             "int main(int argc, char *argv[]){\n"
192             "  int errcode;\n"
193             "\n"
194             "  _gras_procname = \"%s\";\n"
195             "  errcode=%s(argc,argv);\n"
196             " \n" "  return errcode;\n" "}\n", key, key, key);
197     fprintf(OUT, "\n%s\n", warning);
198     fclose(OUT);
199     free(filename);
200   }
201 }
202
203 void generate_makefile_am(const char *project)
204 {
205   xbt_dict_cursor_t cursor = NULL;
206   char *key = NULL;
207   void *data = NULL;
208   char *filename = NULL;
209   FILE *OUT = NULL;
210
211   filename = xbt_new(char, strlen(project) + strlen(MAKEFILE_FILENAME_AM));
212   sprintf(filename, MAKEFILE_FILENAME_AM, project);
213
214   OUT = fopen(filename, "w");
215   xbt_assert1(OUT, "Unable to open %s for writing", filename);
216
217   fprintf(OUT, "# AUTOMAKE variable definition\n");
218   fprintf(OUT, "INCLUDES= @CFLAGS_SimGrid@\n\n");
219   fprintf(OUT, "PROGRAMS=");
220   fprintf(OUT, SIM_BINARYNAME, project);
221
222   xbt_dict_foreach(process_function_set, cursor, key, data) {
223     fprintf(OUT, " ");
224     fprintf(OUT, RL_BINARYNAME, project, key);
225   }
226
227   fprintf(OUT, "\n\n");
228   fprintf(OUT, SIM_SOURCENAME_SOURCES, project);
229   fprintf(OUT, "=\t");
230   fprintf(OUT, SIM_SOURCENAME, project);
231   fprintf(OUT, " %s.c\n", project);
232   fprintf(OUT, SIM_SOURCENAME_LDADD, project);
233   fprintf(OUT, "=\tpath/to/libsimgrid.a\n\n");
234
235   xbt_dict_foreach(process_function_set, cursor, key, data) {
236     fprintf(OUT, RL_SOURCENAME_SOURCES, project, key);
237     fprintf(OUT, "=\t");
238     fprintf(OUT, RL_SOURCENAME, project, key);
239     fprintf(OUT, " %s.c\n", project);
240     fprintf(OUT, RL_SOURCENAME_LDADD, project, key);
241     fprintf(OUT, "=\tpath/to/libgras.a\n\n");
242   }
243
244   fprintf(OUT,
245           "\n# cleanup temps (allowing the user to add extra clean files)\n");
246   fprintf(OUT, "CLEANFILES?= \n");
247   fprintf(OUT, "CLEANFILES+= ");
248   fprintf(OUT, SIM_SOURCENAME, project);
249
250   xbt_dict_foreach(process_function_set, cursor, key, data) {
251     fprintf(OUT, " ");
252     fprintf(OUT, RL_SOURCENAME, project, key);
253   }
254   fprintf(OUT, "\n");
255
256   fprintf(OUT, "\n# generate temps\n");
257   fprintf(OUT,
258           "\n# A rule to generate the source file each time the deployment file changes\n");
259
260   xbt_dict_foreach(process_function_set, cursor, key, data) {
261     fprintf(OUT, RL_SOURCENAME, project, key);
262     fprintf(OUT, " ");
263   }
264   fprintf(OUT, SIM_SOURCENAME, project);
265   /*fprintf(OUT, ": %s\n", deployment);
266   fprintf(OUT, "\tgras_stub_generator %s %s >/dev/null\n", project,
267           deployment);*/
268   fclose(OUT);
269 }
270
271 void generate_makefile_local(const char *project)
272 {
273   xbt_dict_cursor_t cursor = NULL;
274   char *key = NULL;
275   void *data = NULL;
276   char *filename = NULL;
277   FILE *OUT = NULL;
278
279   filename = xbt_new(char, strlen(project) + strlen(MAKEFILE_FILENAME_LOCAL));
280   sprintf(filename, MAKEFILE_FILENAME_LOCAL, project);
281
282   OUT = fopen(filename, "w");
283   xbt_assert1(OUT, "Unable to open %s for writing", filename);
284   free(filename);
285
286   fprintf(OUT,
287           "\n"
288           "####\n"
289           "#### THIS FILE WAS GENERATED, DO NOT EDIT BEFORE RENAMING IT\n"
290           "####\n\n\n");
291
292   fprintf(OUT, "## Variable declarations\n"
293           "PROJECT_NAME=%s\n" "DISTDIR=gras-$(PROJECT_NAME)\n\n", project);
294
295   fprintf(OUT,
296           "# Set the GRAS_ROOT environment variable to the path under which you installed SimGrid\n"
297           "# Compilation will fail if you don't do so\n"
298           "GRAS_ROOT?= $(shell if [ -e /usr/local/lib/libgras.so ] ; then echo /usr/local ; else echo \"\\\"<<<< GRAS_ROOT undefined !!! >>>>\\\"\"; fi)\n\n"
299           "# You can fiddle the following to make it fit your taste\n"
300           "INCLUDES = -I$(GRAS_ROOT)/include\n"
301           "CFLAGS ?= -O3 -w -g -Wall\n"
302           "LIBS_SIM = -lm  -L$(GRAS_ROOT)/lib/ -lsimgrid\n"
303           "LIBS_RL = -lm  -L$(GRAS_ROOT)/lib/ -lgras\n" "LIBS = \n" "\n");
304
305   fprintf(OUT, "PRECIOUS_C_FILES ?= %s.c\n", project);
306
307   fprintf(OUT, "GENERATED_C_FILES = ");
308   fprintf(OUT, SIM_SOURCENAME " ", project);
309   xbt_dict_foreach(process_function_set, cursor, key, data) {
310     fprintf(OUT, RL_SOURCENAME " ", project, key);
311   }
312   fprintf(OUT, "\n");
313
314   fprintf(OUT, "OBJ_FILES = $(patsubst %%.c,%%.o,$(PRECIOUS_C_FILES))\n");
315
316   fprintf(OUT, "BIN_FILES = ");
317
318   fprintf(OUT, SIM_BINARYNAME " ", project);
319   xbt_dict_foreach(process_function_set, cursor, key, data) {
320     fprintf(OUT, RL_BINARYNAME " ", project, key);
321   }
322   fprintf(OUT, "\n");
323
324   fprintf(OUT,
325           "\n"
326           "## By default, build all the binaries\n"
327           "all: $(BIN_FILES)\n" "\n");
328
329   //No Need to generate source files, already done.
330
331   fprintf(OUT, "\n## Generate the binaries\n");
332   fprintf(OUT, SIM_BINARYNAME ": " SIM_OBJNAME " $(OBJ_FILES)\n", project,
333           project);
334   fprintf(OUT,
335           "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS_SIM) $(LIBS) $(LDADD) -o $@ \n");
336   xbt_dict_foreach(process_function_set, cursor, key, data) {
337     fprintf(OUT, RL_BINARYNAME " : " RL_OBJNAME " $(OBJ_FILES)\n", project,
338             key, project, key);
339     fprintf(OUT,
340             "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS_RL) $(LIBS) $(LDADD) -o $@ \n");
341   }
342   fprintf(OUT,
343           "\n"
344           "%%: %%.o\n"
345           "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS) $(LDADD) -o $@ \n"
346           "\n"
347           "%%.o: %%.c\n"
348           "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) -c -o $@ $<\n" "\n");
349
350   fprintf(OUT,
351           "##┬áRules for tarballs and cleaning\n"
352           "DIST_FILES= $(EXTRA_DIST) $(GENERATED_C_FILES) $(PRECIOUS_C_FILES) "
353           MAKEFILE_FILENAME_LOCAL " " /*MAKEFILE_FILENAME_REMOTE */ "\n"
354           "distdir: $(DIST_FILES)\n" "\trm -rf $(DISTDIR)\n"
355           "\tmkdir -p $(DISTDIR)\n" "\tcp $^ $(DISTDIR)\n" "\n"
356           "dist: clean distdir\n"
357           "\ttar c $(DISTDIR) | gzip -c9 > $(DISTDIR).tar.gz\n" "\n",
358           project /*, project */ );
359
360   fprintf(OUT,
361           "clean:\n"
362           "\trm -f $(CLEANFILES) $(BIN_FILES) $(OBJ_FILES) *~ %s.o "
363           SIM_OBJNAME, project, project);
364   xbt_dict_foreach(process_function_set, cursor, key, data) {
365     fprintf(OUT, " " RL_OBJNAME, project, key);
366   }
367   fprintf(OUT,
368           "\n"
369           "\trm -rf $(DISTDIR)\n"
370           "\n" ".SUFFIXES:\n" ".PHONY : clean\n" "\n");
371   fclose(OUT);
372 }
373
374 static void print(void *p)
375 {
376   printf("%p", p);
377 }