Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cleanup a bit the parser code; Do not use attribute content in E[nding]Tag functions...
[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/function_types.h"
13 #include "xbt/log.h"
14 #include "surf/surfxml_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.mk"
32 #define MAKEFILE_FILENAME_REMOTE  "%s.Makefile.remote"
33 #define DEPLOYMENT  "%s.deploy.sh"
34
35 char *warning = NULL;
36
37 /**********************************************/
38 /**** Generate the file for the simulator *****/
39 /**********************************************/
40
41 const char *SIM_PREEMBULE =
42 "#include <stdlib.h>\n"
43 "#include <stdio.h>\n"
44 "#include \"msg/msg.h\"\n"
45 "#include <gras.h>\n"
46 "\n"
47 "char *gras_log=NULL;\n";
48
49
50 #define SIM_LAUNCH_FUNC  \
51 "int launch_%s(int argc, char **argv) {\n" \
52 "  char **myargv=argv;\n" \
53 "  int myargc=argc;\n" \
54 "  int i;\n" \
55 "  int retcode;\n"\
56 "    \n"\
57 "  if (gras_log) {\n"\
58 "    myargv=malloc((argc+1) * sizeof(char**));\n" \
59 "    for (i=0; i<argc; i++)\n" \
60 "      myargv[i] = argv[i];\n" \
61 "    myargv[myargc++] = gras_log;\n" \
62 "  }\n" \
63 "  retcode = %s(myargc,myargv);\n" \
64 "  if (myargv != argv)\n" \
65 "    free(myargv);\n" \
66 "  return retcode;\n" \
67 "}\n"
68
69 const char *SIM_MAIN_POSTEMBULE = "\n"
70 "\n"
71 "  MSG_launch_application(argv[2]);\n"
72 "\n"
73 "  /*  Run the simulation */\n"
74 "  MSG_main();\n"
75 "\n"
76 "  /* cleanup the place */\n"
77 "  MSG_clean();\n"
78 "  if (gras_log)\n"
79 "    free(gras_log);\n"
80 "  return 0;\n"
81 "}\n";
82
83
84
85 /**********************************************/
86 /********* Parse XML deployment file **********/
87 /**********************************************/
88 xbt_dict_t process_function_set = NULL;
89 xbt_dynar_t process_list = NULL;
90 xbt_dict_t machine_set = NULL;
91
92 typedef struct s_process_t {
93   int argc;
94   char **argv;
95   char *host;
96 } s_process_t;
97
98 static void s_process_free(void *process)
99 {
100   int i;
101   for(i=0;i<((s_process_t*)process)->argc; i++)
102     free(((s_process_t*)process)->argv[i]);
103   free(((s_process_t*)process)->host);
104 }
105
106 static s_process_t process;
107
108 static void parse_process_init(void)
109 {
110   xbt_dict_set(process_function_set, A_surfxml_process_function, NULL, NULL);
111   xbt_dict_set(machine_set, A_surfxml_process_host, NULL, NULL);
112   process.argc = 1 ;
113   process.argv = xbt_new(char*,1);
114   process.argv[0] = xbt_strdup(A_surfxml_process_function);
115   process.host=strdup(A_surfxml_process_host);
116   VERB1("Function: %s",A_surfxml_process_function);
117 }
118
119 static void parse_argument(void)
120 {
121   process.argc++;
122   process.argv = xbt_realloc(process.argv, (process.argc) * sizeof(char *));
123   process.argv[(process.argc) - 1] = xbt_strdup(A_surfxml_argument_value);
124 }
125
126 static void parse_process_finalize(void)
127 {
128   xbt_dynar_push(process_list,&process);
129   VERB1("Function: %s",process.argv[0]);
130 }
131
132 static void generate_sim(char *project)
133 {
134   xbt_dict_cursor_t cursor=NULL;
135   char *key = NULL;
136   void *data = NULL;
137   char *filename = NULL;
138   FILE *OUT = NULL;
139   filename = xbt_new(char,strlen(project) + strlen(SIM_SOURCENAME));
140   sprintf(filename,SIM_SOURCENAME,project);
141   
142   OUT=fopen(filename,"w");
143   xbt_assert1(OUT, "Unable to open %s for writing",filename);
144
145   fprintf(OUT, "%s\n",warning);
146   fprintf(OUT, "%s", SIM_PREEMBULE);
147   xbt_dict_foreach(process_function_set,cursor,key,data) {
148     fprintf(OUT,"int %s(int argc,char *argv[]);\n",key);
149   }
150   fprintf(OUT,"\n");
151   xbt_dict_foreach(process_function_set,cursor,key,data) {
152     fprintf(OUT,"int launch_%s(int argc,char *argv[]);\n",key);
153   }
154   fprintf(OUT, "\n%s\n",warning);
155   xbt_dict_foreach(process_function_set,cursor,key,data) {
156     fprintf(OUT,SIM_LAUNCH_FUNC,key,key);
157   }
158   fprintf(OUT, "\n%s\n",warning);
159
160   fprintf(OUT, "%s", "int main (int argc,char *argv[]) {\n" 
161                      "\n" 
162                      "  /*  Simulation setup */\n" 
163                      "  MSG_global_init(&argc,argv);\n" 
164                      "  if (argc != 3) {\n" 
165                      "    fprintf(stderr, \"Usage: %s platform.xml deployment.xml [--gras-log=...]\\n\",argv[0]);\n" 
166                      "    exit(1);\n" 
167                      "  }\n"
168                      "\n");
169    fprintf(OUT, 
170            "  MSG_paje_output(\"%s.trace\");\n" 
171            "  MSG_set_channel_number(XBT_MAX_CHANNEL); /* Using at most 10 channel (ports) per host. Change it here if needed */\n" 
172            "  MSG_create_environment(argv[1]);\n" 
173            "\n" 
174            "  /*  Application deployment */\n",
175            project);
176   xbt_dict_foreach(process_function_set,cursor,key,data) {
177     fprintf(OUT,"  MSG_function_register(\"%s\", launch_%s);\n",key,key);
178   }
179   fprintf(OUT, "%s", SIM_MAIN_POSTEMBULE);
180   fclose(OUT);
181   free(filename);
182 }
183
184 /**********************************************/
185 /**** Generate the file for the real life *****/
186 /**********************************************/
187 static void generate_rl(char *project)
188 {
189   xbt_dict_cursor_t cursor=NULL;
190   char *key = NULL;
191   void *data = NULL;
192   char *filename = NULL;
193   FILE *OUT = NULL;
194
195   xbt_dict_foreach(process_function_set,cursor,key,data) {
196     filename = xbt_new(char,strlen(project) + strlen(RL_SOURCENAME) + strlen(key));
197     sprintf(filename,RL_SOURCENAME,project,key);
198   
199     OUT=fopen(filename,"w");
200     xbt_assert1(OUT, "Unable to open %s for writing",filename);
201
202     fprintf(OUT, "\n%s\n",warning);
203     fprintf(OUT, "#include <stdio.h>\n" \
204                  "#include <signal.h>\n" \
205                  "#include <gras.h>\n" \
206                  "\n" \
207                  "extern const char *_gras_procname;\n" \
208                  "/* user code */\n" \
209                  "int %s(int argc, char *argv[]);\n" \
210                  "\n" \
211                  "int main(int argc, char *argv[]){\n" \
212                  "  int errcode;\n" \
213                  "\n" \
214                  "  _gras_procname = \"%s\";\n" \
215                  "  errcode=%s(argc,argv);\n"\
216                  " \n" \
217                  "  return errcode;\n"\
218                  "}\n",
219             key,key,key);
220     fprintf(OUT, "\n%s\n",warning);
221     fclose(OUT);
222     free(filename);
223   }
224 }
225
226 static void generate_makefile_am(char *project, char *deployment)
227 {
228   xbt_dict_cursor_t cursor=NULL;
229   char *key = NULL;
230   void *data = NULL;
231   char *filename = NULL;
232   FILE *OUT = NULL;
233
234   filename = xbt_new(char,strlen(project) + strlen(MAKEFILE_FILENAME_AM));
235   sprintf(filename,MAKEFILE_FILENAME_AM, project);
236   
237   OUT=fopen(filename,"w");
238   xbt_assert1(OUT, "Unable to open %s for writing",filename);
239
240   fprintf(OUT, "# AUTOMAKE variable definition\n");
241   fprintf(OUT, "INCLUDES= @CFLAGS_SimGrid@\n\n");
242   fprintf(OUT, "PROGRAMS=");
243   fprintf(OUT, SIM_BINARYNAME,project);
244
245   xbt_dict_foreach(process_function_set,cursor,key,data) {
246     fprintf(OUT, " ");
247     fprintf(OUT, RL_BINARYNAME, project, key);
248   }
249
250   fprintf(OUT, "\n\n");
251   fprintf(OUT, SIM_SOURCENAME_SOURCES,project);
252   fprintf(OUT, "=\t");
253   fprintf(OUT, SIM_SOURCENAME,project);
254   fprintf(OUT, " %s.c\n", project);
255   fprintf(OUT, SIM_SOURCENAME_LDADD, project);
256   fprintf(OUT, "=\tpath/to/libsimgrid.a\n\n");
257
258   xbt_dict_foreach(process_function_set,cursor,key,data) {
259     fprintf(OUT, RL_SOURCENAME_SOURCES, project,key);
260     fprintf(OUT, "=\t");
261     fprintf(OUT, RL_SOURCENAME, project,key);
262     fprintf(OUT, " %s.c\n", project);
263     fprintf(OUT, RL_SOURCENAME_LDADD, project, key);
264     fprintf(OUT, "=\tpath/to/libgras.a\n\n");
265   }
266
267   fprintf(OUT, "\n# cleanup temps\n");
268   fprintf(OUT, "CLEANFILES= ");
269   fprintf(OUT, SIM_SOURCENAME, project);
270   
271   xbt_dict_foreach(process_function_set,cursor,key,data) {
272     fprintf(OUT, " ");
273     fprintf(OUT, RL_SOURCENAME, project,key);
274   }
275   fprintf(OUT, "\n");
276
277   fprintf(OUT, "\n# generate temps\n");
278   fprintf(OUT, "\n# A rule to generate the source file each time the deployment file changes\n");
279
280   xbt_dict_foreach(process_function_set,cursor,key,data) {
281     fprintf(OUT, RL_SOURCENAME, project,key);
282     fprintf(OUT, " ");
283   }
284   fprintf(OUT, SIM_SOURCENAME, project);
285   fprintf(OUT, ": %s\n", deployment);
286   fprintf(OUT, "\tgras_stub_generator %s %s >/dev/null\n", project, deployment);
287   fclose(OUT);
288 }
289
290 static void generate_makefile_local(char *project, char *deployment)
291 {
292   xbt_dict_cursor_t cursor=NULL;
293   char *key = NULL;
294   void *data = NULL;
295   char *filename = NULL;
296   FILE *OUT = NULL;
297
298   filename = xbt_new(char,strlen(project) + strlen(MAKEFILE_FILENAME_LOCAL));
299   sprintf(filename,MAKEFILE_FILENAME_LOCAL, project);
300   
301   OUT=fopen(filename,"w");
302   xbt_assert1(OUT, "Unable to open %s for writing",filename);
303   free(filename);
304    
305   fprintf(OUT,
306           "\n"
307           "####\n"
308           "#### THIS FILE WAS GENERATED, DO NOT EDIT BEFORE RENAMING IT\n"
309           "####\n\n\n");
310
311   fprintf(OUT,"## Variable declarations\n"
312               "PROJECT_NAME=%s\n"
313               "DISTDIR=gras-$(PROJECT_NAME)\n\n"
314           ,project);
315    
316   fprintf(OUT,
317           "# Set the GRAS_ROOT environment variable to the path under which you installed SimGrid\n"
318           "# Compilation will fail if you don't do so\n" 
319           "GRAS_ROOT?= $(shell echo \"\\\"<<<< GRAS_ROOT undefined !!! >>>>\\\"\")\n\n"
320           "# You can fiddle the following to make it fit your taste\n"
321           "INCLUDES = -I$(GRAS_ROOT)/include\n"
322           "CFLAGS = -O3 -w -g $(INCLUDES)\n"
323           "LIBS_SIM = -lm  -L$(GRAS_ROOT)/lib/ -lsimgrid\n"
324           "LIBS_RL = -lm  -L$(GRAS_ROOT)/lib/ -lgras\n"
325           "LIBS = \n"
326           "\n");
327
328   fprintf(OUT, "C_FILES = ");
329   fprintf(OUT, SIM_SOURCENAME" ",project);
330   xbt_dict_foreach(process_function_set,cursor,key,data) {
331     fprintf(OUT, RL_SOURCENAME " ",project, key);
332   }
333   fprintf(OUT, "%s.c\n", project);
334
335   fprintf(OUT,"OBJ_FILES = \n");
336
337   fprintf(OUT, "BIN_FILES = ");
338
339   fprintf(OUT, SIM_BINARYNAME " ",project);
340   xbt_dict_foreach(process_function_set,cursor,key,data) {
341     fprintf(OUT, RL_BINARYNAME " ", project, key);
342   }
343   fprintf(OUT, "\n");
344
345   fprintf(OUT,
346           "\n"
347           "## By default, build all the binaries\n"
348           "all: $(BIN_FILES)\n"
349           "\n");
350    
351   fprintf(OUT, "\n## generate temps: regenerate the source file each time the deployment file changes\n");
352   xbt_dict_foreach(process_function_set,cursor,key,data) {
353     fprintf(OUT, RL_SOURCENAME, project,key);
354     fprintf(OUT, " ");
355   }
356   fprintf(OUT, SIM_SOURCENAME, project);
357   fprintf(OUT, ": %s\n", deployment);
358   fprintf(OUT, "\tgras_stub_generator %s %s >/dev/null\n", project, deployment);
359
360   fprintf(OUT, "\n## Generate the binaries\n");
361   fprintf(OUT, SIM_BINARYNAME ": " SIM_OBJNAME " %s.o\n",project, project, project);
362   fprintf(OUT, "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS_SIM) $(LIBS) $(LDADD) -o $@ \n");
363   xbt_dict_foreach(process_function_set,cursor,key,data) {
364     fprintf(OUT, RL_BINARYNAME " : " RL_OBJNAME " %s.o\n", project, key, project, key, project);
365     fprintf(OUT, "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS_RL) $(LIBS) $(LDADD) -o $@ \n");
366   }  
367   fprintf(OUT, 
368           "\n"
369           "%%: %%.o\n"
370           "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS) $(LDADD) -o $@ \n"
371           "\n"
372           "%%.o: %%.c\n"
373           "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) -c -o $@ $<\n"
374           "\n");
375    
376   fprintf(OUT,
377           "##┬áRules for tarballs and cleaning\n"
378           "DIST_FILES= $(C_FILES) "MAKEFILE_FILENAME_LOCAL" " /*MAKEFILE_FILENAME_REMOTE*/"\n"
379           "distdir: $(DIST_FILES)\n"
380           "\trm -rf $(DISTDIR)\n"
381           "\tmkdir -p $(DISTDIR)\n"
382           "\tcp $^ $(DISTDIR)\n"
383           "\n"
384           "dist: clean distdir\n"
385           "\ttar c $(DISTDIR) | gzip -c9 > $(DISTDIR).tar.gz\n"
386           "\n", project /*, project*/);
387
388   fprintf(OUT,
389           "clean:\n"
390           "\trm -f $(BIN_FILES) $(OBJ_FILES) *~ %s.o " SIM_OBJNAME, project, project);
391   xbt_dict_foreach(process_function_set,cursor,key,data) {
392      fprintf(OUT, " " RL_OBJNAME, project, key);
393   }
394   fprintf(OUT,   
395           "\n"
396           "\trm -rf $(DISTDIR)\n"
397           "\n"
398           ".SUFFIXES:\n"
399           ".PHONY : clean\n"
400           "\n");
401   /* 
402   fprintf(OUT, "############ REMOTE COMPILING #########\n");
403   fprintf(OUT, 
404           "MACHINES ?= ");
405   xbt_dict_foreach(machine_set,cursor,key,data) {
406     fprintf(OUT, "%s ",key);
407   }
408   fprintf(OUT,"\n");
409
410   fprintf(OUT, 
411           "INSTALL_PATH ?='$$HOME/tmp/src' ### Has to be an absolute path !!! \n"
412           "GRAS_ROOT ?='$(INSTALL_PATH)' ### Has to be an absolute path !!! \n"
413           "SRCDIR ?= ./\n"
414           "SIMGRID_URL ?=http://gcl.ucsd.edu/simgrid/dl/\n"
415           "SIMGRID_VERSION ?=2.92\n"
416           "GRAS_PROJECT ?= %s\n"
417           "GRAS_PROJECT_URL ?= http://www-id.imag.fr/Laboratoire/Membres/Legrand_Arnaud/gras_test/\n"
418           "\n"
419           "remote:\n"
420           "\t@echo;echo \"----[ Compile the package on remote hosts ]----\"\n"
421           "\t@test -e $(SRCDIR)/buildlogs/ || mkdir -p $(SRCDIR)/buildlogs/\n"
422           "\t for site in $(MACHINES) ; do \\\n"
423           "\t   machine=`echo $$site |sed 's/^\\([^%%]*\\)%%.*$$/\\1/'`;\\\n"
424           "\t   machine2=`echo $$site |sed 's/^\\([^%%]*\\)%%\\(.*\\)$$/\\2/'`;\\\n"
425           "\t   cmd_mkdir=\"\\\"sh -c 'env INSTALL_PATH=$(INSTALL_PATH) GRAS_ROOT=$(GRAS_ROOT) \\\n"
426           "\t                        SIMGRID_URL=$(SIMGRID_URL) SIMGRID_VERSION=$(SIMGRID_VERSION) GRAS_PROJECT=$(GRAS_PROJECT) \\\n"
427           "\t                        GRAS_PROJECT_URL=$(GRAS_PROJECT_URL)  mkdir -p $(INSTALL_PATH) 2>&1'\\\"\";\\\n"
428           "\t   cmd_make=\"\\\"sh -c 'env INSTALL_PATH=$(INSTALL_PATH) GRAS_ROOT=$(GRAS_ROOT) \\\n"
429           "\t                        SIMGRID_URL=$(SIMGRID_URL) SIMGRID_VERSION=$(SIMGRID_VERSION) GRAS_PROJECT=$(GRAS_PROJECT) \\\n"
430           "\t                        GRAS_PROJECT_URL=$(GRAS_PROJECT_URL)  make -C $(INSTALL_PATH) -f "MAKEFILE_FILENAME_REMOTE" $(ACTION) 2>&1'\\\"\";\\\n"
431           "\t   if echo $$site | grep  '%%' >/dev/null ; then \\\n"
432           "\t     echo \"----[ Compile on $$machine2 (behind $$machine) ]----\";\\\n"
433           "\t   else \\\n"
434           "\t     machine=$$site;\\\n"
435           "\t     echo \"----[ Compile on $$machine ]----\";\\\n"
436           "\t   fi;\\\n"
437           "\t   if echo $$site | grep  '%%' >/dev/null ; then \\\n"
438           "\t     if ssh $$machine \"ssh -A $$machine2 $$cmd_mkdir\" 2>&1 > $(SRCDIR)/buildlogs/$$site.log;\\\n"
439           "\t     then true; else failed=1;echo \"Failed (check $(SRCDIR)/buildlogs/$$site.log)\"; fi;\\\n"
440           "\t   else \\\n"
441           "\t     if ssh $$machine \"eval $$cmd_mkdir\" 2>&1 > $(SRCDIR)/buildlogs/$$site.log ;\\\n"
442           "\t     then true; else failed=1;echo \"Failed (check $(SRCDIR)/buildlogs/$$site.log)\"; fi; \\\n"
443           "\t   fi;\\\n"
444           "\t   echo \"-- Copy the data over\"; \\\n"
445           "\t   scp "MAKEFILE_FILENAME_REMOTE" $$site:$(INSTALL_PATH) ;\\\n"
446           "\t   echo \"-- Compiling... (the output gets into $(SRCDIR)/buildlogs/$$site.log)\"; \\\n"
447           "\t   if echo $$site | grep  '%%' >/dev/null ; then \\\n"
448           "\t     if ssh $$machine \"ssh -A $$machine2 $$cmd_make\" 2>&1 >> $(SRCDIR)/buildlogs/$$site.log;\\\n"
449           "\t     then echo \"Sucessful\"; else failed=1;echo \"Failed (check $(SRCDIR)/buildlogs/$$site.log)\"; fi;echo; \\\n"
450           "\t   else \\\n"
451           "\t     if ssh $$machine \"eval $$cmd_make\" 2>&1 >> $(SRCDIR)/buildlogs/$$site.log ;\\\n"
452           "\t     then echo \"Sucessful\"; else failed=1;echo \"Failed (check $(SRCDIR)/buildlogs/$$site.log)\"; fi;echo; \\\n"
453           "\t   fi;\\\n"
454           "\t done;\n",project,project,project);
455 */
456   fclose(OUT);
457 }
458
459 static void generate_makefile_remote(char *project, char *deployment)
460 {
461   char *filename = NULL;
462   FILE *OUT = NULL;
463
464   filename = xbt_new(char,strlen(project) + strlen(MAKEFILE_FILENAME_REMOTE));
465   sprintf(filename,MAKEFILE_FILENAME_REMOTE, project);
466   
467   OUT=fopen(filename,"w");
468   xbt_assert1(OUT, "Unable to open %s for writing",filename);
469
470   fprintf(OUT, 
471           "INSTALL_PATH ?= $(shell pwd)\n"
472           "\n"
473           "compile-simgrid:\n"
474           "\tcd $$GRAS_ROOT ; \\\n"
475           "\tretrieved=`LANG=C;wget -N $(SIMGRID_URL)/simgrid-$(SIMGRID_VERSION).tar.gz 2>&1 | grep newer | sed 's/.*no newer.*/yes/'`; \\\n"
476           "\techo $$retrieved; \\\n"
477           "\tif test \"x$$retrieved\" = x; then \\\n"
478           "\t  tar zxf simgrid-$(SIMGRID_VERSION).tar.gz ; \\\n"
479           "\t  cd simgrid-$(SIMGRID_VERSION)/; \\\n"
480           "\t  ./configure --prefix=$$GRAS_ROOT ; \\\n"
481           "\t  make all install ;\\\n"
482            "\tfi\n"
483           "\n"
484           "compile-gras: compile-simgrid\n"
485           "\tnot_retrieved=`LANG=C;wget -N $(GRAS_PROJECT_URL)/gras-$(GRAS_PROJECT).tar.gz 2>&1 | grep newer | sed 's/.*no newer.*/yes/'`; \\\n"
486           "\techo $$not_retrieved; \\\n"
487           "\tif test \"x$$not_retrieved\" != xyes; then \\\n"
488           "\t  tar zxf gras-$(GRAS_PROJECT).tar.gz ; \\\n"
489           "\t  make -C gras-$(GRAS_PROJECT)/ -f $(GRAS_PROJECT).Makefile.local all ; \\\n"
490           "\tfi\n"
491           "\n"
492           "clean-simgrid:\n"
493           "\trm -rf simgrid-$(SIMGRID_VERSION)*\n"
494           "clean-gras clean-gras-$(GRAS_PROJECT):\n"
495           "\trm -rf gras-$(GRAS_PROJECT)*\n"
496           "clean: clean-simgrid clean-gras-$(GRAS_PROJECT)\n"
497           "\n"
498           ".PHONY: clean clean-simgrid clean-gras clean-gras-$(GRAS_PROJECT) \\\n"
499           "        compile-simgrid compile-gras\n"
500           );
501   fclose(OUT);
502 }
503
504
505 static void generate_deployment(char *project, char *deployment)
506 {
507   xbt_dict_cursor_t cursor=NULL;
508   char *key = NULL;
509   void *data = NULL;
510   char *filename = NULL;
511   FILE *OUT = NULL;
512
513   int cpt,i;
514   s_process_t proc;
515
516   filename = xbt_new(char,strlen(project) + strlen(DEPLOYMENT));
517   sprintf(filename,DEPLOYMENT, project);
518   
519   OUT=fopen(filename,"w");
520   xbt_assert1(OUT, "Unable to open %s for writing",filename);
521
522   fprintf(OUT, "#!/bin/sh\n");
523   fprintf(OUT, "############ DEPLOYMENT FILE #########\n");
524   fprintf(OUT,
525           "if test \"${MACHINES+set}\" != set; then \n"
526           "    export MACHINES='");
527   xbt_dict_foreach(machine_set,cursor,key,data) {
528     fprintf(OUT, "%s ",key);
529   }
530   fprintf(OUT,  
531           "';\n"
532           "fi\n"
533           "if test \"${INSTALL_PATH+set}\" != set; then \n"
534           "    export INSTALL_PATH='`echo $HOME`/tmp/src'\n"
535           "fi\n"
536           "if test \"${GRAS_ROOT+set}\" != set; then \n"
537           "    export GRAS_ROOT='`echo $INSTALL_PATH`'\n"
538           "fi\n"
539           "if test \"${SRCDIR+set}\" != set; then \n"
540           "    export SRCDIR=./\n"
541           "fi\n"
542           "if test \"${SIMGRID_URL+set}\" != set; then \n"
543           "    export SIMGRID_URL=http://gcl.ucsd.edu/simgrid/dl/\n"
544           "fi\n"
545           "if test \"${SIMGRID_VERSION+set}\" != set; then \n"
546           "    export SIMGRID_VERSION=2.91\n"
547           "fi\n"
548           "if test \"${GRAS_PROJECT+set}\" != set; then \n"
549           "    export GRAS_PROJECT=%s\n"
550           "fi\n"
551           "if test \"${GRAS_PROJECT_URL+set}\" != set; then \n"
552           "    export GRAS_PROJECT_URL=http://www-id.imag.fr/Laboratoire/Membres/Legrand_Arnaud/gras_test/\n"
553           "fi\n"
554           "\n"
555           "test -e runlogs/ || mkdir -p runlogs/\n",
556           project);
557
558   fprintf(OUT,  
559           "cmd_prolog=\"env INSTALL_PATH=$INSTALL_PATH GRAS_ROOT=$GRAS_ROOT \\\n"
560           "                 SIMGRID_URL=$SIMGRID_URL SIMGRID_VERSION=$SIMGRID_VERSION GRAS_PROJECT=$GRAS_PROJECT \\\n"
561           "                 GRAS_PROJECT_URL=$GRAS_PROJECT_URL LD_LIBRARY_PATH=$GRAS_ROOT/lib/ sh -c \";\n");
562
563   xbt_dynar_foreach (process_list,cpt,proc) {
564     fprintf(OUT,"cmd=\"\\$INSTALL_PATH/gras-%s/"RL_BINARYNAME" ",project,project,proc.argv[0]);
565     for(i=1;i<proc.argc;i++) {
566       fprintf(OUT,"%s ",proc.argv[i]);
567     }
568     fprintf(OUT,"\";\n");
569     fprintf(OUT,"ssh %s \"$cmd_prolog 'export LD_LIBRARY_PATH=\\$INSTALL_PATH/lib:\\$LD_LIBRARY_PATH; echo \\\"$cmd\\\" ; $cmd 2>&1'\" > runlogs/%s_%d.log &\n",proc.host,proc.host,cpt);
570   }
571
572   fclose(OUT);
573 }
574
575 static void print(void *p)
576 {
577   printf("%p",p);
578 }
579
580 int main(int argc, char *argv[])
581 {
582   char *project_name = NULL;
583   char *deployment_file = NULL;
584   int i;
585    
586   surf_init(&argc, argv);
587
588   xbt_assert1((argc >= 3),"Usage: %s project_name deployment_file [deployment_file...]\n",argv[0]);
589
590   project_name = argv[1];
591
592   process_function_set = xbt_dict_new();
593   process_list = xbt_dynar_new(sizeof(s_process_t),s_process_free);
594   machine_set = xbt_dict_new();
595
596   STag_surfxml_process_fun = parse_process_init;
597   ETag_surfxml_argument_fun = parse_argument;
598   ETag_surfxml_process_fun = parse_process_finalize;
599   for(i=2; i<argc; i++) {
600      deployment_file = argv[i];
601      surf_parse_open(deployment_file);
602      if(surf_parse()) xbt_assert1(0,"Parse error in %s",deployment_file);
603      surf_parse_close();
604   }
605
606
607   warning = xbt_new(char,strlen(WARN)+strlen(deployment_file)+10);
608   sprintf(warning,WARN,deployment_file);
609
610   if(XBT_LOG_ISENABLED(stubgen, xbt_log_priority_debug)) {
611     xbt_dict_cursor_t cursor=NULL;
612     char *key = NULL;
613     void *data = NULL;
614
615     for (cursor=NULL, xbt_dict_cursor_first((process_function_set),&(cursor)) ;
616          xbt_dict_cursor_get_or_free(&(cursor),&(key),(void**)(&data));
617          xbt_dict_cursor_step(cursor) ) {
618       DEBUG1("Function %s", key);      
619     }
620     
621     xbt_dict_dump(process_function_set,print);
622   }
623
624   generate_sim(project_name);
625   generate_rl(project_name);
626   generate_makefile_local(project_name, deployment_file);
627 //  generate_makefile_remote(project_name, deployment_file);
628 //  generate_deployment(project_name, deployment_file);
629
630   free(warning);
631   surf_exit();
632   return 0;
633 }