Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
autogenerated files (sorry)
[simgrid.git] / tools / gras / stub_generator.c
index 5fd470f..fbba2ff 100644 (file)
 
 /* gras_stub_generator - creates the main() to use a GRAS program           */
 
-/* Copyright (c) 2003,2004,2005 Martin Quinson, Arnaud Legrand. 
-   All rights reserved.                  */
+/* Copyright (c) 2003-2007 Martin Quinson, Arnaud Legrand, Malek Cherier.   */
+/* 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"xbt/sysdep.h"
-#include"xbt/dict.h"
-#include"xbt/error.h"
-#include "surf/surf_parse.h"
+/* specific to Borland Compiler */
+#ifdef __BORLANDDC__
+#pragma hdrstop
+#endif
+
+#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 */
 
-XBT_LOG_NEW_DEFAULT_SUBCATEGORY(stubgen,gras,"Stub generator");
+#include "gras_stub_generator.h"
+#include <stdarg.h>
 
-const char *WARN = "/***********\n * DO NOT EDIT! THIS FILE WERE AUTOMATICALLY GENERATED FROM %s BY gras_stub_generator\n ***********/\n";
-const char *SIM_FILENAME = "_%s_simulator.c";
-const char *SIM_BINARYNAME = "%s_simulator";
-const char *SIM_FILENAME_LDADD = "_%s_simulator_LDADD";
-const char *SIM_FILENAME_SOURCES = "_%s_simulator_SOURCES";
-const char *RL_FILENAME = "_%s_%s.c";
-const char *RL_BINARYNAME = "%s_%s";
-const char *RL_FILENAME_LDADD = "%s_%s_LDADD";
-const char *RL_FILENAME_SOURCES = "%s_%s_SOURCES";
-const char *MAKEFILE_FILENAME = "%s.Makefile.am";
 
-char *warning = NULL;
 
-/**********************************************/
-/**** Generate the file for the simulator *****/
-/**********************************************/
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(stubgen,gras,"Stub generator");
 
-const char *SIM_PREEMBULE =
-"#include <stdlib.h>\n"
-"#include <stdio.h>\n"
-"#include \"msg/msg.h\"\n"
-"#include <gras.h>\n"
-"\n"
-"char *gras_log=NULL;\n";
-
-
-const char *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_PREEMBULE =
-"int main (int argc,char *argv[]) {\n"
-"  int i,j;\n"
-"\n"
-"  /* Save the gras-log argument of real command line to pass it to all processes */\n"
-"  for (i=1; i<argc; i++) {\n"
-"    if (!strncmp(argv[i],\"--gras-log=\",strlen(\"--gras-log=\"))) {\n"
-"      if (gras_log) {\n"
-"       char *tmp=malloc(strlen(gras_log)+strlen(argv[i])+2);\n"
-"       sprintf(tmp,\"%%s %%s\",gras_log, argv[i]);\n"
-"       free(gras_log);\n"
-"       gras_log=tmp;\n"
-"      } else {\n"
-"         gras_log = strdup(argv[i]);\n"
-"      }\n"
-"      for (j=i+1; j<argc; j++) {\n"
-"      argv[j-1] = argv[j];\n"
-"      } \n"
-"      argv[j-1] = NULL;\n"
-"      argc--;\n"
-"      i--; /* compensate effect of next loop incrementation */\n"
-"    }\n"
-"  }\n"
-"\n"
-"  if (argc != 3) {\n"
-"    fprintf(stderr, \"Usage: %%s platform_file application_description.txt [--gras-log=...]\\n\",argv[0]);\n"
-"    exit(1);\n"
-"  }\n"
-"\n"
-"  /*  Simulation setup */\n"
-"  MSG_global_init();\n"
-"  MSG_set_verbosity(MSG_SILENT);\n"
-"  MSG_set_channel_number(10); // GRAS_MAX_CHANNEL hardcoded since Alvin killed its definition\n"
-"  MSG_create_environment(argv[1]);\n"
-"\n"
-"  /*  Application deployment */\n";
-
-const char *SIM_MAIN_POSTEMBULE = "\n"
-"\n"
-"  MSG_launch_application(argv[2]);\n"
-"\n"
-"  /*  Run the simulation */\n"
-"  MSG_main();\n"
-"\n"
-"  /* cleanup the place */\n"
-"  MSG_clean();\n"
-"  if (gras_log)\n"
-"    free(gras_log);\n"
-"  return 0;\n"
-"}\n";
 
-/**********************************************/
-/**** Generate the file for the real life *****/
-/**********************************************/
+#ifdef _WIN32
+#include <windows.h>
+#endif
+
+/* specific to Borland Compiler */
+#ifdef __BORLANDDC__
+#pragma argsused
+#endif
 
-const char *RL_CODE =
-"#include <stdio.h>\n"
-"#include <signal.h>\n"
-"#include <gras.h>\n"
-"\n"
-"/* user code */\n"
-"int %s(int argc, char *argv[]);\n"
-"\n"
-"int main(int argc, char *argv[]){\n"
-"  int errcode;\n"
-"\n"
-"  errcode=%s(argc,argv);\n"
-" \n"
-"  return errcode;\n"
-"}\n";
 
 /**********************************************/
 /********* Parse XML deployment file **********/
 /**********************************************/
 xbt_dict_t process_function_set = NULL;
+xbt_dynar_t process_list = NULL;
+xbt_dict_t machine_set = NULL;
+
+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 s_process_t process;
 
 static void parse_process_init(void)
-{ 
-  void *p = (void *) 1231;
-  xbt_dict_set(process_function_set, A_process_function, p, NULL);
+{
+  xbt_dict_set(process_function_set, A_surfxml_process_function, NULL, NULL);
+  xbt_dict_set(machine_set, A_surfxml_process_host, NULL, NULL);
+  process.argc = 1 ;
+  process.argv = xbt_new(char*,1);
+  process.argv[0] = xbt_strdup(A_surfxml_process_function);
+  process.host=strdup(A_surfxml_process_host);
+  /*VERB1("Function: %s",A_surfxml_process_function);*/
 }
 
-static void generate_sim(char *project)
+static void parse_argument(void)
 {
-  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(SIM_FILENAME));
-  sprintf(filename,SIM_FILENAME,project);
-  
-  OUT=fopen(filename,"w");
-  xbt_assert1(OUT, "Unable to open %s for writing",filename);
-
-  fprintf(OUT, "%s\n",warning);
-  fprintf(OUT, 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);
-  }
-  fprintf(OUT, "\n%s\n",warning);
-  xbt_dict_foreach(process_function_set,cursor,key,data) {
-    fprintf(OUT,SIM_LAUNCH_FUNC,key,key);
-  }
-  fprintf(OUT, "\n%s\n",warning);
-
-  fprintf(OUT, SIM_MAIN_PREEMBULE);
-  xbt_dict_foreach(process_function_set,cursor,key,data) {
-    fprintf(OUT,"  MSG_function_register(\"%s\", launch_%s);\n",key,key);
-  }
-  fprintf(OUT, SIM_MAIN_POSTEMBULE);
-  fclose(OUT);
-  xbt_free(filename);
+  process.argc++;
+  process.argv = xbt_realloc(process.argv, (process.argc) * sizeof(char *));
+  process.argv[(process.argc) - 1] = xbt_strdup(A_surfxml_argument_value);
 }
 
-static void generate_rl(char *project)
+static void parse_process_finalize(void)
 {
-  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_FILENAME) + strlen(key));
-    sprintf(filename,RL_FILENAME,project,key);
-  
-    OUT=fopen(filename,"w");
-    xbt_assert1(OUT, "Unable to open %s for writing",filename);
-
-    fprintf(OUT, "\n%s\n",warning);
-    fprintf(OUT, RL_CODE, key,key);
-    fprintf(OUT, "\n%s\n",warning);
-    fclose(OUT);
-    xbt_free(filename);
-  }
+  xbt_dynar_push(process_list,&process);
+  /*VERB1("Function: %s",process.argv[0]);*/
 }
 
-static void generate_makefile(char *project, char *deployment)
+void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
 {
-  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));
-  sprintf(filename,MAKEFILE_FILENAME, 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_FILENAME,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_FILENAME_SOURCES,project);
-  fprintf(OUT, "=");
-  fprintf(OUT, SIM_FILENAME,project);
-  fprintf(OUT, " %s.c\n", project);
-
-  xbt_dict_foreach(process_function_set,cursor,key,data) {
-    fprintf(OUT, RL_FILENAME_SOURCES, project,key);
-    fprintf(OUT, "=\t");
-    fprintf(OUT, RL_FILENAME, project,key);
-    fprintf(OUT, " %s.c\n", project);
-  }
-
-  fprintf(OUT, "\n\n");
-  fprintf(OUT, SIM_FILENAME_LDADD, project);
-  fprintf(OUT, "=\tpath/to/libsimgrid.a\n");
-
-
-  xbt_dict_foreach(process_function_set,cursor,key,data) {
-    fprintf(OUT, RL_FILENAME_LDADD, project, key);
-    fprintf(OUT, "=\tpath/to/libgras.a\n");
-  }
-
-  fprintf(OUT, "\n# cleanup temps\n");
-  fprintf(OUT, "CLEANFILES= ");
-  fprintf(OUT, SIM_FILENAME, project);
-  
-  xbt_dict_foreach(process_function_set,cursor,key,data) {
-    fprintf(OUT, " ");
-    fprintf(OUT, RL_FILENAME, 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_FILENAME, project,key);
-    fprintf(OUT, " ");
-  }
-  fprintf(OUT, SIM_FILENAME, project);
-  fprintf(OUT, ": %s\n", deployment);
-  fprintf(OUT, "\tstub_generator %s %s >/dev/null\n", project, filename);
+   xbt_dynar_push(cb_list, &function);
 }
 
+
 int main(int argc, char *argv[])
 {
   char *project_name = NULL;
   char *deployment_file = NULL;
-
+  int i;
+   
   surf_init(&argc, argv);
-
-  xbt_assert1((argc ==3),"Usage: %s project_name deployment_file\n",argv[0]);
+  process_function_set = xbt_dict_new();
+  process_list = xbt_dynar_new(sizeof(s_process_t),s_process_free);
+  machine_set = xbt_dict_new();
+
+  for (i=1; i<argc; i++) {
+     int need_removal = 0;
+     if (!strncmp("--extra-process=",argv[i], strlen("--extra-process="))) {
+       xbt_dict_set(process_function_set, argv[i]+strlen("--extra-process="), NULL, NULL);
+       need_removal = 1;
+     }
+     
+     
+     if (need_removal) { /* remove the handled argument from argv */
+       int j;  
+       for (j=i+1; j<argc; j++) {
+          argv[j-1] = argv[j];
+       }
+       argv[j-1] = NULL;
+       argc--;
+       i--; /* compensate effect of next loop incrementation */
+     }
+  }
+       
+  xbt_assert1((argc >= 3),"Usage: %s project_name deployment_file [deployment_file...]\n",argv[0]);
 
   project_name = argv[1];
-  deployment_file = argv[2];
 
-  process_function_set = xbt_dict_new();
+  surf_parse_reset_parser();
+  DEBUG2("%p %p",parse_process_init,&parse_process_init);
+  surfxml_add_callback(STag_surfxml_process_cb_list, &parse_process_init);
+  surfxml_add_callback(ETag_surfxml_argument_cb_list, &parse_argument);
+  surfxml_add_callback(ETag_surfxml_process_cb_list, &parse_process_finalize);
+  
+  for(i=2; i<argc; i++) {
+     deployment_file = argv[i];
+     surf_parse_open(deployment_file);
+     if(surf_parse()) 
+       xbt_assert1(0,"Parse error in %s",deployment_file);
+     
+     surf_parse_close();
+  }
 
-  STag_process_fun = parse_process_init;
-  surf_parse_open(deployment_file);
-  if(surf_parse()) xbt_assert1(0,"Parse error in %s",deployment_file);
-  surf_parse_close();
 
   warning = xbt_new(char,strlen(WARN)+strlen(deployment_file)+10);
   sprintf(warning,WARN,deployment_file);
 
+  /*if(XBT_LOG_ISENABLED(stubgen, xbt_log_priority_debug)) {
+    xbt_dict_cursor_t cursor=NULL;
+    char *key = NULL;
+    void *data = NULL;
+
+    for (cursor=NULL, xbt_dict_cursor_first((process_function_set),&(cursor)) ;
+        xbt_dict_cursor_get_or_free(&(cursor),&(key),(void**)(&data));
+        xbt_dict_cursor_step(cursor) ) {
+      DEBUG1("Function %s", key);      
+    }
+    
+    xbt_dict_dump(process_function_set,print);
+  }*/
+
   generate_sim(project_name);
   generate_rl(project_name);
-  generate_makefile(project_name, deployment_file);
-
-  xbt_free(warning);
+  generate_makefile_local(project_name, deployment_file);
+#ifdef _WIN32
+  generate_borland_simulation_project(project_name);
+  generate_borland_real_life_project(project_name);
+  generate_simulation_dsp_file(project_name);
+  generate_real_live_dsp_file(project_name);
+
+  if(__gras_path)
+    xbt_free(__gras_path);   
+#endif
+
+  free(warning);
+  surf_exit();
   return 0;
 }