Logo AND Algorithmique Numérique Distribuée

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