Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
eb5de727f0c81386cb9727cfd11e6601bb30daff
[simgrid.git] / tools / gras / stub_generator.c
1 /*      $Id$     */
2
3 /* gras_stub_generator - creates the main() to use a GRAS program           */
4
5 /* Copyright (c) 2003,2004,2005 Martin Quinson, Arnaud Legrand. 
6    All rights reserved.                  */
7
8 /* This program is free software; you can redistribute it and/or modify it
9  * under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #include"xbt/sysdep.h"
12 #include"xbt/dict.h"
13 #include"xbt/error.h"
14 #include "surf/surf_parse.h"
15 #include "surf/surf.h"
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(stubgen,gras,"Stub generator");
18
19 #define WARN "/***********\n * DO NOT EDIT! THIS FILE HAS BEEN AUTOMATICALLY GENERATED FROM %s BY gras_stub_generator\n ***********/\n"
20 #define SIM_SOURCENAME  "_%s_simulator.c"
21 #define SIM_OBJNAME  "_%s_simulator.o"
22 #define SIM_BINARYNAME  "%s_simulator"
23 #define SIM_SOURCENAME_LDADD  "%s_simulator_LDADD"
24 #define SIM_SOURCENAME_SOURCES  "%s_simulator_SOURCES"
25 #define RL_SOURCENAME  "_%s_%s.c"
26 #define RL_OBJNAME  "_%s_%s.o"
27 #define RL_BINARYNAME  "%s_%s"
28 #define RL_SOURCENAME_LDADD  "%s_%s_LDADD"
29 #define RL_SOURCENAME_SOURCES  "%s_%s_SOURCES"
30 #define MAKEFILE_FILENAME_AM  "%s.Makefile.am"
31 #define MAKEFILE_FILENAME_LOCAL  "%s.Makefile.local"
32 #define MAKEFILE_FILENAME_REMOTE  "%s.Makefile.remote"
33
34 char *warning = NULL;
35
36 /**********************************************/
37 /**** Generate the file for the simulator *****/
38 /**********************************************/
39
40 const char *SIM_PREEMBULE =
41 "#include <stdlib.h>\n"
42 "#include <stdio.h>\n"
43 "#include \"msg/msg.h\"\n"
44 "#include <gras.h>\n"
45 "\n"
46 "char *gras_log=NULL;\n";
47
48
49 #define SIM_LAUNCH_FUNC  \
50 "int launch_%s(int argc, char **argv) {\n" \
51 "  char **myargv=argv;\n" \
52 "  int myargc=argc;\n" \
53 "  int i;\n" \
54 "  int retcode;\n"\
55 "    \n"\
56 "  if (gras_log) {\n"\
57 "    myargv=malloc((argc+1) * sizeof(char**));\n" \
58 "    for (i=0; i<argc; i++)\n" \
59 "      myargv[i] = argv[i];\n" \
60 "    myargv[myargc++] = gras_log;\n" \
61 "  }\n" \
62 "  retcode = %s(myargc,myargv);\n" \
63 "  if (myargv != argv)\n" \
64 "    free(myargv);\n" \
65 "  return retcode;\n" \
66 "}\n"
67
68 const char* SIM_MAIN_PREEMBULE =
69 "int main (int argc,char *argv[]) {\n"
70 "  int i,j;\n"
71 "\n"
72 "  /*  Simulation setup */\n"
73 "  MSG_global_init_args(&argc,argv);\n"
74 "  if (argc != 3) {\n"
75 "    fprintf(stderr, \"Usage: %s platform_file application_description.txt [--gras-log=...]\\n\",argv[0]);\n"
76 "    exit(1);\n"
77 "  }\n"
78 "\n"
79 "  MSG_set_channel_number(10); // GRAS_MAX_CHANNEL hardcoded since Alvin killed its definition\n"
80 "  MSG_create_environment(argv[1]);\n"
81 "\n"
82 "  /*  Application deployment */\n";
83
84 const char *SIM_MAIN_POSTEMBULE = "\n"
85 "\n"
86 "  MSG_launch_application(argv[2]);\n"
87 "\n"
88 "  /*  Run the simulation */\n"
89 "  MSG_main();\n"
90 "\n"
91 "  /* cleanup the place */\n"
92 "  MSG_clean();\n"
93 "  if (gras_log)\n"
94 "    free(gras_log);\n"
95 "  return 0;\n"
96 "}\n";
97
98 /**********************************************/
99 /**** Generate the file for the real life *****/
100 /**********************************************/
101
102 #define RL_CODE \
103 "#include <stdio.h>\n" \
104 "#include <signal.h>\n" \
105 "#include <gras.h>\n" \
106 "\n" \
107 "/* user code */\n" \
108 "int %s(int argc, char *argv[]);\n" \
109 "\n" \
110 "int main(int argc, char *argv[]){\n" \
111 "  int errcode;\n" \
112 "\n" \
113 "  errcode=%s(argc,argv);\n"\
114 " \n" \
115 "  return errcode;\n"\
116 "}\n"
117
118 /**********************************************/
119 /********* Parse XML deployment file **********/
120 /**********************************************/
121 xbt_dict_t process_function_set = NULL;
122
123 static void parse_process_init(void)
124
125   void *p = (void *) 1234;
126   xbt_dict_set(process_function_set, A_process_function, p, NULL);
127 }
128
129 static void generate_sim(char *project)
130 {
131   xbt_dict_cursor_t cursor=NULL;
132   char *key = NULL;
133   void *data = NULL;
134   char *filename = NULL;
135   FILE *OUT = NULL;
136   filename = xbt_new(char,strlen(project) + strlen(SIM_SOURCENAME));
137   sprintf(filename,SIM_SOURCENAME,project);
138   
139   OUT=fopen(filename,"w");
140   xbt_assert1(OUT, "Unable to open %s for writing",filename);
141
142   fprintf(OUT, "%s\n",warning);
143   fprintf(OUT, "%s", SIM_PREEMBULE);
144   xbt_dict_foreach(process_function_set,cursor,key,data) {
145     fprintf(OUT,"int %s(int argc,char *argv[]);\n",key);
146   }
147   fprintf(OUT,"\n");
148   xbt_dict_foreach(process_function_set,cursor,key,data) {
149     fprintf(OUT,"int launch_%s(int argc,char *argv[]);\n",key);
150   }
151   fprintf(OUT, "\n%s\n",warning);
152   xbt_dict_foreach(process_function_set,cursor,key,data) {
153     fprintf(OUT,SIM_LAUNCH_FUNC,key,key);
154   }
155   fprintf(OUT, "\n%s\n",warning);
156
157   fprintf(OUT, "%s", SIM_MAIN_PREEMBULE);
158   xbt_dict_foreach(process_function_set,cursor,key,data) {
159     fprintf(OUT,"  MSG_function_register(\"%s\", launch_%s);\n",key,key);
160   }
161   fprintf(OUT, "%s", SIM_MAIN_POSTEMBULE);
162   fclose(OUT);
163   free(filename);
164 }
165
166 static void generate_rl(char *project)
167 {
168   xbt_dict_cursor_t cursor=NULL;
169   char *key = NULL;
170   void *data = NULL;
171   char *filename = NULL;
172   FILE *OUT = NULL;
173
174   xbt_dict_foreach(process_function_set,cursor,key,data) {
175     filename = xbt_new(char,strlen(project) + strlen(RL_SOURCENAME) + strlen(key));
176     sprintf(filename,RL_SOURCENAME,project,key);
177   
178     OUT=fopen(filename,"w");
179     xbt_assert1(OUT, "Unable to open %s for writing",filename);
180
181     fprintf(OUT, "\n%s\n",warning);
182     fprintf(OUT, RL_CODE, key,key);
183     fprintf(OUT, "\n%s\n",warning);
184     fclose(OUT);
185     free(filename);
186   }
187 }
188
189 static void generate_makefile_am(char *project, char *deployment)
190 {
191   xbt_dict_cursor_t cursor=NULL;
192   char *key = NULL;
193   void *data = NULL;
194   char *filename = NULL;
195   FILE *OUT = NULL;
196
197   filename = xbt_new(char,strlen(project) + strlen(MAKEFILE_FILENAME_AM));
198   sprintf(filename,MAKEFILE_FILENAME_AM, project);
199   
200   OUT=fopen(filename,"w");
201   xbt_assert1(OUT, "Unable to open %s for writing",filename);
202
203   fprintf(OUT, "# AUTOMAKE variable definition\n");
204   fprintf(OUT, "INCLUDES= @CFLAGS_SimGrid@\n\n");
205   fprintf(OUT, "PROGRAMS=");
206   fprintf(OUT, SIM_BINARYNAME,project);
207
208   xbt_dict_foreach(process_function_set,cursor,key,data) {
209     fprintf(OUT, " ");
210     fprintf(OUT, RL_BINARYNAME, project, key);
211   }
212
213   fprintf(OUT, "\n\n");
214   fprintf(OUT, SIM_SOURCENAME_SOURCES,project);
215   fprintf(OUT, "=\t");
216   fprintf(OUT, SIM_SOURCENAME,project);
217   fprintf(OUT, " %s.c\n", project);
218   fprintf(OUT, SIM_SOURCENAME_LDADD, project);
219   fprintf(OUT, "=\tpath/to/libsimgrid.a\n\n");
220
221   xbt_dict_foreach(process_function_set,cursor,key,data) {
222     fprintf(OUT, RL_SOURCENAME_SOURCES, project,key);
223     fprintf(OUT, "=\t");
224     fprintf(OUT, RL_SOURCENAME, project,key);
225     fprintf(OUT, " %s.c\n", project);
226     fprintf(OUT, RL_SOURCENAME_LDADD, project, key);
227     fprintf(OUT, "=\tpath/to/libgras.a\n\n");
228   }
229
230   fprintf(OUT, "\n# cleanup temps\n");
231   fprintf(OUT, "CLEANFILES= ");
232   fprintf(OUT, SIM_SOURCENAME, project);
233   
234   xbt_dict_foreach(process_function_set,cursor,key,data) {
235     fprintf(OUT, " ");
236     fprintf(OUT, RL_SOURCENAME, project,key);
237   }
238   fprintf(OUT, "\n");
239
240   fprintf(OUT, "\n# generate temps\n");
241   fprintf(OUT, "\n# A rule to generate the source file each time the deployment file changes\n");
242
243   xbt_dict_foreach(process_function_set,cursor,key,data) {
244     fprintf(OUT, RL_SOURCENAME, project,key);
245     fprintf(OUT, " ");
246   }
247   fprintf(OUT, SIM_SOURCENAME, project);
248   fprintf(OUT, ": %s\n", deployment);
249   fprintf(OUT, "\tstub_generator %s %s >/dev/null\n", project, deployment);
250   fclose(OUT);
251 }
252
253
254 static void generate_makefile(char *project, char *deployment)
255 {
256   xbt_dict_cursor_t cursor=NULL;
257   char *key = NULL;
258   void *data = NULL;
259   char *filename = NULL;
260   FILE *OUT = NULL;
261
262   filename = xbt_new(char,strlen(project) + strlen(MAKEFILE_FILENAME_LOCAL));
263   sprintf(filename,MAKEFILE_FILENAME_LOCAL, project);
264   
265   OUT=fopen(filename,"w");
266   xbt_assert1(OUT, "Unable to open %s for writing",filename);
267
268   fprintf(OUT, "PROJECT_NAME=%s\n",project);
269   fprintf(OUT, 
270           "DISTDIR=gras-$(PROJECT_NAME)\n\n"
271           "SIMGRID_INSTALL_PATH?= $(shell echo \"\\\"<<<< SIMGRID_INSTALL_PATH undefined !!! >>>>\\\"\")\n"
272           "CFLAGS = -O3 -w\n"
273           "INCLUDES = -I$(SIMGRID_INSTALL_PATH)/include\n"
274           "LIBS = -lm  -L$(SIMGRID_INSTALL_PATH)/lib/ -lsimgrid\n"
275           "\n"
276           "C_FILES = _chrono_multiplier.c _chrono_simulator.c chrono.c\n"
277           "BIN_FILES = chrono_multiplier chrono_simulator\n"
278           "\n"
279           "all: $(BIN_FILES)\n"
280           "\n");
281   fprintf(OUT, SIM_BINARYNAME ": " SIM_OBJNAME " %s.c\n",project, project, project);
282   xbt_dict_foreach(process_function_set,cursor,key,data) {
283     fprintf(OUT, RL_BINARYNAME " : " RL_OBJNAME " %s.c\n", project, key, project, key, project);
284   }
285   fprintf(OUT, 
286           "\n"
287           "%%: %%.o\n"
288           "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS) $(LDADD) -o $@ \n"
289           "\n"
290           "%%.o: %%.c\n"
291           "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) -c -o $@ $<\n"
292           "\n"
293           "distdir: $(C_FILES) $(PROJECT_NAME).Makefile\n"
294           "\trm -rf $(DISTDIR)\n"
295           "\tmkdir -p $(DISTDIR)\n"
296           "\tcp $^ $(DISTDIR)\n"
297           "\n"
298           "dist: clean distdir\n"
299           "\ttar c $(DISTDIR) | gzip -c > $(DISTDIR).tar.gz\n"
300           "\n"
301           "clean:\n"
302           "\trm -f $(BIN_FILES) *.o *~\n"
303           "\trm -rf $(DISTDIR)\n"
304           "\n"
305           ".SUFFIXES:\n"
306           ".PHONY : clean\n");
307   fclose(OUT);
308 }
309
310 static void print(void *p)
311 {
312   printf("%p",p);
313 }
314
315 int main(int argc, char *argv[])
316 {
317   char *project_name = NULL;
318   char *deployment_file = NULL;
319
320   surf_init(&argc, argv);
321
322   xbt_assert1((argc ==3),"Usage: %s project_name deployment_file\n",argv[0]);
323
324   project_name = argv[1];
325   deployment_file = argv[2];
326
327   process_function_set = xbt_dict_new();
328
329   STag_process_fun = parse_process_init;
330   surf_parse_open(deployment_file);
331   if(surf_parse()) xbt_assert1(0,"Parse error in %s",deployment_file);
332   surf_parse_close();
333
334   warning = xbt_new(char,strlen(WARN)+strlen(deployment_file)+10);
335   sprintf(warning,WARN,deployment_file);
336
337   if(XBT_LOG_ISENABLED(stubgen, xbt_log_priority_debug)) {
338     xbt_dict_cursor_t cursor=NULL;
339     char *key = NULL;
340     void *data = NULL;
341
342     for (cursor=NULL, xbt_dict_cursor_first((process_function_set),&(cursor)) ;
343          xbt_dict_cursor_get_or_free(&(cursor),&(key),(void**)(&data));
344          xbt_dict_cursor_step(cursor) ) {
345       DEBUG1("Function %s", key);      
346     }
347     
348     xbt_dict_dump(process_function_set,print);
349   }
350
351   generate_sim(project_name);
352   generate_rl(project_name);
353   generate_makefile_am(project_name, deployment_file);
354   generate_makefile_local(project_name, deployment_file);
355
356   free(warning);
357   return 0;
358 }