Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Missing the word line for compiler.
[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 "#line $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   return res;
168 }
169 EOF
170         print OUT $GENERATED;
171         close OUT || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
172     }
173
174    print "  Suite $suite_name: $suite_title (".(scalar @tests)." tests)\n";
175    map {
176        my ($name,$func,$title) = @{$_};
177        print "    unit $name: func=$func; title=$title\n";
178    } @tests;
179
180    #while (my $t = shift @tests) {
181
182    # add this suite to the main
183    my $newmain="";
184    open IN,"simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
185     # search prototypes
186        while (<IN>) {
187            $newmain .= $_;
188            #    print "Look for proto: $_";
189            last if /SGU: BEGIN PROTOTYPES/;
190        }
191
192        # search my prototype
193        while (<IN>) {
194            #    print "Seek protos: $_";
195            last if  (/SGU: END PROTOTYPES/ || /SGU: BEGIN FILE $infile/);
196            $newmain .= $_;
197        }
198        if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it    
199            while (<IN>) {
200                last if /SGU: END FILE/;
201            }
202            $_ = <IN>; # pass extra blank line
203            chomp;
204            die "this line should be blank ($_). Did you edit the file?" if /\W/;
205        }
206        my ($old_)=($_);
207        # add my section
208        $newmain .= "  /* SGU: BEGIN FILE $infile */\n";
209        map {
210            my ($name,$func,$title) = @{$_};
211            $newmain .=  "    void $func(void);\n"
212        } @tests;
213        
214        $newmain .= "  /* SGU: END FILE */\n\n";
215        if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END PROTOTYPES/) {
216            $newmain .= $old_;
217        }
218        
219        # pass remaining prototypes, search declarations
220        while (<IN>) {
221            $newmain .= $_ unless /SGU: END PROTOTYPES/;
222            last if /SGU: BEGIN SUITES DECLARATION/;
223        }
224        
225        ### Done with prototypes. And now, the actual code
226        
227        # search my prototype
228        while (<IN>) {
229            last if  (/SGU: END SUITES DECLARATION/ || /SGU: BEGIN FILE $infile/);
230            $newmain .= $_;
231        }
232        if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it    
233            while (<IN>) {
234                last if /SGU: END FILE/;
235            }
236            $_ = <IN>; # pass extra blank line
237            chomp;
238            die "this line should be blank ($_). Did you edit the file?" if /\W/;
239        }
240        my ($old_)=($_);
241        # add my section
242        $newmain .= "    /* SGU: BEGIN FILE $infile */\n";
243        $newmain .= "      suite = xbt_test_suite_by_name(\"$suite_name\",$suite_title);\n";
244        map {
245            my ($name,$func,$title) = @{$_};
246            $newmain .=  "      xbt_test_suite_push(suite, \"$name\", $func, $title);\n";
247        } @tests;
248        
249        $newmain .= "    /* SGU: END FILE */\n\n";
250        if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END SUITES DECLARATION/) {
251            $newmain .= $old_;
252        }
253        
254        # pass the remaining 
255        while (<IN>) {
256            $newmain .= $_;
257        }
258        close IN || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
259        
260        # write it back to main
261        open OUT,">simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
262        print OUT $newmain;
263        close OUT || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
264 } # end if process_one($)
265
266 0;