Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not require doxygen in maintainer mode
[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 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
85 {
86    xbt_dynar_push(cb_list, &function);
87 }
88
89
90 int main(int argc, char *argv[])
91 {
92   char *project_name = NULL;
93   char *deployment_file = NULL;
94   int i;
95    
96   surf_init(&argc, argv);
97   process_function_set = xbt_dict_new();
98   process_list = xbt_dynar_new(sizeof(s_process_t),s_process_free);
99   machine_set = xbt_dict_new();
100
101   for (i=1; i<argc; i++) {
102      int need_removal = 0;
103      if (!strncmp("--extra-process=",argv[i], strlen("--extra-process="))) {
104         xbt_dict_set(process_function_set, argv[i]+strlen("--extra-process="), 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),"Usage: %s project_name deployment_file [deployment_file...]\n",argv[0]);
121
122   project_name = argv[1];
123
124   surf_parse_reset_parser();
125   DEBUG2("%p %p",parse_process_init,&parse_process_init);
126   surfxml_add_callback(STag_surfxml_process_cb_list, &parse_process_init);
127   surfxml_add_callback(ETag_surfxml_argument_cb_list, &parse_argument);
128   surfxml_add_callback(ETag_surfxml_process_cb_list, &parse_process_finalize);
129   
130   for(i=2; i<argc; i++) {
131      deployment_file = argv[i];
132      surf_parse_open(deployment_file);
133      if(surf_parse()) 
134         xbt_assert1(0,"Parse error in %s",deployment_file);
135      
136      surf_parse_close();
137   }
138
139
140   warning = xbt_new(char,strlen(WARN)+strlen(deployment_file)+10);
141   sprintf(warning,WARN,deployment_file);
142
143   /*if(XBT_LOG_ISENABLED(stubgen, xbt_log_priority_debug)) {
144     xbt_dict_cursor_t cursor=NULL;
145     char *key = NULL;
146     void *data = NULL;
147
148     for (cursor=NULL, xbt_dict_cursor_first((process_function_set),&(cursor)) ;
149          xbt_dict_cursor_get_or_free(&(cursor),&(key),(void**)(&data));
150          xbt_dict_cursor_step(cursor) ) {
151       DEBUG1("Function %s", key);      
152     }
153     
154     xbt_dict_dump(process_function_set,print);
155   }*/
156
157   generate_sim(project_name);
158   generate_rl(project_name);
159   generate_makefile_local(project_name, deployment_file);
160 #ifdef _WIN32
161   generate_borland_simulation_project(project_name);
162   generate_borland_real_life_project(project_name);
163   generate_simulation_dsp_file(project_name);
164   generate_real_live_dsp_file(project_name);
165
166   if(__gras_path)
167     xbt_free(__gras_path);   
168 #endif
169
170   free(warning);
171   surf_exit();
172   return 0;
173 }