3 /* gras_stub_generator - creates the main() to use a GRAS program */
5 /* Copyright (c) 2003-2007 Martin Quinson, Arnaud Legrand, Malek Cherier. */
6 /* All rights reserved. */
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. */
11 /* specific to Borland Compiler */
17 #include "xbt/sysdep.h"
18 #include "xbt/function_types.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"
29 /* stupid stubs so that it compiles on windows */
30 void generate_sim(char *project) {}
31 void generate_rl(char *project) {}
32 void generate_makefile_local(char *project, char *deployment) {}
34 /* real implementation */
35 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(stubgen);
38 #define WARN "/***********\n * DO NOT EDIT! THIS FILE HAS BEEN AUTOMATICALLY GENERATED FROM %s BY gras_stub_generator\n ***********/\n"
39 #define SIM_SOURCENAME "_%s_simulator.c"
40 #define SIM_OBJNAME "_%s_simulator.o"
41 #define SIM_BINARYNAME "%s_simulator"
42 #define SIM_SOURCENAME_LDADD "%s_simulator_LDADD"
43 #define SIM_SOURCENAME_SOURCES "%s_simulator_SOURCES"
44 #define RL_SOURCENAME "_%s_%s.c"
45 #define RL_OBJNAME "_%s_%s.o"
46 #define RL_BINARYNAME "%s_%s"
47 #define RL_SOURCENAME_LDADD "%s_%s_LDADD"
48 #define RL_SOURCENAME_SOURCES "%s_%s_SOURCES"
49 #define MAKEFILE_FILENAME_AM "%s.Makefile.am"
50 #define MAKEFILE_FILENAME_LOCAL "%s.mk"
51 #define MAKEFILE_FILENAME_REMOTE "%s.Makefile.remote"
52 #define DEPLOYMENT "%s.deploy.sh"
56 /**********************************************/
57 /**** Generate the file for the simulator *****/
58 /**********************************************/
60 const char *SIM_PREEMBULE =
61 "/* specific to Borland Compiler */\n"
62 "#ifdef __BORLANDC__\n"
65 "#include <stdlib.h>\n"
66 "#include <stdio.h>\n"
67 "#include \"msg/msg.h\"\n"
70 "char *gras_log=NULL;\n";
73 #define SIM_LAUNCH_FUNC \
74 "int launch_%s(int argc, char **argv) {\n" \
75 " char **myargv=argv;\n" \
76 " int myargc=argc;\n" \
81 " myargv=malloc((argc+1) * sizeof(char**));\n" \
82 " for (i=0; i<argc; i++)\n" \
83 " myargv[i] = argv[i];\n" \
84 " myargv[myargc++] = gras_log;\n" \
86 " retcode = %s(myargc,myargv);\n" \
87 " if (myargv != argv)\n" \
89 " return retcode;\n" \
92 const char *SIM_MAIN_POSTEMBULE = "\n"
94 " gras_launch_application(argv[2]);\n"
96 " /* Run the simulation */\n"
99 " /* cleanup the place */\n"
109 void generate_sim(char *project) {
110 xbt_dict_cursor_t cursor=NULL;
113 char *filename = NULL;
116 /* Output file: <projet>_simulator.c */
117 filename = xbt_new(char,strlen(project) + strlen(SIM_SOURCENAME));
118 sprintf(filename,SIM_SOURCENAME,project);
120 OUT=fopen(filename,"w");
122 xbt_assert1(OUT, "Unable to open %s for writing",filename);
124 fprintf(OUT, "%s\n",warning);
125 fprintf(OUT, "%s", SIM_PREEMBULE);
127 xbt_dict_foreach(process_function_set,cursor,key,data) {
128 fprintf(OUT,"int %s(int argc,char *argv[]);\n",key);
133 xbt_dict_foreach(process_function_set,cursor,key,data) {
134 fprintf(OUT,"int launch_%s(int argc,char *argv[]);\n",key);
137 fprintf(OUT, "\n%s\n",warning);
139 xbt_dict_foreach(process_function_set,cursor,key,data) {
140 fprintf(OUT,SIM_LAUNCH_FUNC,key,key);
142 fprintf(OUT, "\n%s\n",warning);
144 fprintf(OUT,"%s", "/* specific to Borland Compiler */\n"
145 "#ifdef __BORLANDDC__\n"
149 fprintf(OUT, "%s", "int main (int argc,char *argv[]) {\n"
151 " /* Simulation setup */\n"
152 " gras_global_init(&argc,argv);\n"
153 " if (argc != 3) {\n"
154 " fprintf(stderr, \"Usage: %s platform.xml deployment.xml [--log=...]\\n\",argv[0]);\n"
159 " gras_create_environment(argv[1]);\n"
161 " /* Application deployment */\n"
163 xbt_dict_foreach(process_function_set,cursor,key,data) {
164 fprintf(OUT," gras_function_register(\"%s\", launch_%s);\n",key,key);
166 fprintf(OUT, "%s", SIM_MAIN_POSTEMBULE);
171 /**********************************************/
172 /**** Generate the file for the real life *****/
173 /**********************************************/
174 void generate_rl(char *project) {
175 xbt_dict_cursor_t cursor=NULL;
178 char *filename = NULL;
181 xbt_dict_foreach(process_function_set,cursor,key,data) {
182 filename = xbt_new(char,strlen(project) + strlen(RL_SOURCENAME) + strlen(key));
184 sprintf(filename,RL_SOURCENAME,project,key);
186 OUT=fopen(filename,"w");
187 xbt_assert1(OUT, "Unable to open %s for writing",filename);
189 fprintf(OUT, "\n%s\n",warning);
190 fprintf(OUT, "/* specific to Borland Compiler */\n" \
191 "#ifdef __BORLANDC__\n" \
192 "#pragma hdrstop\n" \
194 "#include <stdio.h>\n" \
195 "#include <signal.h>\n" \
196 "#include <gras.h>\n" \
198 "XBT_PUBLIC_DATA(const char *) _gras_procname;\n" \
199 "/* user code */\n" \
200 "int %s(int argc, char *argv[]);\n" \
202 "/* specific to Borland Compiler */\n" \
203 "#ifdef __BORLANDC__\n" \
204 "#pragma argsused\n" \
206 "int main(int argc, char *argv[]){\n" \
209 " _gras_procname = \"%s\";\n" \
210 " errcode=%s(argc,argv);\n"\
212 " return errcode;\n"\
215 fprintf(OUT, "\n%s\n",warning);
221 void generate_makefile_am(char *project, char *deployment) {
222 xbt_dict_cursor_t cursor=NULL;
225 char *filename = NULL;
228 filename = xbt_new(char,strlen(project) + strlen(MAKEFILE_FILENAME_AM));
229 sprintf(filename,MAKEFILE_FILENAME_AM, project);
231 OUT=fopen(filename,"w");
232 xbt_assert1(OUT, "Unable to open %s for writing",filename);
234 fprintf(OUT, "# AUTOMAKE variable definition\n");
235 fprintf(OUT, "INCLUDES= @CFLAGS_SimGrid@\n\n");
236 fprintf(OUT, "PROGRAMS=");
237 fprintf(OUT, SIM_BINARYNAME,project);
239 xbt_dict_foreach(process_function_set,cursor,key,data) {
241 fprintf(OUT, RL_BINARYNAME, project, key);
244 fprintf(OUT, "\n\n");
245 fprintf(OUT, SIM_SOURCENAME_SOURCES,project);
247 fprintf(OUT, SIM_SOURCENAME,project);
248 fprintf(OUT, " %s.c\n", project);
249 fprintf(OUT, SIM_SOURCENAME_LDADD, project);
250 fprintf(OUT, "=\tpath/to/libsimgrid.a\n\n");
252 xbt_dict_foreach(process_function_set,cursor,key,data) {
253 fprintf(OUT, RL_SOURCENAME_SOURCES, project,key);
255 fprintf(OUT, RL_SOURCENAME, project,key);
256 fprintf(OUT, " %s.c\n", project);
257 fprintf(OUT, RL_SOURCENAME_LDADD, project, key);
258 fprintf(OUT, "=\tpath/to/libgras.a\n\n");
261 fprintf(OUT, "\n# cleanup temps (allowing the user to add extra clean files)\n");
262 fprintf(OUT, "CLEANFILES?= \n");
263 fprintf(OUT, "CLEANFILES+= ");
264 fprintf(OUT, SIM_SOURCENAME, project);
266 xbt_dict_foreach(process_function_set,cursor,key,data) {
268 fprintf(OUT, RL_SOURCENAME, project,key);
272 fprintf(OUT, "\n# generate temps\n");
273 fprintf(OUT, "\n# A rule to generate the source file each time the deployment file changes\n");
275 xbt_dict_foreach(process_function_set,cursor,key,data) {
276 fprintf(OUT, RL_SOURCENAME, project,key);
279 fprintf(OUT, SIM_SOURCENAME, project);
280 fprintf(OUT, ": %s\n", deployment);
281 fprintf(OUT, "\tgras_stub_generator %s %s >/dev/null\n", project, deployment);
285 void generate_makefile_local(char *project, char *deployment) {
286 xbt_dict_cursor_t cursor=NULL;
289 char *filename = NULL;
292 filename = xbt_new(char,strlen(project) + strlen(MAKEFILE_FILENAME_LOCAL));
293 sprintf(filename,MAKEFILE_FILENAME_LOCAL, project);
295 OUT=fopen(filename,"w");
296 xbt_assert1(OUT, "Unable to open %s for writing",filename);
302 "#### THIS FILE WAS GENERATED, DO NOT EDIT BEFORE RENAMING IT\n"
305 fprintf(OUT,"## Variable declarations\n"
307 "DISTDIR=gras-$(PROJECT_NAME)\n\n"
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\n"
317 "LIBS_SIM = -lm -L$(GRAS_ROOT)/lib/ -lsimgrid\n"
318 "LIBS_RL = -lm -L$(GRAS_ROOT)/lib/ -lgras\n"
322 fprintf(OUT, "PRECIOUS_C_FILES ?= %s.c\n", project);
324 fprintf(OUT, "GENERATED_C_FILES = ");
325 fprintf(OUT, SIM_SOURCENAME" ",project);
326 xbt_dict_foreach(process_function_set,cursor,key,data) {
327 fprintf(OUT, RL_SOURCENAME " ",project, key);
331 fprintf(OUT,"OBJ_FILES = $(patsubst %%.c,%%.o,$(PRECIOUS_C_FILES))\n");
333 fprintf(OUT, "BIN_FILES = ");
335 fprintf(OUT, SIM_BINARYNAME " ",project);
336 xbt_dict_foreach(process_function_set,cursor,key,data) {
337 fprintf(OUT, RL_BINARYNAME " ", project, key);
343 "## By default, build all the binaries\n"
344 "all: $(BIN_FILES)\n"
347 fprintf(OUT, "\n## generate temps: regenerate the source file each time the deployment file changes\n");
348 xbt_dict_foreach(process_function_set,cursor,key,data) {
349 fprintf(OUT, RL_SOURCENAME, project,key);
352 fprintf(OUT, SIM_SOURCENAME, project);
353 fprintf(OUT, ": %s\n", deployment);
354 fprintf(OUT, "\tgras_stub_generator %s %s >/dev/null\n", project, deployment);
356 fprintf(OUT, "\n## Generate the binaries\n");
357 fprintf(OUT, SIM_BINARYNAME ": " SIM_OBJNAME " $(OBJ_FILES)\n",project, project);
358 fprintf(OUT, "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS_SIM) $(LIBS) $(LDADD) -o $@ \n");
359 xbt_dict_foreach(process_function_set,cursor,key,data) {
360 fprintf(OUT, RL_BINARYNAME " : " RL_OBJNAME " $(OBJ_FILES)\n", project, key, project, key);
361 fprintf(OUT, "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS_RL) $(LIBS) $(LDADD) -o $@ \n");
366 "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS) $(LDADD) -o $@ \n"
369 "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) -c -o $@ $<\n"
373 "## Rules for tarballs and cleaning\n"
374 "DIST_FILES= $(EXTRA_DIST) $(GENERATED_C_FILES) $(PRECIOUS_C_FILES) "MAKEFILE_FILENAME_LOCAL" " /*MAKEFILE_FILENAME_REMOTE*/"\n"
375 "distdir: $(DIST_FILES)\n"
376 "\trm -rf $(DISTDIR)\n"
377 "\tmkdir -p $(DISTDIR)\n"
378 "\tcp $^ $(DISTDIR)\n"
380 "dist: clean distdir\n"
381 "\ttar c $(DISTDIR) | gzip -c9 > $(DISTDIR).tar.gz\n"
382 "\n", project /*, project*/);
386 "\trm -f $(CLEANFILES) $(BIN_FILES) $(OBJ_FILES) *~ %s.o " SIM_OBJNAME, project, project);
387 xbt_dict_foreach(process_function_set,cursor,key,data) {
388 fprintf(OUT, " " RL_OBJNAME, project, key);
392 "\trm -rf $(DISTDIR)\n"
398 fprintf(OUT, "############ REMOTE COMPILING #########\n");
401 xbt_dict_foreach(machine_set,cursor,key,data) {
402 fprintf(OUT, "%s ",key);
407 "INSTALL_PATH ?='$$HOME/tmp/src' ### Has to be an absolute path !!! \n"
408 "GRAS_ROOT ?='$(INSTALL_PATH)' ### Has to be an absolute path !!! \n"
410 "SIMGRID_URL ?=http://gcl.ucsd.edu/simgrid/dl/\n"
411 "SIMGRID_VERSION ?=2.92\n"
412 "GRAS_PROJECT ?= %s\n"
413 "GRAS_PROJECT_URL ?= http://www-id.imag.fr/Laboratoire/Membres/Legrand_Arnaud/gras_test/\n"
416 "\t@echo;echo \"----[ Compile the package on remote hosts ]----\"\n"
417 "\t@test -e $(SRCDIR)/buildlogs/ || mkdir -p $(SRCDIR)/buildlogs/\n"
418 "\t for site in $(MACHINES) ; do \\\n"
419 "\t machine=`echo $$site |sed 's/^\\([^%%]*\\)%%.*$$/\\1/'`;\\\n"
420 "\t machine2=`echo $$site |sed 's/^\\([^%%]*\\)%%\\(.*\\)$$/\\2/'`;\\\n"
421 "\t cmd_mkdir=\"\\\"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) mkdir -p $(INSTALL_PATH) 2>&1'\\\"\";\\\n"
424 "\t cmd_make=\"\\\"sh -c 'env INSTALL_PATH=$(INSTALL_PATH) GRAS_ROOT=$(GRAS_ROOT) \\\n"
425 "\t SIMGRID_URL=$(SIMGRID_URL) SIMGRID_VERSION=$(SIMGRID_VERSION) GRAS_PROJECT=$(GRAS_PROJECT) \\\n"
426 "\t GRAS_PROJECT_URL=$(GRAS_PROJECT_URL) make -C $(INSTALL_PATH) -f "MAKEFILE_FILENAME_REMOTE" $(ACTION) 2>&1'\\\"\";\\\n"
427 "\t if echo $$site | grep '%%' >/dev/null ; then \\\n"
428 "\t echo \"----[ Compile on $$machine2 (behind $$machine) ]----\";\\\n"
430 "\t machine=$$site;\\\n"
431 "\t echo \"----[ Compile on $$machine ]----\";\\\n"
433 "\t if echo $$site | grep '%%' >/dev/null ; then \\\n"
434 "\t if ssh $$machine \"ssh -A $$machine2 $$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"
437 "\t if ssh $$machine \"eval $$cmd_mkdir\" 2>&1 > $(SRCDIR)/buildlogs/$$site.log ;\\\n"
438 "\t then true; else failed=1;echo \"Failed (check $(SRCDIR)/buildlogs/$$site.log)\"; fi; \\\n"
440 "\t echo \"-- Copy the data over\"; \\\n"
441 "\t scp "MAKEFILE_FILENAME_REMOTE" $$site:$(INSTALL_PATH) ;\\\n"
442 "\t echo \"-- Compiling... (the output gets into $(SRCDIR)/buildlogs/$$site.log)\"; \\\n"
443 "\t if echo $$site | grep '%%' >/dev/null ; then \\\n"
444 "\t if ssh $$machine \"ssh -A $$machine2 $$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"
447 "\t if ssh $$machine \"eval $$cmd_make\" 2>&1 >> $(SRCDIR)/buildlogs/$$site.log ;\\\n"
448 "\t then echo \"Sucessful\"; else failed=1;echo \"Failed (check $(SRCDIR)/buildlogs/$$site.log)\"; fi;echo; \\\n"
450 "\t done;\n",project,project,project);
455 static void print(void *p)