Logo AND Algorithmique Numérique Distribuée

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