Logo AND Algorithmique Numérique Distribuée

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