Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Split too long file, various cleanups
[simgrid.git] / tools / gras / stub_generator.c
1 /*      $Id$     */
2
3 /* gras_stub_generator - creates the main() to use a GRAS program           */
4
5 /* Copyright (c) 2003-2007 Martin Quinson, Arnaud Legrand, Malek Cherier.   */
6 /* All rights reserved.                                                     */
7
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. */
10
11 /* specific to Borland Compiler */
12 #ifdef __BORLANDDC__
13 #pragma hdrstop
14 #endif
15
16 #include <stdio.h>
17 #include "xbt/sysdep.h"
18 #include "xbt/function_types.h"
19 #include "xbt/log.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
24 #include "gras_stub_generator.h"
25 #include <stdarg.h>
26
27
28
29 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(stubgen,gras,"Stub generator");
30
31
32 #ifdef _WIN32
33 #include <windows.h>
34 #endif
35
36 /* specific to Borland Compiler */
37 #ifdef __BORLANDDC__
38 #pragma argsused
39 #endif
40
41
42 /**********************************************/
43 /********* Parse XML deployment file **********/
44 /**********************************************/
45 xbt_dict_t process_function_set = NULL;
46 xbt_dynar_t process_list = NULL;
47 xbt_dict_t machine_set = NULL;
48
49 void s_process_free(void *process) {
50   s_process_t*p = (s_process_t*)process;
51   int i;
52   for (i=0; i<p->argc ; i++)
53     free(p->argv[i]);
54   free(p->argv);
55   free(p->host);
56 }
57
58 static s_process_t process;
59
60 static void parse_process_init(void)
61 {
62   xbt_dict_set(process_function_set, A_surfxml_process_function, NULL, NULL);
63   xbt_dict_set(machine_set, A_surfxml_process_host, NULL, NULL);
64   process.argc = 1 ;
65   process.argv = xbt_new(char*,1);
66   process.argv[0] = xbt_strdup(A_surfxml_process_function);
67   process.host=strdup(A_surfxml_process_host);
68   /*VERB1("Function: %s",A_surfxml_process_function);*/
69 }
70
71 static void parse_argument(void)
72 {
73   process.argc++;
74   process.argv = xbt_realloc(process.argv, (process.argc) * sizeof(char *));
75   process.argv[(process.argc) - 1] = xbt_strdup(A_surfxml_argument_value);
76 }
77
78 static void parse_process_finalize(void)
79 {
80   xbt_dynar_push(process_list,&process);
81   /*VERB1("Function: %s",process.argv[0]);*/
82 }
83
84
85 int main(int argc, char *argv[])
86 {
87   char *project_name = NULL;
88   char *deployment_file = NULL;
89   int i;
90    
91   surf_init(&argc, argv);
92   process_function_set = xbt_dict_new();
93   process_list = xbt_dynar_new(sizeof(s_process_t),s_process_free);
94   machine_set = xbt_dict_new();
95
96   for (i=1; i<argc; i++) {
97      int need_removal = 0;
98      if (!strncmp("--extra-process=",argv[i], strlen("--extra-process="))) {
99         xbt_dict_set(process_function_set, argv[i]+strlen("--extra-process="), NULL, NULL);
100         need_removal = 1;
101      }
102      
103      
104      if (need_removal) { /* remove the handled argument from argv */
105         int j;  
106         for (j=i+1; j<argc; j++) {
107            argv[j-1] = argv[j];
108         }
109         argv[j-1] = NULL;
110         argc--;
111         i--; /* compensate effect of next loop incrementation */
112      }
113   }
114         
115   xbt_assert1((argc >= 3),"Usage: %s project_name deployment_file [deployment_file...]\n",argv[0]);
116
117   project_name = argv[1];
118
119   STag_surfxml_process_fun = parse_process_init;
120   ETag_surfxml_argument_fun = parse_argument;
121   ETag_surfxml_process_fun = parse_process_finalize;
122   
123   for(i=2; i<argc; i++) {
124      deployment_file = argv[i];
125      surf_parse_open(deployment_file);
126      if(surf_parse()) 
127         xbt_assert1(0,"Parse error in %s",deployment_file);
128      
129      surf_parse_close();
130   }
131
132
133   warning = xbt_new(char,strlen(WARN)+strlen(deployment_file)+10);
134   sprintf(warning,WARN,deployment_file);
135
136   /*if(XBT_LOG_ISENABLED(stubgen, xbt_log_priority_debug)) {
137     xbt_dict_cursor_t cursor=NULL;
138     char *key = NULL;
139     void *data = NULL;
140
141     for (cursor=NULL, xbt_dict_cursor_first((process_function_set),&(cursor)) ;
142          xbt_dict_cursor_get_or_free(&(cursor),&(key),(void**)(&data));
143          xbt_dict_cursor_step(cursor) ) {
144       DEBUG1("Function %s", key);      
145     }
146     
147     xbt_dict_dump(process_function_set,print);
148   }*/
149
150   generate_sim(project_name);
151   generate_rl(project_name);
152   generate_makefile_local(project_name, deployment_file);
153 #ifdef _WIN32
154   generate_borland_simulation_project(project_name);
155   generate_borland_real_life_project(project_name);
156   generate_simulation_dsp_file(project_name);
157   generate_real_live_dsp_file(project_name);
158
159   if(__gras_path)
160     xbt_free(__gras_path);   
161 #endif
162
163   free(warning);
164   surf_exit();
165   return 0;
166 }