Logo AND Algorithmique Numérique Distribuée

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