Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fixups in mrealloc
[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, NULL);
60   xbt_dict_set(machine_set, A_surfxml_process_host, NULL, NULL);
61   process.argc = 1;
62   process.argv = xbt_new(char *, 1);
63   process.argv[0] = xbt_strdup(A_surfxml_process_function);
64   process.host = strdup(A_surfxml_process_host);
65   /*XBT_VERB("Function: %s",A_surfxml_process_function); */
66 }
67
68 static void parse_argument(void)
69 {
70   process.argc++;
71   process.argv =
72       xbt_realloc(process.argv, (process.argc) * sizeof(char *));
73   process.argv[(process.argc) - 1] = xbt_strdup(A_surfxml_argument_value);
74 }
75
76 static void parse_process_finalize(void)
77 {
78   xbt_dynar_push(process_list, &process);
79   /*XBT_VERB("Function: %s",process.argv[0]); */
80 }
81
82 /*FIXME Defined in surfxml_parse.c*/
83 #ifndef WIN32
84 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
85 {
86   xbt_dynar_push(cb_list, &function);
87 }
88 #endif
89
90
91 int main(int argc, char *argv[])
92 {
93   char *project_name = NULL;
94   char *deployment_file = NULL;
95   int i;
96
97   surf_init(&argc, argv);
98   process_function_set = xbt_dict_new_homogeneous(NULL);
99   process_list = xbt_dynar_new(sizeof(s_process_t), s_process_free);
100   machine_set = xbt_dict_new_homogeneous(NULL);
101
102   for (i = 1; i < argc; i++) {
103     int need_removal = 0;
104     if (!strncmp("--extra-process=", argv[i], strlen("--extra-process="))) {
105       xbt_dict_set(process_function_set,
106                    argv[i] + strlen("--extra-process="), NULL, NULL);
107       need_removal = 1;
108     }
109
110
111     if (need_removal) {         /* remove the handled argument from argv */
112       int j;
113       for (j = i + 1; j < argc; j++) {
114         argv[j - 1] = argv[j];
115       }
116       argv[j - 1] = NULL;
117       argc--;
118       i--;                      /* compensate effect of next loop incrementation */
119     }
120   }
121
122   xbt_assert((argc >= 3),
123               "Usage: %s project_name deployment_file [deployment_file...]\n",
124               argv[0]);
125
126   project_name = argv[1];
127
128   surf_parse_reset_callbacks();
129   XBT_DEBUG("%p %p", parse_process_init, &parse_process_init);
130   surfxml_add_callback(STag_surfxml_process_cb_list, &parse_process_init);
131   surfxml_add_callback(ETag_surfxml_argument_cb_list, &parse_argument);
132   surfxml_add_callback(ETag_surfxml_process_cb_list,
133                        &parse_process_finalize);
134
135   for (i = 2; i < argc; i++) {
136     deployment_file = argv[i];
137     surf_parse_open(deployment_file);
138     if (surf_parse())
139       xbt_die("Parse error in %s", deployment_file);
140
141     surf_parse_close();
142   }
143
144
145   warning = xbt_new(char, strlen(WARN) + strlen(deployment_file) + 10);
146   sprintf(warning, WARN, deployment_file);
147
148   /*if(XBT_LOG_ISENABLED(stubgen, xbt_log_priority_debug)) {
149      xbt_dict_cursor_t cursor=NULL;
150      char *key = NULL;
151      void *data = NULL;
152
153      for (cursor=NULL, xbt_dict_cursor_first((process_function_set),&(cursor)) ;
154      xbt_dict_cursor_get_or_free(&(cursor),&(key),(void**)(&data));
155      xbt_dict_cursor_step(cursor) ) {
156      XBT_DEBUG("Function %s", key);
157      }
158
159      xbt_dict_dump(process_function_set,print);
160      } */
161
162   generate_sim(project_name);
163   generate_rl(project_name);
164   generate_makefile_local(project_name, deployment_file);
165 #ifdef __BORLANDC__
166   generate_borland_simulation_project(project_name);
167   generate_borland_real_life_project(project_name);
168   generate_simulation_dsp_file(project_name);
169   generate_real_live_dsp_file(project_name);
170
171   xbt_free(__gras_path);
172 #endif
173
174   free(warning);
175
176   xbt_dict_free(&process_function_set);
177   xbt_dynar_free(&process_list);
178   xbt_dict_free(&machine_set);
179   surf_exit();
180
181   return 0;
182 }