Logo AND Algorithmique Numérique Distribuée

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