Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
stub_genarator.c adaptation for Borland Builder Compiler
[simgrid.git] / tools / gras / stub_generator.c
1 //---------------------------------------------------------------------------
2
3 /* specific to Borland Compiler */
4 #ifdef __BORLANDDC__
5 #pragma hdrstop
6 #endif
7
8 /*      $Id$     */
9
10 /* gras_stub_generator - creates the main() to use a GRAS program           */
11
12 /* Copyright (c) 2003,2004,2005 Martin Quinson, Arnaud Legrand. 
13    All rights reserved.                  */
14
15 /* This program is free software; you can redistribute it and/or modify it
16  * under the terms of the license (GNU LGPL) which comes with this package. */
17
18
19
20 #include "xbt/sysdep.h"
21 #include "xbt/function_types.h"
22 #include "xbt/log.h"
23 #include "surf/surfxml_parse.h"
24 #include "surf/surf.h"
25
26
27
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(stubgen,gras,"Stub generator");
29
30 #ifdef _WIN32
31
32 /* tabulation level (used to indent the lines of the borland project file */
33 static unsigned int level = 0;
34
35 #ifndef MAX_PATH
36 #define MAX_PATH 260
37 #endif
38
39 /*
40  * A structure which represents a borland project file.
41  */
42 typedef struct s_borland_project
43 {
44     const char* xml_version;/* the xml version used to write the borland project file                   */
45     const char* encoding;   /* the encoding used to write the borland project file                      */
46     const char* comment;    /* the xml comment to write at the begining of the borland project file     */
47         char* name;                     /* the name of the borland project file                                                                         */
48         FILE* stream;                   /* the stream to the borland project file                                                                       */
49         const char* version;    /* the builder version of the borland project file                                                      */
50         char* bin_dir;          /* the directory used to store the generated program                                            */
51         char* obj_dir;          /* the directory used to store the generated object files                                       */
52         char* lib_dir;          /* the directory used to store the librairies used in the borland project       */
53         char* src_dir;          /* the directory use to store the source files of the project                           */
54 }s_borland_project_t,* borland_project_t;
55
56 /*
57  * Write tabs in a borland project file.
58  * @param project The project concerned by the operation.
59  * @param count The count tab to write
60  */
61 void
62 borland_project_write_tabs(borland_project_t project,unsigned int count);
63
64 /*
65  * Write the begin of an xml node in the borland project file.
66  * @param project The borland project concerned by this operation.
67  * @param name The name of the node.
68  */
69 void
70 borland_project_begin_xml_node(borland_project_t project, const char* name);
71
72
73 /*
74  * Write the end of an xml node in the borland project file.
75  * @param project The borland project concerned by this operation.
76  * @param name The name of the node.
77  */
78 void
79 borland_project_end_xml_node(borland_project_t project, const char* name);
80
81 /*
82  * Write an xml element in a borland project file.
83  * @param project The borland project concerned by this operation.
84  * @param name The name of the element to write
85  * @param value The value of the element  
86  */
87 void
88 borland_project_write_xml_element(borland_project_t project,const char* name,const char* value);
89
90 /*
91  * Write a FILE xml element in the borland project file.
92  * @param project The borland project concerned by this operation.
93  * @param file_name The value of the attribute FILENAME.
94  * @param form_name The value of the attribute FORMNAME.
95  * @param unit_name The value of the attribute UNITNAME.
96  * @param container_id The value of the attribute CONTAINERID.
97  * @param design_claas The value of the attribute DESIGNCLASS.
98  * @param local_command The value of the attribute LOCALCOMMAND.
99  */
100 void
101 borland_project_write_file_element(     borland_project_t project,
102                                                                         const char* file_name,
103                                                                         const char* form_name,
104                                                                         const char* unit_name,
105                                                                         const char* container_id,
106                                                                         const char* design_class,
107                                                                         const char* local_command);
108 /*
109  * Write all options of the IDE of the Borland Builder C++ compiler.
110  * @ param project The project concerned by this operation.
111  */
112
113 void
114 borland_project_write_ide_options(borland_project_t project);
115
116 /*
117  * Write the xml header of the xml document.
118  * @param project The project concerned by the operation.
119  */
120 void
121 borland_project_write_xml_header(borland_project_t project);
122
123 /*
124  * Write an xml comment in a borland project file
125  * @param project The project concerned by this operation.
126  */
127 void
128 borland_project_write_xml_comment(borland_project_t project);
129
130 /*
131  * Create a bpf file used by a borland project file.
132  * @param name The name of the bpf file to create.
133  */
134 void
135 borland_project_create_main_file(const char* name);
136
137 /*
138  * Create a borland project file.
139  * @param project The project concerned by this operation.
140  */
141 void
142 borland_project_create(borland_project_t project);
143
144 /*
145  * Close a borland project file.
146  * @param project the borland project file to close.
147  */
148 void
149 borland_project_close(borland_project_t project);
150
151
152 /*
153  * Generate a borland simulation poject file.
154  * @param name The name of the simulation project
155  */
156 void
157 generate_borland_simulation_project(const char* name);
158
159 /*
160  * Generate a borland project file for real life.
161  * @param name The name of the project to create.
162  */
163 void
164 generate_borland_real_life_project(const char* name);
165
166 /*
167  * Generate a borland project file.
168  * @param project The borland project to generate.
169  */
170 void
171 generate_borland_project(borland_project_t project);
172
173 /*
174  * Find the path of a file.
175  * @param file_name The file name to find.
176  * @path path If founded this parameter will contain the path of file.
177  * @return If successful the function returns 1. Otherwise the function
178  *  retruns 0;
179  */
180 int
181 FindFilePath(const char* root_dir,const char* file_name,char* path);
182
183 #endif
184
185
186
187 #define WARN "/***********\n * DO NOT EDIT! THIS FILE HAS BEEN AUTOMATICALLY GENERATED FROM %s BY gras_stub_generator\n ***********/\n"
188 #define SIM_SOURCENAME  "_%s_simulator.c"
189 #define SIM_OBJNAME  "_%s_simulator.o"
190 #define SIM_BINARYNAME  "%s_simulator"
191 #define SIM_SOURCENAME_LDADD  "%s_simulator_LDADD"
192 #define SIM_SOURCENAME_SOURCES  "%s_simulator_SOURCES"
193 #define RL_SOURCENAME  "_%s_%s.c"
194 #define RL_OBJNAME  "_%s_%s.o"
195 #define RL_BINARYNAME  "%s_%s"
196 #define RL_SOURCENAME_LDADD  "%s_%s_LDADD"
197 #define RL_SOURCENAME_SOURCES  "%s_%s_SOURCES"
198 #define MAKEFILE_FILENAME_AM  "%s.Makefile.am"
199 #define MAKEFILE_FILENAME_LOCAL  "%s.mk"
200 #define MAKEFILE_FILENAME_REMOTE  "%s.Makefile.remote"
201 #define DEPLOYMENT  "%s.deploy.sh"
202
203 char *warning = NULL;
204
205 /**********************************************/
206 /**** Generate the file for the simulator *****/
207 /**********************************************/
208
209 const char *SIM_PREEMBULE =
210 "/* specific to Borland Compiler */\n"
211 "#ifdef __BORLANDC__\n"
212 "#pragma hdrstop\n"
213 "#endif\n\n"
214 "#include <stdlib.h>\n"
215 "#include <stdio.h>\n"
216 "#include \"msg/msg.h\"\n"
217 "#include <gras.h>\n"
218 "\n"
219 "char *gras_log=NULL;\n";
220
221
222 #define SIM_LAUNCH_FUNC  \
223 "int launch_%s(int argc, char **argv) {\n" \
224 "  char **myargv=argv;\n" \
225 "  int myargc=argc;\n" \
226 "  int i;\n" \
227 "  int retcode;\n"\
228 "    \n"\
229 "  if (gras_log) {\n"\
230 "    myargv=malloc((argc+1) * sizeof(char**));\n" \
231 "    for (i=0; i<argc; i++)\n" \
232 "      myargv[i] = argv[i];\n" \
233 "    myargv[myargc++] = gras_log;\n" \
234 "  }\n" \
235 "  retcode = %s(myargc,myargv);\n" \
236 "  if (myargv != argv)\n" \
237 "    free(myargv);\n" \
238 "  return retcode;\n" \
239 "}\n"
240
241 const char *SIM_MAIN_POSTEMBULE = "\n"
242 "\n"
243 "  MSG_launch_application(argv[2]);\n"
244 "\n"
245 "  /*  Run the simulation */\n"
246 "  MSG_main();\n"
247 "\n"
248 "  /* cleanup the place */\n"
249 "  MSG_clean();\n"
250 "  if (gras_log)\n"
251 "    free(gras_log);\n"
252 "  return 0;\n"
253 "}\n";
254
255
256
257 /**********************************************/
258 /********* Parse XML deployment file **********/
259 /**********************************************/
260 xbt_dict_t process_function_set = NULL;
261 xbt_dynar_t process_list = NULL;
262 xbt_dict_t machine_set = NULL;
263
264 typedef struct s_process_t {
265   int argc;
266   char **argv;
267   char *host;
268 } s_process_t;
269
270 static void s_process_free(void *process)
271 {
272   int i;
273   for(i=0;i<((s_process_t*)process)->argc; i++)
274     free(((s_process_t*)process)->argv[i]);
275   free(((s_process_t*)process)->host);
276 }
277
278 static s_process_t process;
279
280 /*
281  * Création de deux dictionnaires
282  */
283 static void parse_process_init(void)
284 {
285   xbt_dict_set(process_function_set, A_surfxml_process_function, NULL, NULL);
286   xbt_dict_set(machine_set, A_surfxml_process_host, NULL, NULL);
287   process.argc = 1 ;
288   process.argv = xbt_new(char*,1);
289   process.argv[0] = xbt_strdup(A_surfxml_process_function);
290   process.host=strdup(A_surfxml_process_host);
291   /*VERB1("Function: %s",A_surfxml_process_function);*/
292 }
293
294 static void parse_argument(void)
295 {
296   process.argc++;
297   process.argv = xbt_realloc(process.argv, (process.argc) * sizeof(char *));
298   process.argv[(process.argc) - 1] = xbt_strdup(A_surfxml_argument_value);
299 }
300
301 static void parse_process_finalize(void)
302 {
303   xbt_dynar_push(process_list,&process);
304   /*VERB1("Function: %s",process.argv[0]);*/
305 }
306
307 static void generate_sim(char *project)
308 {
309         xbt_dict_cursor_t cursor=NULL;
310         char *key = NULL;
311         void *data = NULL;
312         char *filename = NULL;
313         FILE *OUT = NULL;
314         
315         /* 
316          * Creation d'un fichier nommé : <projet>_simulator.c 
317          */
318         filename = xbt_new(char,strlen(project) + strlen(SIM_SOURCENAME));
319         sprintf(filename,SIM_SOURCENAME,project);
320         
321         OUT=fopen(filename,"w");
322         
323         
324         xbt_assert1(OUT, "Unable to open %s for writing",filename);
325         
326         /*
327          * Ecriture du message d'avertissement.
328          */
329         fprintf(OUT, "%s\n",warning);
330         
331         /*
332          * Ecriture du préambule (inclusion de certains fichiers d'en-tête
333          */
334         fprintf(OUT, "%s", SIM_PREEMBULE);
335         
336         /*
337          * Déclaration des fonction int <process>(int argc,char *argv[]);
338          */
339         xbt_dict_foreach(process_function_set,cursor,key,data) {
340                 fprintf(OUT,"int %s(int argc,char *argv[]);\n",key);
341         }
342         
343         fprintf(OUT,"\n");
344         
345         /*
346          * Déclaration des fonction int launch_<process>(int argc,char *argv[]);
347          */
348         xbt_dict_foreach(process_function_set,cursor,key,data) {
349         fprintf(OUT,"int launch_%s(int argc,char *argv[]);\n",key);
350         }
351         
352         /*
353          * Ecriture du message d'avertissement.
354          */
355         fprintf(OUT, "\n%s\n",warning);
356
357         xbt_dict_foreach(process_function_set,cursor,key,data) {
358                 fprintf(OUT,SIM_LAUNCH_FUNC,key,key);
359         }
360         fprintf(OUT, "\n%s\n",warning);
361
362     fprintf(OUT,"%s", "/* specific to Borland Compiler */\n"
363     "#ifdef __BORLANDDC__\n"
364     "#pragma argsused\n"
365     "#endif\n\n");
366         
367         fprintf(OUT, "%s", "int main (int argc,char *argv[]) {\n"
368         "\n" 
369         "  /*  Simulation setup */\n" 
370         "  MSG_global_init(&argc,argv);\n" 
371         "  if (argc != 3) {\n" 
372         "    fprintf(stderr, \"Usage: %s platform.xml deployment.xml [--gras-log=...]\\n\",argv[0]);\n" 
373         "    exit(1);\n" 
374         "  }\n"
375         "\n");
376         fprintf(OUT, 
377         "  MSG_paje_output(\"%s.trace\");\n" 
378         "  MSG_set_channel_number(XBT_MAX_CHANNEL); /* Using at most 10 channel (ports) per host. Change it here if needed */\n" 
379         "  MSG_create_environment(argv[1]);\n" 
380         "\n" 
381         "  /*  Application deployment */\n",
382         project);
383         xbt_dict_foreach(process_function_set,cursor,key,data) {
384                 fprintf(OUT,"  MSG_function_register(\"%s\", launch_%s);\n",key,key);
385         }
386         fprintf(OUT, "%s", SIM_MAIN_POSTEMBULE);
387         fclose(OUT);
388         free(filename);
389 }
390
391 /**********************************************/
392 /**** Generate the file for the real life *****/
393 /**********************************************/
394 static void generate_rl(char *project)
395 {
396   xbt_dict_cursor_t cursor=NULL;
397   char *key = NULL;
398   void *data = NULL;
399   char *filename = NULL;
400   FILE *OUT = NULL;
401
402   xbt_dict_foreach(process_function_set,cursor,key,data) {
403     filename = xbt_new(char,strlen(project) + strlen(RL_SOURCENAME) + strlen(key));
404     sprintf(filename,RL_SOURCENAME,project,key);
405
406     OUT=fopen(filename,"w");
407     xbt_assert1(OUT, "Unable to open %s for writing",filename);
408
409     fprintf(OUT, "\n%s\n",warning);
410     fprintf(OUT, "/* specific to Borland Compiler */\n" \
411                  "#ifdef __BORLANDC__\n" \
412                  "#pragma hdrstop\n" \
413                  "#endif\n\n" \
414                  "#include <stdio.h>\n" \
415                  "#include <signal.h>\n" \
416                  "#include <gras.h>\n" \
417                  "\n" \
418                  "extern const char *_gras_procname;\n" \
419                  "/* user code */\n" \
420                  "int %s(int argc, char *argv[]);\n" \
421                  "\n" \
422                  "/* specific to Borland Compiler */\n" \
423                 "#ifdef __BORLANDC__\n" \
424                 "#pragma argsused\n" \
425                 "#endif\n\n" \
426                  "int main(int argc, char *argv[]){\n" \
427                  "  int errcode;\n" \
428                  "\n" \
429                  "  _gras_procname = \"%s\";\n" \
430                  "  errcode=%s(argc,argv);\n"\
431                  " \n" \
432                  "  return errcode;\n"\
433                  "}\n",
434             key,key,key);
435     fprintf(OUT, "\n%s\n",warning);
436     fclose(OUT);
437     free(filename);
438   }
439 }
440
441 static void generate_makefile_am(char *project, char *deployment)
442 {
443   xbt_dict_cursor_t cursor=NULL;
444   char *key = NULL;
445   void *data = NULL;
446   char *filename = NULL;
447   FILE *OUT = NULL;
448
449   filename = xbt_new(char,strlen(project) + strlen(MAKEFILE_FILENAME_AM));
450   sprintf(filename,MAKEFILE_FILENAME_AM, project);
451
452   OUT=fopen(filename,"w");
453   xbt_assert1(OUT, "Unable to open %s for writing",filename);
454
455   fprintf(OUT, "# AUTOMAKE variable definition\n");
456   fprintf(OUT, "INCLUDES= @CFLAGS_SimGrid@\n\n");
457   fprintf(OUT, "PROGRAMS=");
458   fprintf(OUT, SIM_BINARYNAME,project);
459
460   xbt_dict_foreach(process_function_set,cursor,key,data) {
461     fprintf(OUT, " ");
462     fprintf(OUT, RL_BINARYNAME, project, key);
463   }
464
465   fprintf(OUT, "\n\n");
466   fprintf(OUT, SIM_SOURCENAME_SOURCES,project);
467   fprintf(OUT, "=\t");
468   fprintf(OUT, SIM_SOURCENAME,project);
469   fprintf(OUT, " %s.c\n", project);
470   fprintf(OUT, SIM_SOURCENAME_LDADD, project);
471   fprintf(OUT, "=\tpath/to/libsimgrid.a\n\n");
472
473   xbt_dict_foreach(process_function_set,cursor,key,data) {
474     fprintf(OUT, RL_SOURCENAME_SOURCES, project,key);
475     fprintf(OUT, "=\t");
476     fprintf(OUT, RL_SOURCENAME, project,key);
477     fprintf(OUT, " %s.c\n", project);
478     fprintf(OUT, RL_SOURCENAME_LDADD, project, key);
479     fprintf(OUT, "=\tpath/to/libgras.a\n\n");
480   }
481
482   fprintf(OUT, "\n# cleanup temps\n");
483   fprintf(OUT, "CLEANFILES= ");
484   fprintf(OUT, SIM_SOURCENAME, project);
485   
486   xbt_dict_foreach(process_function_set,cursor,key,data) {
487     fprintf(OUT, " ");
488     fprintf(OUT, RL_SOURCENAME, project,key);
489   }
490   fprintf(OUT, "\n");
491
492   fprintf(OUT, "\n# generate temps\n");
493   fprintf(OUT, "\n# A rule to generate the source file each time the deployment file changes\n");
494
495   xbt_dict_foreach(process_function_set,cursor,key,data) {
496     fprintf(OUT, RL_SOURCENAME, project,key);
497     fprintf(OUT, " ");
498   }
499   fprintf(OUT, SIM_SOURCENAME, project);
500   fprintf(OUT, ": %s\n", deployment);
501   fprintf(OUT, "\tgras_stub_generator %s %s >/dev/null\n", project, deployment);
502   fclose(OUT);
503 }
504
505 static void generate_makefile_local(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   filename = xbt_new(char,strlen(project) + strlen(MAKEFILE_FILENAME_LOCAL));
514   sprintf(filename,MAKEFILE_FILENAME_LOCAL, project);
515   
516   OUT=fopen(filename,"w");
517   xbt_assert1(OUT, "Unable to open %s for writing",filename);
518   free(filename);
519    
520   fprintf(OUT,
521           "\n"
522           "####\n"
523           "#### THIS FILE WAS GENERATED, DO NOT EDIT BEFORE RENAMING IT\n"
524           "####\n\n\n");
525
526   fprintf(OUT,"## Variable declarations\n"
527               "PROJECT_NAME=%s\n"
528               "DISTDIR=gras-$(PROJECT_NAME)\n\n"
529           ,project);
530    
531   fprintf(OUT,
532           "# Set the GRAS_ROOT environment variable to the path under which you installed SimGrid\n"
533           "# Compilation will fail if you don't do so\n" 
534           "GRAS_ROOT?= $(shell echo \"\\\"<<<< GRAS_ROOT undefined !!! >>>>\\\"\")\n\n"
535           "# You can fiddle the following to make it fit your taste\n"
536           "INCLUDES = -I$(GRAS_ROOT)/include\n"
537           "CFLAGS ?= -O3 -w -g\n"
538           "LIBS_SIM = -lm  -L$(GRAS_ROOT)/lib/ -lsimgrid\n"
539           "LIBS_RL = -lm  -L$(GRAS_ROOT)/lib/ -lgras\n"
540           "LIBS = \n"
541           "\n");
542
543   fprintf(OUT, "PRECIOUS_C_FILES ?= %s.c\n", project);
544
545   fprintf(OUT, "GENERATED_C_FILES = ");
546   fprintf(OUT, SIM_SOURCENAME" ",project);
547   xbt_dict_foreach(process_function_set,cursor,key,data) {
548     fprintf(OUT, RL_SOURCENAME " ",project, key);
549   }
550   fprintf(OUT, "\n");
551
552   fprintf(OUT,"OBJ_FILES = $(patsubst %%.c,%%.o,$(PRECIOUS_C_FILES))\n");
553
554   fprintf(OUT, "BIN_FILES = ");
555
556   fprintf(OUT, SIM_BINARYNAME " ",project);
557   xbt_dict_foreach(process_function_set,cursor,key,data) {
558     fprintf(OUT, RL_BINARYNAME " ", project, key);
559   }
560   fprintf(OUT, "\n");
561
562   fprintf(OUT,
563           "\n"
564           "## By default, build all the binaries\n"
565           "all: $(BIN_FILES)\n"
566           "\n");
567    
568   fprintf(OUT, "\n## generate temps: regenerate the source file each time the deployment file changes\n");
569   xbt_dict_foreach(process_function_set,cursor,key,data) {
570     fprintf(OUT, RL_SOURCENAME, project,key);
571     fprintf(OUT, " ");
572   }
573   fprintf(OUT, SIM_SOURCENAME, project);
574   fprintf(OUT, ": %s\n", deployment);
575   fprintf(OUT, "\tgras_stub_generator %s %s >/dev/null\n", project, deployment);
576
577   fprintf(OUT, "\n## Generate the binaries\n");
578   fprintf(OUT, SIM_BINARYNAME ": " SIM_OBJNAME " $(OBJ_FILES)\n",project, project);
579   fprintf(OUT, "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS_SIM) $(LIBS) $(LDADD) -o $@ \n");
580   xbt_dict_foreach(process_function_set,cursor,key,data) {
581     fprintf(OUT, RL_BINARYNAME " : " RL_OBJNAME " $(OBJ_FILES)\n", project, key, project, key);
582     fprintf(OUT, "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS_RL) $(LIBS) $(LDADD) -o $@ \n");
583   }  
584   fprintf(OUT, 
585           "\n"
586           "%%: %%.o\n"
587           "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS) $(LDADD) -o $@ \n"
588           "\n"
589           "%%.o: %%.c\n"
590           "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) -c -o $@ $<\n"
591           "\n");
592    
593   fprintf(OUT,
594           "## Rules for tarballs and cleaning\n"
595           "DIST_FILES= $(EXTRA_DIST) $(GENERATED_C_FILES) $(PRECIOUS_C_FILES) "MAKEFILE_FILENAME_LOCAL" " /*MAKEFILE_FILENAME_REMOTE*/"\n"
596           "distdir: $(DIST_FILES)\n"
597           "\trm -rf $(DISTDIR)\n"
598           "\tmkdir -p $(DISTDIR)\n"
599           "\tcp $^ $(DISTDIR)\n"
600           "\n"
601           "dist: clean distdir\n"
602           "\ttar c $(DISTDIR) | gzip -c9 > $(DISTDIR).tar.gz\n"
603           "\n", project /*, project*/);
604
605   fprintf(OUT,
606           "clean:\n"
607           "\trm -f $(BIN_FILES) $(OBJ_FILES) *~ %s.o " SIM_OBJNAME, project, project);
608   xbt_dict_foreach(process_function_set,cursor,key,data) {
609      fprintf(OUT, " " RL_OBJNAME, project, key);
610   }
611   fprintf(OUT,   
612           "\n"
613           "\trm -rf $(DISTDIR)\n"
614           "\n"
615           ".SUFFIXES:\n"
616           ".PHONY : clean\n"
617           "\n");
618   /* 
619   fprintf(OUT, "############ REMOTE COMPILING #########\n");
620   fprintf(OUT, 
621           "MACHINES ?= ");
622   xbt_dict_foreach(machine_set,cursor,key,data) {
623     fprintf(OUT, "%s ",key);
624   }
625   fprintf(OUT,"\n");
626
627   fprintf(OUT, 
628           "INSTALL_PATH ?='$$HOME/tmp/src' ### Has to be an absolute path !!! \n"
629           "GRAS_ROOT ?='$(INSTALL_PATH)' ### Has to be an absolute path !!! \n"
630           "SRCDIR ?= ./\n"
631           "SIMGRID_URL ?=http://gcl.ucsd.edu/simgrid/dl/\n"
632           "SIMGRID_VERSION ?=2.92\n"
633           "GRAS_PROJECT ?= %s\n"
634           "GRAS_PROJECT_URL ?= http://www-id.imag.fr/Laboratoire/Membres/Legrand_Arnaud/gras_test/\n"
635           "\n"
636           "remote:\n"
637           "\t@echo;echo \"----[ Compile the package on remote hosts ]----\"\n"
638           "\t@test -e $(SRCDIR)/buildlogs/ || mkdir -p $(SRCDIR)/buildlogs/\n"
639           "\t for site in $(MACHINES) ; do \\\n"
640           "\t   machine=`echo $$site |sed 's/^\\([^%%]*\\)%%.*$$/\\1/'`;\\\n"
641           "\t   machine2=`echo $$site |sed 's/^\\([^%%]*\\)%%\\(.*\\)$$/\\2/'`;\\\n"
642           "\t   cmd_mkdir=\"\\\"sh -c 'env INSTALL_PATH=$(INSTALL_PATH) GRAS_ROOT=$(GRAS_ROOT) \\\n"
643           "\t                        SIMGRID_URL=$(SIMGRID_URL) SIMGRID_VERSION=$(SIMGRID_VERSION) GRAS_PROJECT=$(GRAS_PROJECT) \\\n"
644           "\t                        GRAS_PROJECT_URL=$(GRAS_PROJECT_URL)  mkdir -p $(INSTALL_PATH) 2>&1'\\\"\";\\\n"
645           "\t   cmd_make=\"\\\"sh -c 'env INSTALL_PATH=$(INSTALL_PATH) GRAS_ROOT=$(GRAS_ROOT) \\\n"
646           "\t                        SIMGRID_URL=$(SIMGRID_URL) SIMGRID_VERSION=$(SIMGRID_VERSION) GRAS_PROJECT=$(GRAS_PROJECT) \\\n"
647           "\t                        GRAS_PROJECT_URL=$(GRAS_PROJECT_URL)  make -C $(INSTALL_PATH) -f "MAKEFILE_FILENAME_REMOTE" $(ACTION) 2>&1'\\\"\";\\\n"
648           "\t   if echo $$site | grep  '%%' >/dev/null ; then \\\n"
649           "\t     echo \"----[ Compile on $$machine2 (behind $$machine) ]----\";\\\n"
650           "\t   else \\\n"
651           "\t     machine=$$site;\\\n"
652           "\t     echo \"----[ Compile on $$machine ]----\";\\\n"
653           "\t   fi;\\\n"
654           "\t   if echo $$site | grep  '%%' >/dev/null ; then \\\n"
655           "\t     if ssh $$machine \"ssh -A $$machine2 $$cmd_mkdir\" 2>&1 > $(SRCDIR)/buildlogs/$$site.log;\\\n"
656           "\t     then true; else failed=1;echo \"Failed (check $(SRCDIR)/buildlogs/$$site.log)\"; fi;\\\n"
657           "\t   else \\\n"
658           "\t     if ssh $$machine \"eval $$cmd_mkdir\" 2>&1 > $(SRCDIR)/buildlogs/$$site.log ;\\\n"
659           "\t     then true; else failed=1;echo \"Failed (check $(SRCDIR)/buildlogs/$$site.log)\"; fi; \\\n"
660           "\t   fi;\\\n"
661           "\t   echo \"-- Copy the data over\"; \\\n"
662           "\t   scp "MAKEFILE_FILENAME_REMOTE" $$site:$(INSTALL_PATH) ;\\\n"
663           "\t   echo \"-- Compiling... (the output gets into $(SRCDIR)/buildlogs/$$site.log)\"; \\\n"
664           "\t   if echo $$site | grep  '%%' >/dev/null ; then \\\n"
665           "\t     if ssh $$machine \"ssh -A $$machine2 $$cmd_make\" 2>&1 >> $(SRCDIR)/buildlogs/$$site.log;\\\n"
666           "\t     then echo \"Sucessful\"; else failed=1;echo \"Failed (check $(SRCDIR)/buildlogs/$$site.log)\"; fi;echo; \\\n"
667           "\t   else \\\n"
668           "\t     if ssh $$machine \"eval $$cmd_make\" 2>&1 >> $(SRCDIR)/buildlogs/$$site.log ;\\\n"
669           "\t     then echo \"Sucessful\"; else failed=1;echo \"Failed (check $(SRCDIR)/buildlogs/$$site.log)\"; fi;echo; \\\n"
670           "\t   fi;\\\n"
671           "\t done;\n",project,project,project);
672 */
673   fclose(OUT);
674 }
675
676 static void generate_makefile_remote(char *project, char *deployment)
677 {
678   char *filename = NULL;
679   FILE *OUT = NULL;
680
681   filename = xbt_new(char,strlen(project) + strlen(MAKEFILE_FILENAME_REMOTE));
682   sprintf(filename,MAKEFILE_FILENAME_REMOTE, project);
683   
684   OUT=fopen(filename,"w");
685   xbt_assert1(OUT, "Unable to open %s for writing",filename);
686
687   fprintf(OUT, 
688           "INSTALL_PATH ?= $(shell pwd)\n"
689           "\n"
690           "compile-simgrid:\n"
691           "\tcd $$GRAS_ROOT ; \\\n"
692           "\tretrieved=`LANG=C;wget -N $(SIMGRID_URL)/simgrid-$(SIMGRID_VERSION).tar.gz 2>&1 | grep newer | sed 's/.*no newer.*/yes/'`; \\\n"
693           "\techo $$retrieved; \\\n"
694           "\tif test \"x$$retrieved\" = x; then \\\n"
695           "\t  tar zxf simgrid-$(SIMGRID_VERSION).tar.gz ; \\\n"
696           "\t  cd simgrid-$(SIMGRID_VERSION)/; \\\n"
697           "\t  ./configure --prefix=$$GRAS_ROOT ; \\\n"
698           "\t  make all install ;\\\n"
699            "\tfi\n"
700           "\n"
701           "compile-gras: compile-simgrid\n"
702           "\tnot_retrieved=`LANG=C;wget -N $(GRAS_PROJECT_URL)/gras-$(GRAS_PROJECT).tar.gz 2>&1 | grep newer | sed 's/.*no newer.*/yes/'`; \\\n"
703           "\techo $$not_retrieved; \\\n"
704           "\tif test \"x$$not_retrieved\" != xyes; then \\\n"
705           "\t  tar zxf gras-$(GRAS_PROJECT).tar.gz ; \\\n"
706           "\t  make -C gras-$(GRAS_PROJECT)/ -f $(GRAS_PROJECT).Makefile.local all ; \\\n"
707           "\tfi\n"
708           "\n"
709           "clean-simgrid:\n"
710           "\trm -rf simgrid-$(SIMGRID_VERSION)*\n"
711           "clean-gras clean-gras-$(GRAS_PROJECT):\n"
712           "\trm -rf gras-$(GRAS_PROJECT)*\n"
713           "clean: clean-simgrid clean-gras-$(GRAS_PROJECT)\n"
714           "\n"
715           ".PHONY: clean clean-simgrid clean-gras clean-gras-$(GRAS_PROJECT) \\\n"
716           "        compile-simgrid compile-gras\n"
717           );
718   fclose(OUT);
719 }
720
721
722 static void generate_deployment(char *project, char *deployment)
723 {
724   xbt_dict_cursor_t cursor=NULL;
725   char *key = NULL;
726   void *data = NULL;
727   char *filename = NULL;
728   FILE *OUT = NULL;
729
730   int cpt,i;
731   s_process_t proc;
732
733   filename = xbt_new(char,strlen(project) + strlen(DEPLOYMENT));
734   sprintf(filename,DEPLOYMENT, project);
735   
736   OUT=fopen(filename,"w");
737   xbt_assert1(OUT, "Unable to open %s for writing",filename);
738
739   fprintf(OUT, "#!/bin/sh\n");
740   fprintf(OUT, "############ DEPLOYMENT FILE #########\n");
741   fprintf(OUT,
742           "if test \"${MACHINES+set}\" != set; then \n"
743           "    export MACHINES='");
744   xbt_dict_foreach(machine_set,cursor,key,data) {
745     fprintf(OUT, "%s ",key);
746   }
747   fprintf(OUT,  
748           "';\n"
749           "fi\n"
750           "if test \"${INSTALL_PATH+set}\" != set; then \n"
751           "    export INSTALL_PATH='`echo $HOME`/tmp/src'\n"
752           "fi\n"
753           "if test \"${GRAS_ROOT+set}\" != set; then \n"
754           "    export GRAS_ROOT='`echo $INSTALL_PATH`'\n"
755           "fi\n"
756           "if test \"${SRCDIR+set}\" != set; then \n"
757           "    export SRCDIR=./\n"
758           "fi\n"
759           "if test \"${SIMGRID_URL+set}\" != set; then \n"
760           "    export SIMGRID_URL=http://gcl.ucsd.edu/simgrid/dl/\n"
761           "fi\n"
762           "if test \"${SIMGRID_VERSION+set}\" != set; then \n"
763           "    export SIMGRID_VERSION=2.91\n"
764           "fi\n"
765           "if test \"${GRAS_PROJECT+set}\" != set; then \n"
766           "    export GRAS_PROJECT=%s\n"
767           "fi\n"
768           "if test \"${GRAS_PROJECT_URL+set}\" != set; then \n"
769           "    export GRAS_PROJECT_URL=http://www-id.imag.fr/Laboratoire/Membres/Legrand_Arnaud/gras_test/\n"
770           "fi\n"
771           "\n"
772           "test -e runlogs/ || mkdir -p runlogs/\n",
773           project);
774
775   fprintf(OUT,  
776           "cmd_prolog=\"env INSTALL_PATH=$INSTALL_PATH GRAS_ROOT=$GRAS_ROOT \\\n"
777           "                 SIMGRID_URL=$SIMGRID_URL SIMGRID_VERSION=$SIMGRID_VERSION GRAS_PROJECT=$GRAS_PROJECT \\\n"
778           "                 GRAS_PROJECT_URL=$GRAS_PROJECT_URL LD_LIBRARY_PATH=$GRAS_ROOT/lib/ sh -c \";\n");
779
780   xbt_dynar_foreach (process_list,cpt,proc) {
781     fprintf(OUT,"cmd=\"\\$INSTALL_PATH/gras-%s/"RL_BINARYNAME" ",project,project,proc.argv[0]);
782     for(i=1;i<proc.argc;i++) {
783       fprintf(OUT,"%s ",proc.argv[i]);
784     }
785     fprintf(OUT,"\";\n");
786     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);
787   }
788
789   fclose(OUT);
790 }
791
792 static void print(void *p)
793 {
794   printf("%p",p);
795 }
796
797
798 #ifdef _WIN32
799 #include <windows.h>
800 #endif
801
802 /* specific to Borland Compiler */
803 #ifdef __BORLANDDC__
804 #pragma argsused
805 #endif
806
807 int main(int argc, char *argv[])
808 {
809   char *project_name = NULL;
810   char *deployment_file = NULL;
811   int i;
812    
813   surf_init(&argc, argv);
814
815   xbt_assert1((argc >= 3),"Usage: %s project_name deployment_file [deployment_file...]\n",argv[0]);
816
817   project_name = argv[1];
818
819   process_function_set = xbt_dict_new();
820   process_list = xbt_dynar_new(sizeof(s_process_t),s_process_free);
821   machine_set = xbt_dict_new();
822
823   STag_surfxml_process_fun = parse_process_init;
824   ETag_surfxml_argument_fun = parse_argument;
825   ETag_surfxml_process_fun = parse_process_finalize;
826   
827   for(i=2; i<argc; i++) {
828      deployment_file = argv[i];
829      surf_parse_open(deployment_file);
830      if(surf_parse()) 
831         xbt_assert1(0,"Parse error in %s",deployment_file);
832      
833      surf_parse_close();
834   }
835
836
837   warning = xbt_new(char,strlen(WARN)+strlen(deployment_file)+10);
838   sprintf(warning,WARN,deployment_file);
839
840   /*if(XBT_LOG_ISENABLED(stubgen, xbt_log_priority_debug)) {
841     xbt_dict_cursor_t cursor=NULL;
842     char *key = NULL;
843     void *data = NULL;
844
845     for (cursor=NULL, xbt_dict_cursor_first((process_function_set),&(cursor)) ;
846          xbt_dict_cursor_get_or_free(&(cursor),&(key),(void**)(&data));
847          xbt_dict_cursor_step(cursor) ) {
848       DEBUG1("Function %s", key);      
849     }
850     
851     xbt_dict_dump(process_function_set,print);
852   }*/
853
854   generate_sim(project_name);
855   generate_rl(project_name);
856   generate_makefile_local(project_name, deployment_file);
857   #ifdef _WIN32
858   generate_borland_simulation_project(project_name);
859   generate_borland_real_life_project(project_name);
860   #endif
861 //  generate_makefile_remote(project_name, deployment_file);
862 //  generate_deployment(project_name, deployment_file);
863
864   free(warning);
865   surf_exit();
866   return 0;
867 }
868 #ifdef _WIN32
869 void
870 generate_borland_project(borland_project_t project)
871 {
872         char* binary_path;      /* the path of the generated binary file                                */
873         char* obj_path;         /* the path of the generated object file                                */
874         char* lib_files;        /* a list of the libraries used in the borland project  */
875         char* main_source;      /* the name of the bpf file used by the borland project */
876         char* file_name;        /* the file name of the main source file                                */
877
878
879     /* create the borland project file */
880     borland_project_create(project);
881
882     /* write the xml document header */
883     borland_project_write_xml_header(project);
884
885     /* write the xml comment to identify a borland project file */
886     borland_project_write_xml_comment(project);
887         
888         /* write the begin of the node PROJECT */
889         borland_project_begin_xml_node(project,"PROJECT");
890         
891         /* write the begin of node MACRO */
892         borland_project_begin_xml_node(project,"MACRO");
893         
894         /* write the borland project version */
895         borland_project_write_xml_element(project,"VERSION",project->version);
896         
897         /* construct and write the borland project binary path */
898         binary_path = xbt_new0(char,strlen(project->name) + strlen(project->bin_dir) + 6);
899         sprintf(binary_path,"%s\\%s.exe",project->bin_dir,project->name);
900         borland_project_write_xml_element(project,"PROJECT",binary_path);
901         xbt_free(binary_path);
902         
903         /* construct an write the object generated file path */
904         obj_path = xbt_new0(char,strlen(project->name) + strlen(project->obj_dir) + 6);
905         sprintf(binary_path,"%s\\%s.obj",project->obj_dir,project->name);
906         borland_project_write_xml_element(project,"OBJFILES",obj_path);
907         xbt_free(obj_path);
908         
909         /* write the RESFILES element (not used) */
910         borland_project_write_xml_element(project,"RESFILES","");
911         
912         /* write the IDLFILES element (not used) */
913         borland_project_write_xml_element(project,"IDLFILES","");
914         
915         /* write the IDLGENFILES element (not used) */
916         borland_project_write_xml_element(project,"IDLGENFILES","");
917         
918         /* write the DEFFILE element (not used) */
919         borland_project_write_xml_element(project,"DEFFILE","");
920         
921         /* write the RESDEPEN element (not used) */
922         borland_project_write_xml_element(project,"RESDEPEN","$(RESFILES)");
923         
924         /* construct and write the LIBFILES element */
925         lib_files = xbt_new0(char,(2 * (strlen(project->lib_dir) + 1)) + strlen("simgrid.lib") + strlen("libgras.lib") + 2);
926         sprintf(lib_files,"%s\\simgrid.lib %s\\libgras.lib",project->lib_dir,project->lib_dir);
927         borland_project_write_xml_element(project,"LIBFILES",lib_files);
928         xbt_free(lib_files);
929         
930         /* write the SPARELIBS element (not used) */
931         borland_project_write_xml_element(project,"SPARELIBS","");
932         
933         /* write the PACKAGES element (not used) */
934         borland_project_write_xml_element(project,"PACKAGES","");
935         
936         /* write the PATHCPP element */
937         borland_project_write_xml_element(project,"PATHCPP",".;");
938         
939         /* write the PATHPAS element (not used) */
940         borland_project_write_xml_element(project,"PATHPAS","");
941         
942         /* write the PATHRC element (not used) */
943         borland_project_write_xml_element(project,"PATHRC","");
944         
945         /* write the PATHASM element (not used) */
946         borland_project_write_xml_element(project,"PATHASM","");
947         
948         /* write the DEBUGLIBPATH element */
949         borland_project_write_xml_element(project,"DEBUGLIBPATH","$(BCB)\\lib\\debug");
950         
951         /* write the RELEASELIBPATH element */
952         borland_project_write_xml_element(project,"RELEASELIBPATH","$(BCB)\\lib\\release");
953         
954         /* write the LINKER element*/
955         borland_project_write_xml_element(project,"LINKER","ilink32");
956         
957         /* write the USERDEFINES element */
958         borland_project_write_xml_element(project,"USERDEFINES","_DEBUG");
959         
960         /* write the SYSDEFINES element */
961         borland_project_write_xml_element(project,"SYSDEFINES","NO_STRICT;_NO_VCL");
962         
963         /* construct and write the MAINSOURCE element */
964         
965         main_source = xbt_new0(char,strlen(project->name) + 5);
966         sprintf(main_source,"%s.bpf",project->name);
967
968         borland_project_write_xml_element(project,"MAINSOURCE",main_source);
969
970         /* create the bpf file used by the borland project */
971         borland_project_create_main_file(main_source);
972
973         /* FIXME resolve the include path */
974         /* write the INCLUDEPATH element  */
975         borland_project_write_xml_element(project,"INCLUDEPATH","");
976
977         /* FIXME check the lib path */
978         /* write the LIBPATH element */
979         borland_project_write_xml_element(project,"LIBPATH","");
980
981         /* write the WARNINGS element */
982         borland_project_write_xml_element(project,"WARNINGS","-w-sus -w-rvl -w-rch -w-pia -w-pch -w-par -w-csu -w-ccc -w-aus");
983
984         /* write the OTHERFILES element (not used) */
985         borland_project_write_xml_element(project,"OTHERFILES","");
986
987         /* write the end of the node MACRO */
988         borland_project_end_xml_node(project,"MACRO");
989
990         /* write the begin of the node OPTIONS */
991         borland_project_begin_xml_node(project,"OPTIONS");
992
993         /* FIXME check the idlcflags
994         /* write the IDLCFLAGS element */
995         borland_project_write_xml_element(project,"IDLCFLAGS","");
996
997         /* write the CFLAG1 element */
998         borland_project_write_xml_element(project,"CFLAG1","-Od -H=$(BCB)\\lib\\vcl60.csm -Hc -Vx -Ve -X- -r- -a1 -b- -k -y -v -vi- -tWC -tWM- -c");
999
1000         /* write the PFLAGS element */
1001         borland_project_write_xml_element(project,"PFLAGS","-N2obj -N0obj -$YD -$W -$O- -$A8 -v -JPHNE -M");
1002
1003         /* write the RFLAGS element */
1004         borland_project_write_xml_element(project,"RFLAGS","");
1005
1006         /* write the AFLAGS element (not used)*/
1007         borland_project_write_xml_element(project,"AFLAGS","/mx /w2 /zd");
1008
1009         /* write the LFLAGS element */
1010         borland_project_write_xml_element(project,"LFLAGS","-Iobj -D&quot;&quot; -ap -Tpe -x -Gn -v");
1011
1012         /* write the OTHERFILES element (not used)*/
1013         borland_project_write_xml_element(project,"OTHERFILES","");
1014
1015         /* write the end of the node OPTIONS */
1016         borland_project_end_xml_node(project,"OPTIONS");
1017
1018         /* write the begin of the node LINKER */
1019         borland_project_begin_xml_node(project,"LINKER");
1020
1021         /* write the ALLOBJ element */
1022         borland_project_write_xml_element(project,"ALLOBJ","c0x32.obj $(OBJFILES)");
1023
1024         /* write the ALLRES element (not used) */
1025         borland_project_write_xml_element(project,"ALLRES","");
1026
1027         /* write the ALLLIB element */
1028         borland_project_write_xml_element(project,"ALLLIB","$(LIBFILES) $(LIBRARIES) import32.lib cw32.lib");
1029
1030         /* write the OTHERFILES element (not used) */
1031         borland_project_write_xml_element(project,"OTHERFILES","");
1032
1033         /* write the end of the node LINKER */
1034         borland_project_end_xml_node(project,"LINKER");
1035
1036         /* write the begin of the node FILELIST */
1037         borland_project_begin_xml_node(project,"FILELIST");
1038         
1039         /* construct and write the list of file elements */
1040         
1041         /* add the bpf file to the list */
1042         borland_project_write_file_element(project,main_source,"",project->name,"BPF","","");
1043         xbt_free(main_source);
1044         
1045         /* FIXME : check the source file directory */
1046         /* add the generated source file to the list */
1047         file_name = xbt_new0(char,strlen(project->src_dir) + strlen(project->name) + 4);
1048         sprintf(file_name,"%s\\%s.c",project->src_dir,project->name);
1049         borland_project_write_file_element(project,file_name,"",project->name,"CCompiler","","");
1050         xbt_free(file_name);
1051
1052         /* FIXME : check the libraries directory */
1053         /* add the simgrid library to the list */
1054         file_name = xbt_new0(char,strlen(project->lib_dir) + strlen("simgrid.lib") + 2);
1055         sprintf(file_name,"%s\\simgrid.lib",project->lib_dir);
1056         borland_project_write_file_element(project,file_name,"","simgrid.lib","LibTool","","");
1057         xbt_free(file_name);
1058
1059         /* add the libgras library to the list */
1060         file_name = xbt_new0(char,strlen(project->lib_dir) + strlen("libgras.lib") + 2);
1061         sprintf(file_name,"%s\\libgras.lib",project->lib_dir);
1062         borland_project_write_file_element(project,file_name,"","libgras.lib","LibTool","","");
1063         xbt_free(file_name);
1064         
1065         /* write the end of the node FILELIST */
1066         borland_project_end_xml_node(project,"FILELIST");
1067         
1068         /* write the begin of the node BUILDTOOLS (not used)*/
1069         borland_project_begin_xml_node(project,"BUILDTOOLS");
1070         
1071         /* write the end of the node BUILDTOOLS (not used)*/
1072         borland_project_end_xml_node(project,"BUILDTOOLS");
1073         
1074         /* write the begin of the node IDEOPTIONS */
1075         borland_project_begin_xml_node(project,"IDEOPTIONS");
1076         
1077         /* write all of the option of the IDE of Borland C++ Builder */
1078         borland_project_write_ide_options(project);
1079         
1080         /* write the end of the node IDEOPTIONS */
1081         borland_project_end_xml_node(project,"IDEOPTIONS");
1082
1083     /* write the end of the node PROJECT */
1084         borland_project_end_xml_node(project,"PROJECT");
1085
1086     /* close the borland project file */
1087     borland_project_close(project);
1088 }
1089
1090 void
1091 borland_project_write_tabs(borland_project_t project,unsigned int count)
1092 {
1093         unsigned int pos;
1094         
1095         for(pos = 0; pos < count; pos++)
1096                 fprintf(project->stream,"\t");          
1097 }
1098
1099 void
1100 borland_project_begin_xml_node(borland_project_t project, const char* name)
1101 {
1102         if(level)
1103                 borland_project_write_tabs(project,level);
1104                 
1105         fprintf(project->stream,"<%s>\n",name);
1106         
1107         level++;
1108 }
1109
1110 void
1111 borland_project_end_xml_node(borland_project_t project, const char* name)
1112 {
1113         level--;
1114         
1115         if(level)
1116                 borland_project_write_tabs(project,level);
1117         
1118         fprintf(project->stream,"</%s>\n",name);        
1119 }
1120
1121
1122 void
1123 borland_project_write_xml_element(borland_project_t project,const char* name,const char* value)
1124 {
1125         borland_project_write_tabs(project,level);
1126
1127         fprintf(project->stream,"<%s value=\"%s\"/>\n",name,value);
1128 }
1129
1130 void
1131 borland_project_write_xml_header(borland_project_t project)
1132 {
1133     fprintf(project->stream,"<?xml version='%s' encoding='%s' ?>\n",project->xml_version, project->encoding);
1134 }
1135
1136 void
1137 borland_project_create_main_file(const char* name)
1138 {
1139         FILE* stream = fopen(name,"w+");
1140
1141         fprintf(stream,"Ce fichier est uniquement utilisé par le gestionnaire de projets et doit Ãªtre traité comme le fichier projet\n\n\nmain\n");
1142
1143         fclose(stream);
1144 }
1145
1146 void
1147 borland_project_create(borland_project_t project)
1148 {
1149     char* file_name = xbt_new0(char,strlen(project->name) + 5);
1150     sprintf(file_name,"%s.bpr",project->name);
1151     project->stream = fopen(file_name,"w+");
1152     xbt_free(file_name);
1153 }
1154
1155 void
1156 borland_project_write_xml_comment(borland_project_t project)
1157 {
1158     fprintf(project->stream,"<!-- %s -->\n",project->comment);
1159 }
1160 void
1161 borland_project_write_file_element(     borland_project_t project,
1162                                                                         const char* file_name,
1163                                                                         const char* form_name,
1164                                                                         const char* unit_name,
1165                                                                         const char* container_id,
1166                                                                         const char* design_class,
1167                                                                         const char* local_command)
1168 {
1169         borland_project_write_tabs(project,level);
1170
1171         fprintf(project->stream,"<FILE FILENAME=\"%s\" FORMNAME=\"%s\" UNITNAME=\"%s\" CONTAINERID=\"%s\" DESIGNCLASS=\"%s\" LOCALCOMMAND=\"%s\"/>\n",file_name,form_name,unit_name,container_id,design_class,local_command);
1172 }
1173
1174 void
1175 borland_project_write_ide_options(borland_project_t project)
1176 {
1177
1178         const char* ide_options =
1179         "[Version Info]\nIncludeVerInfo=0\nAutoIncBuild=0\nMajorVer=1\nMinorVer=0\nRelease=0\nBuild=0\nDebug=0\nPreRelease=0\nSpecial=0\nPrivate=0\nDLL=0\nLocale=1036\nCodePage=1252\n\n"  \
1180         "[Version Info Keys]\nCompanyName=\nFileDescription=\nFileVersion=1.0.0.0\nInternalName=\nLegalCopyright=\nLegalTrademarks=\nOriginalFilename=\nProductName=\nProductVersion=1.0.0.0\nComments=\n\n" \
1181         "[Excluded Packages]\n$(BCB)\\dclclxdb60.bpl=Composants BD CLX Borland\n$(BCB)\\Bin\\dclclxstd60.bpl=Composants Standard CLX Borland\n\n" \
1182         "[HistoryLists\\hlIncludePath]\nCount=1\nItem0=$(BCB)\\include;$(BCB)\\include\\vcl;\n\n" \
1183         "[HistoryLists\\hlLibraryPath]\nCount=1\nItem0=$(BCB)\\lib\\obj;$(BCB)\\lib\n\n" \
1184         "[HistoryLists\\hlDebugSourcePath]\nCount=1\nItem0=$(BCB)\\source\\vcl\\\n\n" \
1185         "[HistoryLists\\hlConditionals]\nCount=1\nItem0=_DEBUG\n\n" \
1186         "[HistoryLists\\hlIntOutputDir]\nCount=0\n\n" \
1187         "[HistoryLists\\hlFinalOutputDir]\nCount=0\n\n" \
1188         "[HistoryLists\\hIBPIOutputDir]\nCount=0\n\n" \
1189         "[Debugging]\nDebugSourceDirs=$(BCB)\\source\\vcl\n\n" \
1190         "[Parameters]\nRunParams=\nLauncher=\nUseLauncher=0\nDebugCWD=\nHostApplication=\nRemoteHost=\nRemotePath=\nRemoteLauncher=\nRemoteCWD=\nRemoteDebug=0\n\n" \
1191         "[Compiler]\nShowInfoMsgs=0\nLinkDebugVcl=0\nLinkCGLIB=0\n\n" \
1192         "[CORBA]\nAddServerUnit=1\nAddClientUnit=1\nPrecompiledHeaders=1\n\n" \
1193         "[Language]\nActiveLang=\nProjectLang=\nRootDir=\n";
1194
1195         fprintf(project->stream,ide_options);
1196 }
1197
1198 void
1199 borland_project_close(borland_project_t project)
1200 {
1201     fclose(project->stream);
1202 }
1203
1204 void
1205 generate_borland_simulation_project(const char* name)
1206 {
1207     char buffer[MAX_PATH] = {0};
1208
1209     HANDLE hDir;
1210     WIN32_FIND_DATA wfd = {0};
1211
1212     s_borland_project_t borland_project = {0};
1213     borland_project.xml_version = "1.0";
1214     borland_project.encoding ="utf-8";
1215     borland_project.comment ="C++Builder XML Project";
1216     borland_project.version = "BCB.06.00";
1217
1218     borland_project.lib_dir = xbt_new0(char,MAX_PATH);
1219
1220     FindFilePath("C:\\","simgrid.lib",borland_project.lib_dir);
1221
1222
1223     GetCurrentDirectory(MAX_PATH,buffer);
1224
1225     borland_project.src_dir = xbt_new0(char,strlen(buffer) + 1);
1226
1227     strcpy(borland_project.src_dir,buffer);
1228
1229     borland_project.lib_dir = " ";
1230
1231     borland_project.name = xbt_new0(char,strlen(name) + strlen("_simulator") + 2);
1232     sprintf(borland_project.name,"_%s_simulator",name);
1233
1234     borland_project.bin_dir = xbt_new0(char,strlen(buffer) + strlen("\\bin") + 1);
1235     sprintf(borland_project.bin_dir,"%s\\bin",buffer);
1236
1237     hDir = FindFirstFile(borland_project.bin_dir,&wfd);
1238
1239     if(!hDir)
1240         CreateDirectory(borland_project.bin_dir,NULL);
1241
1242     borland_project.obj_dir = xbt_new0(char,strlen(buffer) + strlen("\\obj") + 1);
1243     sprintf(borland_project.obj_dir,"%s\\obj",buffer);
1244
1245     hDir = FindFirstFile(borland_project.obj_dir,&wfd);
1246
1247     if(INVALID_HANDLE_VALUE == hDir)
1248         CreateDirectory(borland_project.obj_dir,NULL);
1249
1250     generate_borland_project(&borland_project);
1251
1252     xbt_free(borland_project.name);
1253     xbt_free(borland_project.src_dir);
1254     xbt_free(borland_project.bin_dir);
1255     xbt_free(borland_project.obj_dir);
1256     xbt_free(borland_project.lib_dir);
1257 }
1258
1259 void
1260 generate_borland_real_life_project(const char* name)
1261 {
1262     HANDLE hDir;
1263     WIN32_FIND_DATA wfd = {0};
1264     char buffer[MAX_PATH] = {0};
1265     xbt_dict_cursor_t cursor = NULL;
1266     char *key = NULL;
1267     void *data = NULL;
1268         s_borland_project_t borland_project = {0};
1269
1270         borland_project.xml_version = "1.0";
1271     borland_project.encoding ="utf-8";
1272     borland_project.comment ="C++Builder XML Project";
1273     borland_project.version = "BCB.06.00";
1274     borland_project.lib_dir = " ";
1275
1276     borland_project.lib_dir = xbt_new0(char,MAX_PATH);
1277
1278     FindFilePath("C:\\","simgrid.lib",borland_project.lib_dir);
1279
1280     GetCurrentDirectory(MAX_PATH,buffer);
1281
1282     borland_project.src_dir = xbt_new0(char,strlen(buffer) + 1);
1283
1284     strcpy(borland_project.src_dir,buffer);
1285
1286     borland_project.bin_dir = xbt_new0(char,strlen(buffer) + strlen("\\bin") + 1);
1287     sprintf(borland_project.bin_dir,"%s\\bin",buffer);
1288
1289     hDir = FindFirstFile(borland_project.bin_dir,&wfd);
1290
1291     if(INVALID_HANDLE_VALUE == hDir)
1292         CreateDirectory(borland_project.bin_dir,NULL);
1293
1294     borland_project.obj_dir = xbt_new0(char,strlen(buffer) + strlen("\\obj") + 1);
1295     sprintf(borland_project.obj_dir,"%s\\obj",buffer);
1296
1297     hDir = FindFirstFile(borland_project.obj_dir,&wfd);
1298
1299     if(!hDir)
1300         CreateDirectory(borland_project.obj_dir,NULL);
1301
1302
1303         xbt_dict_foreach(process_function_set,cursor,key,data) {
1304         borland_project.name = xbt_new0(char,strlen(name) + strlen(key) + 3);
1305
1306         sprintf(borland_project.name,"_%s_%s",name,key);
1307
1308         generate_borland_project(&borland_project);
1309         xbt_free(borland_project.name);
1310     }
1311
1312     xbt_free(borland_project.src_dir);
1313     xbt_free(borland_project.bin_dir);
1314     xbt_free(borland_project.obj_dir);
1315     xbt_free(borland_project.lib_dir);
1316 }
1317 int
1318 FindFilePath(const char* root_dir,const char* file_name,char* path)
1319
1320         HANDLE hFind;
1321         WIN32_FIND_DATA wfd;
1322     char* prev_dir = (char*)calloc(MAX_PATH,sizeof(char));
1323     GetCurrentDirectory(MAX_PATH,prev_dir);
1324     SetCurrentDirectory(root_dir);
1325
1326         // begining of the scan
1327         hFind=FindFirstFile ("*.*", &wfd);
1328         
1329         if(hFind!=INVALID_HANDLE_VALUE){
1330                 
1331                 /* it's a file */
1332                 if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
1333                         
1334                         if(!strcmp(file_name,wfd.cFileName)){
1335                 GetCurrentDirectory(MAX_PATH,path);
1336                 SetCurrentDirectory(prev_dir);
1337                 free(prev_dir);
1338                                 FindClose(hFind);
1339                                 return 1;
1340                         }
1341                         
1342                 }
1343                 /* it's a directory, scan it*/
1344                 else {
1345
1346             if(strcmp(wfd.cFileName,".") && strcmp(wfd.cFileName,"..")){
1347                             if(FindFilePath(wfd.cFileName,file_name,path)){
1348                                     FindClose(hFind);
1349                     SetCurrentDirectory(prev_dir);
1350                                     return 1;
1351                             }
1352             }
1353                 }
1354                  
1355                 /* next file or directory */
1356                 while(FindNextFile (hFind,&wfd)) 
1357                 { 
1358                         /* it's a file */
1359                         if(!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) 
1360                         { 
1361                                 if(!strcmp(file_name,wfd.cFileName)){
1362                     GetCurrentDirectory(MAX_PATH,path);
1363                     SetCurrentDirectory(prev_dir);
1364                     free(prev_dir);
1365                                         FindClose(hFind);
1366                                         return 1;
1367                                 }
1368                         }
1369                         /* it's a file scan it */
1370                         else {
1371                                 
1372                          if(strcmp(wfd.cFileName,".") && strcmp(wfd.cFileName,"..")){
1373
1374                             if(FindFilePath(wfd.cFileName,file_name,path)){
1375                     SetCurrentDirectory(prev_dir);
1376                                     FindClose(hFind);
1377                                     return 1;
1378                             }
1379
1380             }
1381                                 
1382                         } 
1383                 } 
1384         }
1385
1386         SetCurrentDirectory(prev_dir);
1387     free(prev_dir);
1388         FindClose (hFind);
1389         return 0; 
1390 }
1391 #endif
1392
1393
1394
1395
1396