Logo AND Algorithmique Numérique Distribuée

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