Logo AND Algorithmique Numérique Distribuée

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