Logo AND Algorithmique Numérique Distribuée

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