Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix a fuckin bug preventing to pass --gras-log argument to nodes in simulation mode...
[simgrid.git] / examples / gras_stub_generator
1 #! /usr/bin/perl
2
3 # gras_stub_generator - creates the main() to use a GRAS program
4
5 # Authors: Martin Quinson
6 # Copyright (C) 2003 the OURAGAN project.
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 use strict;
12 eval qq{
13   use warnings;
14 }; # warnings not defined in 5.00503/sun4-solaris
15
16 sub usage {
17     my ($msg)=@_;
18     fail ($msg? "gras_stub_generator: $msg\n":"").
19          "gras_stub_generator: USAGE\n".
20          "  gras_stub_generator project_name deployment_file\n"
21 }
22
23 my ($project,$deploy_file)=@ARGV;
24
25 $project && $deploy_file || usage();
26
27 my (%process);
28
29 open (DEPLOY, $deploy_file) || usage("Cannot open $deploy_file: $!");
30 my $linenum=0;
31 while (<DEPLOY>) {
32     $linenum++;
33     /^\W*\w*\W*(\w*)/ || usage("$deploy_file:$linenum: Parse error");
34     $process{$1}=1;
35 }
36
37 my $warn="/***********\n * DO NOT EDIT! THIS FILE WERE AUTOMATICALLY GENERATED FROM $deploy_file BY gras_stub_generator\n ***********/\n";
38
39 #####
40 # Generate the file for the simulator
41 #####
42
43 open (OUT,">_${project}_simulator.c") || die "Cannot open _${project}_simulator,c: $!";
44 print OUT <<EOF
45 $warn
46
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <msg.h>
50 #include <gras.h>
51
52 char *gras_log=NULL;
53     
54 EOF
55  ;
56 foreach (keys %process) { print OUT "int $_(int argc,char *argv[]);\n";        }
57 print OUT "\n";
58 foreach (keys %process) { print OUT "int launch_$_(int argc,char *argv[]);\n"; }
59 print OUT "\n$warn\n";
60
61 foreach (keys %process) {
62     print OUT<<EOF
63 int launch_$_(int argc, char **argv) {
64   char **myargv=argv;
65   int myargc=argc;
66   int i;
67     
68   if (gras_log) {
69     myargv=malloc((argc+1) * sizeof(char**));
70     for (i=0; i<argc; i++)
71       myargv[i] = argv[i];
72     myargv[myargc++] = gras_log;
73   }
74   $_(myargc,myargv);
75   if (myargv != argv)
76     free(myargv);
77   return 0;
78 }
79
80 EOF
81     ;
82 }
83 print OUT "\n$warn\n";
84
85 print OUT <<EOF
86 int main (int argc,char *argv[]) {
87   int i,j;
88
89   /* Save the gras-log argument of real command line to pass it to all processes */
90   for (i=1; i<argc; i++) {
91     if (!strncmp(argv[i],"--gras-log=",strlen("--gras-log="))) {
92       if (gras_log) {
93          char *tmp=malloc(strlen(gras_log)+strlen(argv[i])+2);
94          sprintf(tmp,"%s %s",gras_log, argv[i]);
95          free(gras_log);
96          gras_log=tmp;
97       } else {
98          gras_log = strdup(argv[i]);
99       }
100       for (j=i+1; j<argc; j++) {
101         argv[j-1] = argv[j];
102       } 
103       argv[j-1] = NULL;
104       argc--;
105       i--; /* compensate effect of next loop incrementation */
106     }
107   }
108
109   if (argc != 3) {
110     fprintf(stderr, "Usage: %s platform_file application_description.txt [--gras-log=...]\\n",argv[0]);
111     exit(1);
112   }
113
114   /*  Simulation setup */
115   MSG_global_init();
116   MSG_set_verbosity(MSG_SILENT);
117   MSG_set_channel_number(GRAS_MAX_CHANNEL);
118   MSG_create_environment(argv[1]);
119
120   /*  Application deployment */
121 EOF
122 ;
123 foreach (keys %process) {
124     print OUT "  MSG_function_register(\"$_\", launch_$_);\n";
125 }
126 print OUT <<EOF
127
128   MSG_launch_application(argv[2]);
129
130   /*  Run the simulation */
131   MSG_main();
132
133   if (gras_log)
134     free(gras_log);
135   return 0;
136 }
137 $warn
138 EOF
139     ;
140 close OUT || die "Cannot write _${project}_simulator,c: $!";
141
142 #####
143 # Generate the files for the real life
144 #####
145 foreach my $pname (keys %process) {
146     open (OUT,">_${project}_$pname.c") || die "Cannot open _${project}_$pname,c: $!";
147     print OUT <<EOF
148 $warn
149 #include <stdio.h>
150 #include <signal.h>
151 #include <gras.h>
152
153 /* user code */
154 int $pname(int argc, char *argv[]);
155
156 $warn
157
158 int main(int argc, char *argv[]){
159   int errcode;
160
161   errcode=$pname(argc,argv);
162  
163   return errcode;
164 }
165
166 $warn
167 EOF
168 ;
169     close OUT || die "Cannot write _${project}_$pname,c: $!";
170 }
171
172 #####
173 # Outputs the Makefile.am snippet
174 #####
175
176 print ">>> Files for project '$project' successfully generated.\n";
177 print ">>> Add (and edit) the following to you Makefile.am:\n\n";
178
179 print "# AUTOMAKE variable definition\n";
180 print "INCLUDES= \@CFLAGS_GRAS\@ \@CFLAGS_XML\@ \@CFLAGS_SimGrid\@\n\n";
181 print "PROGRAMS=${project}_simulator ";
182
183 foreach (keys %process) {
184     print "${project}_$_ ";
185 }
186 print "\n\n${project}_simulator_SOURCES=\t_${project}_simulator.c $project.c\n";
187 foreach (keys %process) {
188     print "${project}_${_}_SOURCES=\t_${project}_${_}.c $project.c\n";
189 }
190
191 print "\n\n${project}_simulator_LDADD=\tpath/to/libgrassg.a \@LIBS_SimGrid\@ \@LIBS_XML\@\n";
192 foreach (keys %process) {
193     print "${project}_${_}_LDADD=\tpath/to/libgrasrl.a\n";
194 }
195
196 print "\n# cleanup temps\n";
197 print "CLEANFILES= _${project}_simulator.c ";
198 foreach (keys %process) {
199     print "_${project}_$_.c ";
200 }
201 print "\n";
202
203 print "\n# generate temps\n";
204 # A rule to generate the source file each time the deployment file changes
205 foreach (keys %process) {
206     print "_${project}_$_.c ";
207 }
208 print "_${project}_simulator.c: $deploy_file\n";
209 print "\tgras_stub_generator $project $deploy_file >/dev/null\n";
210
211
212 print "\n>>> Bye.\n"