Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Allow users to add extra CLEANFILES
[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 (allowing the user to add extra clean files)\n");
256   fprintf(OUT, "CLEANFILES?= \n");
257   fprintf(OUT, "CLEANFILES+= ");
258   fprintf(OUT, SIM_SOURCENAME, project);
259   
260   xbt_dict_foreach(process_function_set,cursor,key,data) {
261     fprintf(OUT, " ");
262     fprintf(OUT, RL_SOURCENAME, project,key);
263   }
264   fprintf(OUT, "\n");
265
266   fprintf(OUT, "\n# generate temps\n");
267   fprintf(OUT, "\n# A rule to generate the source file each time the deployment file changes\n");
268
269   xbt_dict_foreach(process_function_set,cursor,key,data) {
270     fprintf(OUT, RL_SOURCENAME, project,key);
271     fprintf(OUT, " ");
272   }
273   fprintf(OUT, SIM_SOURCENAME, project);
274   fprintf(OUT, ": %s\n", deployment);
275   fprintf(OUT, "\tgras_stub_generator %s %s >/dev/null\n", project, deployment);
276   fclose(OUT);
277 }
278
279 void generate_makefile_local(char *project, char *deployment) {
280   xbt_dict_cursor_t cursor=NULL;
281   char *key = NULL;
282   void *data = NULL;
283   char *filename = NULL;
284   FILE *OUT = NULL;
285
286   filename = xbt_new(char,strlen(project) + strlen(MAKEFILE_FILENAME_LOCAL));
287   sprintf(filename,MAKEFILE_FILENAME_LOCAL, project);
288   
289   OUT=fopen(filename,"w");
290   xbt_assert1(OUT, "Unable to open %s for writing",filename);
291   free(filename);
292    
293   fprintf(OUT,
294           "\n"
295           "####\n"
296           "#### THIS FILE WAS GENERATED, DO NOT EDIT BEFORE RENAMING IT\n"
297           "####\n\n\n");
298
299   fprintf(OUT,"## Variable declarations\n"
300               "PROJECT_NAME=%s\n"
301               "DISTDIR=gras-$(PROJECT_NAME)\n\n"
302           ,project);
303    
304   fprintf(OUT,
305           "# Set the GRAS_ROOT environment variable to the path under which you installed SimGrid\n"
306           "# Compilation will fail if you don't do so\n" 
307           "GRAS_ROOT?= $(shell echo \"\\\"<<<< GRAS_ROOT undefined !!! >>>>\\\"\")\n\n"
308           "# You can fiddle the following to make it fit your taste\n"
309           "INCLUDES = -I$(GRAS_ROOT)/include\n"
310           "CFLAGS ?= -O3 -w -g\n"
311           "LIBS_SIM = -lm  -L$(GRAS_ROOT)/lib/ -lsimgrid\n"
312           "LIBS_RL = -lm  -L$(GRAS_ROOT)/lib/ -lgras\n"
313           "LIBS = \n"
314           "\n");
315
316   fprintf(OUT, "PRECIOUS_C_FILES ?= %s.c\n", project);
317
318   fprintf(OUT, "GENERATED_C_FILES = ");
319   fprintf(OUT, SIM_SOURCENAME" ",project);
320   xbt_dict_foreach(process_function_set,cursor,key,data) {
321     fprintf(OUT, RL_SOURCENAME " ",project, key);
322   }
323   fprintf(OUT, "\n");
324
325   fprintf(OUT,"OBJ_FILES = $(patsubst %%.c,%%.o,$(PRECIOUS_C_FILES))\n");
326
327   fprintf(OUT, "BIN_FILES = ");
328
329   fprintf(OUT, SIM_BINARYNAME " ",project);
330   xbt_dict_foreach(process_function_set,cursor,key,data) {
331     fprintf(OUT, RL_BINARYNAME " ", project, key);
332   }
333   fprintf(OUT, "\n");
334
335   fprintf(OUT,
336           "\n"
337           "## By default, build all the binaries\n"
338           "all: $(BIN_FILES)\n"
339           "\n");
340    
341   fprintf(OUT, "\n## generate temps: regenerate the source file each time the deployment file changes\n");
342   xbt_dict_foreach(process_function_set,cursor,key,data) {
343     fprintf(OUT, RL_SOURCENAME, project,key);
344     fprintf(OUT, " ");
345   }
346   fprintf(OUT, SIM_SOURCENAME, project);
347   fprintf(OUT, ": %s\n", deployment);
348   fprintf(OUT, "\tgras_stub_generator %s %s >/dev/null\n", project, deployment);
349
350   fprintf(OUT, "\n## Generate the binaries\n");
351   fprintf(OUT, SIM_BINARYNAME ": " SIM_OBJNAME " $(OBJ_FILES)\n",project, project);
352   fprintf(OUT, "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS_SIM) $(LIBS) $(LDADD) -o $@ \n");
353   xbt_dict_foreach(process_function_set,cursor,key,data) {
354     fprintf(OUT, RL_BINARYNAME " : " RL_OBJNAME " $(OBJ_FILES)\n", project, key, project, key);
355     fprintf(OUT, "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS_RL) $(LIBS) $(LDADD) -o $@ \n");
356   }  
357   fprintf(OUT, 
358           "\n"
359           "%%: %%.o\n"
360           "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS) $(LDADD) -o $@ \n"
361           "\n"
362           "%%.o: %%.c\n"
363           "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) -c -o $@ $<\n"
364           "\n");
365    
366   fprintf(OUT,
367           "##┬áRules for tarballs and cleaning\n"
368           "DIST_FILES= $(EXTRA_DIST) $(GENERATED_C_FILES) $(PRECIOUS_C_FILES) "MAKEFILE_FILENAME_LOCAL" " /*MAKEFILE_FILENAME_REMOTE*/"\n"
369           "distdir: $(DIST_FILES)\n"
370           "\trm -rf $(DISTDIR)\n"
371           "\tmkdir -p $(DISTDIR)\n"
372           "\tcp $^ $(DISTDIR)\n"
373           "\n"
374           "dist: clean distdir\n"
375           "\ttar c $(DISTDIR) | gzip -c9 > $(DISTDIR).tar.gz\n"
376           "\n", project /*, project*/);
377
378   fprintf(OUT,
379           "clean:\n"
380           "\trm -f $(CLEANFILES) $(BIN_FILES) $(OBJ_FILES) *~ %s.o " SIM_OBJNAME, project, project);
381   xbt_dict_foreach(process_function_set,cursor,key,data) {
382      fprintf(OUT, " " RL_OBJNAME, project, key);
383   }
384   fprintf(OUT,   
385           "\n"
386           "\trm -rf $(DISTDIR)\n"
387           "\n"
388           ".SUFFIXES:\n"
389           ".PHONY : clean\n"
390           "\n");
391   /* 
392   fprintf(OUT, "############ REMOTE COMPILING #########\n");
393   fprintf(OUT, 
394           "MACHINES ?= ");
395   xbt_dict_foreach(machine_set,cursor,key,data) {
396     fprintf(OUT, "%s ",key);
397   }
398   fprintf(OUT,"\n");
399
400   fprintf(OUT, 
401           "INSTALL_PATH ?='$$HOME/tmp/src' ### Has to be an absolute path !!! \n"
402           "GRAS_ROOT ?='$(INSTALL_PATH)' ### Has to be an absolute path !!! \n"
403           "SRCDIR ?= ./\n"
404           "SIMGRID_URL ?=http://gcl.ucsd.edu/simgrid/dl/\n"
405           "SIMGRID_VERSION ?=2.92\n"
406           "GRAS_PROJECT ?= %s\n"
407           "GRAS_PROJECT_URL ?= http://www-id.imag.fr/Laboratoire/Membres/Legrand_Arnaud/gras_test/\n"
408           "\n"
409           "remote:\n"
410           "\t@echo;echo \"----[ Compile the package on remote hosts ]----\"\n"
411           "\t@test -e $(SRCDIR)/buildlogs/ || mkdir -p $(SRCDIR)/buildlogs/\n"
412           "\t for site in $(MACHINES) ; do \\\n"
413           "\t   machine=`echo $$site |sed 's/^\\([^%%]*\\)%%.*$$/\\1/'`;\\\n"
414           "\t   machine2=`echo $$site |sed 's/^\\([^%%]*\\)%%\\(.*\\)$$/\\2/'`;\\\n"
415           "\t   cmd_mkdir=\"\\\"sh -c 'env INSTALL_PATH=$(INSTALL_PATH) GRAS_ROOT=$(GRAS_ROOT) \\\n"
416           "\t                        SIMGRID_URL=$(SIMGRID_URL) SIMGRID_VERSION=$(SIMGRID_VERSION) GRAS_PROJECT=$(GRAS_PROJECT) \\\n"
417           "\t                        GRAS_PROJECT_URL=$(GRAS_PROJECT_URL)  mkdir -p $(INSTALL_PATH) 2>&1'\\\"\";\\\n"
418           "\t   cmd_make=\"\\\"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)  make -C $(INSTALL_PATH) -f "MAKEFILE_FILENAME_REMOTE" $(ACTION) 2>&1'\\\"\";\\\n"
421           "\t   if echo $$site | grep  '%%' >/dev/null ; then \\\n"
422           "\t     echo \"----[ Compile on $$machine2 (behind $$machine) ]----\";\\\n"
423           "\t   else \\\n"
424           "\t     machine=$$site;\\\n"
425           "\t     echo \"----[ Compile on $$machine ]----\";\\\n"
426           "\t   fi;\\\n"
427           "\t   if echo $$site | grep  '%%' >/dev/null ; then \\\n"
428           "\t     if ssh $$machine \"ssh -A $$machine2 $$cmd_mkdir\" 2>&1 > $(SRCDIR)/buildlogs/$$site.log;\\\n"
429           "\t     then true; else failed=1;echo \"Failed (check $(SRCDIR)/buildlogs/$$site.log)\"; fi;\\\n"
430           "\t   else \\\n"
431           "\t     if ssh $$machine \"eval $$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   fi;\\\n"
434           "\t   echo \"-- Copy the data over\"; \\\n"
435           "\t   scp "MAKEFILE_FILENAME_REMOTE" $$site:$(INSTALL_PATH) ;\\\n"
436           "\t   echo \"-- Compiling... (the output gets into $(SRCDIR)/buildlogs/$$site.log)\"; \\\n"
437           "\t   if echo $$site | grep  '%%' >/dev/null ; then \\\n"
438           "\t     if ssh $$machine \"ssh -A $$machine2 $$cmd_make\" 2>&1 >> $(SRCDIR)/buildlogs/$$site.log;\\\n"
439           "\t     then echo \"Sucessful\"; else failed=1;echo \"Failed (check $(SRCDIR)/buildlogs/$$site.log)\"; fi;echo; \\\n"
440           "\t   else \\\n"
441           "\t     if ssh $$machine \"eval $$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   fi;\\\n"
444           "\t done;\n",project,project,project);
445 */
446   fclose(OUT);
447 }
448
449 static void print(void *p)
450 {
451   printf("%p",p);
452 }