Logo AND Algorithmique Numérique Distribuée

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