Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix perl syntax
[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     die ($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/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   int retcode;
68     
69   if (gras_log) {
70     myargv=malloc((argc+1) * sizeof(char**));
71     for (i=0; i<argc; i++)
72       myargv[i] = argv[i];
73     myargv[myargc++] = gras_log;
74   }
75   retcode = $_(myargc,myargv);
76   if (myargv != argv)
77     free(myargv);
78   return retcode;
79 }
80
81 EOF
82     ;
83 }
84 print OUT "\n$warn\n";
85
86 print OUT <<EOF
87 int main (int argc,char *argv[]) {
88   int i,j;
89
90   /* Save the gras-log argument of real command line to pass it to all processes */
91   for (i=1; i<argc; i++) {
92     if (!strncmp(argv[i],"--gras-log=",strlen("--gras-log="))) {
93       if (gras_log) {
94          char *tmp=malloc(strlen(gras_log)+strlen(argv[i])+2);
95          sprintf(tmp,"%s %s",gras_log, argv[i]);
96          free(gras_log);
97          gras_log=tmp;
98       } else {
99          gras_log = strdup(argv[i]);
100       }
101       for (j=i+1; j<argc; j++) {
102         argv[j-1] = argv[j];
103       } 
104       argv[j-1] = NULL;
105       argc--;
106       i--; /* compensate effect of next loop incrementation */
107     }
108   }
109
110   if (argc != 3) {
111     fprintf(stderr, "Usage: %s platform_file application_description.txt [--gras-log=...]\\n",argv[0]);
112     exit(1);
113   }
114
115   /*  Simulation setup */
116   MSG_global_init();
117   MSG_set_verbosity(MSG_SILENT);
118   MSG_set_channel_number(10); // GRAS_MAX_CHANNEL hardcoded since Alvin killed its definition
119   MSG_create_environment(argv[1]);
120
121   /*  Application deployment */
122 EOF
123 ;
124 foreach (keys %process) {
125     print OUT "  MSG_function_register(\"$_\", launch_$_);\n";
126 }
127 print OUT <<EOF
128
129   MSG_launch_application(argv[2]);
130
131   /*  Run the simulation */
132   MSG_main();
133
134   /* cleanup the place */
135   MSG_clean();
136   if (gras_log)
137     free(gras_log);
138   return 0;
139 }
140 $warn
141 EOF
142     ;
143 close OUT || die "Cannot write _${project}_simulator,c: $!";
144
145 #####
146 # Generate the files for the real life
147 #####
148 foreach my $pname (keys %process) {
149     open (OUT,">_${project}_$pname.c") || die "Cannot open _${project}_$pname,c: $!";
150     print OUT <<EOF
151 $warn
152 #include <stdio.h>
153 #include <signal.h>
154 #include <gras.h>
155
156 /* user code */
157 int $pname(int argc, char *argv[]);
158
159 $warn
160
161 int main(int argc, char *argv[]){
162   int errcode;
163
164   errcode=$pname(argc,argv);
165  
166   return errcode;
167 }
168
169 $warn
170 EOF
171 ;
172     close OUT || die "Cannot write _${project}_$pname,c: $!";
173 }
174
175 #####
176 # Outputs the Makefile.am snippet
177 #####
178
179 print ">>> Files for project '$project' successfully generated.\n";
180 print ">>> Add (and edit) the following to you Makefile.am:\n\n";
181
182 print "# AUTOMAKE variable definition\n";
183 print "INCLUDES= \@CFLAGS_GRAS\@ \@CFLAGS_XML\@ \@CFLAGS_SimGrid\@\n\n";
184 print "PROGRAMS=${project}_simulator ";
185
186 foreach (keys %process) {
187     print "${project}_$_ ";
188 }
189 print "\n\n${project}_simulator_SOURCES=\t_${project}_simulator.c $project.c\n";
190 foreach (keys %process) {
191     print "${project}_${_}_SOURCES=\t_${project}_${_}.c $project.c\n";
192 }
193
194 print "\n\n${project}_simulator_LDADD=\tpath/to/libgrassg.a \@LIBS_SimGrid\@ \@LIBS_XML\@\n";
195 foreach (keys %process) {
196     print "${project}_${_}_LDADD=\tpath/to/libgrasrl.a\n";
197 }
198
199 print "\n# cleanup temps\n";
200 print "CLEANFILES= _${project}_simulator.c ";
201 foreach (keys %process) {
202     print "_${project}_$_.c ";
203 }
204 print "\n";
205
206 print "\n# generate temps\n";
207 # A rule to generate the source file each time the deployment file changes
208 foreach (keys %process) {
209     print "_${project}_$_.c ";
210 }
211 print "_${project}_simulator.c: $deploy_file\n";
212 print "\tgras_stub_generator $project $deploy_file >/dev/null\n";
213
214
215 print "\n>>> Bye.\n"