Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add new source file to lua bindings code, having possibility to use a 'lua' version...
authorcoldpeace <coldpeace@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Tue, 10 Aug 2010 16:28:59 +0000 (16:28 +0000)
committercoldpeace <coldpeace@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Tue, 10 Aug 2010 16:28:59 +0000 (16:28 +0000)
git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@8153 48e7efb5-ca39-0410-a469-dd3cf9ba447f

buildtools/Cmake/DefinePackages.cmake
src/bindings/lua/lua_stub_generator.c [new file with mode: 0644]
src/bindings/lua/lua_stub_generator.h [new file with mode: 0644]
src/bindings/lua/simgrid_lua.c

index 4d3eb88..1c01327 100755 (executable)
@@ -321,6 +321,7 @@ set(AMOK_SRC
 set(LUA_SRC
        src/simix/smx_context_lua.c
        src/bindings/lua/simgrid_lua.c
+       src/bindings/lua/lua_stub_generator.c
 )
 
 set(TRACING_SRC
diff --git a/src/bindings/lua/lua_stub_generator.c b/src/bindings/lua/lua_stub_generator.c
new file mode 100644 (file)
index 0000000..33d8a64
--- /dev/null
@@ -0,0 +1,378 @@
+/* gras_stub_generator - creates the main() to use a GRAS program           */
+
+/* Copyright (c) 2005, 2006, 2007, 2009, 2010. The SimGrid Team.
+ * All rights reserved.                                                     */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+#include "lua_stub_generator.h"
+
+#include <stdio.h>
+#include "xbt/sysdep.h"
+#include "xbt/function_types.h"
+#include "xbt/log.h"
+#include "surf/surfxml_parse.h"
+#include "surf/surf.h"
+#include "portable.h"           /* Needed for the time of the SIMIX convertion */
+#include <stdarg.h>
+
+#include <lua.h>
+#include <lauxlib.h>
+#include <lualib.h>
+
+//XBT_LOG_NEW_DEFAULT_SUBCATEGORY(stubgen, gras, "lua Stub generator");
+
+//to geretae associed files
+//XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(stubgen);
+
+
+#define WARN "/***********\n * DO NOT EDIT! THIS FILE HAS BEEN AUTOMATICALLY GENERATED FROM %s BY gras_stub_generator\n ***********/\n"
+#define SIM_SOURCENAME  "_%s_simulator.c"
+#define SIM_OBJNAME  "_%s_simulator.o"
+#define SIM_BINARYNAME  "%s_simulator"
+#define SIM_SOURCENAME_LDADD  "%s_simulator_LDADD"
+#define SIM_SOURCENAME_SOURCES  "%s_simulator_SOURCES"
+#define RL_SOURCENAME  "_%s_%s.c"
+#define RL_OBJNAME  "_%s_%s.o"
+#define RL_BINARYNAME  "%s_%s"
+#define RL_SOURCENAME_LDADD  "%s_%s_LDADD"
+#define RL_SOURCENAME_SOURCES  "%s_%s_SOURCES"
+#define MAKEFILE_FILENAME_AM  "%s.Makefile.am"
+#define MAKEFILE_FILENAME_LOCAL  "%s.mk"
+#define MAKEFILE_FILENAME_REMOTE  "%s.Makefile.remote"
+#define DEPLOYMENT  "%s.deploy.sh"
+
+char *warning = NULL;
+
+/**********************************************/
+/**** Generate the file for the simulator *****/
+/**********************************************/
+
+const char *SIM_PREEMBULE =
+  "/* specific to Borland Compiler */\n"
+  "#ifdef __BORLANDC__\n"
+  "#pragma hdrstop\n"
+  "#endif\n\n"
+  "#include <stdlib.h>\n"
+  "#include <stdio.h>\n"
+  "#include \"msg/msg.h\"\n"
+  "#include <gras.h>\n" "\n" "char *gras_log=NULL;\n";
+
+
+#define SIM_LAUNCH_FUNC  \
+"int launch_%s(int argc, char **argv) {\n" \
+"  char **myargv=argv;\n" \
+"  int myargc=argc;\n" \
+"  int i;\n" \
+"  int retcode;\n"\
+"    \n"\
+"  if (gras_log) {\n"\
+"    myargv=malloc((argc+1) * sizeof(char**));\n" \
+"    for (i=0; i<argc; i++)\n" \
+"      myargv[i] = argv[i];\n" \
+"    myargv[myargc++] = gras_log;\n" \
+"  }\n" \
+"  retcode = %s(myargc,myargv);\n" \
+"  if (myargv != argv)\n" \
+"    free(myargv);\n" \
+"  return retcode;\n" \
+"}\n"
+
+const char *SIM_MAIN_POSTEMBULE = "\n"
+  "\n"
+  "  gras_load_platform_script(argv[1]);\n"
+  "\n"
+  "  /*  Run the simulation */\n"
+  "  gras_main();\n"
+  "\n"
+  "  /* cleanup the place */\n"
+  "  gras_clean();\n"
+  "  if (gras_log)\n" "    free(gras_log);\n" "  return 0;\n" "}\n";
+
+
+/***************************************
+ * generator functions
+ ***************************************/
+
+
+
+void generate_sim(const char *project)
+{
+  xbt_dict_cursor_t cursor = NULL;
+  char *key = NULL;
+  void *data = NULL;
+  char *filename = NULL;
+  FILE *OUT = NULL;
+
+  /* Output file: <projet>_simulator.c */
+  filename = xbt_new(char, strlen(project) + strlen(SIM_SOURCENAME));
+  sprintf(filename, SIM_SOURCENAME, project);
+
+  OUT = fopen(filename, "w");
+
+  xbt_assert1(OUT, "Unable to open %s for writing", filename);
+
+  fprintf(OUT, "%s", SIM_PREEMBULE);
+
+  xbt_dict_foreach(process_function_set, cursor, key, data) {
+    fprintf(OUT, "int %s(int argc,char *argv[]);\n", key);
+  }
+
+  fprintf(OUT, "\n");
+
+  xbt_dict_foreach(process_function_set, cursor, key, data) {
+    fprintf(OUT, "int launch_%s(int argc,char *argv[]);\n", key);
+  }
+
+
+  xbt_dict_foreach(process_function_set, cursor, key, data) {
+    fprintf(OUT, SIM_LAUNCH_FUNC, key, key);
+  }
+  fprintf(OUT, "\n%s\n", warning);
+
+  fprintf(OUT, "%s", "/* specific to Borland Compiler */\n"
+          "#ifdef __BORLANDDC__\n" "#pragma argsused\n" "#endif\n\n");
+
+  fprintf(OUT, "%s", "int main (int argc,char *argv[]) {\n"
+          "\n"
+          "  /*  Simulation setup */\n"
+          "  gras_global_init(&argc,argv);\n"
+          "  if (argc != 2) {\n"
+          "    fprintf(stderr, \"Usage: lua platform_script.lua [--log=...]\\n\");\n"
+          "    exit(1);\n" "  }\n" "\n");
+  fprintf(OUT,
+          "  gras_load_platform_script(argv[1]);\n"
+          "\n" "  /*  Application deployment */\n");
+  xbt_dict_foreach(process_function_set, cursor, key, data) {
+    fprintf(OUT, "  gras_function_register(\"%s\", launch_%s);\n", key, key);
+  }
+  fprintf(OUT, "%s", SIM_MAIN_POSTEMBULE);
+  fclose(OUT);
+  free(filename);
+}
+
+/**********************************************/
+/**** Generate the file for the real life *****/
+/**********************************************/
+void generate_rl(const char *project)
+{
+  xbt_dict_cursor_t cursor = NULL;
+  char *key = NULL;
+  void *data = NULL;
+  char *filename = NULL;
+  FILE *OUT = NULL;
+
+  xbt_dict_foreach(process_function_set, cursor, key, data) {
+    filename =
+      xbt_new(char, strlen(project) + strlen(RL_SOURCENAME) + strlen(key));
+
+    sprintf(filename, RL_SOURCENAME, project, key);
+
+    OUT = fopen(filename, "w");
+    xbt_assert1(OUT, "Unable to open %s for writing", filename);
+
+    fprintf(OUT, "\n%s\n", warning);
+    fprintf(OUT, "/* specific to Borland Compiler */\n"
+            "#ifdef __BORLANDC__\n"
+            "#pragma hdrstop\n"
+            "#endif\n\n"
+            "#include <stdio.h>\n"
+            "#include <signal.h>\n"
+            "#include <gras.h>\n"
+            "\n"
+            "XBT_PUBLIC_DATA(const char *) _gras_procname;\n"
+            "/* user code */\n"
+            "int %s(int argc, char *argv[]);\n"
+            "\n"
+            "/* specific to Borland Compiler */\n"
+            "#ifdef __BORLANDC__\n"
+            "#pragma argsused\n"
+            "#endif\n\n"
+            "int main(int argc, char *argv[]){\n"
+            "  int errcode;\n"
+            "\n"
+            "  _gras_procname = \"%s\";\n"
+            "  errcode=%s(argc,argv);\n"
+            " \n" "  return errcode;\n" "}\n", key, key, key);
+    fprintf(OUT, "\n%s\n", warning);
+    fclose(OUT);
+    free(filename);
+  }
+}
+
+void generate_makefile_am(const char *project)
+{
+  xbt_dict_cursor_t cursor = NULL;
+  char *key = NULL;
+  void *data = NULL;
+  char *filename = NULL;
+  FILE *OUT = NULL;
+
+  filename = xbt_new(char, strlen(project) + strlen(MAKEFILE_FILENAME_AM));
+  sprintf(filename, MAKEFILE_FILENAME_AM, project);
+
+  OUT = fopen(filename, "w");
+  xbt_assert1(OUT, "Unable to open %s for writing", filename);
+
+  fprintf(OUT, "# AUTOMAKE variable definition\n");
+  fprintf(OUT, "INCLUDES= @CFLAGS_SimGrid@\n\n");
+  fprintf(OUT, "PROGRAMS=");
+  fprintf(OUT, SIM_BINARYNAME, project);
+
+  xbt_dict_foreach(process_function_set, cursor, key, data) {
+    fprintf(OUT, " ");
+    fprintf(OUT, RL_BINARYNAME, project, key);
+  }
+
+  fprintf(OUT, "\n\n");
+  fprintf(OUT, SIM_SOURCENAME_SOURCES, project);
+  fprintf(OUT, "=\t");
+  fprintf(OUT, SIM_SOURCENAME, project);
+  fprintf(OUT, " %s.c\n", project);
+  fprintf(OUT, SIM_SOURCENAME_LDADD, project);
+  fprintf(OUT, "=\tpath/to/libsimgrid.a\n\n");
+
+  xbt_dict_foreach(process_function_set, cursor, key, data) {
+    fprintf(OUT, RL_SOURCENAME_SOURCES, project, key);
+    fprintf(OUT, "=\t");
+    fprintf(OUT, RL_SOURCENAME, project, key);
+    fprintf(OUT, " %s.c\n", project);
+    fprintf(OUT, RL_SOURCENAME_LDADD, project, key);
+    fprintf(OUT, "=\tpath/to/libgras.a\n\n");
+  }
+
+  fprintf(OUT,
+          "\n# cleanup temps (allowing the user to add extra clean files)\n");
+  fprintf(OUT, "CLEANFILES?= \n");
+  fprintf(OUT, "CLEANFILES+= ");
+  fprintf(OUT, SIM_SOURCENAME, project);
+
+  xbt_dict_foreach(process_function_set, cursor, key, data) {
+    fprintf(OUT, " ");
+    fprintf(OUT, RL_SOURCENAME, project, key);
+  }
+  fprintf(OUT, "\n");
+
+  fprintf(OUT, "\n# generate temps\n");
+  fprintf(OUT,
+          "\n# A rule to generate the source file each time the deployment file changes\n");
+
+  xbt_dict_foreach(process_function_set, cursor, key, data) {
+    fprintf(OUT, RL_SOURCENAME, project, key);
+    fprintf(OUT, " ");
+  }
+  fprintf(OUT, SIM_SOURCENAME, project);
+  /*fprintf(OUT, ": %s\n", deployment);
+  fprintf(OUT, "\tgras_stub_generator %s %s >/dev/null\n", project,
+          deployment);*/
+  fclose(OUT);
+}
+
+void generate_makefile_local(const char *project)
+{
+  xbt_dict_cursor_t cursor = NULL;
+  char *key = NULL;
+  void *data = NULL;
+  char *filename = NULL;
+  FILE *OUT = NULL;
+
+  filename = xbt_new(char, strlen(project) + strlen(MAKEFILE_FILENAME_LOCAL));
+  sprintf(filename, MAKEFILE_FILENAME_LOCAL, project);
+
+  OUT = fopen(filename, "w");
+  xbt_assert1(OUT, "Unable to open %s for writing", filename);
+  free(filename);
+
+  fprintf(OUT,
+          "\n"
+          "####\n"
+          "#### THIS FILE WAS GENERATED, DO NOT EDIT BEFORE RENAMING IT\n"
+          "####\n\n\n");
+
+  fprintf(OUT, "## Variable declarations\n"
+          "PROJECT_NAME=%s\n" "DISTDIR=gras-$(PROJECT_NAME)\n\n", project);
+
+  fprintf(OUT,
+          "# Set the GRAS_ROOT environment variable to the path under which you installed SimGrid\n"
+          "# Compilation will fail if you don't do so\n"
+          "GRAS_ROOT?= $(shell if [ -e /usr/local/lib/libgras.so ] ; then echo /usr/local ; else echo \"\\\"<<<< GRAS_ROOT undefined !!! >>>>\\\"\"; fi)\n\n"
+          "# You can fiddle the following to make it fit your taste\n"
+          "INCLUDES = -I$(GRAS_ROOT)/include\n"
+          "CFLAGS ?= -O3 -w -g -Wall\n"
+          "LIBS_SIM = -lm  -L$(GRAS_ROOT)/lib/ -lsimgrid\n"
+          "LIBS_RL = -lm  -L$(GRAS_ROOT)/lib/ -lgras\n" "LIBS = \n" "\n");
+
+  fprintf(OUT, "PRECIOUS_C_FILES ?= %s.c\n", project);
+
+  fprintf(OUT, "GENERATED_C_FILES = ");
+  fprintf(OUT, SIM_SOURCENAME " ", project);
+  xbt_dict_foreach(process_function_set, cursor, key, data) {
+    fprintf(OUT, RL_SOURCENAME " ", project, key);
+  }
+  fprintf(OUT, "\n");
+
+  fprintf(OUT, "OBJ_FILES = $(patsubst %%.c,%%.o,$(PRECIOUS_C_FILES))\n");
+
+  fprintf(OUT, "BIN_FILES = ");
+
+  fprintf(OUT, SIM_BINARYNAME " ", project);
+  xbt_dict_foreach(process_function_set, cursor, key, data) {
+    fprintf(OUT, RL_BINARYNAME " ", project, key);
+  }
+  fprintf(OUT, "\n");
+
+  fprintf(OUT,
+          "\n"
+          "## By default, build all the binaries\n"
+          "all: $(BIN_FILES)\n" "\n");
+
+  //No Need to generate source files, already done.
+
+  fprintf(OUT, "\n## Generate the binaries\n");
+  fprintf(OUT, SIM_BINARYNAME ": " SIM_OBJNAME " $(OBJ_FILES)\n", project,
+          project);
+  fprintf(OUT,
+          "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS_SIM) $(LIBS) $(LDADD) -o $@ \n");
+  xbt_dict_foreach(process_function_set, cursor, key, data) {
+    fprintf(OUT, RL_BINARYNAME " : " RL_OBJNAME " $(OBJ_FILES)\n", project,
+            key, project, key);
+    fprintf(OUT,
+            "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS_RL) $(LIBS) $(LDADD) -o $@ \n");
+  }
+  fprintf(OUT,
+          "\n"
+          "%%: %%.o\n"
+          "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) $^ $(LIBS) $(LDADD) -o $@ \n"
+          "\n"
+          "%%.o: %%.c\n"
+          "\t$(CC) $(INCLUDES) $(DEFS) $(CFLAGS) -c -o $@ $<\n" "\n");
+
+  fprintf(OUT,
+          "##┬áRules for tarballs and cleaning\n"
+          "DIST_FILES= $(EXTRA_DIST) $(GENERATED_C_FILES) $(PRECIOUS_C_FILES) "
+          MAKEFILE_FILENAME_LOCAL " " /*MAKEFILE_FILENAME_REMOTE */ "\n"
+          "distdir: $(DIST_FILES)\n" "\trm -rf $(DISTDIR)\n"
+          "\tmkdir -p $(DISTDIR)\n" "\tcp $^ $(DISTDIR)\n" "\n"
+          "dist: clean distdir\n"
+          "\ttar c $(DISTDIR) | gzip -c9 > $(DISTDIR).tar.gz\n" "\n",
+          project /*, project */ );
+
+  fprintf(OUT,
+          "clean:\n"
+          "\trm -f $(CLEANFILES) $(BIN_FILES) $(OBJ_FILES) *~ %s.o "
+          SIM_OBJNAME, project, project);
+  xbt_dict_foreach(process_function_set, cursor, key, data) {
+    fprintf(OUT, " " RL_OBJNAME, project, key);
+  }
+  fprintf(OUT,
+          "\n"
+          "\trm -rf $(DISTDIR)\n"
+          "\n" ".SUFFIXES:\n" ".PHONY : clean\n" "\n");
+  fclose(OUT);
+}
+
+static void print(void *p)
+{
+  printf("%p", p);
+}
diff --git a/src/bindings/lua/lua_stub_generator.h b/src/bindings/lua/lua_stub_generator.h
new file mode 100644 (file)
index 0000000..7d393f2
--- /dev/null
@@ -0,0 +1,29 @@
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+#ifndef GRAS_STUB_GENERATOR_H
+#define GRAS_STUB_GENERATOR_H
+
+#include "xbt/dynar.h"
+#include "xbt/dict.h"
+xbt_dict_t process_function_set;
+xbt_dynar_t process_list;
+xbt_dict_t machine_set;
+
+typedef struct s_process_t {
+  int argc;
+  char **argv;
+  char *host;
+} s_process_t;
+
+char *warning;
+
+void s_process_free(void *process);
+
+/* UNIX files */
+void generate_sim(const char *project);
+void generate_rl(const char *project);
+void generate_makefile_am(const char *project);
+void generate_makefile_local(const char *project);
+
+#endif
index dc07914..915c5ad 100644 (file)
@@ -13,6 +13,7 @@
 #include "simdag/simdag.h"
 #include <gras.h>
 #include "xbt.h"
+#include "lua_stub_generator.h"
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua,bindings,"Lua Bindings");
 
@@ -681,6 +682,77 @@ static int surf_parse_bypass_application()
          return 0;
 }
 
+
+/**
+ *  Generate Gras Templates File from lua
+ */
+
+xbt_dict_t process_function_set;
+xbt_dynar_t process_list;
+xbt_dict_t machine_set;
+static s_process_t process;
+
+void s_process_free(void *process)
+{
+  s_process_t *p = (s_process_t *) process;
+  int i;
+  for (i = 0; i < p->argc; i++)
+    free(p->argv[i]);
+  free(p->argv);
+  free(p->host);
+}
+
+static int gras_add_process_function(lua_State *L)
+{
+       const char * arg;
+       const char* process_host = luaL_checkstring(L,1);
+       const char *process_function = luaL_checkstring(L,2);
+
+       if(xbt_dict_is_empty(machine_set) || xbt_dict_is_empty(process_function_set)
+                       || xbt_dynar_is_empty(process_list))
+               {
+                 process_function_set = xbt_dict_new();
+                 process_list = xbt_dynar_new(sizeof(s_process_t), s_process_free);
+                 machine_set = xbt_dict_new();
+               }
+
+       xbt_dict_set(machine_set,process_host, NULL, NULL);
+       xbt_dict_set(process_function_set,process_function, NULL, NULL);
+
+       process.argc = 1;
+       process.argv = xbt_new(char *, 1);
+       process.argv[0] = xbt_strdup(process_function);
+       process.host = strdup(process_host);
+
+       lua_pushnil(L);
+       while (lua_next(L,3) != 0) {
+               arg = lua_tostring(L, -1);
+               process.argc++;
+               process.argv = xbt_realloc(process.argv, (process.argc) * sizeof(char *));
+               process.argv[(process.argc) - 1] = xbt_strdup(arg);
+
+               DEBUG2("index = %f , arg = %s \n",lua_tonumber(L, -2),lua_tostring(L, -1));
+               lua_pop(L, 1);
+               }
+       lua_pop(L, 1);
+       //add to the process list
+       xbt_dynar_push(process_list, &process);
+
+       return 0;
+
+}
+
+
+static int gras_generate(lua_State *L)
+{
+  const char *project_name = luaL_checkstring(L,1);
+  generate_sim(project_name);
+  generate_rl(project_name);
+  generate_makefile_local(project_name);
+
+  return 0;
+}
+
 //***********Register Methods *******************************************//
 /*
  * Host Methods
@@ -835,7 +907,7 @@ static int sd_register_platform(lua_State *L)
  */
 static int gras_register_platform(lua_State *L)
 {
-       /* Tell Simgrid we dont wanna use its parser*/
+       /* Tell Simgrid we dont wanna use surf parser*/
        surf_parse = surf_parse_bypass_platform;
        gras_create_environment(NULL);
        return 0;
@@ -878,6 +950,9 @@ static const luaL_Reg simgrid_funcs[] = {
     { "msg_register_application",msg_register_application},
     { "gras_register_platform",gras_register_platform},
     { "gras_register_application",gras_register_application},
+    /* gras sub generator method*/
+    {"gras_set_process_function",gras_add_process_function},
+    {"gras_generate",gras_generate},
     { NULL, NULL }
 };