Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
try to fix 'make dist' after GRAS murder (SMPI test still break it)
[simgrid.git] / tools / sg_unit_extractor.pl
1 #! /usr/bin/perl
2
3 use strict;
4
5 use strict;
6 use Getopt::Long qw(GetOptions);
7
8 my $progname="sg_unit_extractor";
9 # Get the args 
10
11 sub usage($) {
12     my $ret;
13     print "USAGE: $progname [--root=part/to/cut] [--path=where/to/search NOT WORKING] [--outdir=where/to/generate/files] infile [infile+]\n";
14     exit $ret;
15 }
16
17 my $path=undef;
18 my $outdir=undef;
19 my $root;
20 my $help;
21
22 Getopt::Long::config('permute','no_getopt_compat', 'no_auto_abbrev');
23 GetOptions(
24         'help|h'                => sub {usage(0)},
25         'root=s' =>\$root,
26         'path=s' =>\$path,
27         'outdir=s' =>\$outdir) or usage(1);
28
29 usage(1) if (scalar @ARGV == 0);
30
31 map {process_one($_)} @ARGV;
32
33 sub process_one($) {
34     my $infile = shift;
35     my $outfile;
36     
37     $infile =~ s|src/|| unless (-e $infile);
38     
39     $outfile =  $infile;
40     $outfile =~ s/\.c$/_unit.c/;
41     $outfile =~ s|.*/([^/]*)$|$1| if $outfile =~ m|/|;
42     $outfile = "$outdir$outfile";
43     
44     print "$progname: processing $infile (generating $outfile)...\n";    
45     
46     # Get the unit data
47     my ($unit_source,$suite_name,$suite_title)=("","","");
48     my (%tests); # to detect multiple definition
49     my (@tests); # actual content
50     
51     open IN, "$infile" || die "$progname: Cannot open input file '$infile': $!\n";
52     $infile =~ s|$root|| if defined($root);
53     
54     my $takeit=0;
55     my $line=0;
56     my $beginline=0;
57     while (<IN>) {
58         $line++;
59         if (m/ifdef +SIMGRID_TEST/) {
60             $beginline = $line;
61             $takeit = 1;
62             next;
63         }
64         if (m/endif.*SIMGRID_TEST/) {
65             $takeit = 0;
66             next
67         }
68         
69         if (m/XBT_TEST_SUITE\(\w*"([^"]*)"\w*, *(.*?)\);/) { #" {
70             die "$progname: Multiple suites in the same file ($infile) are not supported yet\n" if length($suite_name);
71             ($suite_name,$suite_title)=($1,$2);
72             die "$progname: Empty suite name in $infile" unless length($suite_name);
73             die "$progname: Empty suite title in $infile" unless length($suite_title);
74             next;
75         } elsif (m/XBT_TEST_SUITE/) {
76             die "$progname: Parse error: This line seem to be a test suite declaration, but failed to parse it\n$_\n";
77         }
78
79         if (m/XBT_TEST_UNIT\(\w*"([^"]*)"\w*,([^,]*),(.*?)\)/) { #"{
80             die "$progname: multiply defined unit in file $infile: $1\n" if (defined($tests{$1}));
81             
82             my @t=($1,$2,$3);
83             push @tests,\@t;
84             $tests{$1} = 1;
85         } elsif (m/XBT_TEST_UNIT/) {
86             die "$progname: Parse error: This line seem to be a test unit, but failed to parse it\n$_\n";
87         }
88         $unit_source .= $_ if $takeit;
89     }
90     close IN || die "$progname: cannot close input file '$infile': $!\n";
91
92
93     if ($takeit) {
94         die "$progname: end of file reached in SIMGRID_TEST block.\n".
95           "You should end each of the with a line matching: /endif.*SIMGRID_TEST/\n".
96           "Example:\n".
97           "#endif /* SIMGRID_TEST */\n"
98     }
99
100     die "$progname: no suite defined in $infile\n" unless (length($suite_name));
101   
102     # Write the test
103
104     my ($GENERATED)=("/*******************************/\n".
105                      "/* GENERATED FILE, DO NOT EDIT */\n".
106                      "/*******************************/\n\n");
107     $beginline+=2;
108     open OUT,">$outfile" || die "$progname: Cannot open output file '$outfile': $!\n";
109     print OUT $GENERATED;
110     print OUT "#include <stdio.h>\n";
111     print OUT "#include \"xbt.h\"\n";
112     print OUT $GENERATED;
113     print OUT "#line $beginline \"$infile\" \n";
114     print OUT "$unit_source";
115     print OUT $GENERATED;
116     close OUT || die "$progname: Cannot close output file '$outfile': $!\n";
117
118     # write the main skeleton if needed
119     if (! -e "${outdir}simgrid_units_main.c") {
120         open OUT,">${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
121         print OUT $GENERATED;
122         print OUT "#include <stdio.h>\n\n";
123         print OUT "#include \"msg/msg.h\" /* MSG_init, xbt_init is not enough when using synchronized dynars */\n";
124         print OUT "#include \"xbt.h\"\n\n";
125         print OUT "extern xbt_test_unit_t _xbt_current_unit;\n\n";
126         print OUT "/* SGU: BEGIN PROTOTYPES */\n";
127         print OUT "/* SGU: END PROTOTYPES */\n\n";
128         print OUT $GENERATED;
129         #  print OUT "# 93 \"sg_unit_extractor.pl\"\n";
130         print OUT <<EOF;
131 int main(int argc, char *argv[]) {
132   xbt_test_suite_t suite; 
133   char selection[1024];
134   int verbosity = 0;
135   int i;
136   int res;
137
138   /* SGU: BEGIN SUITES DECLARATION */
139   /* SGU: END SUITES DECLARATION */
140       
141   MSG_init(&argc,argv);
142     
143   /* Search for the tests to do */
144     selection[0]='\\0';
145     for (i=1;i<argc;i++) {
146       if (!strncmp(argv[i],\"--tests=\",strlen(\"--tests=\"))) {
147         char *p=strchr(argv[i],'=')+1;
148         if (selection[0] == '\\0') {
149           strcpy(selection, p);
150         } else {
151           strcat(selection, \",\");
152           strcat(selection, p);
153         }
154       } else if (!strcmp(argv[i], \"--verbose\")) {
155         verbosity++;
156       } else if (!strcmp(argv[i], \"--dump-only\")||
157                  !strcmp(argv[i], \"--dump\")) {
158         xbt_test_dump(selection);
159         return 0;
160       } else if (!strcmp(argv[i], \"--help\")) {
161           printf(
162               "Usage: testall [--help] [--tests=selection] [--dump-only]\\n\\n"
163               "--help: display this help\\n"
164               "--verbose: print the name for each running test\\n"
165               "--dump-only: don't run the tests, but display some debuging info about the tests\\n"
166               "--tests=selection: Use argument to select which suites/units/tests to run\\n"
167               "                   --tests can be used more than once, and selection may be a comma\\n"
168               "                   separated list of directives.\\n\\n"
169               "Directives are of the form:\\n"
170               "   [-]suitename[:unitname]\\n\\n"
171               "If the first char is a '-', the directive disables its argument instead of enabling it\\n"
172               "suitename/unitname is the set of tests to en/disable. If a unitname is not specified,\\n"
173               "it applies on any unit.\\n\\n"
174               "By default, everything is enabled.\\n\\n"
175               "'all' as suite name apply to all suites.\\n\\n"
176               "Example 1: \\"-toto,+toto:tutu\\"\\n"
177               "  disables the whole toto testsuite (any unit in it),\\n"
178               "  then reenables the tutu unit of the toto test suite.\\n\\n"
179               "Example 2: \\"-all,+toto\\"\\n"
180               "  Run nothing but the toto suite.\\n");
181           return 0;
182       } else {
183         printf("testall: Unknown option: %s\\n",argv[i]);
184         return 1;
185       }
186     }
187   /* Got all my tests to do */
188       
189   res = xbt_test_run(selection, verbosity);
190   xbt_test_exit();
191   return res;
192 }
193 EOF
194         print OUT $GENERATED;
195         close OUT || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
196     }
197
198    print "  Suite $suite_name: $suite_title (".(scalar @tests)." tests)\n";
199    map {
200        my ($name,$func,$title) = @{$_};
201        print "    unit $name: func=$func; title=$title\n";
202    } @tests;
203
204    #while (my $t = shift @tests) {
205
206    # add this suite to the main
207    my $newmain="";
208    open IN,"${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
209     # search prototypes
210        while (<IN>) {
211            $newmain .= $_;
212            #    print "Look for proto: $_";
213            last if /SGU: BEGIN PROTOTYPES/;
214        }
215
216        # search my prototype
217        while (<IN>) {
218            #    print "Seek protos: $_";
219            last if  (/SGU: END PROTOTYPES/ || /SGU: BEGIN FILE $infile/);
220            $newmain .= $_;
221        }
222        if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it    
223            while (<IN>) {
224                last if /SGU: END FILE/;
225            }
226            $_ = <IN>; # pass extra blank line
227            chomp;
228            die "this line should be blank ($_). Did you edit the file?" if /\W/;
229        }
230        my ($old_)=($_);
231        # add my section
232        $newmain .= "  /* SGU: BEGIN FILE $infile */\n";
233        map {
234            my ($name,$func,$title) = @{$_};
235            $newmain .=  "    void $func(void);\n"
236        } @tests;
237        
238        $newmain .= "  /* SGU: END FILE */\n\n";
239        if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END PROTOTYPES/) {
240            $newmain .= $old_;
241        }
242        
243        # pass remaining prototypes, search declarations
244        while (<IN>) {
245            $newmain .= $_ unless /SGU: END PROTOTYPES/;
246            last if /SGU: BEGIN SUITES DECLARATION/;
247        }
248        
249        ### Done with prototypes. And now, the actual code
250        
251        # search my prototype
252        while (<IN>) {
253            last if  (/SGU: END SUITES DECLARATION/ || /SGU: BEGIN FILE $infile/);
254            $newmain .= $_;
255        }
256        if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it    
257            while (<IN>) {
258                last if /SGU: END FILE/;
259            }
260            $_ = <IN>; # pass extra blank line
261            chomp;
262            die "this line should be blank ($_). Did you edit the file?" if /\W/;
263        }
264        my ($old_)=($_);
265        # add my section
266        $newmain .= "    /* SGU: BEGIN FILE $infile */\n";
267        $newmain .= "      suite = xbt_test_suite_by_name(\"$suite_name\",$suite_title);\n";
268        map {
269            my ($name,$func,$title) = @{$_};
270            $newmain .=  "      xbt_test_suite_push(suite, \"$name\", $func, $title);\n";
271        } @tests;
272        
273        $newmain .= "    /* SGU: END FILE */\n\n";
274        if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END SUITES DECLARATION/) {
275            $newmain .= $old_;
276        }
277        
278        # pass the remaining 
279        while (<IN>) {
280            $newmain .= $_;
281        }
282        close IN || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
283        
284        # write it back to main
285        open OUT,">${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
286        print OUT $newmain;
287        close OUT || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
288 } # end if process_one($)
289
290 0;