Logo AND Algorithmique Numérique Distribuée

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