Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Bug Fix. This function used to return the last task of the list when no matching...
[simgrid.git] / tools / gras / unix_stub_generator.c
1 /*      $Id$     */
2
3 /* gras_stub_generator - creates the main() to use a GRAS program           */
4
5 /* Copyright (c) 2003-2007 Martin Quinson, Arnaud Legrand, Malek Cherier.   */
6 /* All rights reserved.                                                     */
7
8 /* This program is free software; you can redistribute it and/or modify it
9  * under the terms of the license (GNU LGPL) which comes with this package. */
10
11 /* specific to Borland Compiler */
12 #ifdef __BORLANDDC__
13 #pragma hdrstop
14 #endif
15
16 #include <stdio.h>
17 #include "xbt/sysdep.h"
18 #include "xbt/function_types.h"
19 #include "xbt/log.h"
20 #include "surf/surfxml_parse.h"
21 #include "surf/surf.h"
22 #include "portable.h" /* Needed for the time of the SIMIX convertion */
23 #include "gras_stub_generator.h"
24
25 #include <stdarg.h>
26
27
28 #ifdef _WIN32
29 /* stupid stubs so that it compiles on windows */
30 void generate_sim(char *project) {}
31 void generate_rl(char *project) {}
32 void generate_makefile_local(char *project, char *deployment) {}
33 #else 
34 /* real implementation */
35 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(stubgen);
36
37
38 #define WARN "/***********\n * DO NOT EDIT! THIS FILE HAS BEEN AUTOMATICALLY GENERATED FROM %s BY gras_stub_generator\n ***********/\n"
39 #define SIM_SOURCENAME  "_%s_simulator.c"
40 #define SIM_OBJNAME  "_%s_simulator.o"
41 #define SIM_BINARYNAME  "%s_simulator"
42 #define SIM_SOURCENAME_LDADD  "%s_simulator_LDADD"
43 #define SIM_SOURCENAME_SOURCES  "%s_simulator_SOURCES"
44 #define RL_SOURCENAME  "_%s_%s.c"
45 #define RL_OBJNAME  "_%s_%s.o"
46 #define RL_BINARYNAME  "%s_%s"
47 #define RL_SOURCENAME_LDADD  "%s_%s_LDADD"
48 #define RL_SOURCENAME_SOURCES  "%s_%s_SOURCES"
49 #define MAKEFILE_FILENAME_AM  "%s.Makefile.am"
50 #define MAKEFILE_FILENAME_LOCAL  "%s.mk"
51 #define MAKEFILE_FILENAME_REMOTE  "%s.Makefile.remote"
52 #define DEPLOYMENT  "%s.deploy.sh"
53
54 char *warning = NULL;
55
56 /**********************************************/
57 /**** Generate the file for the simulator *****/
58 /**********************************************/
59
60 const char *SIM_PREEMBULE =
61 "/* specific to Borland Compiler */\n"
62 "#ifdef __BORLANDC__\n"
63 "#pragma hdrstop\n"
64 "#endif\n\n"
65 "#include <stdlib.h>\n"
66 "#include <stdio.h>\n"
67 "#include \"msg/msg.h\"\n"
68 "#include <gras.h>\n"
69 "\n"
70 "char *gras_log=NULL;\n";
71
72
73 #define SIM_LAUNCH_FUNC  \
74 "int launch_%s(int argc, char **argv) {\n" \
75 "  char **myargv=argv;\n" \
76 "  int myargc=argc;\n" \
77 "  int i;\n" \
78 "  int retcode;\n"\
79 "    \n"\
80 "  if (gras_log) {\n"\
81 "    myargv=malloc((argc+1) * sizeof(char**));\n" \
82 "    for (i=0; i<argc; i++)\n" \
83 "      myargv[i] = argv[i];\n" \
84 "    myargv[myargc++] = gras_log;\n" \
85 "  }\n" \
86 "  retcode = %s(myargc,myargv);\n" \
87 "  if (myargv != argv)\n" \
88 "    free(myargv);\n" \
89 "  return retcode;\n" \
90 "}\n"
91
92 const char *SIM_MAIN_POSTEMBULE = "\n"
93 "\n"
94 "  gras_launch_application(argv[2]);\n"
95 "\n"
96 "  /*  Run the simulation */\n"
97 "  gras_main();\n"
98 "\n"
99 "  /* cleanup the place */\n"
100 "  gras_clean();\n"
101 "  if (gras_log)\n"
102 "    free(gras_log);\n"
103 "  return 0;\n"
104 "}\n";
105
106
107
108
109 void generate_sim(char *project) {
110    xbt_dict_cursor_t cursor=NULL;
111    char *key = NULL;
112    void *data = NULL;
113    char *filename = NULL;
114    FILE *OUT = NULL;
115    
116    /* Output file: <projet>_simulator.c */
117    filename = xbt_new(char,strlen(project) + strlen(SIM_SOURCENAME));
118    sprintf(filename,SIM_SOURCENAME,project);
119    
120    OUT=fopen(filename,"w");
121    
122    xbt_assert1(OUT, "Unable to open %s for writing",filename);
123    
124    fprintf(OUT, "%s\n",warning);
125    fprintf(OUT, "%s", SIM_PREEMBULE);
126    
127    xbt_dict_foreach(process_function_set,cursor,key,data) {
128       fprintf(OUT,"int %s(int argc,char *argv[]);\n",key);
129    }
130    
131    fprintf(OUT,"\n");
132    
133    xbt_dict_foreach(process_function_set,cursor,key,data) {
134       fprintf(OUT,"int launch_%s(int argc,char *argv[]);\n",key);
135    }
136    
137    fprintf(OUT, "\n%s\n",warning);
138    
139    xbt_dict_foreach(process_function_set,cursor,key,data) {
140       fprintf(OUT,SIM_LAUNCH_FUNC,key,key);
141    }
142    fprintf(OUT, "\n%s\n",warning);
143    
144    fprintf(OUT,"%s", "/* specific to Borland Compiler */\n"
145            "#ifdef __BORLANDDC__\n"
146            "#pragma argsused\n"
147            "#endif\n\n");
148    
149    fprintf(OUT, "%s", "int main (int argc,char *argv[]) {\n"
150            "\n" 
151            "  /*  Simulation setup */\n" 
152            "  gras_global_init(&argc,argv);\n"
153            "  if (argc != 3) {\n" 
154            "    fprintf(stderr, \"Usage: %s platform.xml deployment.xml [--log=...]\\n\",argv[0]);\n" 
155            "    exit(1);\n" 
156            "  }\n"
157            "\n");
158    fprintf(OUT,
159            "  gras_create_environment(argv[1]);\n" 
160            "\n" 
161            "  /*  Application deployment */\n"
162            );
163    xbt_dict_foreach(process_function_set,cursor,key,data) {
164       fprintf(OUT,"  gras_function_register(\"%s\", launch_%s);\n",key,key);
165    }
166    fprintf(OUT, "%s", SIM_MAIN_POSTEMBULE);
167    fclose(OUT);
168    free(filename);
169 }
170
171 /**********************************************/
172 /**** Generate the file for the real life *****/
173 /**********************************************/
174 void generate_rl(char *project) {
175   xbt_dict_cursor_t cursor=NULL;
176   char *key = NULL;
177   void *data = NULL;
178   char *filename = NULL;
179   FILE *OUT = NULL;
180
181   xbt_dict_foreach(process_function_set,cursor,key,data) {
182     filename = xbt_new(char,strlen(project) + strlen(RL_SOURCENAME) + strlen(key));
183
184         sprintf(filename,RL_SOURCENAME,project,key);
185
186     OUT=fopen(filename,"w");
187     xbt_assert1(OUT, "Unable to open %s for writing",filename);
188
189     fprintf(OUT, "\n%s\n",warning);
190     fprintf(OUT, "/* specific to Borland Compiler */\n" \
191                  "#ifdef __BORLANDC__\n" \
192                  "#pragma hdrstop\n" \
193                  "#endif\n\n" \
194                  "#include <stdio.h>\n" \
195                  "#include <signal.h>\n" \
196                  "#include <gras.h>\n" \
197                  "\n" \
198                  "XBT_PUBLIC_DATA(const char *) _gras_procname;\n" \
199                  "/* user code */\n" \
200                  "int %s(int argc, char *argv[]);\n" \
201                  "\n" \
202                  "/* specific to Borland Compiler */\n" \
203                 "#ifdef __BORLANDC__\n" \
204                 "#pragma argsused\n" \
205                 "#endif\n\n" \
206                  "int main(int argc, char *argv[]){\n" \
207                  "  int errcode;\n" \
208                  "\n" \
209                  "  _gras_procname = \"%s\";\n" \
210                  "  errcode=%s(argc,argv);\n"\
211                  " \n" \
212                  "  return errcode;\n"\
213                  "}\n",
214             key,key,key);
215     fprintf(OUT, "\n%s\n",warning);
216     fclose(OUT);
217     free(filename);
218   }
219 }
220
221 void generate_makefile_am(char *project, char *deployment) {
222   xbt_dict_cursor_t cursor=NULL;
223   char *key = NULL;
224   void *data = NULL;
225   char *filename = NULL;
226   FILE *OUT = NULL;
227
228   filename = xbt_new(char,strlen(project) + strlen(MAKEFILE_FILENAME_AM));
229   sprintf(filename,MAKEFILE_FILENAME_AM, project);
230
231   OUT=fopen(filename,"w");
232   xbt_assert1(OUT, "Unable to open %s for writing",filename);
233
234   fprintf(OUT, "# AUTOMAKE variable definition\n");
235   fprintf(OUT, "INCLUDES= @CFLAGS_SimGrid@\n\n");
236   fprintf(OUT, "PROGRAMS=");
237   fprintf(OUT, SIM_BINARYNAME,project);
238
239   xbt_dict_foreach(process_function_set,cursor,key,data) {
240     fprintf(OUT, " ");
241     fprintf(OUT, RL_BINARYNAME, project, key);
242   }
243
244   fprintf(OUT, "\n\n");
245   fprintf(OUT, SIM_SOURCENAME_SOURCES,project);
246   fprintf(OUT, "=\t");
247   fprintf(OUT, SIM_SOURCENAME,project);
248   fprintf(OUT, " %s.c\n", project);
249   fprintf(OUT, SIM_SOURCENAME_LDADD, project);
250   fprintf(OUT, "=\tpath/to/libsimgrid.a\n\n");
251
252   xbt_dict_foreach(process_function_set,cursor,key,data) {
253     fprintf(OUT, RL_SOURCENAME_SOURCES, project,key);
254     fprintf(OUT, "=\t");
255     fprintf(OUT, RL_SOURCENAME, project,key);
256     fprintf(OUT, " %s.c\n", project);
257     fprintf(OUT, RL_SOURCENAME_LDADD, project, key);
258     fprintf(OUT, "=\tpath/to/libgras.a\n\n");
259   }
260
261   fprintf(OUT, "\n# cleanup temps (allowing the user to add extra clean files)\n");
262   fprintf(OUT, "CLEANFILES?= \n");
263   fprintf(OUT, "CLEANFILES+= ");
264   fprintf(OUT, SIM_SOURCENAME, project);
265   
266   xbt_dict_foreach(process_function_set,cursor,key,data) {
267     fprintf(OUT, " ");
268     fprintf(OUT, RL_SOURCENAME, project,key);
269   }
270   fprintf(OUT, "\n");
271
272   fprintf(OUT, "\n# generate temps\n");
273   fprintf(OUT, "\n# A rule to generate the source file each time the deployment file changes\n");
274
275   xbt_dict_foreach(process_function_set,cursor,key,data) {
276     fprintf(OUT, RL_SOURCENAME, project,key);
277     fprintf(OUT, " ");
278   }
279   fprintf(OUT, SIM_SOURCENAME, project);
280   fprintf(OUT, ": %s\n", deployment);
281   fprintf(OUT, "\tgras_stub_generator %s %s >/dev/null\n", project, deployment);
282   fclose(OUT);
283 }
284
285 void generate_makefile_local(char *project, char *deployment) {
286   xbt_dict_cursor_t cursor=NULL;
287   char *key = NULL;
288   void *data = NULL;
289   char *filename = NULL;
290   FILE *OUT = NULL;
291
292   filename = xbt_new(char,strlen(project) + strlen(MAKEFILE_FILENAME_LOCAL));
293   sprintf(filename,MAKEFILE_FILENAME_LOCAL, project);
294   
295   OUT=fopen(filename,"w");
296   xbt_assert1(OUT, "Unable to open %s for writing",filename);
297   free(filename);
298    
299   fprintf(OUT,
300           "\n"
301           "####\n"
302           "#### THIS FILE WAS GENERATED, DO NOT EDIT BEFORE RENAMING IT\n"
303           "####\n\n\n");
304
305   fprintf(OUT,"## Variable declarations\n"
306               "PROJECT_NAME=%s\n"
307               "DISTDIR=gras-$(PROJECT_NAME)\n\n"
308           ,project);
309    
310   fprintf(OUT,
311           "# Set the GRAS_ROOT environment variable to the path under which you installed SimGrid\n"
312           "# Compilation will fail if you don't do so\n" 
313           "GRAS_ROOT?= $(shell echo \"\\\"<<<< GRAS_ROOT undefined !!! >>>>\\\"\")\n\n"
314           "# You can fiddle the following to make it fit your taste\n"
315           "INCLUDES = -I$(GRAS_ROOT)/include\n"
316           "CFLAGS ?= -O3 -w -g\n"
317           "LIBS_SIM = -lm  -L$(GRAS_ROOT)/lib/ -lsimgrid\n"
318           "LIBS_RL = -lm  -L$(GRAS_ROOT)/lib/ -lgras\n"
319           "LIBS = \n"
320           "\n");
321
322   fprintf(OUT, "PRECIOUS_C_FILES ?= %s.c\n", project);
323
324   fprintf(OUT, "GENERATED_C_FILES = ");
325   fprintf(OUT, SIM_SOURCENAME" ",project);
326   xbt_dict_foreach(process_function_set,cursor,key,data) {
327     fprintf(OUT, RL_SOURCENAME " ",project, key);
328   }
329   fprintf(OUT, "\n");
330
331   fprintf(OUT,"OBJ_FILES = $(patsubst %%.c,%%.o,$(PRECIOUS_C_FILES))\n");
332
333   fprintf(OUT, "BIN_FILES = ");
334
335   fprintf(OUT, SIM_BINARYNAME " ",project);
336   xbt_dict_foreach(process_function_set,cursor,key,data) {
337     fprintf(OUT, RL_BINARYNAME " ", project, key);
338   }
339   fprintf(OUT, "\n");
340
341   fprintf(OUT,
342           "\n"
343           "## By default, build all the binaries\n"
344           "all: $(BIN_FILES)\n"
345           "\n");
346    
347   fprintf(OUT, "\n## generate temps: regenerate the source file each time the deployment file changes\n");
348   xbt_dict_foreach(process_function_set,cursor,key,data) {
349     fprintf(OUT, RL_SOURCENAME, project,key);
350     fprintf(OUT, " ");
351   }
352   fprintf(OUT, SIM_SOURCENAME, project);
353   fprintf(OUT, ": %s\n", deployment);
354   fprintf(OUT, "\tgras_stub_generator %s %s >/dev/null\n", project, deployment);
355
356   fprintf(OUT, "\n## Generate the binaries\n");
357   fprintf(OUT, SIM_BINARYNAME ": " SIM_OBJNAME " $(OBJ_FILES)\n",project, project);
358   fprintf(OUT, "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS_SIM) $(LIBS) $(LDADD) -o $@ \n");
359   xbt_dict_foreach(process_function_set,cursor,key,data) {
360     fprintf(OUT, RL_BINARYNAME " : " RL_OBJNAME " $(OBJ_FILES)\n", project, key, project, key);
361     fprintf(OUT, "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS_RL) $(LIBS) $(LDADD) -o $@ \n");
362   }  
363   fprintf(OUT, 
364           "\n"
365           "%%: %%.o\n"
366           "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS) $(LDADD) -o $@ \n"
367           "\n"
368           "%%.o: %%.c\n"
369           "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) -c -o $@ $<\n"
370           "\n");
371    
372   fprintf(OUT,
373           "## Rules for tarballs and cleaning\n"
374           "DIST_FILES= $(EXTRA_DIST) $(GENERATED_C_FILES) $(PRECIOUS_C_FILES) "MAKEFILE_FILENAME_LOCAL" " /*MAKEFILE_FILENAME_REMOTE*/"\n"
375           "distdir: $(DIST_FILES)\n"
376           "\trm -rf $(DISTDIR)\n"
377           "\tmkdir -p $(DISTDIR)\n"
378           "\tcp $^ $(DISTDIR)\n"
379           "\n"
380           "dist: clean distdir\n"
381           "\ttar c $(DISTDIR) | gzip -c9 > $(DISTDIR).tar.gz\n"
382           "\n", project /*, project*/);
383
384   fprintf(OUT,
385           "clean:\n"
386           "\trm -f $(CLEANFILES) $(BIN_FILES) $(OBJ_FILES) *~ %s.o " SIM_OBJNAME, project, project);
387   xbt_dict_foreach(process_function_set,cursor,key,data) {
388      fprintf(OUT, " " RL_OBJNAME, project, key);
389   }
390   fprintf(OUT,   
391           "\n"
392           "\trm -rf $(DISTDIR)\n"
393           "\n"
394           ".SUFFIXES:\n"
395           ".PHONY : clean\n"
396           "\n");
397   /* 
398   fprintf(OUT, "############ REMOTE COMPILING #########\n");
399   fprintf(OUT, 
400           "MACHINES ?= ");
401   xbt_dict_foreach(machine_set,cursor,key,data) {
402     fprintf(OUT, "%s ",key);
403   }
404   fprintf(OUT,"\n");
405
406   fprintf(OUT, 
407           "INSTALL_PATH ?='$$HOME/tmp/src' ### Has to be an absolute path !!! \n"
408           "GRAS_ROOT ?='$(INSTALL_PATH)' ### Has to be an absolute path !!! \n"
409           "SRCDIR ?= ./\n"
410           "SIMGRID_URL ?=http://gcl.ucsd.edu/simgrid/dl/\n"
411           "SIMGRID_VERSION ?=2.92\n"
412           "GRAS_PROJECT ?= %s\n"
413           "GRAS_PROJECT_URL ?= http://www-id.imag.fr/Laboratoire/Membres/Legrand_Arnaud/gras_test/\n"
414           "\n"
415           "remote:\n"
416           "\t@echo;echo \"----[ Compile the package on remote hosts ]----\"\n"
417           "\t@test -e $(SRCDIR)/buildlogs/ || mkdir -p $(SRCDIR)/buildlogs/\n"
418           "\t for site in $(MACHINES) ; do \\\n"
419           "\t   machine=`echo $$site |sed 's/^\\([^%%]*\\)%%.*$$/\\1/'`;\\\n"
420           "\t   machine2=`echo $$site |sed 's/^\\([^%%]*\\)%%\\(.*\\)$$/\\2/'`;\\\n"
421           "\t   cmd_mkdir=\"\\\"sh -c 'env INSTALL_PATH=$(INSTALL_PATH) GRAS_ROOT=$(GRAS_ROOT) \\\n"
422           "\t                        SIMGRID_URL=$(SIMGRID_URL) SIMGRID_VERSION=$(SIMGRID_VERSION) GRAS_PROJECT=$(GRAS_PROJECT) \\\n"
423           "\t                        GRAS_PROJECT_URL=$(GRAS_PROJECT_URL)  mkdir -p $(INSTALL_PATH) 2>&1'\\\"\";\\\n"
424           "\t   cmd_make=\"\\\"sh -c 'env INSTALL_PATH=$(INSTALL_PATH) GRAS_ROOT=$(GRAS_ROOT) \\\n"
425           "\t                        SIMGRID_URL=$(SIMGRID_URL) SIMGRID_VERSION=$(SIMGRID_VERSION) GRAS_PROJECT=$(GRAS_PROJECT) \\\n"
426           "\t                        GRAS_PROJECT_URL=$(GRAS_PROJECT_URL)  make -C $(INSTALL_PATH) -f "MAKEFILE_FILENAME_REMOTE" $(ACTION) 2>&1'\\\"\";\\\n"
427           "\t   if echo $$site | grep  '%%' >/dev/null ; then \\\n"
428           "\t     echo \"----[ Compile on $$machine2 (behind $$machine) ]----\";\\\n"
429           "\t   else \\\n"
430           "\t     machine=$$site;\\\n"
431           "\t     echo \"----[ Compile on $$machine ]----\";\\\n"
432           "\t   fi;\\\n"
433           "\t   if echo $$site | grep  '%%' >/dev/null ; then \\\n"
434           "\t     if ssh $$machine \"ssh -A $$machine2 $$cmd_mkdir\" 2>&1 > $(SRCDIR)/buildlogs/$$site.log;\\\n"
435           "\t     then true; else failed=1;echo \"Failed (check $(SRCDIR)/buildlogs/$$site.log)\"; fi;\\\n"
436           "\t   else \\\n"
437           "\t     if ssh $$machine \"eval $$cmd_mkdir\" 2>&1 > $(SRCDIR)/buildlogs/$$site.log ;\\\n"
438           "\t     then true; else failed=1;echo \"Failed (check $(SRCDIR)/buildlogs/$$site.log)\"; fi; \\\n"
439           "\t   fi;\\\n"
440           "\t   echo \"-- Copy the data over\"; \\\n"
441           "\t   scp "MAKEFILE_FILENAME_REMOTE" $$site:$(INSTALL_PATH) ;\\\n"
442           "\t   echo \"-- Compiling... (the output gets into $(SRCDIR)/buildlogs/$$site.log)\"; \\\n"
443           "\t   if echo $$site | grep  '%%' >/dev/null ; then \\\n"
444           "\t     if ssh $$machine \"ssh -A $$machine2 $$cmd_make\" 2>&1 >> $(SRCDIR)/buildlogs/$$site.log;\\\n"
445           "\t     then echo \"Sucessful\"; else failed=1;echo \"Failed (check $(SRCDIR)/buildlogs/$$site.log)\"; fi;echo; \\\n"
446           "\t   else \\\n"
447           "\t     if ssh $$machine \"eval $$cmd_make\" 2>&1 >> $(SRCDIR)/buildlogs/$$site.log ;\\\n"
448           "\t     then echo \"Sucessful\"; else failed=1;echo \"Failed (check $(SRCDIR)/buildlogs/$$site.log)\"; fi;echo; \\\n"
449           "\t   fi;\\\n"
450           "\t done;\n",project,project,project);
451 */
452   fclose(OUT);
453 }
454
455 static void print(void *p)
456 {
457   printf("%p",p);
458 }
459 #endif