Logo AND Algorithmique Numérique Distribuée

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