Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge back master branch
[simgrid.git] / tools / gras / stub_generator.c
1 /* gras_stub_generator - creates the main() to use a GRAS program           */
2
3 /* Copyright (c) 2005, 2006, 2007, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9 /* specific to Borland Compiler */
10 #ifdef __BORLANDDC__
11 #pragma hdrstop
12 #endif
13
14 #include <stdio.h>
15 #include "xbt/sysdep.h"
16 #include "xbt/function_types.h"
17 #include "xbt/log.h"
18 #include "surf/surfxml_parse.h"
19 #include "surf/surf.h"
20 #include "portable.h"           /* Needed for the time of the SIMIX convertion */
21
22 #include "gras_stub_generator.h"
23 #include <stdarg.h>
24
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(stubgen, gras, "Stub generator");
26
27 #ifdef _XBT_WIN32
28 #include <windows.h>
29 #endif
30
31 /* specific to Borland Compiler */
32 #ifdef __BORLANDDC__
33 #pragma argsused
34 #endif
35
36
37 /**********************************************/
38 /********* Parse XML deployment file **********/
39 /**********************************************/
40 xbt_dict_t process_function_set = NULL;
41 xbt_dynar_t process_list = NULL;
42 xbt_dict_t machine_set = NULL;
43 char *warning = NULL;
44
45 void s_process_free(void *process)
46 {
47   s_process_t *p = (s_process_t *) process;
48   int i;
49   for (i = 0; i < p->argc; i++)
50     free(p->argv[i]);
51   free(p->argv);
52   free(p->host);
53 }
54
55 static s_process_t process;
56
57 static void parse_process_init(void)
58 {
59   xbt_dict_set(process_function_set, A_surfxml_process_function, NULL,
60                NULL);
61   xbt_dict_set(machine_set, A_surfxml_process_host, NULL, NULL);
62   process.argc = 1;
63   process.argv = xbt_new(char *, 1);
64   process.argv[0] = xbt_strdup(A_surfxml_process_function);
65   process.host = strdup(A_surfxml_process_host);
66   /*XBT_VERB("Function: %s",A_surfxml_process_function); */
67 }
68
69 static void parse_argument(void)
70 {
71   process.argc++;
72   process.argv =
73       xbt_realloc(process.argv, (process.argc) * sizeof(char *));
74   process.argv[(process.argc) - 1] = xbt_strdup(A_surfxml_argument_value);
75 }
76
77 static void parse_process_finalize(void)
78 {
79   xbt_dynar_push(process_list, &process);
80   /*XBT_VERB("Function: %s",process.argv[0]); */
81 }
82
83 /*FIXME Defined in surfxml_parse.c*/
84 #ifndef WIN32
85 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
86 {
87   xbt_dynar_push(cb_list, &function);
88 }
89 #endif
90
91
92 int main(int argc, char *argv[])
93 {
94   char *project_name = NULL;
95   char *deployment_file = NULL;
96   int i;
97
98   surf_init(&argc, argv);
99   process_function_set = xbt_dict_new();
100   process_list = xbt_dynar_new(sizeof(s_process_t), s_process_free);
101   machine_set = xbt_dict_new();
102
103   for (i = 1; i < argc; i++) {
104     int need_removal = 0;
105     if (!strncmp("--extra-process=", argv[i], strlen("--extra-process="))) {
106       xbt_dict_set(process_function_set,
107                    argv[i] + strlen("--extra-process="), NULL, NULL);
108       need_removal = 1;
109     }
110
111
112     if (need_removal) {         /* remove the handled argument from argv */
113       int j;
114       for (j = i + 1; j < argc; j++) {
115         argv[j - 1] = argv[j];
116       }
117       argv[j - 1] = NULL;
118       argc--;
119       i--;                      /* compensate effect of next loop incrementation */
120     }
121   }
122
123   xbt_assert((argc >= 3),
124               "Usage: %s project_name deployment_file [deployment_file...]\n",
125               argv[0]);
126
127   project_name = argv[1];
128
129   surf_parse_reset_callbacks();
130   XBT_DEBUG("%p %p", parse_process_init, &parse_process_init);
131   surfxml_add_callback(STag_surfxml_process_cb_list, &parse_process_init);
132   surfxml_add_callback(ETag_surfxml_argument_cb_list, &parse_argument);
133   surfxml_add_callback(ETag_surfxml_process_cb_list,
134                        &parse_process_finalize);
135
136   for (i = 2; i < argc; i++) {
137     deployment_file = argv[i];
138     surf_parse_open(deployment_file);
139     if (surf_parse())
140       xbt_die("Parse error in %s", deployment_file);
141
142     surf_parse_close();
143   }
144
145
146   warning = xbt_new(char, strlen(WARN) + strlen(deployment_file) + 10);
147   sprintf(warning, WARN, deployment_file);
148
149   /*if(XBT_LOG_ISENABLED(stubgen, xbt_log_priority_debug)) {
150      xbt_dict_cursor_t cursor=NULL;
151      char *key = NULL;
152      void *data = NULL;
153
154      for (cursor=NULL, xbt_dict_cursor_first((process_function_set),&(cursor)) ;
155      xbt_dict_cursor_get_or_free(&(cursor),&(key),(void**)(&data));
156      xbt_dict_cursor_step(cursor) ) {
157      XBT_DEBUG("Function %s", key);
158      }
159
160      xbt_dict_dump(process_function_set,print);
161      } */
162
163   generate_sim(project_name);
164   generate_rl(project_name);
165   generate_makefile_local(project_name, deployment_file);
166 #ifdef __BORLANDC__
167   generate_borland_simulation_project(project_name);
168   generate_borland_real_life_project(project_name);
169   generate_simulation_dsp_file(project_name);
170   generate_real_live_dsp_file(project_name);
171
172   if (__gras_path)
173     xbt_free(__gras_path);
174 #endif
175
176   free(warning);
177
178   xbt_dict_free(&process_function_set);
179   xbt_dynar_free(&process_list);
180   xbt_dict_free(&machine_set);
181   surf_exit();
182
183   return 0;
184 }