Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Not test with WIN32 but BORLAND because on windows there is GNU too.
[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 #ifdef __BORLANDC__
28 /* stupid stubs so that it compiles on windows */
29 void generate_sim(char *project)
30 {
31 }
32
33 void generate_rl(char *project)
34 {
35 }
36
37 void generate_makefile_local(char *project, char *deployment)
38 {
39 }
40 #else
41 /* real implementation */
42 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(stubgen);
43
44
45 #define WARN "/***********\n * DO NOT EDIT! THIS FILE HAS BEEN AUTOMATICALLY GENERATED FROM %s BY gras_stub_generator\n ***********/\n"
46 #define SIM_SOURCENAME  "_%s_simulator.c"
47 #define SIM_OBJNAME  "_%s_simulator.o"
48 #define SIM_BINARYNAME  "%s_simulator"
49 #define SIM_SOURCENAME_LDADD  "%s_simulator_LDADD"
50 #define SIM_SOURCENAME_SOURCES  "%s_simulator_SOURCES"
51 #define RL_SOURCENAME  "_%s_%s.c"
52 #define RL_OBJNAME  "_%s_%s.o"
53 #define RL_BINARYNAME  "%s_%s"
54 #define RL_SOURCENAME_LDADD  "%s_%s_LDADD"
55 #define RL_SOURCENAME_SOURCES  "%s_%s_SOURCES"
56 #define MAKEFILE_FILENAME_AM  "%s.Makefile.am"
57 #define MAKEFILE_FILENAME_LOCAL  "%s.mk"
58 #define MAKEFILE_FILENAME_REMOTE  "%s.Makefile.remote"
59 #define DEPLOYMENT  "%s.deploy.sh"
60
61 char *warning = NULL;
62
63 /**********************************************/
64 /**** Generate the file for the simulator *****/
65 /**********************************************/
66
67 const char *SIM_PREEMBULE =
68   "/* specific to Borland Compiler */\n"
69   "#ifdef __BORLANDC__\n"
70   "#pragma hdrstop\n"
71   "#endif\n\n"
72   "#include <stdlib.h>\n"
73   "#include <stdio.h>\n"
74   "#include \"msg/msg.h\"\n"
75   "#include <gras.h>\n" "\n" "char *gras_log=NULL;\n";
76
77
78 #define SIM_LAUNCH_FUNC  \
79 "int launch_%s(int argc, char **argv) {\n" \
80 "  char **myargv=argv;\n" \
81 "  int myargc=argc;\n" \
82 "  int i;\n" \
83 "  int retcode;\n"\
84 "    \n"\
85 "  if (gras_log) {\n"\
86 "    myargv=malloc((argc+1) * sizeof(char**));\n" \
87 "    for (i=0; i<argc; i++)\n" \
88 "      myargv[i] = argv[i];\n" \
89 "    myargv[myargc++] = gras_log;\n" \
90 "  }\n" \
91 "  retcode = %s(myargc,myargv);\n" \
92 "  if (myargv != argv)\n" \
93 "    free(myargv);\n" \
94 "  return retcode;\n" \
95 "}\n"
96
97 const char *SIM_MAIN_POSTEMBULE = "\n"
98   "\n"
99   "  gras_launch_application(argv[2]);\n"
100   "\n"
101   "  /*  Run the simulation */\n"
102   "  gras_main();\n"
103   "\n"
104   "  /* cleanup the place */\n"
105   "  gras_clean();\n"
106   "  if (gras_log)\n" "    free(gras_log);\n" "  return 0;\n" "}\n";
107
108
109
110
111 void generate_sim(char *project)
112 {
113   xbt_dict_cursor_t cursor = NULL;
114   char *key = NULL;
115   void *data = NULL;
116   char *filename = NULL;
117   FILE *FICOUT = NULL;
118
119   /* Output file: <projet>_simulator.c */
120   filename = xbt_new(char, strlen(project) + strlen(SIM_SOURCENAME));
121   sprintf(filename, SIM_SOURCENAME, project);
122
123   FICOUT = fopen(filename, "w");
124
125   xbt_assert1(FICOUT, "Unable to open %s for writing", filename);
126
127   fprintf(FICOUT, "%s\n", warning);
128   fprintf(FICOUT, "%s", SIM_PREEMBULE);
129
130   xbt_dict_foreach(process_function_set, cursor, key, data) {
131     fprintf(FICOUT, "int %s(int argc,char *argv[]);\n", key);
132   }
133
134   fprintf(FICOUT, "\n");
135
136   xbt_dict_foreach(process_function_set, cursor, key, data) {
137     fprintf(FICOUT, "int launch_%s(int argc,char *argv[]);\n", key);
138   }
139
140   fprintf(FICOUT, "\n%s\n", warning);
141
142   xbt_dict_foreach(process_function_set, cursor, key, data) {
143     fprintf(FICOUT, SIM_LAUNCH_FUNC, key, key);
144   }
145   fprintf(FICOUT, "\n%s\n", warning);
146
147   fprintf(FICOUT, "%s", "/* specific to Borland Compiler */\n"
148           "#ifdef __BORLANDDC__\n" "#pragma argsused\n" "#endif\n\n");
149
150   fprintf(FICOUT, "%s", "int main (int argc,char *argv[]) {\n"
151           "\n"
152           "  /*  Simulation setup */\n"
153           "  gras_global_init(&argc,argv);\n"
154           "  if (argc != 3) {\n"
155           "    fprintf(stderr, \"Usage: %s platform.xml deployment.xml [--log=...]\\n\",argv[0]);\n"
156           "    exit(1);\n" "  }\n" "\n");
157   fprintf(FICOUT,
158           "  gras_create_environment(argv[1]);\n"
159           "\n" "  /*  Application deployment */\n");
160   xbt_dict_foreach(process_function_set, cursor, key, data) {
161     fprintf(FICOUT, "  gras_function_register(\"%s\", launch_%s);\n", key, key);
162   }
163   fprintf(FICOUT, "%s", SIM_MAIN_POSTEMBULE);
164   fclose(FICOUT);
165   free(filename);
166 }
167
168 /**********************************************/
169 /**** Generate the file for the real life *****/
170 /**********************************************/
171 void generate_rl(char *project)
172 {
173   xbt_dict_cursor_t cursor = NULL;
174   char *key = NULL;
175   void *data = NULL;
176   char *filename = NULL;
177   FILE *FICOUT = NULL;
178
179   xbt_dict_foreach(process_function_set, cursor, key, data) {
180     filename =
181       xbt_new(char, strlen(project) + strlen(RL_SOURCENAME) + strlen(key));
182
183     sprintf(filename, RL_SOURCENAME, project, key);
184
185     FICOUT = fopen(filename, "w");
186     xbt_assert1(FICOUT, "Unable to open %s for writing", filename);
187
188     fprintf(FICOUT, "\n%s\n", warning);
189     fprintf(FICOUT, "/* specific to Borland Compiler */\n"
190             "#ifdef __BORLANDC__\n"
191             "#pragma hdrstop\n"
192             "#endif\n\n"
193             "#include <stdio.h>\n"
194             "#include <signal.h>\n"
195             "#include <gras.h>\n"
196             "\n"
197             "XBT_PUBLIC_DATA(const char *) _gras_procname;\n"
198             "/* user code */\n"
199             "int %s(int argc, char *argv[]);\n"
200             "\n"
201             "/* specific to Borland Compiler */\n"
202             "#ifdef __BORLANDC__\n"
203             "#pragma argsused\n"
204             "#endif\n\n"
205             "int main(int argc, char *argv[]){\n"
206             "  int errcode;\n"
207             "\n"
208             "  _gras_procname = \"%s\";\n"
209             "  errcode=%s(argc,argv);\n"
210             " \n" "  return errcode;\n" "}\n", key, key, key);
211     fprintf(FICOUT, "\n%s\n", warning);
212     fclose(FICOUT);
213     free(filename);
214   }
215 }
216
217 void generate_makefile_am(char *project, char *deployment)
218 {
219   xbt_dict_cursor_t cursor = NULL;
220   char *key = NULL;
221   void *data = NULL;
222   char *filename = NULL;
223   FILE *FICOUT = NULL;
224
225   filename = xbt_new(char, strlen(project) + strlen(MAKEFILE_FILENAME_AM));
226   sprintf(filename, MAKEFILE_FILENAME_AM, project);
227
228   FICOUT = fopen(filename, "w");
229   xbt_assert1(FICOUT, "Unable to open %s for writing", filename);
230
231   fprintf(FICOUT, "# AUTOMAKE variable definition\n");
232   fprintf(FICOUT, "INCLUDES= @CFLAGS_SimGrid@\n\n");
233   fprintf(FICOUT, "PROGRAMS=");
234   fprintf(FICOUT, SIM_BINARYNAME, project);
235
236   xbt_dict_foreach(process_function_set, cursor, key, data) {
237     fprintf(FICOUT, " ");
238     fprintf(FICOUT, RL_BINARYNAME, project, key);
239   }
240
241   fprintf(FICOUT, "\n\n");
242   fprintf(FICOUT, SIM_SOURCENAME_SOURCES, project);
243   fprintf(FICOUT, "=\t");
244   fprintf(FICOUT, SIM_SOURCENAME, project);
245   fprintf(FICOUT, " %s.c\n", project);
246   fprintf(FICOUT, SIM_SOURCENAME_LDADD, project);
247   fprintf(FICOUT, "=\tpath/to/libsimgrid.a\n\n");
248
249   xbt_dict_foreach(process_function_set, cursor, key, data) {
250     fprintf(FICOUT, RL_SOURCENAME_SOURCES, project, key);
251     fprintf(FICOUT, "=\t");
252     fprintf(FICOUT, RL_SOURCENAME, project, key);
253     fprintf(FICOUT, " %s.c\n", project);
254     fprintf(FICOUT, RL_SOURCENAME_LDADD, project, key);
255     fprintf(FICOUT, "=\tpath/to/libgras.a\n\n");
256   }
257
258   fprintf(FICOUT,
259           "\n# cleanup temps (allowing the user to add extra clean files)\n");
260   fprintf(FICOUT, "CLEANFILES?= \n");
261   fprintf(FICOUT, "CLEANFILES+= ");
262   fprintf(FICOUT, SIM_SOURCENAME, project);
263
264   xbt_dict_foreach(process_function_set, cursor, key, data) {
265     fprintf(FICOUT, " ");
266     fprintf(FICOUT, RL_SOURCENAME, project, key);
267   }
268   fprintf(FICOUT, "\n");
269
270   fprintf(FICOUT, "\n# generate temps\n");
271   fprintf(FICOUT,
272           "\n# A rule to generate the source file each time the deployment file changes\n");
273
274   xbt_dict_foreach(process_function_set, cursor, key, data) {
275     fprintf(FICOUT, RL_SOURCENAME, project, key);
276     fprintf(FICOUT, " ");
277   }
278   fprintf(FICOUT, SIM_SOURCENAME, project);
279   fprintf(FICOUT, ": %s\n", deployment);
280   fprintf(FICOUT, "\tgras_stub_generator %s %s >/dev/null\n", project,
281           deployment);
282   fclose(FICOUT);
283 }
284
285 void generate_makefile_local(char *project, char *deployment)
286 {
287   xbt_dict_cursor_t cursor = NULL;
288   char *key = NULL;
289   void *data = NULL;
290   char *filename = NULL;
291   FILE *FICOUT = NULL;
292
293   filename = xbt_new(char, strlen(project) + strlen(MAKEFILE_FILENAME_LOCAL));
294   sprintf(filename, MAKEFILE_FILENAME_LOCAL, project);
295
296   FICOUT = fopen(filename, "w");
297   xbt_assert1(FICOUT, "Unable to open %s for writing", filename);
298   free(filename);
299
300   fprintf(FICOUT,
301           "\n"
302           "####\n"
303           "#### THIS FILE WAS GENERATED, DO NOT EDIT BEFORE RENAMING IT\n"
304           "####\n\n\n");
305
306   fprintf(FICOUT, "## Variable declarations\n"
307           "PROJECT_NAME=%s\n" "DISTDIR=gras-$(PROJECT_NAME)\n\n", project);
308
309   fprintf(FICOUT,
310           "# Set the GRAS_ROOT environment variable to the path under which you installed SimGrid\n"
311           "# Compilation will fail if you don't do so\n"
312           "GRAS_ROOT?= $(shell if [ -e /usr/local/lib/libgras.so ] ; then echo /usr/local ; else echo \"\\\"<<<< GRAS_ROOT undefined !!! >>>>\\\"\"; fi)\n\n"
313           "# You can fiddle the following to make it fit your taste\n"
314           "INCLUDES = -I$(GRAS_ROOT)/include\n"
315           "CFLAGS ?= -O3 -w -g -Wall\n"
316           "LIBS_SIM = -lm  -L$(GRAS_ROOT)/lib/ -lsimgrid\n"
317           "LIBS_RL = -lm  -L$(GRAS_ROOT)/lib/ -lgras\n" "LIBS = \n" "\n");
318
319   fprintf(FICOUT, "PRECIOUS_C_FILES ?= %s.c\n", project);
320
321   fprintf(FICOUT, "GENERATED_C_FILES = ");
322   fprintf(FICOUT, SIM_SOURCENAME " ", project);
323   xbt_dict_foreach(process_function_set, cursor, key, data) {
324     fprintf(FICOUT, RL_SOURCENAME " ", project, key);
325   }
326   fprintf(FICOUT, "\n");
327
328   fprintf(FICOUT, "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", project,
356           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", project,
361             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