Logo AND Algorithmique Numérique Distribuée

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