3 /* gras_stub_generator - creates the main() to use a GRAS program */
5 /* Copyright (c) 2003,2004,2005 Martin Quinson, Arnaud Legrand.
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 #include"xbt/sysdep.h"
14 #include "surf/surf_parse.h"
15 #include "surf/surf.h"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(stubgen,gras,"Stub generator");
19 #define WARN "/***********\n * DO NOT EDIT! THIS FILE HAS BEEN AUTOMATICALLY GENERATED FROM %s BY gras_stub_generator\n ***********/\n"
20 #define SIM_SOURCENAME "_%s_simulator.c"
21 #define SIM_OBJNAME "_%s_simulator.o"
22 #define SIM_BINARYNAME "%s_simulator"
23 #define SIM_SOURCENAME_LDADD "%s_simulator_LDADD"
24 #define SIM_SOURCENAME_SOURCES "%s_simulator_SOURCES"
25 #define RL_SOURCENAME "_%s_%s.c"
26 #define RL_OBJNAME "_%s_%s.o"
27 #define RL_BINARYNAME "%s_%s"
28 #define RL_SOURCENAME_LDADD "%s_%s_LDADD"
29 #define RL_SOURCENAME_SOURCES "%s_%s_SOURCES"
30 #define MAKEFILE_FILENAME_AM "%s.Makefile.am"
31 #define MAKEFILE_FILENAME_LOCAL "%s.Makefile.local"
32 #define MAKEFILE_FILENAME_REMOTE "%s.Makefile.remote"
36 /**********************************************/
37 /**** Generate the file for the simulator *****/
38 /**********************************************/
40 const char *SIM_PREEMBULE =
41 "#include <stdlib.h>\n"
42 "#include <stdio.h>\n"
43 "#include \"msg/msg.h\"\n"
46 "char *gras_log=NULL;\n";
49 #define SIM_LAUNCH_FUNC \
50 "int launch_%s(int argc, char **argv) {\n" \
51 " char **myargv=argv;\n" \
52 " int myargc=argc;\n" \
57 " myargv=malloc((argc+1) * sizeof(char**));\n" \
58 " for (i=0; i<argc; i++)\n" \
59 " myargv[i] = argv[i];\n" \
60 " myargv[myargc++] = gras_log;\n" \
62 " retcode = %s(myargc,myargv);\n" \
63 " if (myargv != argv)\n" \
65 " return retcode;\n" \
68 const char* SIM_MAIN_PREEMBULE =
69 "int main (int argc,char *argv[]) {\n"
72 " /* Simulation setup */\n"
73 " MSG_global_init_args(&argc,argv);\n"
75 " fprintf(stderr, \"Usage: %s platform_file application_description.txt [--gras-log=...]\\n\",argv[0]);\n"
79 " MSG_set_channel_number(10); // GRAS_MAX_CHANNEL hardcoded since Alvin killed its definition\n"
80 " MSG_create_environment(argv[1]);\n"
82 " /* Application deployment */\n";
84 const char *SIM_MAIN_POSTEMBULE = "\n"
86 " MSG_launch_application(argv[2]);\n"
88 " /* Run the simulation */\n"
91 " /* cleanup the place */\n"
98 /**********************************************/
99 /**** Generate the file for the real life *****/
100 /**********************************************/
103 "#include <stdio.h>\n" \
104 "#include <signal.h>\n" \
105 "#include <gras.h>\n" \
107 "/* user code */\n" \
108 "int %s(int argc, char *argv[]);\n" \
110 "int main(int argc, char *argv[]){\n" \
113 " errcode=%s(argc,argv);\n"\
115 " return errcode;\n"\
118 /**********************************************/
119 /********* Parse XML deployment file **********/
120 /**********************************************/
121 xbt_dict_t process_function_set = NULL;
123 static void parse_process_init(void)
125 void *p = (void *) 1234;
126 xbt_dict_set(process_function_set, A_process_function, p, NULL);
129 static void generate_sim(char *project)
131 xbt_dict_cursor_t cursor=NULL;
134 char *filename = NULL;
136 filename = xbt_new(char,strlen(project) + strlen(SIM_SOURCENAME));
137 sprintf(filename,SIM_SOURCENAME,project);
139 OUT=fopen(filename,"w");
140 xbt_assert1(OUT, "Unable to open %s for writing",filename);
142 fprintf(OUT, "%s\n",warning);
143 fprintf(OUT, "%s", SIM_PREEMBULE);
144 xbt_dict_foreach(process_function_set,cursor,key,data) {
145 fprintf(OUT,"int %s(int argc,char *argv[]);\n",key);
148 xbt_dict_foreach(process_function_set,cursor,key,data) {
149 fprintf(OUT,"int launch_%s(int argc,char *argv[]);\n",key);
151 fprintf(OUT, "\n%s\n",warning);
152 xbt_dict_foreach(process_function_set,cursor,key,data) {
153 fprintf(OUT,SIM_LAUNCH_FUNC,key,key);
155 fprintf(OUT, "\n%s\n",warning);
157 fprintf(OUT, "%s", SIM_MAIN_PREEMBULE);
158 xbt_dict_foreach(process_function_set,cursor,key,data) {
159 fprintf(OUT," MSG_function_register(\"%s\", launch_%s);\n",key,key);
161 fprintf(OUT, "%s", SIM_MAIN_POSTEMBULE);
166 static void generate_rl(char *project)
168 xbt_dict_cursor_t cursor=NULL;
171 char *filename = NULL;
174 xbt_dict_foreach(process_function_set,cursor,key,data) {
175 filename = xbt_new(char,strlen(project) + strlen(RL_SOURCENAME) + strlen(key));
176 sprintf(filename,RL_SOURCENAME,project,key);
178 OUT=fopen(filename,"w");
179 xbt_assert1(OUT, "Unable to open %s for writing",filename);
181 fprintf(OUT, "\n%s\n",warning);
182 fprintf(OUT, RL_CODE, key,key);
183 fprintf(OUT, "\n%s\n",warning);
189 static void generate_makefile_am(char *project, char *deployment)
191 xbt_dict_cursor_t cursor=NULL;
194 char *filename = NULL;
197 filename = xbt_new(char,strlen(project) + strlen(MAKEFILE_FILENAME_AM));
198 sprintf(filename,MAKEFILE_FILENAME_AM, project);
200 OUT=fopen(filename,"w");
201 xbt_assert1(OUT, "Unable to open %s for writing",filename);
203 fprintf(OUT, "# AUTOMAKE variable definition\n");
204 fprintf(OUT, "INCLUDES= @CFLAGS_SimGrid@\n\n");
205 fprintf(OUT, "PROGRAMS=");
206 fprintf(OUT, SIM_BINARYNAME,project);
208 xbt_dict_foreach(process_function_set,cursor,key,data) {
210 fprintf(OUT, RL_BINARYNAME, project, key);
213 fprintf(OUT, "\n\n");
214 fprintf(OUT, SIM_SOURCENAME_SOURCES,project);
216 fprintf(OUT, SIM_SOURCENAME,project);
217 fprintf(OUT, " %s.c\n", project);
218 fprintf(OUT, SIM_SOURCENAME_LDADD, project);
219 fprintf(OUT, "=\tpath/to/libsimgrid.a\n\n");
221 xbt_dict_foreach(process_function_set,cursor,key,data) {
222 fprintf(OUT, RL_SOURCENAME_SOURCES, project,key);
224 fprintf(OUT, RL_SOURCENAME, project,key);
225 fprintf(OUT, " %s.c\n", project);
226 fprintf(OUT, RL_SOURCENAME_LDADD, project, key);
227 fprintf(OUT, "=\tpath/to/libgras.a\n\n");
230 fprintf(OUT, "\n# cleanup temps\n");
231 fprintf(OUT, "CLEANFILES= ");
232 fprintf(OUT, SIM_SOURCENAME, project);
234 xbt_dict_foreach(process_function_set,cursor,key,data) {
236 fprintf(OUT, RL_SOURCENAME, project,key);
240 fprintf(OUT, "\n# generate temps\n");
241 fprintf(OUT, "\n# A rule to generate the source file each time the deployment file changes\n");
243 xbt_dict_foreach(process_function_set,cursor,key,data) {
244 fprintf(OUT, RL_SOURCENAME, project,key);
247 fprintf(OUT, SIM_SOURCENAME, project);
248 fprintf(OUT, ": %s\n", deployment);
249 fprintf(OUT, "\tstub_generator %s %s >/dev/null\n", project, deployment);
254 static void generate_makefile(char *project, char *deployment)
256 xbt_dict_cursor_t cursor=NULL;
259 char *filename = NULL;
262 filename = xbt_new(char,strlen(project) + strlen(MAKEFILE_FILENAME_LOCAL));
263 sprintf(filename,MAKEFILE_FILENAME_LOCAL, project);
265 OUT=fopen(filename,"w");
266 xbt_assert1(OUT, "Unable to open %s for writing",filename);
268 fprintf(OUT, "PROJECT_NAME=%s\n",project);
270 "DISTDIR=gras-$(PROJECT_NAME)\n\n"
271 "SIMGRID_INSTALL_PATH?= $(shell echo \"\\\"<<<< SIMGRID_INSTALL_PATH undefined !!! >>>>\\\"\")\n"
273 "INCLUDES = -I$(SIMGRID_INSTALL_PATH)/include\n"
274 "LIBS = -lm -L$(SIMGRID_INSTALL_PATH)/lib/ -lsimgrid\n"
276 "C_FILES = _chrono_multiplier.c _chrono_simulator.c chrono.c\n"
277 "BIN_FILES = chrono_multiplier chrono_simulator\n"
279 "all: $(BIN_FILES)\n"
281 fprintf(OUT, SIM_BINARYNAME ": " SIM_OBJNAME " %s.c\n",project, project, project);
282 xbt_dict_foreach(process_function_set,cursor,key,data) {
283 fprintf(OUT, RL_BINARYNAME " : " RL_OBJNAME " %s.c\n", project, key, project, key, project);
288 "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS) $(LDADD) -o $@ \n"
291 "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) -c -o $@ $<\n"
293 "distdir: $(C_FILES) $(PROJECT_NAME).Makefile\n"
294 "\trm -rf $(DISTDIR)\n"
295 "\tmkdir -p $(DISTDIR)\n"
296 "\tcp $^ $(DISTDIR)\n"
298 "dist: clean distdir\n"
299 "\ttar c $(DISTDIR) | gzip -c > $(DISTDIR).tar.gz\n"
302 "\trm -f $(BIN_FILES) *.o *~\n"
303 "\trm -rf $(DISTDIR)\n"
310 static void print(void *p)
315 int main(int argc, char *argv[])
317 char *project_name = NULL;
318 char *deployment_file = NULL;
320 surf_init(&argc, argv);
322 xbt_assert1((argc ==3),"Usage: %s project_name deployment_file\n",argv[0]);
324 project_name = argv[1];
325 deployment_file = argv[2];
327 process_function_set = xbt_dict_new();
329 STag_process_fun = parse_process_init;
330 surf_parse_open(deployment_file);
331 if(surf_parse()) xbt_assert1(0,"Parse error in %s",deployment_file);
334 warning = xbt_new(char,strlen(WARN)+strlen(deployment_file)+10);
335 sprintf(warning,WARN,deployment_file);
337 if(XBT_LOG_ISENABLED(stubgen, xbt_log_priority_debug)) {
338 xbt_dict_cursor_t cursor=NULL;
342 for (cursor=NULL, xbt_dict_cursor_first((process_function_set),&(cursor)) ;
343 xbt_dict_cursor_get_or_free(&(cursor),&(key),(void**)(&data));
344 xbt_dict_cursor_step(cursor) ) {
345 DEBUG1("Function %s", key);
348 xbt_dict_dump(process_function_set,print);
351 generate_sim(project_name);
352 generate_rl(project_name);
353 generate_makefile_am(project_name, deployment_file);
354 generate_makefile_local(project_name, deployment_file);