Logo AND Algorithmique Numérique Distribuée

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