Logo AND Algorithmique Numérique Distribuée

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