Logo AND Algorithmique Numérique Distribuée

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