Logo AND Algorithmique Numérique Distribuée

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