Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Gtnets works with new struct network_element_t
[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 \"xbt.h\"\n\n";
124         print OUT "extern xbt_test_unit_t _xbt_current_unit;\n\n";
125         print OUT "/* SGU: BEGIN PROTOTYPES */\n";
126         print OUT "/* SGU: END PROTOTYPES */\n\n";
127         print OUT $GENERATED;
128         #  print OUT "# 93 \"sg_unit_extractor.pl\"\n";
129         print OUT <<EOF;
130 int main(int argc, char *argv[]) {
131   xbt_test_suite_t suite; 
132   char selection[1024];
133   int i;\n
134   int res;\n
135   /* SGU: BEGIN SUITES DECLARATION */
136   /* SGU: END SUITES DECLARATION */
137       
138   xbt_init(&argc,argv);
139     
140   /* Search for the tests to do */
141     selection[0]='\\0';
142     for (i=1;i<argc;i++) {
143       if (!strncmp(argv[i],\"--tests=\",strlen(\"--tests=\"))) {
144         char *p=strchr(argv[i],'=')+1;
145         if (selection[0] == '\\0') {
146           strcpy(selection, p);
147         } else {
148           strcat(selection, \",\");
149           strcat(selection, p);
150         }
151       } else if (!strncmp(argv[i],\"--dump-only\",strlen(\"--dump-only\"))||
152                  !strncmp(argv[i],\"--dump\",     strlen(\"--dump\"))) {
153         xbt_test_dump(selection);
154         return 0;
155       } else if (!strncmp(argv[i],\"--help\",strlen(\"--help\"))) {
156           printf(
157               "Usage: testall [--help] [--tests=selection] [--dump-only]\\n\\n"
158               "--help: display this help\\n"
159               "--dump-only: don't run the tests, but display some debuging info about the tests\\n"
160               "--tests=selection: Use argument to select which suites/units/tests to run\\n"
161               "                   --tests can be used more than once, and selection may be a comma\\n"
162               "                   separated list of directives.\\n\\n"
163               "Directives are of the form:\\n"
164               "   [-]suitename[:unitname]\\n\\n"
165               "If the first char is a '-', the directive disables its argument instead of enabling it\\n"
166               "suitename/unitname is the set of tests to en/disable. If a unitname is not specified,\\n"
167               "it applies on any unit.\\n\\n"
168               "By default, everything is enabled.\\n\\n"
169               "'all' as suite name apply to all suites.\\n\\n"
170               "Example 1: \\"-toto,+toto:tutu\\"\\n"
171               "  disables the whole toto testsuite (any unit in it),\\n"
172               "  then reenables the tutu unit of the toto test suite.\\n\\n"
173               "Example 2: \\"-all,+toto\\"\\n"
174               "  Run nothing but the toto suite.\\n");
175           return 0;
176       } else {
177         printf("testall: Unknown option: %s\\n",argv[i]);
178         return 1;
179       }
180     }
181   /* Got all my tests to do */
182       
183   res = xbt_test_run(selection);
184   xbt_test_exit();
185   return res;
186 }
187 EOF
188         print OUT $GENERATED;
189         close OUT || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
190     }
191
192    print "  Suite $suite_name: $suite_title (".(scalar @tests)." tests)\n";
193    map {
194        my ($name,$func,$title) = @{$_};
195        print "    unit $name: func=$func; title=$title\n";
196    } @tests;
197
198    #while (my $t = shift @tests) {
199
200    # add this suite to the main
201    my $newmain="";
202    open IN,"${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
203     # search prototypes
204        while (<IN>) {
205            $newmain .= $_;
206            #    print "Look for proto: $_";
207            last if /SGU: BEGIN PROTOTYPES/;
208        }
209
210        # search my prototype
211        while (<IN>) {
212            #    print "Seek protos: $_";
213            last if  (/SGU: END PROTOTYPES/ || /SGU: BEGIN FILE $infile/);
214            $newmain .= $_;
215        }
216        if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it    
217            while (<IN>) {
218                last if /SGU: END FILE/;
219            }
220            $_ = <IN>; # pass extra blank line
221            chomp;
222            die "this line should be blank ($_). Did you edit the file?" if /\W/;
223        }
224        my ($old_)=($_);
225        # add my section
226        $newmain .= "  /* SGU: BEGIN FILE $infile */\n";
227        map {
228            my ($name,$func,$title) = @{$_};
229            $newmain .=  "    void $func(void);\n"
230        } @tests;
231        
232        $newmain .= "  /* SGU: END FILE */\n\n";
233        if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END PROTOTYPES/) {
234            $newmain .= $old_;
235        }
236        
237        # pass remaining prototypes, search declarations
238        while (<IN>) {
239            $newmain .= $_ unless /SGU: END PROTOTYPES/;
240            last if /SGU: BEGIN SUITES DECLARATION/;
241        }
242        
243        ### Done with prototypes. And now, the actual code
244        
245        # search my prototype
246        while (<IN>) {
247            last if  (/SGU: END SUITES DECLARATION/ || /SGU: BEGIN FILE $infile/);
248            $newmain .= $_;
249        }
250        if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it    
251            while (<IN>) {
252                last if /SGU: END FILE/;
253            }
254            $_ = <IN>; # pass extra blank line
255            chomp;
256            die "this line should be blank ($_). Did you edit the file?" if /\W/;
257        }
258        my ($old_)=($_);
259        # add my section
260        $newmain .= "    /* SGU: BEGIN FILE $infile */\n";
261        $newmain .= "      suite = xbt_test_suite_by_name(\"$suite_name\",$suite_title);\n";
262        map {
263            my ($name,$func,$title) = @{$_};
264            $newmain .=  "      xbt_test_suite_push(suite, \"$name\", $func, $title);\n";
265        } @tests;
266        
267        $newmain .= "    /* SGU: END FILE */\n\n";
268        if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END SUITES DECLARATION/) {
269            $newmain .= $old_;
270        }
271        
272        # pass the remaining 
273        while (<IN>) {
274            $newmain .= $_;
275        }
276        close IN || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
277        
278        # write it back to main
279        open OUT,">${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
280        print OUT $newmain;
281        close OUT || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
282 } # end if process_one($)
283
284 0;