Logo AND Algorithmique Numérique Distribuée

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