1 /* gras_stub_generator - creates the main() to use a GRAS program */
3 /* Copyright (c) 2005, 2006, 2007, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "lua_stub_generator.h"
12 #include "xbt/sysdep.h"
13 #include "xbt/function_types.h"
15 #include "surf/surfxml_parse.h"
16 #include "surf/surf.h"
17 #include "portable.h" /* Needed for the time of the SIMIX convertion */
24 //XBT_LOG_NEW_DEFAULT_SUBCATEGORY(stubgen, gras, "lua Stub generator");
26 //to geretae associed files
27 //XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(stubgen);
30 #define WARN "/***********\n * DO NOT EDIT! THIS FILE HAS BEEN AUTOMATICALLY GENERATED FROM %s BY gras_stub_generator\n ***********/\n"
31 #define SIM_SOURCENAME "_%s_simulator.c"
32 #define SIM_OBJNAME "_%s_simulator.o"
33 #define SIM_BINARYNAME "%s_simulator"
34 #define SIM_SOURCENAME_LDADD "%s_simulator_LDADD"
35 #define SIM_SOURCENAME_SOURCES "%s_simulator_SOURCES"
36 #define RL_SOURCENAME "_%s_%s.c"
37 #define RL_OBJNAME "_%s_%s.o"
38 #define RL_BINARYNAME "%s_%s"
39 #define RL_SOURCENAME_LDADD "%s_%s_LDADD"
40 #define RL_SOURCENAME_SOURCES "%s_%s_SOURCES"
41 #define MAKEFILE_FILENAME_AM "%s.Makefile.am"
42 #define MAKEFILE_FILENAME_LOCAL "%s.mk"
43 #define MAKEFILE_FILENAME_REMOTE "%s.Makefile.remote"
44 #define DEPLOYMENT "%s.deploy.sh"
48 /**********************************************/
49 /**** Generate the file for the simulator *****/
50 /**********************************************/
52 const char *SIM_PREEMBULE =
53 "/* specific to Borland Compiler */\n"
54 "#ifdef __BORLANDC__\n"
57 "#include <stdlib.h>\n"
58 "#include <stdio.h>\n"
59 "#include \"msg/msg.h\"\n"
60 "#include <gras.h>\n" "\n" "char *gras_log=NULL;\n";
63 #define SIM_LAUNCH_FUNC \
64 "int launch_%s(int argc, char **argv) {\n" \
65 " char **myargv=argv;\n" \
66 " int myargc=argc;\n" \
71 " myargv=malloc((argc+1) * sizeof(char**));\n" \
72 " for (i=0; i<argc; i++)\n" \
73 " myargv[i] = argv[i];\n" \
74 " myargv[myargc++] = gras_log;\n" \
76 " retcode = %s(myargc,myargv);\n" \
77 " if (myargv != argv)\n" \
79 " return retcode;\n" \
82 const char *SIM_MAIN_POSTEMBULE = "\n"
84 " gras_load_platform_script(argv[1]);\n"
86 " /* Run the simulation */\n"
89 " /* cleanup the place */\n"
91 " if (gras_log)\n" " free(gras_log);\n" " return 0;\n" "}\n";
94 /***************************************
96 ***************************************/
100 void generate_sim(const char *project)
102 xbt_dict_cursor_t cursor = NULL;
105 char *filename = NULL;
108 /* Output file: <projet>_simulator.c */
109 filename = xbt_new(char, strlen(project) + strlen(SIM_SOURCENAME));
110 sprintf(filename, SIM_SOURCENAME, project);
112 OUT = fopen(filename, "w");
114 xbt_assert1(OUT, "Unable to open %s for writing", filename);
116 fprintf(OUT, "%s", SIM_PREEMBULE);
118 xbt_dict_foreach(process_function_set, cursor, key, data) {
119 fprintf(OUT, "int %s(int argc,char *argv[]);\n", key);
124 xbt_dict_foreach(process_function_set, cursor, key, data) {
125 fprintf(OUT, "int launch_%s(int argc,char *argv[]);\n", key);
129 xbt_dict_foreach(process_function_set, cursor, key, data) {
130 fprintf(OUT, SIM_LAUNCH_FUNC, key, key);
132 fprintf(OUT, "\n%s\n", warning);
134 fprintf(OUT, "%s", "/* specific to Borland Compiler */\n"
135 "#ifdef __BORLANDDC__\n" "#pragma argsused\n" "#endif\n\n");
137 fprintf(OUT, "%s", "int main (int argc,char *argv[]) {\n"
139 " /* Simulation setup */\n"
140 " gras_global_init(&argc,argv);\n"
141 " if (argc != 2) {\n"
142 " fprintf(stderr, \"Usage: lua platform_script.lua [--log=...]\\n\");\n"
143 " exit(1);\n" " }\n" "\n");
145 "\n" " /* Application deployment */\n");
146 xbt_dict_foreach(process_function_set, cursor, key, data) {
147 fprintf(OUT, " gras_function_register(\"%s\", launch_%s);\n", key, key);
149 fprintf(OUT, "%s", SIM_MAIN_POSTEMBULE);
154 /**********************************************/
155 /**** Generate the file for the real life *****/
156 /**********************************************/
157 void generate_rl(const char *project)
159 xbt_dict_cursor_t cursor = NULL;
162 char *filename = NULL;
165 xbt_dict_foreach(process_function_set, cursor, key, data) {
167 xbt_new(char, strlen(project) + strlen(RL_SOURCENAME) + strlen(key));
169 sprintf(filename, RL_SOURCENAME, project, key);
171 OUT = fopen(filename, "w");
172 xbt_assert1(OUT, "Unable to open %s for writing", filename);
174 fprintf(OUT, "\n%s\n", warning);
175 fprintf(OUT, "/* specific to Borland Compiler */\n"
176 "#ifdef __BORLANDC__\n"
179 "#include <stdio.h>\n"
180 "#include <signal.h>\n"
181 "#include <gras.h>\n"
183 "XBT_PUBLIC_DATA(const char *) _gras_procname;\n"
185 "int %s(int argc, char *argv[]);\n"
187 "/* specific to Borland Compiler */\n"
188 "#ifdef __BORLANDC__\n"
191 "int main(int argc, char *argv[]){\n"
194 " _gras_procname = \"%s\";\n"
195 " errcode=%s(argc,argv);\n"
196 " \n" " return errcode;\n" "}\n", key, key, key);
197 fprintf(OUT, "\n%s\n", warning);
203 void generate_makefile_am(const char *project)
205 xbt_dict_cursor_t cursor = NULL;
208 char *filename = NULL;
211 filename = xbt_new(char, strlen(project) + strlen(MAKEFILE_FILENAME_AM));
212 sprintf(filename, MAKEFILE_FILENAME_AM, project);
214 OUT = fopen(filename, "w");
215 xbt_assert1(OUT, "Unable to open %s for writing", filename);
217 fprintf(OUT, "# AUTOMAKE variable definition\n");
218 fprintf(OUT, "INCLUDES= @CFLAGS_SimGrid@\n\n");
219 fprintf(OUT, "PROGRAMS=");
220 fprintf(OUT, SIM_BINARYNAME, project);
222 xbt_dict_foreach(process_function_set, cursor, key, data) {
224 fprintf(OUT, RL_BINARYNAME, project, key);
227 fprintf(OUT, "\n\n");
228 fprintf(OUT, SIM_SOURCENAME_SOURCES, project);
230 fprintf(OUT, SIM_SOURCENAME, project);
231 fprintf(OUT, " %s.c\n", project);
232 fprintf(OUT, SIM_SOURCENAME_LDADD, project);
233 fprintf(OUT, "=\tpath/to/libsimgrid.a\n\n");
235 xbt_dict_foreach(process_function_set, cursor, key, data) {
236 fprintf(OUT, RL_SOURCENAME_SOURCES, project, key);
238 fprintf(OUT, RL_SOURCENAME, project, key);
239 fprintf(OUT, " %s.c\n", project);
240 fprintf(OUT, RL_SOURCENAME_LDADD, project, key);
241 fprintf(OUT, "=\tpath/to/libgras.a\n\n");
245 "\n# cleanup temps (allowing the user to add extra clean files)\n");
246 fprintf(OUT, "CLEANFILES?= \n");
247 fprintf(OUT, "CLEANFILES+= ");
248 fprintf(OUT, SIM_SOURCENAME, project);
250 xbt_dict_foreach(process_function_set, cursor, key, data) {
252 fprintf(OUT, RL_SOURCENAME, project, key);
256 fprintf(OUT, "\n# generate temps\n");
258 "\n# A rule to generate the source file each time the deployment file changes\n");
260 xbt_dict_foreach(process_function_set, cursor, key, data) {
261 fprintf(OUT, RL_SOURCENAME, project, key);
264 fprintf(OUT, SIM_SOURCENAME, project);
265 /*fprintf(OUT, ": %s\n", deployment);
266 fprintf(OUT, "\tgras_stub_generator %s %s >/dev/null\n", project,
271 void generate_makefile_local(const char *project)
273 xbt_dict_cursor_t cursor = NULL;
276 char *filename = NULL;
279 filename = xbt_new(char, strlen(project) + strlen(MAKEFILE_FILENAME_LOCAL));
280 sprintf(filename, MAKEFILE_FILENAME_LOCAL, project);
282 OUT = fopen(filename, "w");
283 xbt_assert1(OUT, "Unable to open %s for writing", filename);
289 "#### THIS FILE WAS GENERATED, DO NOT EDIT BEFORE RENAMING IT\n"
292 fprintf(OUT, "## Variable declarations\n"
293 "PROJECT_NAME=%s\n" "DISTDIR=gras-$(PROJECT_NAME)\n\n", project);
296 "# Set the GRAS_ROOT environment variable to the path under which you installed SimGrid\n"
297 "# Compilation will fail if you don't do so\n"
298 "GRAS_ROOT?= $(shell if [ -e /usr/local/lib/libgras.so ] ; then echo /usr/local ; else echo \"\\\"<<<< GRAS_ROOT undefined !!! >>>>\\\"\"; fi)\n\n"
299 "# You can fiddle the following to make it fit your taste\n"
300 "INCLUDES = -I$(GRAS_ROOT)/include\n"
301 "CFLAGS ?= -O3 -w -g -Wall\n"
302 "LIBS_SIM = -lm -L$(GRAS_ROOT)/lib/ -lsimgrid\n"
303 "LIBS_RL = -lm -L$(GRAS_ROOT)/lib/ -lgras\n" "LIBS = \n" "\n");
305 fprintf(OUT, "PRECIOUS_C_FILES ?= %s.c\n", project);
307 fprintf(OUT, "GENERATED_C_FILES = ");
308 fprintf(OUT, SIM_SOURCENAME " ", project);
309 xbt_dict_foreach(process_function_set, cursor, key, data) {
310 fprintf(OUT, RL_SOURCENAME " ", project, key);
314 fprintf(OUT, "OBJ_FILES = $(patsubst %%.c,%%.o,$(PRECIOUS_C_FILES))\n");
316 fprintf(OUT, "BIN_FILES = ");
318 fprintf(OUT, SIM_BINARYNAME " ", project);
319 xbt_dict_foreach(process_function_set, cursor, key, data) {
320 fprintf(OUT, RL_BINARYNAME " ", project, key);
326 "## By default, build all the binaries\n"
327 "all: $(BIN_FILES)\n" "\n");
329 //No Need to generate source files, already done.
331 fprintf(OUT, "\n## Generate the binaries\n");
332 fprintf(OUT, SIM_BINARYNAME ": " SIM_OBJNAME " $(OBJ_FILES)\n", project,
335 "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS_SIM) $(LIBS) $(LDADD) -o $@ \n");
336 xbt_dict_foreach(process_function_set, cursor, key, data) {
337 fprintf(OUT, RL_BINARYNAME " : " RL_OBJNAME " $(OBJ_FILES)\n", project,
340 "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS_RL) $(LIBS) $(LDADD) -o $@ \n");
345 "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS) $(LDADD) -o $@ \n"
348 "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) -c -o $@ $<\n" "\n");
351 "## Rules for tarballs and cleaning\n"
352 "DIST_FILES= $(EXTRA_DIST) $(GENERATED_C_FILES) $(PRECIOUS_C_FILES) "
353 MAKEFILE_FILENAME_LOCAL " " /*MAKEFILE_FILENAME_REMOTE */ "\n"
354 "distdir: $(DIST_FILES)\n" "\trm -rf $(DISTDIR)\n"
355 "\tmkdir -p $(DISTDIR)\n" "\tcp $^ $(DISTDIR)\n" "\n"
356 "dist: clean distdir\n"
357 "\ttar c $(DISTDIR) | gzip -c9 > $(DISTDIR).tar.gz\n" "\n",
358 project /*, project */ );
362 "\trm -f $(CLEANFILES) $(BIN_FILES) $(OBJ_FILES) *~ %s.o "
363 SIM_OBJNAME, project, project);
364 xbt_dict_foreach(process_function_set, cursor, key, data) {
365 fprintf(OUT, " " RL_OBJNAME, project, key);
369 "\trm -rf $(DISTDIR)\n"
370 "\n" ".SUFFIXES:\n" ".PHONY : clean\n" "\n");
374 static void print(void *p)