Logo AND Algorithmique Numérique Distribuée

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