Logo AND Algorithmique Numérique Distribuée

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