Logo AND Algorithmique Numérique Distribuée

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