Logo AND Algorithmique Numérique Distribuée

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