Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
stub_generator reading XML files
[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 const char *WARN = "/***********\n * DO NOT EDIT! THIS FILE WERE AUTOMATICALLY GENERATED FROM %s BY gras_stub_generator\n ***********/\n";
20 const char *SIM_FILENAME = "_%s_simulator.c";
21 const char *SIM_BINARYNAME = "%s_simulator";
22 const char *SIM_FILENAME_LDADD = "_%s_simulator_LDADD";
23 const char *SIM_FILENAME_SOURCES = "_%s_simulator_SOURCES";
24 const char *RL_FILENAME = "_%s_%s.c";
25 const char *RL_BINARYNAME = "%s_%s";
26 const char *RL_FILENAME_LDADD = "%s_%s_LDADD";
27 const char *RL_FILENAME_SOURCES = "%s_%s_SOURCES";
28 const char *MAKEFILE_FILENAME = "%s.Makefile.am";
29
30 char *warning = NULL;
31
32 /**********************************************/
33 /**** Generate the file for the simulator *****/
34 /**********************************************/
35
36 const char *SIM_PREEMBULE =
37 "#include <stdlib.h>\n"
38 "#include <stdio.h>\n"
39 "#include \"msg/msg.h\"\n"
40 "#include <gras.h>\n"
41 "\n"
42 "char *gras_log=NULL;\n";
43
44
45 const char *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_PREEMBULE =
65 "int main (int argc,char *argv[]) {\n"
66 "  int i,j;\n"
67 "\n"
68 "  /* Save the gras-log argument of real command line to pass it to all processes */\n"
69 "  for (i=1; i<argc; i++) {\n"
70 "    if (!strncmp(argv[i],\"--gras-log=\",strlen(\"--gras-log=\"))) {\n"
71 "      if (gras_log) {\n"
72 "        char *tmp=malloc(strlen(gras_log)+strlen(argv[i])+2);\n"
73 "        sprintf(tmp,\"%%s %%s\",gras_log, argv[i]);\n"
74 "        free(gras_log);\n"
75 "        gras_log=tmp;\n"
76 "      } else {\n"
77 "         gras_log = strdup(argv[i]);\n"
78 "      }\n"
79 "      for (j=i+1; j<argc; j++) {\n"
80 "       argv[j-1] = argv[j];\n"
81 "      } \n"
82 "      argv[j-1] = NULL;\n"
83 "      argc--;\n"
84 "      i--; /* compensate effect of next loop incrementation */\n"
85 "    }\n"
86 "  }\n"
87 "\n"
88 "  if (argc != 3) {\n"
89 "    fprintf(stderr, \"Usage: %%s platform_file application_description.txt [--gras-log=...]\\n\",argv[0]);\n"
90 "    exit(1);\n"
91 "  }\n"
92 "\n"
93 "  /*  Simulation setup */\n"
94 "  MSG_global_init();\n"
95 "  MSG_set_verbosity(MSG_SILENT);\n"
96 "  MSG_set_channel_number(10); // GRAS_MAX_CHANNEL hardcoded since Alvin killed its definition\n"
97 "  MSG_create_environment(argv[1]);\n"
98 "\n"
99 "  /*  Application deployment */\n";
100
101 const char *SIM_MAIN_POSTEMBULE = "\n"
102 "\n"
103 "  MSG_launch_application(argv[2]);\n"
104 "\n"
105 "  /*  Run the simulation */\n"
106 "  MSG_main();\n"
107 "\n"
108 "  /* cleanup the place */\n"
109 "  MSG_clean();\n"
110 "  if (gras_log)\n"
111 "    free(gras_log);\n"
112 "  return 0;\n"
113 "}\n";
114
115 /**********************************************/
116 /**** Generate the file for the real life *****/
117 /**********************************************/
118
119 const char *RL_CODE =
120 "#include <stdio.h>\n"
121 "#include <signal.h>\n"
122 "#include <gras.h>\n"
123 "\n"
124 "/* user code */\n"
125 "int %s(int argc, char *argv[]);\n"
126 "\n"
127 "int main(int argc, char *argv[]){\n"
128 "  int errcode;\n"
129 "\n"
130 "  errcode=%s(argc,argv);\n"
131 " \n"
132 "  return errcode;\n"
133 "}\n";
134
135 /**********************************************/
136 /********* Parse XML deployment file **********/
137 /**********************************************/
138 xbt_dict_t process_function_set = NULL;
139
140 static void parse_process_init(void)
141
142   void *p = (void *) 1231;
143   xbt_dict_set(process_function_set, A_process_function, p, NULL);
144 }
145
146 static void generate_sim(char *project)
147 {
148   xbt_dict_cursor_t cursor=NULL;
149   char *key = NULL;
150   void *data = NULL;
151   char *filename = NULL;
152   FILE *OUT = NULL;
153   filename = xbt_new(char,strlen(project) + strlen(SIM_FILENAME));
154   sprintf(filename,SIM_FILENAME,project);
155   
156   OUT=fopen(filename,"w");
157   xbt_assert1(OUT, "Unable to open %s for writing",filename);
158
159   fprintf(OUT, "%s\n",warning);
160   fprintf(OUT, SIM_PREEMBULE);
161   xbt_dict_foreach(process_function_set,cursor,key,data) {
162     fprintf(OUT,"int %s(int argc,char *argv[]);\n",key);
163   }
164   fprintf(OUT,"\n");
165   xbt_dict_foreach(process_function_set,cursor,key,data) {
166     fprintf(OUT,"int launch_%s(int argc,char *argv[]);\n",key);
167   }
168   fprintf(OUT, "\n%s\n",warning);
169   xbt_dict_foreach(process_function_set,cursor,key,data) {
170     fprintf(OUT,SIM_LAUNCH_FUNC,key,key);
171   }
172   fprintf(OUT, "\n%s\n",warning);
173
174   fprintf(OUT, SIM_MAIN_PREEMBULE);
175   xbt_dict_foreach(process_function_set,cursor,key,data) {
176     fprintf(OUT,"  MSG_function_register(\"%s\", launch_%s);\n",key,key);
177   }
178   fprintf(OUT, SIM_MAIN_POSTEMBULE);
179   fclose(OUT);
180   xbt_free(filename);
181 }
182
183 static void generate_rl(char *project)
184 {
185   xbt_dict_cursor_t cursor=NULL;
186   char *key = NULL;
187   void *data = NULL;
188   char *filename = NULL;
189   FILE *OUT = NULL;
190
191   xbt_dict_foreach(process_function_set,cursor,key,data) {
192     filename = xbt_new(char,strlen(project) + strlen(RL_FILENAME) + strlen(key));
193     sprintf(filename,RL_FILENAME,project,key);
194   
195     OUT=fopen(filename,"w");
196     xbt_assert1(OUT, "Unable to open %s for writing",filename);
197
198     fprintf(OUT, "\n%s\n",warning);
199     fprintf(OUT, RL_CODE, key,key);
200     fprintf(OUT, "\n%s\n",warning);
201     fclose(OUT);
202     xbt_free(filename);
203   }
204 }
205
206 static void generate_makefile(char *project, char *deployment)
207 {
208   xbt_dict_cursor_t cursor=NULL;
209   char *key = NULL;
210   void *data = NULL;
211   char *filename = NULL;
212   FILE *OUT = NULL;
213
214   filename = xbt_new(char,strlen(project) + strlen(MAKEFILE_FILENAME));
215   sprintf(filename,MAKEFILE_FILENAME, project);
216   
217   OUT=fopen(filename,"w");
218   xbt_assert1(OUT, "Unable to open %s for writing",filename);
219
220   fprintf(OUT, "# AUTOMAKE variable definition\n");
221   fprintf(OUT, "INCLUDES= @CFLAGS_SimGrid@\n\n");
222   fprintf(OUT, "PROGRAMS=");
223   fprintf(OUT, SIM_FILENAME,project);
224
225   xbt_dict_foreach(process_function_set,cursor,key,data) {
226     fprintf(OUT, " ");
227     fprintf(OUT, RL_BINARYNAME, project, key);
228   }
229
230   fprintf(OUT, "\n\n");
231   fprintf(OUT, SIM_FILENAME_SOURCES,project);
232   fprintf(OUT, "=");
233   fprintf(OUT, SIM_FILENAME,project);
234   fprintf(OUT, " %s.c\n", project);
235
236   xbt_dict_foreach(process_function_set,cursor,key,data) {
237     fprintf(OUT, RL_FILENAME_SOURCES, project,key);
238     fprintf(OUT, "=\t");
239     fprintf(OUT, RL_FILENAME, project,key);
240     fprintf(OUT, " %s.c\n", project);
241   }
242
243   fprintf(OUT, "\n\n");
244   fprintf(OUT, SIM_FILENAME_LDADD, project);
245   fprintf(OUT, "=\tpath/to/libsimgrid.a\n");
246
247
248   xbt_dict_foreach(process_function_set,cursor,key,data) {
249     fprintf(OUT, RL_FILENAME_LDADD, project, key);
250     fprintf(OUT, "=\tpath/to/libgras.a\n");
251   }
252
253   fprintf(OUT, "\n# cleanup temps\n");
254   fprintf(OUT, "CLEANFILES= ");
255   fprintf(OUT, SIM_FILENAME, project);
256   
257   xbt_dict_foreach(process_function_set,cursor,key,data) {
258     fprintf(OUT, " ");
259     fprintf(OUT, RL_FILENAME, project,key);
260   }
261   fprintf(OUT, "\n");
262
263   fprintf(OUT, "\n# generate temps\n");
264   fprintf(OUT, "\n# A rule to generate the source file each time the deployment file changes\n");
265
266   xbt_dict_foreach(process_function_set,cursor,key,data) {
267     fprintf(OUT, RL_FILENAME, project,key);
268     fprintf(OUT, " ");
269   }
270   fprintf(OUT, SIM_FILENAME, project);
271   fprintf(OUT, ": %s\n", deployment);
272   fprintf(OUT, "\tstub_generator %s %s >/dev/null\n", project, filename);
273 }
274
275 int main(int argc, char *argv[])
276 {
277   char *project_name = NULL;
278   char *deployment_file = NULL;
279
280   surf_init(&argc, argv);
281
282   xbt_assert1((argc ==3),"Usage: %s project_name deployment_file\n",argv[0]);
283
284   project_name = argv[1];
285   deployment_file = argv[2];
286
287   process_function_set = xbt_dict_new();
288
289   STag_process_fun = parse_process_init;
290   surf_parse_open(deployment_file);
291   if(surf_parse()) xbt_assert1(0,"Parse error in %s",deployment_file);
292   surf_parse_close();
293
294   warning = xbt_new(char,strlen(WARN)+strlen(deployment_file)+10);
295   sprintf(warning,WARN,deployment_file);
296
297   generate_sim(project_name);
298   generate_rl(project_name);
299   generate_makefile(project_name, deployment_file);
300
301   xbt_free(warning);
302   return 0;
303 }