Logo AND Algorithmique Numérique Distribuée

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