Logo AND Algorithmique Numérique Distribuée

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