Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix the location of the gras library (again)
[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   if (gras_process_init()) exit(1);
62   $_(argc,argv);
63   if (gras_process_finalize()) exit(1);
64
65   return 0;
66 }
67
68 EOF
69     ;
70 }
71 print OUT "\n$warn\n";
72
73 print OUT <<EOF
74 int main (int argc,char *argv[]) {
75
76   if (argc != 3) {
77     fprintf(stderr, "Usage: %s platform_file application_description.txt\\n",argv[0]);
78     exit(1);
79   }
80
81   /*  Simulation setting */
82   MSG_global_init();
83   MSG_set_verbosity(MSG_SILENT);
84   MSG_set_channel_number(MAX_CHANNEL);
85   MSG_create_environment(argv[1]);
86
87   /*  Application deployment */
88 EOF
89 ;
90 foreach (keys %process) {
91     print OUT "  MSG_function_register(\"$_\", launch_$_);\n";
92 }
93 print OUT <<EOF
94
95   MSG_launch_application(argv[2]);
96
97   /*  Run the simulation */
98   MSG_main();
99
100   return 0;
101 }
102 $warn
103 EOF
104     ;
105 close OUT || die "Cannot write _${project}_simulator,c: $!";
106
107 #####
108 # Generate the files for the real life
109 #####
110 foreach my $pname (keys %process) {
111     open (OUT,">_${project}_$pname.c") || die "Cannot open _${project}_$pname,c: $!";
112     print OUT <<EOF
113 $warn
114 #include <stdio.h>
115 #include <signal.h>
116 #include <gras.h>
117
118 /* signal handler for SIGPIPE from NWS */
119 void SocketFailure(int sig);
120 /* minimum needed from diagnostic.h for initialization, so that we don''t have to ship this file */
121 typedef enum {DIAGLOG, DIAGINFO, DIAGWARN, DIAGERROR, DIAGFATAL, DIAGDEBUG} DiagLevels;
122 void DirectDiagnostics(DiagLevels level, FILE *whereTo);
123 /* user code */
124 int $pname(int argc, char *argv[]);
125
126 $warn
127
128 int main(int argc, char *argv[]){
129   int errcode;
130
131   DirectDiagnostics(DIAGDEBUG, stdout);
132   DirectDiagnostics(DIAGINFO, stdout);
133   DirectDiagnostics(DIAGLOG, stdout);
134   DirectDiagnostics(DIAGWARN, stderr);
135   DirectDiagnostics(DIAGERROR, stderr);
136   DirectDiagnostics(DIAGFATAL, stderr);
137
138   signal(SIGPIPE, SocketFailure);
139
140   gras_process_init();
141   errcode=$pname(argc,argv);
142   gras_process_finalize();
143
144   return errcode;
145 }
146
147 $warn
148 EOF
149 ;
150     close OUT || die "Cannot write _${project}_$pname,c: $!";
151 }
152
153 #####
154 # Outputs the Makefile.am snippet
155 #####
156
157 print ">>> Files for project '$project' successfully generated.\n";
158 print ">>> Add (and edit) the following to you Makefile.am:\n\n";
159
160 print "# AUTOMAKE variable definition\n";
161 print "INCLUDES= \@CFLAGS_GRAS\@ \@CFLAGS_XML\@ \@CFLAGS_SimGrid\@\n\n";
162 print "PROGRAMS=${project}_simulator ";
163
164 foreach (keys %process) {
165     print "${project}_$_ ";
166 }
167 print "\n\n${project}_simulator_SOURCES=\t_${project}_simulator.c $project.c\n";
168 foreach (keys %process) {
169     print "${project}_${_}_SOURCES=\t_${project}_${_}.c $project.c\n";
170 }
171
172 print "\n\n${project}_simulator_LDADD=\tpath/to/libgrassg.a \@LIBS_SimGrid\@ \@LIBS_XML\@\n";
173 foreach (keys %process) {
174     print "${project}_${_}_LDADD=\tpath/to/libgrasrl.a\n";
175 }
176
177 print "\n# cleanup temps\n";
178 print "CLEANFILES= _${project}_simulator.c ";
179 foreach (keys %process) {
180     print "_${project}_$_.c ";
181 }
182 print "\n";
183
184 print "\n# generate temps\n";
185 # A rule to generate the source file each time the deployment file changes
186 foreach (keys %process) {
187     print "_${project}_$_.c ";
188 }
189 print "_${project}_simulator.c: $deploy_file\n";
190 print "\tgras_stub_generator $project $deploy_file >/dev/null\n";
191
192
193 print "\n>>> Bye.\n"